maruku 0.6.1 → 0.7.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (263) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/MIT-LICENSE.txt +20 -0
  5. data/bin/maruku +153 -152
  6. data/bin/marutex +2 -29
  7. data/data/entities.xml +261 -0
  8. data/docs/math.md +14 -18
  9. data/lib/maruku.rb +65 -77
  10. data/lib/maruku/attributes.rb +109 -214
  11. data/lib/maruku/defaults.rb +45 -67
  12. data/lib/maruku/document.rb +43 -0
  13. data/lib/maruku/element.rb +112 -0
  14. data/lib/maruku/errors.rb +71 -0
  15. data/lib/maruku/ext/div.rb +105 -113
  16. data/lib/maruku/ext/fenced_code.rb +97 -0
  17. data/lib/maruku/ext/math.rb +22 -26
  18. data/lib/maruku/ext/math/elements.rb +20 -26
  19. data/lib/maruku/ext/math/mathml_engines/blahtex.rb +92 -104
  20. data/lib/maruku/ext/math/mathml_engines/itex2mml.rb +33 -26
  21. data/lib/maruku/ext/math/mathml_engines/none.rb +11 -19
  22. data/lib/maruku/ext/math/mathml_engines/ritex.rb +2 -4
  23. data/lib/maruku/ext/math/parsing.rb +107 -113
  24. data/lib/maruku/ext/math/to_html.rb +184 -187
  25. data/lib/maruku/ext/math/to_latex.rb +30 -21
  26. data/lib/maruku/helpers.rb +158 -257
  27. data/lib/maruku/html.rb +254 -0
  28. data/lib/maruku/input/charsource.rb +272 -319
  29. data/lib/maruku/input/extensions.rb +62 -63
  30. data/lib/maruku/input/html_helper.rb +220 -189
  31. data/lib/maruku/input/linesource.rb +90 -110
  32. data/lib/maruku/input/mdline.rb +129 -0
  33. data/lib/maruku/input/parse_block.rb +618 -612
  34. data/lib/maruku/input/parse_doc.rb +145 -215
  35. data/lib/maruku/input/parse_span.rb +658 -0
  36. data/lib/maruku/input/rubypants.rb +200 -128
  37. data/lib/maruku/inspect_element.rb +60 -0
  38. data/lib/maruku/maruku.rb +10 -31
  39. data/lib/maruku/output/entity_table.rb +33 -0
  40. data/lib/maruku/output/s5/fancy.rb +462 -462
  41. data/lib/maruku/output/s5/to_s5.rb +115 -135
  42. data/lib/maruku/output/to_html.rb +898 -983
  43. data/lib/maruku/output/to_latex.rb +561 -560
  44. data/lib/maruku/output/to_markdown.rb +207 -162
  45. data/lib/maruku/output/to_s.rb +11 -52
  46. data/lib/maruku/string_utils.rb +129 -179
  47. data/lib/maruku/toc.rb +185 -196
  48. data/lib/maruku/version.rb +33 -38
  49. data/spec/block_docs/abbrev.md +776 -0
  50. data/{tests/unittest → spec/block_docs}/abbreviations.md +11 -20
  51. data/{tests/unittest → spec/block_docs}/alt.md +2 -14
  52. data/{tests/unittest/pending → spec/block_docs}/amps.md +1 -13
  53. data/{tests/unittest → spec/block_docs}/attributes/att2.md +0 -12
  54. data/{tests/unittest → spec/block_docs}/attributes/att3.md +2 -14
  55. data/{tests/unittest → spec/block_docs}/attributes/attributes.md +12 -16
  56. data/{tests/unittest → spec/block_docs}/attributes/circular.md +0 -12
  57. data/{tests/unittest → spec/block_docs}/attributes/default.md +1 -13
  58. data/{tests/unittest → spec/block_docs}/blank.md +0 -12
  59. data/{tests/unittest → spec/block_docs}/blanks_in_code.md +16 -15
  60. data/{tests/unittest/loss.md → spec/block_docs/bug_def.md} +6 -18
  61. data/{tests/unittest → spec/block_docs}/bug_table.md +3 -15
  62. data/{tests/unittest → spec/block_docs}/code.md +7 -14
  63. data/{tests/unittest → spec/block_docs}/code2.md +4 -14
  64. data/{tests/unittest → spec/block_docs}/code3.md +12 -16
  65. data/{tests/unittest → spec/block_docs}/data_loss.md +2 -14
  66. data/{tests/unittest → spec/block_docs}/divs/div1.md +0 -12
  67. data/{tests/unittest → spec/block_docs}/divs/div2.md +0 -12
  68. data/{tests/unittest → spec/block_docs}/divs/div3_nest.md +3 -15
  69. data/{tests/unittest → spec/block_docs}/easy.md +1 -13
  70. data/spec/block_docs/email.md +29 -0
  71. data/{tests/unittest/pending → spec/block_docs}/empty_cells.md +3 -15
  72. data/{tests/unittest → spec/block_docs}/encoding/iso-8859-1.md +1 -14
  73. data/{tests/unittest → spec/block_docs}/encoding/utf-8.md +0 -12
  74. data/{tests/unittest → spec/block_docs}/entities.md +27 -29
  75. data/{tests/unittest/notyet → spec/block_docs}/escape.md +2 -14
  76. data/{tests/unittest → spec/block_docs}/escaping.md +11 -22
  77. data/{tests/unittest → spec/block_docs}/extra_dl.md +2 -13
  78. data/{tests/unittest → spec/block_docs}/extra_header_id.md +14 -20
  79. data/{tests/unittest → spec/block_docs}/extra_table1.md +3 -15
  80. data/spec/block_docs/fenced_code_blocks.md +66 -0
  81. data/spec/block_docs/fenced_code_blocks_highlighted.md +18 -0
  82. data/{tests/unittest → spec/block_docs}/footnotes.md +12 -24
  83. data/spec/block_docs/footnotes2.md +78 -0
  84. data/spec/block_docs/hard.md +25 -0
  85. data/spec/block_docs/header_after_par.md +62 -0
  86. data/{tests/unittest → spec/block_docs}/headers.md +10 -18
  87. data/{tests/unittest → spec/block_docs}/hex_entities.md +7 -18
  88. data/{tests/unittest → spec/block_docs}/hrule.md +5 -12
  89. data/{tests/unittest → spec/block_docs}/html3.md +1 -13
  90. data/{tests/unittest → spec/block_docs}/html4.md +2 -14
  91. data/{tests/unittest → spec/block_docs}/html5.md +2 -14
  92. data/spec/block_docs/html_block_in_para.md +22 -0
  93. data/spec/block_docs/html_inline.md +25 -0
  94. data/spec/block_docs/html_trailing.md +31 -0
  95. data/spec/block_docs/ie.md +62 -0
  96. data/spec/block_docs/iframe.md +29 -0
  97. data/{tests/unittest → spec/block_docs}/images.md +22 -28
  98. data/{tests/unittest → spec/block_docs}/images2.md +7 -17
  99. data/{tests/unittest → spec/block_docs}/inline_html.md +37 -67
  100. data/{tests/unittest → spec/block_docs}/inline_html2.md +1 -13
  101. data/spec/block_docs/inline_html_beginning.md +10 -0
  102. data/spec/block_docs/issue20.md +9 -0
  103. data/spec/block_docs/issue26.md +22 -0
  104. data/spec/block_docs/issue29.md +9 -0
  105. data/spec/block_docs/issue30.md +30 -0
  106. data/spec/block_docs/issue31.md +25 -0
  107. data/spec/block_docs/issue40.md +40 -0
  108. data/spec/block_docs/issue64.md +55 -0
  109. data/spec/block_docs/issue67.md +19 -0
  110. data/spec/block_docs/issue70.md +11 -0
  111. data/spec/block_docs/issue72.md +17 -0
  112. data/spec/block_docs/issue74.md +38 -0
  113. data/spec/block_docs/issue79.md +15 -0
  114. data/spec/block_docs/issue83.md +13 -0
  115. data/spec/block_docs/issue85.md +25 -0
  116. data/spec/block_docs/issue88.md +19 -0
  117. data/spec/block_docs/issue89.md +12 -0
  118. data/spec/block_docs/issue90.md +38 -0
  119. data/{tests/unittest/pending → spec/block_docs}/link.md +21 -18
  120. data/{tests/unittest → spec/block_docs}/links.md +33 -32
  121. data/spec/block_docs/links2.md +21 -0
  122. data/{tests/unittest → spec/block_docs}/list1.md +0 -12
  123. data/{tests/unittest → spec/block_docs}/list12.md +2 -14
  124. data/{tests/unittest → spec/block_docs}/list2.md +2 -14
  125. data/spec/block_docs/list_multipara.md +42 -0
  126. data/{tests/unittest → spec/block_docs}/lists.md +28 -29
  127. data/{tests/unittest → spec/block_docs}/lists10.md +2 -14
  128. data/spec/block_docs/lists11.md +23 -0
  129. data/spec/block_docs/lists12.md +43 -0
  130. data/spec/block_docs/lists13.md +55 -0
  131. data/spec/block_docs/lists14.md +61 -0
  132. data/spec/block_docs/lists15.md +36 -0
  133. data/spec/block_docs/lists6.md +88 -0
  134. data/spec/block_docs/lists7b.md +58 -0
  135. data/spec/block_docs/lists9.md +53 -0
  136. data/{tests/unittest → spec/block_docs}/lists_after_paragraph.md +19 -25
  137. data/spec/block_docs/lists_blank.md +35 -0
  138. data/{tests/unittest/list3.md → spec/block_docs/lists_blockquote_code.md} +2 -14
  139. data/{tests/unittest/list4.md → spec/block_docs/lists_need_blank_line.md} +50 -21
  140. data/spec/block_docs/lists_nested.md +44 -0
  141. data/spec/block_docs/lists_nested_blankline.md +28 -0
  142. data/spec/block_docs/lists_nested_deep.md +43 -0
  143. data/{tests/unittest → spec/block_docs}/lists_ol.md +37 -54
  144. data/spec/block_docs/lists_paraindent.md +47 -0
  145. data/spec/block_docs/lists_tab.md +54 -0
  146. data/spec/block_docs/loss.md +17 -0
  147. data/spec/block_docs/math-blahtex/equations.md +30 -0
  148. data/spec/block_docs/math-blahtex/inline.md +48 -0
  149. data/spec/block_docs/math-blahtex/math2.md +45 -0
  150. data/spec/block_docs/math-blahtex/table.md +25 -0
  151. data/spec/block_docs/math/embedded_invalid_svg.md +79 -0
  152. data/spec/block_docs/math/embedded_svg.md +97 -0
  153. data/spec/block_docs/math/equations.md +44 -0
  154. data/{tests/unittest → spec/block_docs}/math/inline.md +7 -19
  155. data/spec/block_docs/math/math2.md +45 -0
  156. data/{tests/unittest → spec/block_docs}/math/notmath.md +0 -12
  157. data/spec/block_docs/math/raw_mathml.md +87 -0
  158. data/spec/block_docs/math/table.md +25 -0
  159. data/{tests/unittest → spec/block_docs}/math/table2.md +5 -17
  160. data/{tests/unittest → spec/block_docs}/misc_sw.md +181 -118
  161. data/{tests/unittest → spec/block_docs}/olist.md +6 -18
  162. data/{tests/unittest → spec/block_docs}/one.md +0 -12
  163. data/{tests/unittest → spec/block_docs}/paragraph.md +0 -12
  164. data/{tests/unittest → spec/block_docs}/paragraph_rules/dont_merge_ref.md +4 -12
  165. data/{tests/unittest → spec/block_docs}/paragraph_rules/tab_is_blank.md +0 -12
  166. data/{tests/unittest → spec/block_docs}/paragraphs.md +1 -13
  167. data/{tests/unittest → spec/block_docs}/recover/recover_links.md +4 -16
  168. data/{tests/unittest/pending/ref.md → spec/block_docs/ref_with_period.md} +7 -16
  169. data/spec/block_docs/ref_with_title.md +22 -0
  170. data/{tests/unittest → spec/block_docs}/references/long_example.md +16 -23
  171. data/{tests/unittest → spec/block_docs}/references/spaces_and_numbers.md +0 -12
  172. data/{tests/unittest → spec/block_docs}/smartypants.md +24 -31
  173. data/{tests/unittest → spec/block_docs}/syntax_hl.md +13 -17
  174. data/{tests/unittest → spec/block_docs}/table_attributes.md +2 -14
  175. data/spec/block_docs/tables.md +58 -0
  176. data/{tests/unittest → spec/block_docs}/test.md +1 -13
  177. data/{tests/unittest/notyet → spec/block_docs}/ticks.md +1 -13
  178. data/spec/block_docs/toc.md +87 -0
  179. data/{tests/unittest/notyet → spec/block_docs}/triggering.md +14 -25
  180. data/{tests/unittest → spec/block_docs}/underscore_in_words.md +0 -12
  181. data/{tests/unittest → spec/block_docs}/wrapping.md +4 -16
  182. data/spec/block_docs/xml.md +33 -0
  183. data/{tests/unittest → spec/block_docs}/xml2.md +0 -12
  184. data/spec/block_docs/xml3.md +24 -0
  185. data/{tests/unittest → spec/block_docs}/xml_instruction.md +9 -20
  186. data/spec/block_spec.rb +110 -0
  187. data/spec/cli_spec.rb +8 -0
  188. data/spec/span_spec.rb +256 -0
  189. data/spec/spec_helper.rb +2 -0
  190. data/spec/to_html_utf8_spec.rb +13 -0
  191. metadata +205 -243
  192. metadata.gz.sig +3 -0
  193. data/Rakefile +0 -48
  194. data/bin/marudown +0 -29
  195. data/bin/marutest +0 -345
  196. data/docs/changelog.md +0 -334
  197. data/lib/maruku/errors_management.rb +0 -92
  198. data/lib/maruku/ext/math/latex_fix.rb +0 -12
  199. data/lib/maruku/input/parse_span_better.rb +0 -746
  200. data/lib/maruku/input/type_detection.rb +0 -147
  201. data/lib/maruku/output/to_latex_entities.rb +0 -367
  202. data/lib/maruku/output/to_latex_strings.rb +0 -64
  203. data/lib/maruku/structures.rb +0 -167
  204. data/lib/maruku/structures_inspect.rb +0 -87
  205. data/lib/maruku/structures_iterators.rb +0 -61
  206. data/lib/maruku/tests/benchmark.rb +0 -82
  207. data/lib/maruku/tests/new_parser.rb +0 -373
  208. data/lib/maruku/tests/tests.rb +0 -136
  209. data/lib/maruku/usage/example1.rb +0 -33
  210. data/tests/bugs/code_in_links.md +0 -101
  211. data/tests/bugs/complex_escaping.md +0 -38
  212. data/tests/math/syntax.md +0 -46
  213. data/tests/math_usage/document.md +0 -13
  214. data/tests/others/abbreviations.md +0 -11
  215. data/tests/others/blank.md +0 -4
  216. data/tests/others/code.md +0 -5
  217. data/tests/others/code2.md +0 -8
  218. data/tests/others/code3.md +0 -16
  219. data/tests/others/email.md +0 -4
  220. data/tests/others/entities.md +0 -19
  221. data/tests/others/escaping.md +0 -16
  222. data/tests/others/extra_dl.md +0 -101
  223. data/tests/others/extra_header_id.md +0 -13
  224. data/tests/others/extra_table1.md +0 -40
  225. data/tests/others/footnotes.md +0 -17
  226. data/tests/others/headers.md +0 -10
  227. data/tests/others/hrule.md +0 -10
  228. data/tests/others/images.md +0 -20
  229. data/tests/others/inline_html.md +0 -42
  230. data/tests/others/links.md +0 -38
  231. data/tests/others/list1.md +0 -4
  232. data/tests/others/list2.md +0 -5
  233. data/tests/others/list3.md +0 -8
  234. data/tests/others/lists.md +0 -32
  235. data/tests/others/lists_after_paragraph.md +0 -44
  236. data/tests/others/lists_ol.md +0 -39
  237. data/tests/others/misc_sw.md +0 -105
  238. data/tests/others/one.md +0 -1
  239. data/tests/others/paragraphs.md +0 -13
  240. data/tests/others/sss06.md +0 -352
  241. data/tests/others/test.md +0 -4
  242. data/tests/s5/s5profiling.md +0 -48
  243. data/tests/unittest/bug_def.md +0 -28
  244. data/tests/unittest/email.md +0 -32
  245. data/tests/unittest/html2.md +0 -34
  246. data/tests/unittest/ie.md +0 -61
  247. data/tests/unittest/links2.md +0 -34
  248. data/tests/unittest/lists11.md +0 -28
  249. data/tests/unittest/lists6.md +0 -53
  250. data/tests/unittest/lists9.md +0 -76
  251. data/tests/unittest/math/equations.md +0 -86
  252. data/tests/unittest/math/math2.md +0 -57
  253. data/tests/unittest/math/table.md +0 -37
  254. data/tests/unittest/notyet/header_after_par.md +0 -70
  255. data/tests/unittest/red_tests/abbrev.md +0 -1388
  256. data/tests/unittest/red_tests/lists7.md +0 -68
  257. data/tests/unittest/red_tests/lists7b.md +0 -128
  258. data/tests/unittest/red_tests/lists8.md +0 -76
  259. data/tests/unittest/red_tests/xml.md +0 -70
  260. data/tests/unittest/xml3.md +0 -38
  261. data/tests/utf8-files/simple.md +0 -1
  262. data/unit_test_block.sh +0 -5
  263. data/unit_test_span.sh +0 -3
