class Tex2SvgTreeprocessor

Constants

AsciiMathInlineMacroRx
LatexmathInlineMacroRx
LineFeed
StemInlineMacroRx

Public Instance Methods

handle_inline_stem(node, text, image_output_dir, image_target_dir, inline) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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