Class: HTML::Pipeline::SVGTeX::PreFilter

Inherits:
TextFilter show all
Defined in:
lib/html/pipeline/svgtex.rb

Constant Summary collapse

BLOCK_SVGTEX =
/^\$\$([^$]+)\$\$\s*$/
INLINE_SVGTEX =
/(?<!\p{Word})\$([^$\n]+)\$(?!\p{Word})/
INDENTED_CODE =
/(\A|\n\r?\n)(((\t|\s{4}).*(\n|\Z))+)(\r?\n|\Z)/
FENCED_CODE =
/^``` ?(.*?)\r?\n(.+?)\r?\n```\r?$/m
INLINE_CODE =
/`(.*)`/

Instance Attribute Summary

Attributes inherited from TextFilter

#text

Attributes inherited from Filter

#context, #result

Instance Method Summary collapse

Methods inherited from Filter

#base_url, call, #current_user, #doc, #has_ancestor?, #html, #needs, #parse_html, #repository, to_document, to_html, #validate

Constructor Details

#initialize(text, context = nil, result = nil) ⇒ PreFilter

Returns a new instance of PreFilter.



17
18
19
20
21
22
# File 'lib/html/pipeline/svgtex.rb', line 17

def initialize(text, context = nil, result = nil)
  super text, context, result
  @text = @text.gsub "\r", ''
  @inline = {}
  @codemap = {}
end

Instance Method Details

#callObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/html/pipeline/svgtex.rb', line 24

def call
  extract_fenced_code!
  extract_indented_code!
  @text.gsub!(BLOCK_SVGTEX) do
    "\n\n```mathjax\n\\displaystyle{#{$1.gsub "\\", "\\\\\\\\"}}\n```\n\n"
  end
  extract_fenced_code!
  extract_inline_code!
  @text.gsub!(INLINE_SVGTEX) do
    "`{mathjax} #{$1}`"
  end
  reinsert_code!
  @text
end

#extract_fenced_code!Object

Code taken from gollum (github.com/github/gollum)



57
58
59
60
61
62
63
# File 'lib/html/pipeline/svgtex.rb', line 57

def extract_fenced_code!
  @text.gsub!(FENCED_CODE) do
    id = Digest::SHA1.hexdigest($2)
    @codemap[id] = { :lang => $1, :code => $2 }
    id
  end
end

#extract_indented_code!Object



47
48
49
50
51
52
53
54
# File 'lib/html/pipeline/svgtex.rb', line 47

def extract_indented_code!
  @text.gsub!(INDENTED_CODE) do
    code = $2.gsub(/^(\t|\s{4})/, '').sub(/\r?\n\Z/, '')
    id = Digest::SHA1.hexdigest(code)
    @codemap[id] = { :code => code }
    "\n#{id}"
  end
end

#extract_inline_code!Object



39
40
41
42
43
44
45
# File 'lib/html/pipeline/svgtex.rb', line 39

def extract_inline_code!
  @text.gsub!(INLINE_CODE) do
    id = Digest::SHA1.hexdigest($1)
    @inline[id] = $1
    id
  end
end

#reinsert_code!Object



65
66
67
68
69
70
71
72
# File 'lib/html/pipeline/svgtex.rb', line 65

def reinsert_code!
  @inline.each do |id, code|
    @text.gsub!(id) { "`#{code}`" }
  end
  @codemap.each do |id, spec|
    @text.gsub!(id) { "```#{spec[:lang]}\n#{spec[:code]}\n```" }
  end
end