Class: AsciidoctorPDFExtensions

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor-pdf-mathjax.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.tempfilesObject (readonly)

Returns the value of attribute tempfiles.



21
22
23
# File 'lib/asciidoctor-pdf-mathjax.rb', line 21

def tempfiles
  @tempfiles
end

Instance Method Details

#convert_inline_quoted(node) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/asciidoctor-pdf-mathjax.rb', line 74

def convert_inline_quoted node
  latex_content = extract_latex_content(node.text, node.type)
  return super if latex_content.nil?

  theme = (load_theme node.document)

  svg_output, error = stem_to_svg(latex_content, true)
  if svg_output.nil? || svg_output.empty?
    logger.warn "Error processing stem: #{error || 'No SVG output'}"
    return super
  end
  adjusted_svg, svg_width = adjust_svg_to_match_text(svg_output, node, theme)
  tmp_svg = Tempfile.new(['stem-', '.svg'])
  self.class.tempfiles << tmp_svg
  begin
    tmp_svg.write(adjusted_svg)
    tmp_svg.close

    logger.debug "Successfully embedded stem inline #{node.text} as SVG image"
    quoted_text = "<img src=\"#{tmp_svg.path}\" format=\"svg\" width=\"#{svg_width}\" alt=\"#{node.text}\">"
    node.id ? %(<a id="#{node.id}">#{DummyText}</a>#{quoted_text}) : quoted_text
  rescue => e
    logger.warn "Failed to process SVG: #{e.message}"
    super
  end
end

#convert_stem(node) ⇒ Object



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/asciidoctor-pdf-mathjax.rb', line 24

def convert_stem node
  arrange_block node do |_|
    add_dest_for_block node if node.id

    latex_content = extract_latex_content(node.content, node.style.to_sym)

    svg_output, error = stem_to_svg(latex_content, false)

    if svg_output.nil? || svg_output.empty?
      logger.warn "Failed to convert STEM to SVG: #{error} (Fallback to code block)"
      pad_box @theme.code_padding, node do
        theme_font :code do
          typeset_formatted_text [{ text: (guard_indentation latex_content), color: @font_color }],
                                 (calc_line_metrics @base_line_height),
                                 bottom_gutter: @bottom_gutters[-1][node]
        end
      end
    else
      svg_output = adjust_svg_color(svg_output, @font_color)
      svg_default_font_size = FALLBACK_FONT_SIZE

      svg_doc = REXML::Document.new(svg_output)
      svg_width = svg_doc.root.attributes['width'].to_f * POINTS_PER_EX || raise("No width found in SVG")

      scaling_factor = @font_size.to_f / svg_default_font_size
      svg_width = svg_width * scaling_factor

      svg_file = Tempfile.new(['stem', '.svg'])
      begin
        svg_file.write(svg_output)
        svg_file.close

        pad_box @theme.code_padding, node do
          begin
            image_obj = image svg_file.path, position: :center, width: svg_width, height: nil
            logger.debug "Successfully embedded stem block (as latex) #{latex_content} as SVG image" if image_obj
          rescue Prawn::Errors::UnsupportedImageType => e
            logger.warn "Unsupported image type error: #{e.message}"
          rescue StandardError => e
            logger.warn "Failed embedding SVG: #{e.message}"
          end
        end
      ensure
        svg_file.unlink
      end
    end
  end
  theme_margin :block, :bottom, (next_enclosed_block node)
end