remi-maruku 0.5.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (183) hide show
  1. data/Rakefile +73 -0
  2. data/bin/marudown +29 -0
  3. data/bin/maruku +176 -0
  4. data/bin/marutest +338 -0
  5. data/bin/marutex +31 -0
  6. data/docs/changelog.md +334 -0
  7. data/docs/div_syntax.md +36 -0
  8. data/docs/entity_test.md +23 -0
  9. data/docs/markdown_syntax.md +899 -0
  10. data/docs/maruku.md +346 -0
  11. data/docs/math.md +194 -0
  12. data/docs/other_stuff.md +51 -0
  13. data/docs/proposal.md +309 -0
  14. data/lib/maruku.rb +141 -0
  15. data/lib/maruku/attributes.rb +227 -0
  16. data/lib/maruku/defaults.rb +70 -0
  17. data/lib/maruku/errors_management.rb +92 -0
  18. data/lib/maruku/ext/div.rb +100 -0
  19. data/lib/maruku/ext/math.rb +41 -0
  20. data/lib/maruku/ext/math/elements.rb +27 -0
  21. data/lib/maruku/ext/math/latex_fix.rb +11 -0
  22. data/lib/maruku/ext/math/mathml_engines/blahtex.rb +104 -0
  23. data/lib/maruku/ext/math/mathml_engines/itex2mml.rb +29 -0
  24. data/lib/maruku/ext/math/mathml_engines/none.rb +20 -0
  25. data/lib/maruku/ext/math/mathml_engines/ritex.rb +24 -0
  26. data/lib/maruku/ext/math/parsing.rb +105 -0
  27. data/lib/maruku/ext/math/to_html.rb +170 -0
  28. data/lib/maruku/ext/math/to_latex.rb +22 -0
  29. data/lib/maruku/helpers.rb +260 -0
  30. data/lib/maruku/input/charsource.rb +326 -0
  31. data/lib/maruku/input/extensions.rb +69 -0
  32. data/lib/maruku/input/html_helper.rb +189 -0
  33. data/lib/maruku/input/linesource.rb +111 -0
  34. data/lib/maruku/input/parse_block.rb +613 -0
  35. data/lib/maruku/input/parse_doc.rb +227 -0
  36. data/lib/maruku/input/parse_span_better.rb +732 -0
  37. data/lib/maruku/input/rubypants.rb +225 -0
  38. data/lib/maruku/input/type_detection.rb +144 -0
  39. data/lib/maruku/input_textile2/t2_parser.rb +163 -0
  40. data/lib/maruku/maruku.rb +33 -0
  41. data/lib/maruku/output/s5/fancy.rb +756 -0
  42. data/lib/maruku/output/s5/to_s5.rb +125 -0
  43. data/lib/maruku/output/to_html.rb +971 -0
  44. data/lib/maruku/output/to_latex.rb +563 -0
  45. data/lib/maruku/output/to_latex_entities.rb +367 -0
  46. data/lib/maruku/output/to_latex_strings.rb +64 -0
  47. data/lib/maruku/output/to_markdown.rb +164 -0
  48. data/lib/maruku/output/to_s.rb +53 -0
  49. data/lib/maruku/string_utils.rb +191 -0
  50. data/lib/maruku/structures.rb +165 -0
  51. data/lib/maruku/structures_inspect.rb +87 -0
  52. data/lib/maruku/structures_iterators.rb +61 -0
  53. data/lib/maruku/tests/benchmark.rb +82 -0
  54. data/lib/maruku/tests/new_parser.rb +370 -0
  55. data/lib/maruku/tests/tests.rb +136 -0
  56. data/lib/maruku/textile2.rb +1 -0
  57. data/lib/maruku/toc.rb +199 -0
  58. data/lib/maruku/usage/example1.rb +33 -0
  59. data/lib/maruku/version.rb +40 -0
  60. data/tests/bugs/code_in_links.md +16 -0
  61. data/tests/bugs/complex_escaping.md +4 -0
  62. data/tests/math/syntax.md +46 -0
  63. data/tests/math_usage/document.md +13 -0
  64. data/tests/others/abbreviations.md +11 -0
  65. data/tests/others/blank.md +4 -0
  66. data/tests/others/code.md +5 -0
  67. data/tests/others/code2.md +8 -0
  68. data/tests/others/code3.md +16 -0
  69. data/tests/others/email.md +4 -0
  70. data/tests/others/entities.md +19 -0
  71. data/tests/others/escaping.md +16 -0
  72. data/tests/others/extra_dl.md +101 -0
  73. data/tests/others/extra_header_id.md +13 -0
  74. data/tests/others/extra_table1.md +40 -0
  75. data/tests/others/footnotes.md +17 -0
  76. data/tests/others/headers.md +10 -0
  77. data/tests/others/hrule.md +10 -0
  78. data/tests/others/images.md +20 -0
  79. data/tests/others/inline_html.md +42 -0
  80. data/tests/others/links.md +38 -0
  81. data/tests/others/list1.md +4 -0
  82. data/tests/others/list2.md +5 -0
  83. data/tests/others/list3.md +8 -0
  84. data/tests/others/lists.md +32 -0
  85. data/tests/others/lists_after_paragraph.md +44 -0
  86. data/tests/others/lists_ol.md +39 -0
  87. data/tests/others/misc_sw.md +105 -0
  88. data/tests/others/one.md +1 -0
  89. data/tests/others/paragraphs.md +13 -0
  90. data/tests/others/sss06.md +352 -0
  91. data/tests/others/test.md +4 -0
  92. data/tests/s5/s5profiling.md +48 -0
  93. data/tests/unittest/abbreviations.md +72 -0
  94. data/tests/unittest/alt.md +30 -0
  95. data/tests/unittest/attributes/att2.md +34 -0
  96. data/tests/unittest/attributes/att3.md +45 -0
  97. data/tests/unittest/attributes/attributes.md +82 -0
  98. data/tests/unittest/attributes/circular.md +43 -0
  99. data/tests/unittest/attributes/default.md +38 -0
  100. data/tests/unittest/blank.md +39 -0
  101. data/tests/unittest/blanks_in_code.md +106 -0
  102. data/tests/unittest/bug_def.md +29 -0
  103. data/tests/unittest/bug_table.md +67 -0
  104. data/tests/unittest/code.md +53 -0
  105. data/tests/unittest/code2.md +46 -0
  106. data/tests/unittest/code3.md +102 -0
  107. data/tests/unittest/data_loss.md +42 -0
  108. data/tests/unittest/divs/div1.md +204 -0
  109. data/tests/unittest/divs/div2.md +34 -0
  110. data/tests/unittest/divs/div3_nest.md +62 -0
  111. data/tests/unittest/easy.md +28 -0
  112. data/tests/unittest/email.md +33 -0
  113. data/tests/unittest/encoding/iso-8859-1.md +38 -0
  114. data/tests/unittest/encoding/utf-8.md +33 -0
  115. data/tests/unittest/entities.md +124 -0
  116. data/tests/unittest/escaping.md +89 -0
  117. data/tests/unittest/extra_dl.md +72 -0
  118. data/tests/unittest/extra_header_id.md +86 -0
  119. data/tests/unittest/extra_table1.md +55 -0
  120. data/tests/unittest/footnotes.md +126 -0
  121. data/tests/unittest/headers.md +54 -0
  122. data/tests/unittest/hex_entities.md +50 -0
  123. data/tests/unittest/hrule.md +60 -0
  124. data/tests/unittest/html2.md +38 -0
  125. data/tests/unittest/html3.md +47 -0
  126. data/tests/unittest/html4.md +42 -0
  127. data/tests/unittest/html5.md +38 -0
  128. data/tests/unittest/ie.md +82 -0
  129. data/tests/unittest/images.md +114 -0
  130. data/tests/unittest/images2.md +46 -0
  131. data/tests/unittest/inline_html.md +260 -0
  132. data/tests/unittest/inline_html2.md +36 -0
  133. data/tests/unittest/links.md +197 -0
  134. data/tests/unittest/list1.md +66 -0
  135. data/tests/unittest/list2.md +76 -0
  136. data/tests/unittest/list3.md +88 -0
  137. data/tests/unittest/list4.md +116 -0
  138. data/tests/unittest/lists.md +241 -0
  139. data/tests/unittest/lists11.md +31 -0
  140. data/tests/unittest/lists6.md +54 -0
  141. data/tests/unittest/lists7.md +79 -0
  142. data/tests/unittest/lists7b.md +136 -0
  143. data/tests/unittest/lists8.md +83 -0
  144. data/tests/unittest/lists9.md +85 -0
  145. data/tests/unittest/lists_after_paragraph.md +268 -0
  146. data/tests/unittest/lists_ol.md +324 -0
  147. data/tests/unittest/loss.md +29 -0
  148. data/tests/unittest/math/equations.md +69 -0
  149. data/tests/unittest/math/inline.md +66 -0
  150. data/tests/unittest/math/math2.md +110 -0
  151. data/tests/unittest/math/notmath.md +40 -0
  152. data/tests/unittest/math/table.md +43 -0
  153. data/tests/unittest/math/table2.md +60 -0
  154. data/tests/unittest/misc_sw.md +615 -0
  155. data/tests/unittest/notyet/escape.md +36 -0
  156. data/tests/unittest/notyet/header_after_par.md +81 -0
  157. data/tests/unittest/notyet/ticks.md +31 -0
  158. data/tests/unittest/notyet/triggering.md +206 -0
  159. data/tests/unittest/olist.md +64 -0
  160. data/tests/unittest/one.md +28 -0
  161. data/tests/unittest/paragraph.md +29 -0
  162. data/tests/unittest/paragraph_rules/dont_merge_ref.md +57 -0
  163. data/tests/unittest/paragraph_rules/tab_is_blank.md +39 -0
  164. data/tests/unittest/paragraphs.md +66 -0
  165. data/tests/unittest/pending/amps.md +29 -0
  166. data/tests/unittest/pending/empty_cells.md +53 -0
  167. data/tests/unittest/pending/link.md +103 -0
  168. data/tests/unittest/recover/recover_links.md +28 -0
  169. data/tests/unittest/references/long_example.md +88 -0
  170. data/tests/unittest/references/spaces_and_numbers.md +28 -0
  171. data/tests/unittest/smartypants.md +171 -0
  172. data/tests/unittest/syntax_hl.md +80 -0
  173. data/tests/unittest/table_attributes.md +52 -0
  174. data/tests/unittest/test.md +32 -0
  175. data/tests/unittest/wrapping.md +88 -0
  176. data/tests/unittest/xml.md +54 -0
  177. data/tests/unittest/xml2.md +34 -0
  178. data/tests/unittest/xml3.md +44 -0
  179. data/tests/unittest/xml_instruction.md +72 -0
  180. data/tests/utf8-files/simple.md +1 -0
  181. data/unit_test_block.sh +5 -0
  182. data/unit_test_span.sh +2 -0
  183. metadata +243 -0