@@ -1,121 +1,107 @@
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
- #++
1
+ require 'set'
20
2
 
21
- module MaRuKu
3
+ module MaRuKu::Out::Latex
4
+ module MDDocumentExtensions
5
+ # @return [Set<String>]
6
+ attr_accessor :latex_required_packages
7
+
8
+ def latex_require_package(p)
9
+ self.latex_required_packages << p
10
+ end
22
11
 
23
- class MDDocument
12
+ def initialize(*args)
13
+ self.latex_required_packages = Set.new
14
+ super
15
+ end
16
+ end
24
17
 
25
- Latex_preamble_enc_cjk =
26
- "\\usepackage[C40]{fontenc}
18
+ Latex_preamble_enc_cjk =
19
+ "\\usepackage[C40]{fontenc}
27
20
  \\usepackage[cjkjis]{ucs}
28
21
  \\usepackage[utf8x]{inputenc}"
29
22
 
30
- Latex_preamble_enc_utf8 =
31
- "\\usepackage{ucs}
23
+ Latex_preamble_enc_utf8 =
24
+ "\\usepackage{ucs}
32
25
  \\usepackage[utf8x]{inputenc}"
33
26
 
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}
27
+ # Render as a LaTeX fragment
28
+ def to_latex
29
+ children_to_latex
30
+ end
31
+
32
+ #=begin maruku_doc
33
+ # Attribute: maruku_signature
34
+ # Scope: document
35
+ # Output: html, latex
36
+ # Summary: Enables Maruku's signature.
37
+ # Default: true
38
+ #
39
+ # If false, Maruku does not append a signature to the
40
+ # generated file.
41
+ #=end
42
+
43
+ # Render as a complete LaTeX document
44
+ def to_latex_document
45
+ body = to_latex
46
+
47
+ if get_setting(:maruku_signature)
48
+ body << render_latex_signature
49
+ end
50
+
51
+ required = self.latex_required_packages.map do |p|
52
+ "\\usepackage{#{p}}\n"
53
+ end.join
54
+
55
+ #=begin maruku_doc
56
+ # Attribute: latex_cjk
57
+ # Scope: document
58
+ # Output: latex
59
+ # Summary: Support for CJK characters.
60
+ #
61
+ # If the `latex_cjk` attribute is specified, then appropriate headers
62
+ # are added to the LaTeX preamble to support Japanese fonts.
63
+ # You have to have these fonts installed -- and this can be a pain.
64
+ #
65
+ # If `latex_cjk` is specified, this is added to the preamble:
66
+ #
67
+ # <?mrk puts "ciao" ?>
68
+ #
69
+ # <?mrk md_codeblock(Maruku::MDDocument::Latex_preamble_enc_cjk) ?>
70
+ #
71
+ #
72
+ # while the default is to add this:
73
+ #
74
+ # <?mrk md_codeblock(Maruku::MDDocument::Latex_preamble_enc_utf8) ?>
75
+ #
76
+ #=end
77
+
78
+ encoding = get_setting(:latex_cjk) ? Latex_preamble_enc_cjk : Latex_preamble_enc_utf8
79
+
80
+ #=begin maruku_doc
81
+ # Attribute: latex_preamble
82
+ # Scope: document
83
+ # Output: latex
84
+ # Summary: User-defined preamble.
85
+ #
86
+ # If the `latex_preamble` attribute is specified, then its value
87
+ # will be used as a custom preamble.
88
+ #
89
+ # For example:
90
+ #
91
+ # Title: My document
92
+ # Latex preamble: preamble.tex
93
+ #
94
+ # will produce:
95
+ #
96
+ # ...
97
+ # \input{preamble.tex}
98
+ # ...
99
+ #
100
+ #=end
101
+
102
+ user_preamble = (file = @doc.attributes[:latex_preamble]) ? "\\input{#{file}}\n" : ""
103
+
104
+ "\\documentclass{article}
119
105
 
