Class: Gmi2LaTeX::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/gmi2latex/renderer.rb

Instance Method Summary collapse

Constructor Details

#initialize(document, header: nil, footer: nil) ⇒ Renderer

Returns a new instance of Renderer.



5
6
7
8
9
# File 'lib/gmi2latex/renderer.rb', line 5

def initialize(document, header: nil, footer: nil)
  @document = document
  @header = header
  @footer = footer
end

Instance Method Details

#end_itemizeObject



111
112
113
114
115
# File 'lib/gmi2latex/renderer.rb', line 111

def end_itemize
  @listing or return
  @listing = false
  print "\\end{itemize}\n"
end

#end_textObject



105
106
107
108
109
# File 'lib/gmi2latex/renderer.rb', line 105

def end_text
  @was_text or return
  print "\n"
  @was_text = false
end

#renderObject



13
14
15
16
17
18
# File 'lib/gmi2latex/renderer.rb', line 13

def render
  @header and print @header
  @document.nodes.each { |node| render_node(node) }
  end_itemize
  @footer and print @footer
end

#render_list_item(content) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/gmi2latex/renderer.rb', line 89

def render_list_item(content)
  if @listing
    print <<~LaTeX
      \\item{}
      #{content}
    LaTeX
  else
    print <<~LaTeX
      \\begin{itemize}
      \\item{}
      #{content}
    LaTeX
    @listing = true
  end
end

#render_node(node) ⇒ Object



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
58
# File 'lib/gmi2latex/renderer.rb', line 20

def render_node(node)
  case node
  in Gemtext::Heading[content:]
    end_text
    end_itemize
    print "\\section{#{content}}\n"
  in Gemtext::SubHeading[content:]
    end_text
    end_itemize
    print "\\subsection{#{content}}\n"
  in Gemtext::SubSubHeading[content:]
    end_text
    end_itemize
    print "\\subsubsection{#{content}}\n"
  in Gemtext::Whitespace
    end_text
    end_itemize
    print "\n"
  in Gemtext::Text[content:]
    render_text(content)
  in Gemtext::Link[target:, description:]
    end_text
    end_itemize
    print "#{description} (\\url{#{target}})\n"
  in Gemtext::Preformatted[content:]
    end_text
    end_itemize
    print <<~LaTeX
      \\begin{verbatim}
      #{content}
      \\end{verbatim}
    LaTeX
  in Gemtext::ListItem[content:]
    end_text
    render_list_item(content)
  else
    raise node.inspect
  end
end

#render_text(content) ⇒ Object



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
# File 'lib/gmi2latex/renderer.rb', line 60

def render_text(content)
  end_itemize
  if content.match(/ \A \s* \Z /x)
    end_text
    print content
  else
    @was_text and print "\\\\\n"
    content.each_char do |char|
      case char
      in "_"
        print "\\_"
      in "#"
        print "\\#"
      in "%"
        print "\\%"
      in "<"
        print "\\textlangle{}"
      in ">"
        print "\\textrangle{}"
      in "`"
        print "\\`{}"
      else
        print char
      end
    end
  end
  @was_text = true
end