Class: BayeuxLatexGen
- Inherits:
-
Object
- Object
- BayeuxLatexGen
- Defined in:
- lib/bayeux/latex_gen.rb
Instance Method Summary collapse
-
#generate ⇒ Object
Return the AST as an HTML string (stored in @lines).
-
#generate_full_node(block) ⇒ Object
Output the full contents of the node, properly bracketed as an LaTeX expression.
- #generate_node_end(block) ⇒ Object
-
#generate_node_start(block) ⇒ Object
Output only the start of a node.
-
#initialize(syntax_tree) ⇒ BayeuxLatexGen
constructor
Default Constructor.
-
#typeset(string) ⇒ Object
Fix Typography, according to HTML standards.
Constructor Details
#initialize(syntax_tree) ⇒ BayeuxLatexGen
Default Constructor
4 5 6 7 8 |
# File 'lib/bayeux/latex_gen.rb', line 4 def initialize(syntax_tree) @syntax_tree = syntax_tree @lines = Array.new @text = String.new end |
Instance Method Details
#generate ⇒ Object
Return the AST as an HTML string (stored in @lines)
11 12 13 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 |
# File 'lib/bayeux/latex_gen.rb', line 11 def generate # Create a walker for the trees in the forest walker = TreeWalker.new walker.on_before_down = self.method(:generate_node_start) walker.on_after_up = self.method(:generate_node_end) walker.on_no_siblings = self.method(:generate_full_node) walker.on_no_children = self.method(:generate_full_node) # Clear the internal LaTeX replacement @lines.clear # Walk the forest @syntax_tree.block_forest.each{|tree| walker.walk_tree(tree) } #puts @syntax_tree.to_s #puts @lines latex_string = String.new @lines.each{|line| unless (line.empty? or (/\S/ !~ line)) then #unless line.empty? then latex_string << line + "\n" end } return latex_string end |
#generate_full_node(block) ⇒ Object
Output the full contents of the node, properly bracketed as an LaTeX expression. This is only called if we have no sub-nodes to deal with
52 53 54 55 56 57 58 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 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 |
# File 'lib/bayeux/latex_gen.rb', line 52 def generate_full_node(block) # Check if we need to add a space # Check if we need to add a space if not block.content[0] =~ /\s|[:;.,?!]/ @lines << " " end case block.type # Headers when :h1 @lines << "\\chapter{#{block.content}}\n" when :h2 @lines << "\\section{#{block.content}}\n" when :h3 @lines << "\\subsection{#{block.content}}\n" when :h4 @lines << "\\subsubsection{#{block.content}}\n" when :h5 @lines << "\\paragraph{#{block.content}}\n" when :h5 @lines << "\\subparagraph{#{block.content}}\n" # Ordinary paragraphs when :paragraph @lines << "#{typeset(block.content)}\n" when :none @text << "#{block.content}" # Special paragraphs when :block_quote @lines << "\\begin{quotation}" << "#{typeset(block.content)}" << "\\end{quotation}\n" when :single_quote @lines << "'#{typeset(block.content)}'" when :double_quote @lines << "\"#{typeset(block.content)}\"" when :note @lines << "\\textbf{Note:}~#{typeset(block.content)}\n" when :command @lines << "\\begin{Verbatim}[frame=lines,rulecolor=\\color{highlight}]" << "#{block.content.strip.indent(3)}" << "\\end{Verbatim}\n" when :code_language @code_language = block.content when :code_start_number @code_start_number = block.content when :file @lines << "<pre class=\"file\">#{block.content}</pre>" when :output @lines << "\\crule" << "\\begin{verbatim}" << "#{block.content.strip}" << "\\end{verbatim}" << "\\crule\n" # Special Characters when :em_dash @text << "---" when :en_dash @text << "--" when :elipses @text << "\\ldots" when :elipses_stop @text << "\\dots." # Links when :link_target @link_target = block.content when :link_text @link_text = block.content # Lists when :item @lines << "\\item #{typeset(block.content)}" when :dl_header @lines << "\\item[#{block.content}]" when :dl_text @lines << "#{block.content}" # Tags when :ac @text << "\\ac{#{block.content}}" when :emph @text << "\\emph{#{block.content}}" when :tt @text << "\\texttt{#{block.content}}" end end |
#generate_node_end(block) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/bayeux/latex_gen.rb', line 240 def generate_node_end(block) case block.type # Headers when :h1, :h2, :h3, :h4, :h5, :h6 @lines << "#{typeset(@text)}}\n" @text = "" # Ordinary paragraphs when :paragraph @lines << "#{typeset(@text)}\n" @text = "" # Special paragraphs when :block_quote @lines << "\\end{quotation}\n" when :single_quote @lines << "'" when :double_quote @lines << "\"" when :note @lines << "" when :command @lines << "\\end{Verbatim}\n" when :code # Assemble the code block from the sub-tree nodes begin pretty_code = Uv.parse(block.content, "xhtml", @code_language, @code_start_number.to_i, "dawn") @lines << pretty_code rescue pretty_code = Uv.parse(block.content, "xhtml", "plain_text", @code_start_number.to_i, "dawn") @lines << pretty_code end # Links when :link # Assemble the link from the sub-tree nodes @lines << "\\href{#{@link_target}\"}{#{@link_text}}" when :link_target @link_target = block.content when :link_text @link_text = block.content # Lists when :ol @lines << "\\end{enumerate}" when :ul @lines << "\\end{itemize}" when :item @lines << "\\item #{typeset(@text)}" @text = "" when :dl @lines << "" when :dl_header @lines << "]" when :dl_text @lines << "" # Tags when :ac, :tt, :strong @lines << "}" when :emph @lines << "\\/}" end end |
#generate_node_start(block) ⇒ Object
Output only the start of a node
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 172 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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/bayeux/latex_gen.rb', line 146 def generate_node_start(block) # Check if we need to add a space if not block.content[0] =~ /\s|[:;.,?!]/ @lines << " " end case block.type # Headers when :h1 @text << "\\chapter{#{block.content}" when :h2 @text << "\\section{#{block.content}" when :h3 @text << "\\subsection{#{block.content}" when :h4 @text << "\\subsubsection{#{block.content}" when :h5 @text << "\\paragraph{#{block.content}" when :h5 @text << "\\subparagraph{#{block.content}" # Ordinary paragraphs when :paragraph @text << "#{block.content}" when :none @text << "#{block.content}" # Special paragraphs when :block_quote @lines << "<blockquote>#{typeset(block.content)}" when :single_quote @text << "'#{block.content}" when :double_quote @text << "\"{block.content}" when :note @lines << "\\textbf{Note:}~#{typeset(block.content)}\n" when :command @lines << "\\begin{Verbatim}[frame=lines,rulecolor=\\color{highlight}]" << "#{typeset(block.content)}" when :code_language @code_language = block.content when :code_start_number @code_start_number = block.content # Special Characters when :em_dash @text << "---" when :en_dash @text << "--" when :elipses @text << "\ldots" when :elipses_stop @text << "\dots." # Links when :link_target @link_target = block.content when :link_text @link_text = block.content # Lists when :ol @lines << "\\begin{enumerate}" when :ul @lines << "\\begin{itemize}" when :dl @lines << "\\begin{description}" when :dl_header @lines << "\\item[#{block.content}" when :dl_text @lines << "#{block.content}" # Tags when :ac @text << "\\ac{#{block.content}}" when :emph @text << "\\emph{#{block.content}\\/}" when :strong @text << "\\texttt{#{block.content}\\/}" when :tt @text << "\\texttt{#{block.content}}" end end |
#typeset(string) ⇒ Object
Fix Typography, according to HTML standards
319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/bayeux/latex_gen.rb', line 319 def typeset(string) unless string.nil? then return_str = string return_str = return_str.strip return_str = return_str.squeeze(" ") return_str = return_str.word_wrap(70) return return_str.rstrip else return "" end end |