120
106
  % Packages required to support encoding
121
107
  #{encoding}
@@ -131,460 +117,475 @@ will produce:
131
117
 
132
118
  #{user_preamble}
133
119
 
134
- \\begin{document}
120
+ \\begin{document}
135
121
  #{body}
136
122
  \\end{document}
137
- "
138
- end
139
-
140
-
141
- def render_latex_signature
142
- "\\vfill
123
+ "
124
+ end
125
+
126
+ def render_latex_signature
127
+ "\\vfill
143
128
  \\hrule
144
129
  \\vspace{1.2mm}
145
130
  \\begin{tiny}
146
- Created by \\href{http://maruku.rubyforge.org}{Maruku} #{self.nice_date}.
131
+ Created by \\href{#{MaRuKu::MARUKU_URL}}{Maruku} #{self.nice_date}.
147
132
  \\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}}%
133
+ end
134
+
135
+ def to_latex_hrule
136
+ "\n\\vspace{.5em} \\hrule \\vspace{.5em}\n"
137
+ end
138
+
139
+ def to_latex_linebreak
140
+ "\\newline "
141
+ end
142
+
143
+ def to_latex_paragraph
144
+ children_to_latex + "\n\n"
145
+ end
146
+
147
+ #=begin maruku_doc
148
+ # Title: Input format for colors
149
+ # Output: latex, html
150
+ # Related: code_background_color
151
+ #
152
+ # Admissible formats:
153
+ #
154
+ # green
155
+ # #abc
156
+ # #aabbcc
157
+ #=end
158
+
159
+ # \color{name}
160
+ # \color[rgb]{1,0.2,0.3}
161
+ def latex_color(s, command='color')
162
+ if s =~ /\A\#([1-9A-F]{1,2})([1-9A-F]{1,2})([1-9A-F]{1,2})\z/i
163
+ # convert from 0-255 or 0-15 to 0.0-1.0
164
+ r, g, b = [$1, $2, $3].map {|c| c.hex / (c.length == 1 ? 15.0 : 255.0) }
165
+ "\\#{command}[rgb]{%0.2f,%0.2f,%0.2f}" % [r, g, b]
166
+ else
167
+ "\\#{command}{#{s}}"
168
+ end
169
+ end
170
+
171
+ #=begin maruku_doc
172
+ # Attribute: code_show_spaces
173
+ # Scope: global, document, element
174
+ #
175
+ # If `true`, shows spaces and tabs in code blocks.
176
+ #
177
+ # Example:
178
+ #
179
+ # One space
180
+ # Two spaces
181
+ # Tab, space, tab
182
+ # Tab, tab, tab and all is green!
183
+ # {:code_show_spaces code_background_color=#ffeedd}
184
+ # {:markdown}
185
+ #
186
+ # That will produce:
187
+ #
188
+ # One space
189
+ # Two spaces
190
+ # Tab, space, tab
191
+ # Tab, tab, tab and all is green!
192
+ # {:code_show_spaces code_background_color=#ffeedd}
193
+ #
194
+ #=end
195
+
196
+ #=begin maruku_doc
197
+ # Attribute: latex_use_listings
198
+ # Scope: document
199
+ # Output: latex
200
+ # Summary: Support for `listings` package.
201
+ # Related: code_show_spaces, code_background_color, lang, code_lang
202
+ #
203
+ # If the `latex_use_listings` attribute is specified, then
204
+ # code block are rendered using the `listings` package.
205
+ # Otherwise, a standard `verbatim` environment is used.
206
+ #
207
+ # * If the `lang` attribute for the code block has been specified,
208
+ # it gets passed to the `listings` package using the `lstset` macro.
209
+ # The default lang for code blocks is specified through
210
+ # the `code_lang` attribute.
211
+ #
212
+ # \lstset{language=ruby}
213
+ #
214
+ # Please refer to the documentation of the `listings` package for
215
+ # supported languages.
216
+ #
217
+ # If a language is not supported, the `listings` package will emit
218
+ # a warning during the compilation. Just press enter and nothing
219
+ # wrong will happen.
220
+ #
221
+ # * If the `code_show_spaces` is specified, than spaces and tabs will
222
+ # be shown using the macro:
223
+ #
224
+ # \lstset{showspaces=true,showtabs=true}
225
+ #
226
+ # * The background color is given by `code_background_color`.
227
+ #
228
+ #=end
229
+
230
+ def to_latex_code
231
+ if get_setting(:latex_use_listings)
232
+ @doc.latex_require_package('listings')
233
+
234
+ s = "\\lstset{columns=fixed,frame=shadowbox}"
235
+
236
+ if get_setting(:code_show_spaces)
237
+ s << "\\lstset{showspaces=true,showtabs=true}\n"
238
+ else
239
+ s << "\\lstset{showspaces=false,showtabs=false}\n"
240
+ end
241
+
242
+ color = latex_color get_setting(:code_background_color)
243
+
244
+ s << "\\lstset{backgroundcolor=#{color}}\n"
245
+
246
+ s << "\\lstset{basicstyle=\\ttfamily\\footnotesize}\n"
247
+
248
+
249
+ lang = self.attributes[:lang] || @doc.attributes[:code_lang] || '{}'
250
+ s << "\\lstset{language=#{lang}}\n" if lang
251
+
252
+ "#{s}\n\\begin{lstlisting}\n#{self.raw_code}\n\\end{lstlisting}"
253
+ else
254
+ "\\begin{verbatim}#{self.raw_code}\\end{verbatim}\n"
255
+ end
256
+ end
257
+
258
+ def to_latex_header
259
+ header_levels = %w(section subsection subsubsection)
260
+ h = header_levels[self.level - 1] || 'paragraph'
261
+
262
+ title = children_to_latex
263
+ if number = section_number
264
+ title = number + title
265
+ end
266
+
267
+ if id = self.attributes[:id]
268
+ # drop '#' at the beginning
269
+ id = id[1..-1] if id.start_with? '#'
270
+ %{\\hypertarget{%s}{}\\%s*{{%s}}\\label{%s}\n\n} % [ id, h, title, id ]
271
+ else
272
+ %{\\%s*{%s}\n\n} % [ h, title]
273
+ end
274
+ end
275
+
276
+ def to_latex_ul
277
+ if self.attributes[:toc]
278
+ @doc.toc.to_latex
279
+ else
280
+ wrap_as_environment('itemize')
281
+ end
282
+ end
283
+
284
+ def to_latex_quote
285
+ wrap_as_environment('quote')
286
+ end
287
+
288
+ def to_latex_ol
289
+ wrap_as_environment('enumerate')
290
+ end
291
+
292
+ def to_latex_li
293
+ "\\item #{children_to_latex}\n"
294
+ end
295
+
296
+ def to_latex_strong
297
+ "\\textbf{#{children_to_latex}}"
298
+ end
299
+
300
+ def to_latex_emphasis
301
+ "\\emph{#{children_to_latex}}"
302
+ end
303
+
304
+ def wrap_as_span(c)
305
+ "{#{c} #{children_to_latex}}"
306
+ end
307
+
308
+ def wrap_as_environment(name)
309
+ "\\begin{#{name}}%
337
310
  #{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
