Class: DocTemplate::Tags::LatexTag

Inherits:
BaseTag
  • Object
show all
Defined in:
lib/doc_template/tags/latex_tag.rb

Constant Summary collapse

SPACE_RE =
/[[:space:]]/.freeze
TAG_NAME =
'latex'

Constants inherited from BaseTag

BaseTag::SOFT_RETURN_RE, BaseTag::UNICODE_SPACES_RE

Instance Attribute Summary

Attributes inherited from BaseTag

#anchor, #content

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseTag

#before_tag, #check_tag_soft_return, #content_until_break, #content_until_materials, #ela2?, #ela6?, #gdoc?, #include_break?, #include_break_for?, #materials, parse, #parse_nested, #parse_template, #placeholder, #placeholder_id, #render, #replace_tag, tag_with_html_regexp, #template_name, #template_path, template_path_for

Class Method Details

.s3_folderObject



11
12
13
# File 'lib/doc_template/tags/latex_tag.rb', line 11

def self.s3_folder
  @s3_folder ||= ENV.fetch('SWAP_DOCS_LATEX', 'documents-latex-equations')
end

Instance Method Details

#parse(node, opts = {}) ⇒ Object



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
# File 'lib/doc_template/tags/latex_tag.rb', line 15

def parse(node, opts = {})
  @parent_node = opts[:parent_node]
  @value = opts[:value].gsub(SPACE_RE, '')
  expression =
    begin
      # TODO: Refactor to handle GDoc in the ActiveJob
      if opts[:context_type]&.to_sym == :gdoc
        key = "#{self.class.s3_folder}/#{SecureRandom.hex(20)}.png"
        generate_image do |png|
          url = S3Service.upload key, png
          %(<img class="o-ld-latex" src="#{url}">)
        end
      else
        Lcms::Engine::EmbedEquations.tex_to_svg @value, preserve_color: preserve_color?
      end
    rescue StandardError => e
      raise if Rails.env.test?

      msg = "Error converting Latex expression: #{@value}"
      Rails.logger.warn "#{e.message} => #{msg}"
      msg
    end

  node.inner_html = node.inner_html.sub DocTemplate::FULL_TAG, expression
  @result = node
  self
end

#tag_dataObject



43
44
45
# File 'lib/doc_template/tags/latex_tag.rb', line 43

def tag_data
  { latex: value }
end