Class: Tex2SvgTreeprocessor
- Inherits:
-
Asciidoctor::Extensions::Treeprocessor
- Object
- Asciidoctor::Extensions::Treeprocessor
- Tex2SvgTreeprocessor
- Defined in:
- lib/asciidoctor-tex2svg/extension.rb
Constant Summary collapse
- LineFeed =
%(\n)
- StemInlineMacroRx =
/\\?(?:stem|latexmath|asciimath):([a-z,]*)\[(.*?[^\\])\]/m
- LatexmathInlineMacroRx =
/\\?latexmath:([a-z,]*)\[(.*?[^\\])\]/m
- AsciiMathInlineMacroRx =
/\\?asciimath:([a-z,]*)\[(.*?[^\\])\]/m
Instance Method Summary collapse
- #handle_inline_stem(node, text, image_output_dir, image_target_dir, inline) ⇒ Object
- #handle_nonasciidoc_table_cell(cell, image_output_dir, image_target_dir, inline) ⇒ Object
- #handle_prose_block(prose, image_output_dir, image_target_dir, inline) ⇒ Object
- #handle_section_title(sect, image_output_dir, image_target_dir, inline) ⇒ Object
- #handle_stem_block(stem, image_output_dir, image_target_dir, inline) ⇒ Object
- #image_output_and_target_dir(doc) ⇒ Object
- #make_equ_image(equ_data, equ_id, equ_inline, image_output_dir, image_target_dir, inline) ⇒ Object
- #process(document) ⇒ Object
Instance Method Details
#handle_inline_stem(node, text, image_output_dir, image_target_dir, inline) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/asciidoctor-tex2svg/extension.rb', line 119 def handle_inline_stem(node, text, image_output_dir, image_target_dir, inline) document = node.document to_html = document.basebackend? 'html' case document.attr 'stem' when 'latexmath' support_stem_prefix = true stem_rx = LatexmathInlineMacroRx when 'asciimath' support_stem_prefix = true stem_rx = AsciiMathInlineMacroRx else support_stem_prefix = false stem_rx = StemInlineMacroRx end source_modified = false # TODO skip passthroughs in the source (e.g., +stem:[x^2]+) if text != nil && (text.include? ':') text = text.gsub(stem_rx) { if (m = $~)[0].start_with? '\\' next m[0][1..-1] end if (eq_data = m[2].rstrip).empty? next else source_modified = true end if text.include? 'asciimath:' eq_data = AsciiMath.parse(eq_data).to_latex elsif (support_stem_prefix && (text.include? 'stem:')) || (text.include? 'latexmath:') eq_data.gsub! '\]', ']' subs = m[1].nil_or_empty? ? (to_html ? [:specialcharacters] : []) : (node.resolve_pass_subs m[1]) eq_data = node.apply_subs eq_data, subs unless subs.empty? else source_modified = false return text end img_target = make_equ_image eq_data, nil, true, image_output_dir, image_target_dir, inline if inline %(pass:[<span class="steminline"> #{img_target} </span>]) else %(image:#{img_target}[]) end } end [text, source_modified] end |
#handle_nonasciidoc_table_cell(cell, image_output_dir, image_target_dir, inline) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/asciidoctor-tex2svg/extension.rb', line 102 def handle_nonasciidoc_table_cell(cell, image_output_dir, image_target_dir, inline) text = cell.instance_variable_get :@text text, source_modified = handle_inline_stem cell, text, image_output_dir, image_target_dir, inline if source_modified cell.instance_variable_set :@text, text end end |
#handle_prose_block(prose, image_output_dir, image_target_dir, inline) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/asciidoctor-tex2svg/extension.rb', line 90 def handle_prose_block(prose, image_output_dir, image_target_dir, inline) text = prose.context == :list_item ? (prose.instance_variable_get :@text) : (prose.lines * LineFeed) text, source_modified = handle_inline_stem prose, text, image_output_dir, image_target_dir, inline if source_modified if prose.context == :list_item prose.instance_variable_set :@text, text else prose.lines = text.split LineFeed end end end |
#handle_section_title(sect, image_output_dir, image_target_dir, inline) ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/asciidoctor-tex2svg/extension.rb', line 110 def handle_section_title(sect, image_output_dir, image_target_dir, inline) text = sect.instance_variable_get :@title text, source_modified = handle_inline_stem sect, text, image_output_dir, image_target_dir, inline if source_modified sect.instance_variable_set :@title, text sect.remove_instance_variable :@subbed_title end end |
#handle_stem_block(stem, image_output_dir, image_target_dir, inline) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/asciidoctor-tex2svg/extension.rb', line 59 def handle_stem_block(stem, image_output_dir, image_target_dir, inline) equation_type = stem.style.to_sym case equation_type when :latexmath content = stem.content when :asciimath content = AsciiMath.parse(stem.content).to_latex else return end img_target = make_equ_image content, stem.id, false, image_output_dir, image_target_dir, inline parent = stem.parent if inline stem_image = create_pass_block parent, %{<div class="stemblock"> #{img_target} </div>}, {} parent.blocks[parent.blocks.index stem] = stem_image else alt_text = stem.attr 'alt', (equation_type == :latexmath ? %($$#{content}$$) : %(`#{content}`)) attrs = {'target' => img_target, 'alt' => alt_text, 'align' => 'center'} parent = stem.parent stem_image = create_image_block parent, attrs stem_image.id = stem.id if stem.id if (title = stem.attributes['title']) stem_image.title = title end parent.blocks[parent.blocks.index stem] = stem_image end end |
#image_output_and_target_dir(doc) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/asciidoctor-tex2svg/extension.rb', line 201 def image_output_and_target_dir(doc) output_dir = doc.attr('imagesoutdir') if output_dir if doc.attr('imagesdir').nil_or_empty? target_dir = output_dir else # When imagesdir attribute is set, every relative path is prefixed with it. So the real target dir shall then be relative to the imagesdir, instead of being relative to document root. abs_imagesdir = ::Pathname.new doc.normalize_system_path(doc.attr('imagesdir')) abs_outdir = ::Pathname.new doc.normalize_system_path(output_dir) target_dir = abs_outdir.relative_path_from(abs_imagesdir).to_s end else output_dir = doc.attr('imagesdir') # since we store images directly to imagesdir, target dir shall be NULL and asciidoctor converters will prefix imagesdir. target_dir = "." end output_dir = doc.normalize_system_path(output_dir, doc.attr('docdir')) return [output_dir, target_dir] end |
#make_equ_image(equ_data, equ_id, equ_inline, image_output_dir, image_target_dir, inline) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/asciidoctor-tex2svg/extension.rb', line 173 def make_equ_image(equ_data, equ_id, equ_inline, image_output_dir, image_target_dir, inline) input = equ_data # TODO: consider only making the image if the file isn't already there. if inline data, = Open3.capture2('tex2svg', '--inline', input) else data, = Open3.capture2('tex2svg', input) end if inline data else unless equ_id equ_id = %(stem-#{::Digest::MD5.hexdigest input}) end image_ext = '.svg' img_target = %(#{equ_id}#{image_ext}) img_file = ::File.join image_output_dir, img_target ::IO.write img_file, data img_target = ::File.join image_target_dir, img_target unless image_target_dir == '.' img_target end end |
#process(document) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/asciidoctor-tex2svg/extension.rb', line 14 def process document inline = document.attr 'tex2svg-inline' unless inline image_output_dir, image_target_dir = image_output_and_target_dir document ::Asciidoctor::Helpers.mkdir_p image_output_dir unless ::File.directory? image_output_dir end unless (stem_blocks = document.find_by context: :stem).nil_or_empty? stem_blocks.each do |stem| handle_stem_block stem, image_output_dir, image_target_dir, inline end end unless (prose_blocks = document.find_by {|b| (b.content_model == :simple && (b.subs.include? :macros)) || b.context == :list_item }).nil_or_empty? prose_blocks.each do |prose| handle_prose_block prose, image_output_dir, image_target_dir, inline end end unless (table_blocks = document.find_by context: :table).nil_or_empty? table_blocks.each do |table| (table.rows[:body] + table.rows[:foot]).each do |row| row.each do |cell| if cell.style == :asciidoc process cell.inner_document elsif cell.style != :literal handle_nonasciidoc_table_cell cell, image_output_dir, image_target_dir, inline end end end end end unless (sect_blocks = document.find_by content: :section).nil_or_empty? sect_blocks.each do |sect| handle_section_title sect, image_output_dir, image_target_dir, inline end end nil end |