- def to_latex_div
522
- type = self.attributes[:class]
523
- id = self.attributes[:id]
524
- case type
525
- when /^un_(\w*)/
526
- s = "\\begin{u#{$1}}"
527
- # s += "[#{@children[0].send('children_to_latex')}]"
528
- @children.delete_at(0)
529
- s += "\n" + children_to_latex
530
- s += "\\end{u#{$1}}\n"
531
- when /^num_(\w*)/
532
- s = "\\begin{#{$1}}"
533
- # s += "[#{@children[0].send('children_to_latex')}]"
534
- @children.delete_at(0)
535
- s += "\n\\label{#{id}}\\hypertarget{#{id}}{}\n"
536
- s += children_to_latex
537
- s += "\\end{#{$1}}\n"
538
- when /^proof/
539
- s = "\\begin{proof}"
540
- @children.delete_at(0)
541
- s += "\n" + children_to_latex
542
- s += "\\end{proof}\n"
543
- else
544
- s = children_to_latex
545
- end
546
- s
547
- end
548
-
549
- # Convert each child to html
550
- def children_to_latex
551
- array_to_latex(@children)
552
- end
553
-
554
- def array_to_latex(array, join_char='')
555
- e = []
556
- array.each do |c|
557
- method = c.kind_of?(MDElement) ?
558
- "to_latex_#{c.node_type}" : "to_latex"
559
-
560
- if not c.respond_to?(method)
561
- # raise "Object does not answer to #{method}: #{c.class} #{c.inspect[0,100]}"
562
- next
563
- end
564
-
565
- h = c.send(method)
566
-
567
- if h.nil?
568
- raise "Nil html for #{c.inspect} created with method #{method}"
569
- end
570
-
571
- if h.kind_of?Array
572
- e = e + h
573
- else
574
- e << h
575
- end
576
- end
577
-
578
- # puts a space after commands if needed
579
- # e.each_index do |i|
580
- # if e[i] =~ /\\\w+\s*$/ # command
581
- # if (s=e[i+1]) && s[0] == ?\ # space
582
- # e[i] = e[i] + "\\ "
583
- # end
584
- # end
585
- # end
586
-
587
- e.join(join_char)
588
- end
589
-
590
- end end end # MaRuKu::Out::Latex
311
+ \\end{#{name}}\n"
312
+ end
313
+
314
+ SAFE_CHARS = Set.new(('a'..'z').to_a + ('A'..'Z').to_a)
315
+
316
+ # the ultimate escaping
317
+ # (is much better than using \verb)
318
+ def latex_escape(source)
319
+ source.chars.inject('') do |s, b|
320
+ s << if b == '\\'
321
+ '~'
322
+ elsif SAFE_CHARS.include? b
323
+ b
324
+ else
325
+ "\\char%d" % b[0].ord
326
+ end
327
+ end
328
+ end
329
+
330
+ def to_latex_entity
331
+ entity_name = self.entity_name
332
+
333
+ entity = MaRuKu::Out::EntityTable.instance.entity(entity_name)
334
+ unless entity
335
+ maruku_error "I don't know how to translate entity '#{entity_name}' to LaTeX."
336
+ return ""
337
+ end
338
+
339
+ replace = entity.latex_string
340
+ @doc.latex_require_package entity.latex_package if entity.latex_package
341
+
342
+ if replace
343
+ if replace.start_with?("\\") && !replace.end_with?('$', '}')
344
+ replace + "{}"
345
+ else
346
+ replace
347
+ end
348
+ else
349
+ tell_user "Cannot translate entity #{entity_name.inspect} to LaTeX."
350
+ entity_name
351
+ end
352
+ end
353
+
354
+ def to_latex_inline_code
355
+ # Convert to printable latex chars
356
+ s = latex_escape(self.raw_code)
357
+
358
+ color = get_setting(:code_background_color)
359
+ colorspec = latex_color(color, 'colorbox')
360
+
361
+ "{#{colorspec}{\\tt #{s}}}"
362
+ end
363
+
364
+ def to_latex_immediate_link
365
+ url = self.url
366
+ text = url.gsub(/^mailto:/,'') # don't show mailto
367
+ text = latex_escape(text)
368
+ if url.start_with? '#'
369
+ url = url[1..-1]
370
+ "\\hyperlink{#{url}}{#{text}}"
371
+ else
372
+ "\\href{#{url}}{#{text}}"
373
+ end
374
+ end
375
+
376
+ def to_latex_im_link
377
+ url = self.url
378
+
379
+ if url.start_with? '#'
380
+ url = url[1..-1]
381
+ "\\hyperlink{#{url}}{#{children_to_latex}}"
382
+ else
383
+ "\\href{#{url}}{#{children_to_latex}}"
384
+ end
385
+ end
386
+
387
+ def to_latex_link
388
+ id = self.ref_id || children_to_s
389
+ ref = @doc.refs[sanitize_ref_id(id)] || @doc.refs[sanitize_ref_id(children_to_s)]
390
+ if ref
391
+ url = ref[:url]
392
+
393
+ if url.start_with? '#'
394
+ url = url[1..-1]
395
+ "\\hyperlink{#{url}}{#{children_to_latex}}"
396
+ else
397
+ "\\href{#{url}}{#{children_to_latex}}"
398
+ end
399
+ else
400
+ $stderr.puts "Could not find id = '#{id}'"
401
+ children_to_latex
402
+ end
403
+ end
404
+
405
+ def to_latex_email_address
406
+ "\\href{mailto:#{self.email}}{#{latex_escape(self.email)}}"
407
+ end
408
+
409
+ def to_latex_table
410
+ num_columns = self.align.size
411
+
412
+ head, *rows = @children.each_slice(num_columns).to_a
413
+
414
+ h = { :center => 'c' , :left => 'l' , :right => 'r'}
415
+ align_string = self.align.map {|a| h[a] }.join('|')
416
+
417
+ s = "\\begin{tabular}{#{align_string}}\n"
418
+
419
+ s << array_to_latex(head, '&') + "\\\\" + "\n"
420
+
421
+ s << "\\hline \n"
422
+
423
+ rows.each do |row|
424
+ s << array_to_latex(row, '&') + "\\\\" + "\n"
425
+ end
426
+
427
+ s << "\\end{tabular}"
428
+
429
+ # puts table in its own paragraph
430
+ s << "\n\n"
431
+ end
432
+
433
+
434
+ def to_latex_head_cell
435
+ children_to_latex
436
+ end
437
+
438
+ def to_latex_cell
439
+ children_to_latex
440
+ end
441
+
442
+ def to_latex_footnote_reference
443
+ id = self.footnote_id
444
+ if f = @doc.footnotes[id]
445
+ "\\footnote{#{f.children_to_latex.strip}} "
446
+ else
447
+ $stderr.puts "Could not find footnote '#{id}'"
448
+ end
449
+ end
450
+
451
+ def to_latex_raw_html
452
+ # Raw HTML removed in latex version
453
+ ""
454
+ end
455
+
456
+ ## Definition lists ###
457
+ def to_latex_definition_list
458
+ s = "\\begin{description}\n"
459
+ s << children_to_latex
460
+ s << "\\end{description}\n"
461
+ end
462
+
463
+ def to_latex_definition
464
+ s = ""
465
+
466
+ self.terms.each do |t|
467
+ s << "\n\\item[#{t.children_to_latex}] "
468
+ end
469
+
470
+ self.definitions.each do |d|
471
+ s << "#{d.children_to_latex} \n"
472
+ end
473
+
474
+ s
475
+ end
476
+
477
+ def to_latex_abbr
478
+ children_to_latex
479
+ end
480
+
481
+ def to_latex_image
482
+ id = self.ref_id
483
+ ref = @doc.refs[sanitize_ref_id(id)] || @doc.refs[sanitize_ref_id(children_to_s)]
484
+ if ref
485
+ url = ref[:url]
486
+ $stderr.puts "Images not supported yet (#{url})"
487
+ ""
488
+ else
489
+ maruku_error "Could not find ref #{id.inspect} for image.\n"+
490
+ "Available are: #{@docs.refs.keys.inspect}"
491
+ ""
492
+ end
493
+ end
494
+
495
+ def to_latex_div
496
+ type = self.attributes[:class]
497
+ id = self.attributes[:id]
498
+ case type
499
+ when /^un_(\w*)/
500
+ @children.shift
501
+ s = "\\begin{u#{$1}}\n"
502
+ s << children_to_latex
503
+ s << "\\end{u#{$1}}\n"
504
+ when /^num_(\w*)/
505
+ @children.delete_at(0)
506
+ s = "\\begin{#{$1}}"
507
+ s << "\n\\label{#{id}}\\hypertarget{#{id}}{}\n"
508
+ s << children_to_latex
509
+ s << "\\end{#{$1}}\n"
510
+ when /^proof/
511
+ @children.delete_at(0)
512
+ s = "\\begin{proof}\n"
513
+ s << children_to_latex
514
+ s << "\\end{proof}\n"
515
+ else
516
+ children_to_latex
517
+ end
518
+ end
519
+
520
+ # Convert each child to html
521
+ def children_to_latex
522
+ array_to_latex(@children)
523
+ end
524
+
525
+ def array_to_latex(array, join_char='')
526
+ e = []
527
+ array.each do |c|
528
+ if c.kind_of?(String)
529
+ e << string_to_latex(c)
530
+ else method = c.kind_of?(Maruku::MDElement) ? "to_latex_#{c.node_type}" : "to_latex"
531
+ next unless c.respond_to?(method)
532
+
533
+ h = c.send(method)
534
+
535
+ unless h
536
+ raise "Nil latex for #{c.inspect} created with method #{method}"
537
+ end
538
+
539
+ if h.kind_of? Array
540
+ e.concat h
541
+ else
542
+ e << h
543
+ end
544
+ end
545
+ end
546
+ e.join(join_char)
547
+ end
548
+
549
+ # These are TeX's special characters
550
+ LATEX_ADD_SLASH = %w({ } $ & # _ %)
551
+
552
+ # These, we transform to {\tt \char<ascii code>}
553
+ LATEX_TO_CHARCODE = %w(^ ~ > <)
554
+
555
+ # escapes special characters
556
+ def string_to_latex(s)
557
+ s = escape_to_latex(s)
558
+ OtherGoodies.each do |k, v|
559
+ s.gsub!(k, v)
560
+ end
561
+ s
562
+ end
563
+
564
+ # other things that are good on the eyes
565
+ OtherGoodies = {
566
+ /(\s)LaTeX/ => '\1\\LaTeX\\xspace ', # XXX not if already \LaTeX
567
+ }
568
+
569
+ private
570
+
571
+ def escape_to_latex(s)
572
+ s.chars.inject("") do |result, b|
573
+ if LATEX_TO_CHARCODE.include? b
574
+ result << "{\\tt \\symbol{#{b[0].ord}}}"
575
+ elsif LATEX_ADD_SLASH.include? b
576
+ result << '\\' << b
577
+ elsif b == '\\'
578
+ # there is no backslash in cmr10 fonts
579
+ result << "$\\backslash$"
580
+ else
581
+ result << b
582
+ end
583
+ end
584
+ end
585
+ end
586
+
587
+ module MaRuKu
588
+ class MDDocument
589
+ include MaRuKu::Out::Latex::MDDocumentExtensions
590
+ end
591
+ end