@@ -0,0 +1,563 @@
1
+ #--
2
+ # Copyright (C) 2006 Andrea Censi <andrea (at) rubyforge.org>
3
+ #
4
+ # This file is part of Maruku.
5
+ #
6
+ # Maruku is free software; you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation; either version 2 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Maruku is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Maruku; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
+ #++
20
+
21
+ module MaRuKu
22
+
23
+ class MDDocument
24
+
25
+ Latex_preamble_enc_cjk =
26
+ "\\usepackage[C40]{fontenc}
27
+ \\usepackage[cjkjis]{ucs}
28
+ \\usepackage[utf8x]{inputenc}"
29
+
30
+ Latex_preamble_enc_utf8 =
31
+ "\\usepackage{ucs}
32
+ \\usepackage[utf8x]{inputenc}"
33
+
34
+ def latex_require_package(p)
35
+ if not self.latex_required_packages.include? p
36
+ self.latex_required_packages.push p
37
+ end
38
+ end
39
+
40
+ # Render as a LaTeX fragment
41
+ def to_latex
42
+ children_to_latex
43
+ end
44
+
45
+ =begin maruku_doc
46
+ Attribute: maruku_signature
47
+ Scope: document
48
+ Output: html, latex
49
+ Summary: Enables Maruku's signature.
50
+ Default: true
51
+
52
+ If false, Maruku does not append a signature to the
53
+ generated file.
54
+ =end
55
+
56
+ # Render as a complete LaTeX document
57
+ def to_latex_document
58
+ body = to_latex
59
+
60
+ if get_setting(:maruku_signature)
61
+ body += render_latex_signature
62
+ end
63
+
64
+ required =
65
+ self.latex_required_packages.map {|p|
66
+ "\\usepackage{#{p}}\n"
67
+ }.join
68
+
69
+ =begin maruku_doc
70
+ Attribute: latex_cjk
71
+ Scope: document
72
+ Output: latex
73
+ Summary: Support for CJK characters.
74
+
75
+ If the `latex_cjk` attribute is specified, then appropriate headers
76
+ are added to the LaTeX preamble to support Japanese fonts.
77
+ You have to have these fonts installed -- and this can be a pain.
78
+
79
+ If `latex_cjk` is specified, this is added to the preamble:
80
+
81
+ <?mrk puts "ciao" ?>
82
+
83
+ <?mrk md_codeblock(Maruku::MDDocument::Latex_preamble_enc_cjk) ?>
84
+
85
+
86
+ while the default is to add this:
87
+
88
+ <?mrk md_codeblock(Maruku::MDDocument::Latex_preamble_enc_utf8) ?>
89
+
90
+ =end
91
+ encoding = get_setting(:latex_cjk) ?
92
+ Latex_preamble_enc_cjk : Latex_preamble_enc_utf8
93
+
94
+ =begin maruku_doc
95
+ Attribute: latex_preamble
96
+ Scope: document
97
+ Output: latex
98
+ Summary: User-defined preamble.
99
+
100
+ If the `latex_preamble` attribute is specified, then its value
101
+ will be used as a custom preamble.
102
+
103
+ For example:
104
+
105
+ Title: My document
106
+ Latex preamble: preamble.tex
107
+
108
+ will produce:
109
+
110
+ ...
111
+ \input{preamble.tex}
112
+ ...
113
+
114
+ =end
115
+ user_preamble = (file = @doc.attributes[:latex_preamble]) ?
116
+ "\\input{#{file}}\n" : ""
117
+
118
+ "\\documentclass{article}
119
+
120
+ % Packages required to support encoding
121
+ #{encoding}
122
+
123
+ % Packages required by code
124
+ #{required}
125
+
126
+ % Packages always used
127
+ \\usepackage{hyperref}
128
+ \\usepackage{xspace}
129
+ \\usepackage[usenames,dvipsnames]{color}
130
+ \\hypersetup{colorlinks=true,urlcolor=blue}
131
+
132
+ #{user_preamble}
133
+
134
+ \\begin{document}
135
+ #{body}
136
+ \\end{document}
137
+ "
138
+ end
139
+
140
+
141
+ def render_latex_signature
142
+ "\\vfill
143
+ \\hrule
144
+ \\vspace{1.2mm}
145
+ \\begin{tiny}
146
+ Created by \\href{http://maruku.rubyforge.org}{Maruku} #{self.nice_date}.
147
+ \\end{tiny}"
148
+ end
149
+
150
+ end end
151
+
152
+ module MaRuKu; module Out; module Latex
153
+
154
+ def to_latex_hrule; "\n\\vspace{.5em} \\hrule \\vspace{.5em}\n" end
155
+ def to_latex_linebreak; "\\newline " end
156
+
157
+ def to_latex_paragraph
158
+ children_to_latex+"\n\n"
159
+ end
160
+
161
+
162
+ =begin maruku_doc
163
+ Title: Input format for colors
164
+ Output: latex, html
165
+ Related: code_background_color
166
+
167
+ Admissible formats:
168
+
169
+ green
170
+ #abc
171
+ #aabbcc
172
+ =end
173
+
174
+ # \color[named]{name}
175
+ # \color[rgb]{1,0.2,0.3}
176
+ def latex_color(s, command='color')
177
+ if s =~ /^\#(\w\w)(\w\w)(\w\w)$/
178
+ r = $1.hex; g = $2.hex; b=$3.hex
179
+ # convert from 0-255 to 0.0-1.0
180
+ r = r / 255.0; g = g / 255.0; b = b / 255.0;
181
+ "\\#{command}[rgb]{%0.2f,%0.2f,%0.2f}" % [r,g,b]
182
+ elsif s =~ /^\#(\w)(\w)(\w)$/
183
+ r = $1.hex; g = $2.hex; b=$3.hex
184
+ # convert from 0-15 to 0.0-1.0
185
+ r = r / 15.0; g = g / 15.0; b = b / 15.0;
186
+ "\\#{command}[rgb]{%0.2f,%0.2f,%0.2f}" % [r,g,b]
187
+ else
188
+ "\\#{command}{#{s}}"
189
+ end
190
+ end
191
+
192
+ =begin maruku_doc
193
+ Attribute: code_show_spaces
194
+ Scope: global, document, element
195
+
196
+ If `true`, shows spaces and tabs in code blocks.
197
+
198
+ Example:
199
+
200
+ One space
201
+ Two spaces
202
+ Tab, space, tab
203
+ Tab, tab, tab and all is green!
204
+ {:code_show_spaces code_background_color=#ffeedd}
205
+ {:markdown}
206
+
207
+ That will produce:
208
+
209
+ One space
210
+ Two spaces
211
+ Tab, space, tab
212
+ Tab, tab, tab and all is green!
213
+ {:code_show_spaces code_background_color=#ffeedd}
214
+
215
+ =end
216
+
217
+ =begin maruku_doc
218
+ Attribute: latex_use_listings
219
+ Scope: document
220
+ Output: latex
221
+ Summary: Support for `listings` package.
222
+ Related: code_show_spaces, code_background_color, lang, code_lang
223
+
224
+ If the `latex_use_listings` attribute is specified, then
225
+ code block are rendered using the `listings` package.
226
+ Otherwise, a standard `verbatim` environment is used.
227
+
228
+ * If the `lang` attribute for the code block has been specified,
229
+ it gets passed to the `listings` package using the `lstset` macro.
230
+ The default lang for code blocks is specified through
231
+ the `code_lang` attribute.
232
+
233
+ \lstset{language=ruby}
234
+
235
+ Please refer to the documentation of the `listings` package for
236
+ supported languages.
237
+
238
+ If a language is not supported, the `listings` package will emit
239
+ a warning during the compilation. Just press enter and nothing
240
+ wrong will happen.
241
+
242
+ * If the `code_show_spaces` is specified, than spaces and tabs will
243
+ be shown using the macro:
244
+
245
+ \lstset{showspaces=true,showtabs=true}
246
+
247
+ * The background color is given by `code_background_color`.
248
+
249
+ =end
250
+
251
+ def to_latex_code;
252
+ raw_code = self.raw_code
253
+
254
+ if get_setting(:latex_use_listings)
255
+ @doc.latex_require_package('listings')
256
+
257
+ s = "\\lstset{columns=fixed,frame=shadowbox}"
258
+
259
+ if get_setting(:code_show_spaces)
260
+ s+= "\\lstset{showspaces=true,showtabs=true}\n"
261
+ else
262
+ s+= "\\lstset{showspaces=false,showtabs=false}\n"
263
+ end
264
+
265
+ color = latex_color get_setting(:code_background_color)
266
+
267
+ s+= "\\lstset{backgroundcolor=#{color}}\n"
268
+
269
+ s+= "\\lstset{basicstyle=\\ttfamily\\footnotesize}\n"
270
+
271
+
272
+ lang = self.attributes[:lang] || @doc.attributes[:code_lang] || '{}'
273
+ if lang
274
+ s += "\\lstset{language=#{lang}}\n"
275
+ end
276
+
277
+ "#{s}\n\\begin{lstlisting}\n#{raw_code}\n\\end{lstlisting}"
278
+ else
279
+ "\\begin{verbatim}#{raw_code}\\end{verbatim}\n"
280
+ end
281
+ end
282
+
283
+ TexHeaders = {
284
+ 1=>'section',
285
+ 2=>'subsection',
286
+ 3=>'subsubsection',
287
+ 4=>'paragraph'}
288
+
289
+ def to_latex_header
290
+ h = TexHeaders[self.level] || 'paragraph'
291
+
292
+ title = children_to_latex
293
+ if number = section_number
294
+ title = number + title
295
+ end
296
+
297
+ if id = self.attributes[:id]
298
+ # drop '#' at the beginning
299
+ if id[0,1] == '#' then id = [1,id.size] end
300
+ %{\\hypertarget{%s}{}\\%s*{{%s}}\\label{%s}\n\n} % [ id, h, title, id ]
301
+ else
302
+ %{\\%s*{%s}\n\n} % [ h, title]
303
+ end
304
+ end
305
+
306
+
307
+ def to_latex_ul;
308
+ if self.attributes[:toc]
309
+ @doc.toc.to_latex
310
+ else
311
+ wrap_as_environment('itemize')
312
+ end
313
+ end
314
+
315
+ def to_latex_quote; wrap_as_environment('quote') end
316
+ def to_latex_ol; wrap_as_environment('enumerate') end
317
+ def to_latex_li;
318
+ "\\item #{children_to_latex}\n"
319
+ end
320
+ def to_latex_li_span;
321
+ "\\item #{children_to_latex}\n"
322
+ end
323
+
324
+ def to_latex_strong
325
+ "\\textbf{#{children_to_latex}}"
326
+ end
327
+ def to_latex_emphasis
328
+ "\\emph{#{children_to_latex}}"
329
+ end
330
+
331
+ def wrap_as_span(c)
332
+ "{#{c} #{children_to_latex}}"
333
+ end
334
+
335
+ def wrap_as_environment(name)
336
+ "\\begin{#{name}}%
337
+ #{children_to_latex}
338
+ \\end{#{name}}\n"
339
+ end
340
+
341
+ SAFE_CHARS = Set.new((?a..?z).to_a + (?A..?Z).to_a)
342
+ # the ultimate escaping
343
+ # (is much better than using \verb)
344
+ def latex_escape(source)
345
+ s="";
346
+
347
+ source.each_byte do |b|
348
+ if b == ?\
349
+ s << '~'
350
+ elsif SAFE_CHARS.include? b
351
+ s << b
352
+ else
353
+ s += "\\char%d" % b
354
+ end
355
+ end
356
+ s
357
+ end
358
+
359
+ def to_latex_inline_code;
360
+ source = self.raw_code
361
+
362
+ # Convert to printable latex chars
363
+ s = latex_escape(source)
364
+
365
+ color = get_setting(:code_background_color)
366
+ colorspec = latex_color(color, 'colorbox')
367
+
368
+ "{#{colorspec}{\\tt #{s}}}"
369
+ end
370
+
371
+ def to_latex_immediate_link
372
+ url = self.url
373
+ text = url.gsub(/^mailto:/,'') # don't show mailto
374
+ # gsub('~','$\sim$')
375
+ text = latex_escape(text)
376
+ if url[0,1] == '#'
377
+ url = url[1,url.size]
378
+ return "\\hyperlink{#{url}}{#{text}}"
379
+ else
380
+
381
+ return "\\href{#{url}}{#{text}}"
382
+ end
383
+ end
384
+
385
+ def to_latex_im_link
386
+ url = self.url
387
+
388
+ if url[0,1] == '#'
389
+ url = url[1,url.size]
390
+ return "\\hyperlink{#{url}}{#{children_to_latex}}"
391
+ else
392
+ return "\\href{#{url}}{#{children_to_latex}}"
393
+ end
394
+ end
395
+
396
+ def to_latex_link
397
+ id = self.ref_id
398
+ ref = @doc.refs[id]
399
+ if not ref
400
+ $stderr.puts "Could not find id = '#{id}'"
401
+ return children_to_latex
402
+ else
403
+ url = ref[:url]
404
+ #title = ref[:title] || 'no title'
405
+
406
+ if url[0,1] == '#'
407
+ url = url[1,url.size]
408
+ return "\\hyperlink{#{url}}{#{children_to_latex}}"
409
+ else
410
+ return "\\href{#{url}}{#{children_to_latex}}"
411
+ end
412
+ end
413
+
414
+ end
415
+
416
+ def to_latex_email_address
417
+ email = self.email
418
+ "\\href{mailto:#{email}}{#{latex_escape(email)}}"
419
+ end
420
+
421
+
422
+ def to_latex_table
423
+ align = self.align
424
+ num_columns = align.size
425
+
426
+ head = @children.slice(0, num_columns)
427
+ rows = []
428
+ i = num_columns
429
+ while i<@children.size
430
+ rows << @children.slice(i, num_columns)
431
+ i+=num_columns
432
+ end
433
+
434
+ h = {:center=>'c',:left=>'l',:right=>'r'}
435
+ align_string = align.map{|a| h[a]}.join('|')
436
+
437
+ s = "\\begin{tabular}{#{align_string}}\n"
438
+
439
+ s += array_to_latex(head, '&') + "\\\\" +"\n"
440
+
441
+ s += "\\hline \n"
442
+
443
+ rows.each do |row|
444
+ s += array_to_latex(row, '&') + "\\\\" +"\n"
445
+ end
446
+
447
+ s += "\\end{tabular}"
448
+
449
+ # puts table in its own paragraph
450
+ s += "\n\n"
451
+
452
+ s
453
+ end
454
+
455
+
456
+ def to_latex_head_cell; children_to_latex end
457
+ def to_latex_cell; children_to_latex end
458
+
459
+
460
+ def to_latex_footnote_reference
461
+ id = self.footnote_id
462
+ f = @doc.footnotes[id]
463
+ if f
464
+ "\\footnote{#{f.children_to_latex.strip}} "
465
+ else
466
+ $stderr.puts "Could not find footnote '#{fid}'"
467
+ end
468
+ end
469
+
470
+ def to_latex_raw_html
471
+ #'{\bf Raw HTML removed in latex version }'
472
+ ""
473
+ end
474
+
475
+ ## Definition lists ###
476
+ def to_latex_definition_list
477
+ s = "\\begin{description}\n"
478
+ s += children_to_latex
479
+ s += "\\end{description}\n"
480
+ s
481
+ end
482
+
483
+ def to_latex_definition
484
+ terms = self.terms
485
+ definitions = self.definitions
486
+
487
+ s = ""
488
+ terms.each do |t|
489
+ s +="\n\\item[#{t.children_to_latex}] "
490
+ end
491
+
492
+ definitions.each do |d|
493
+ s += "#{d.children_to_latex} \n"
494
+ end
495
+
496
+ s
497
+ end
498
+
499
+
500
+ def to_latex_abbr
501
+ children_to_latex
502
+ end
503
+
504
+ def to_latex_image
505
+ id = self.ref_id
506
+ ref = @doc.refs[id]
507
+ if not ref
508
+ maruku_error "Could not find ref #{id.inspect} for image.\n"+
509
+ "Available are: #{@docs.refs.keys.inspect}"
510
+ # $stderr.puts "Could not find id = '#{id}'"
511
+ ""
512
+ else
513
+ url = ref[:url]
514
+ $stderr.puts "Images not supported yet (#{url})"
515
+ # "{\\bf Images not supported yet (#{latex_escape(url)})}"
516
+ ""
517
+ end
518
+
519
+ end
520
+
521
+
522
+ # Convert each child to html
523
+ def children_to_latex
524
+ array_to_latex(@children)
525
+ end
526
+
527
+ def array_to_latex(array, join_char='')
528
+ e = []
529
+ array.each do |c|
530
+ method = c.kind_of?(MDElement) ?
531
+ "to_latex_#{c.node_type}" : "to_latex"
532
+
533
+ if not c.respond_to?(method)
534
+ # raise "Object does not answer to #{method}: #{c.class} #{c.inspect[0,100]}"
535
+ next
536
+ end
537
+
538
+ h = c.send(method)
539
+
540
+ if h.nil?
541
+ raise "Nil html for #{c.inspect} created with method #{method}"
542
+ end
543
+
544
+ if h.kind_of?Array
545
+ e = e + h
546
+ else
547
+ e << h
548
+ end
549
+ end
550
+
551
+ # puts a space after commands if needed
552
+ # e.each_index do |i|
553
+ # if e[i] =~ /\\\w+\s*$/ # command
554
+ # if (s=e[i+1]) && s[0] == ?\ # space
555
+ # e[i] = e[i] + "\\ "
556
+ # end
557
+ # end
558
+ # end
559
+
560
+ e.join(join_char)
561
+ end
562
+
563
+ end end end # MaRuKu::Out::Latex