maruku 0.6.0 → 0.7.3

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