patcito-maruku 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. data/AUTHORS +23 -0
  2. data/LICENSE +340 -0
  3. data/README.md +73 -0
  4. data/bin/maruku +196 -0
  5. data/bin/marutex +4 -0
  6. data/data/entities.xml +261 -0
  7. data/docs/changelog.md +334 -0
  8. data/docs/div_syntax.md +36 -0
  9. data/docs/entity_test.md +23 -0
  10. data/docs/markdown_syntax.md +899 -0
  11. data/docs/maruku.md +346 -0
  12. data/docs/math.md +194 -0
  13. data/docs/other_stuff.md +51 -0
  14. data/docs/proposal.md +309 -0
  15. data/docs/website/src/bluecloth.md +25 -0
  16. data/docs/website/src/download.md +31 -0
  17. data/docs/website/src/maruku.md +261 -0
  18. data/docs/website/src/proposal.md +271 -0
  19. data/lib/maruku.rb +132 -0
  20. data/lib/maruku/attributes.rb +138 -0
  21. data/lib/maruku/defaults.rb +69 -0
  22. data/lib/maruku/errors.rb +89 -0
  23. data/lib/maruku/ext/div.rb +121 -0
  24. data/lib/maruku/ext/fenced_code.rb +78 -0
  25. data/lib/maruku/ext/math.rb +37 -0
  26. data/lib/maruku/ext/math/elements.rb +21 -0
  27. data/lib/maruku/ext/math/latex_fix.rb +12 -0
  28. data/lib/maruku/ext/math/mathml_engines/blahtex.rb +93 -0
  29. data/lib/maruku/ext/math/mathml_engines/itex2mml.rb +39 -0
  30. data/lib/maruku/ext/math/mathml_engines/none.rb +21 -0
  31. data/lib/maruku/ext/math/mathml_engines/ritex.rb +24 -0
  32. data/lib/maruku/ext/math/parsing.rb +125 -0
  33. data/lib/maruku/ext/math/to_html.rb +237 -0
  34. data/lib/maruku/ext/math/to_latex.rb +36 -0
  35. data/lib/maruku/ext/yaml.rb +43 -0
  36. data/lib/maruku/helpers.rb +214 -0
  37. data/lib/maruku/input/charsource.rb +326 -0
  38. data/lib/maruku/input/extensions.rb +69 -0
  39. data/lib/maruku/input/html_helper.rb +189 -0
  40. data/lib/maruku/input/linesource.rb +111 -0
  41. data/lib/maruku/input/parse_block.rb +608 -0
  42. data/lib/maruku/input/parse_doc.rb +240 -0
  43. data/lib/maruku/input/parse_span_better.rb +746 -0
  44. data/lib/maruku/input/rubypants.rb +225 -0
  45. data/lib/maruku/input/type_detection.rb +147 -0
  46. data/lib/maruku/input_textile2/t2_parser.rb +163 -0
  47. data/lib/maruku/maruku.rb +31 -0
  48. data/lib/maruku/output/s5/fancy.rb +756 -0
  49. data/lib/maruku/output/s5/to_s5.rb +138 -0
  50. data/lib/maruku/output/to_html.rb +994 -0
  51. data/lib/maruku/output/to_latex.rb +580 -0
  52. data/lib/maruku/output/to_latex_entities.rb +101 -0
  53. data/lib/maruku/output/to_latex_strings.rb +64 -0
  54. data/lib/maruku/output/to_markdown.rb +164 -0
  55. data/lib/maruku/output/to_s.rb +54 -0
  56. data/lib/maruku/string_utils.rb +185 -0
  57. data/lib/maruku/structures.rb +143 -0
  58. data/lib/maruku/structures_inspect.rb +51 -0
  59. data/lib/maruku/structures_iterators.rb +48 -0
  60. data/lib/maruku/textile2.rb +1 -0
  61. data/lib/maruku/toc.rb +214 -0
  62. data/lib/maruku/usage/example1.rb +33 -0
  63. data/lib/maruku/version +0 -0
  64. data/lib/maruku/version.rb +54 -0
  65. data/spec/block_docs/abbreviations.md +52 -0
  66. data/spec/block_docs/alt.md +17 -0
  67. data/spec/block_docs/attributes/att2.md +20 -0
  68. data/spec/block_docs/attributes/att3.md +28 -0
  69. data/spec/block_docs/attributes/attributes.md +57 -0
  70. data/spec/block_docs/attributes/circular.md +26 -0
  71. data/spec/block_docs/attributes/default.md +22 -0
  72. data/spec/block_docs/blank.md +24 -0
  73. data/spec/block_docs/blanks_in_code.md +75 -0
  74. data/spec/block_docs/bug_def.md +16 -0
  75. data/spec/block_docs/bug_table.md +46 -0
  76. data/spec/block_docs/code.md +34 -0
  77. data/spec/block_docs/code2.md +28 -0
  78. data/spec/block_docs/code3.md +71 -0
  79. data/spec/block_docs/data_loss.md +25 -0
  80. data/spec/block_docs/divs/div1.md +167 -0
  81. data/spec/block_docs/divs/div2.md +21 -0
  82. data/spec/block_docs/divs/div3_nest.md +45 -0
  83. data/spec/block_docs/easy.md +15 -0
  84. data/spec/block_docs/email.md +20 -0
  85. data/spec/block_docs/encoding/iso-8859-1.md +23 -0
  86. data/spec/block_docs/encoding/utf-8.md +18 -0
  87. data/spec/block_docs/entities.md +94 -0
  88. data/spec/block_docs/escaping.md +67 -0
  89. data/spec/block_docs/extra_dl.md +52 -0
  90. data/spec/block_docs/extra_header_id.md +63 -0
  91. data/spec/block_docs/extra_table1.md +37 -0
  92. data/spec/block_docs/footnotes.md +97 -0
  93. data/spec/block_docs/headers.md +37 -0
  94. data/spec/block_docs/hex_entities.md +37 -0
  95. data/spec/block_docs/hrule.md +39 -0
  96. data/spec/block_docs/html2.md +22 -0
  97. data/spec/block_docs/html3.md +31 -0
  98. data/spec/block_docs/html4.md +25 -0
  99. data/spec/block_docs/html5.md +23 -0
  100. data/spec/block_docs/ie.md +49 -0
  101. data/spec/block_docs/images.md +90 -0
  102. data/spec/block_docs/images2.md +31 -0
  103. data/spec/block_docs/inline_html.md +152 -0
  104. data/spec/block_docs/inline_html2.md +21 -0
  105. data/spec/block_docs/links.md +152 -0
  106. data/spec/block_docs/links2.md +22 -0
  107. data/spec/block_docs/list1.md +46 -0
  108. data/spec/block_docs/list12.md +28 -0
  109. data/spec/block_docs/list2.md +56 -0
  110. data/spec/block_docs/list3.md +64 -0
  111. data/spec/block_docs/list4.md +89 -0
  112. data/spec/block_docs/lists.md +192 -0
  113. data/spec/block_docs/lists10.md +34 -0
  114. data/spec/block_docs/lists11.md +23 -0
  115. data/spec/block_docs/lists6.md +41 -0
  116. data/spec/block_docs/lists9.md +64 -0
  117. data/spec/block_docs/lists_after_paragraph.md +208 -0
  118. data/spec/block_docs/lists_ol.md +262 -0
  119. data/spec/block_docs/loss.md +16 -0
  120. data/spec/block_docs/math/equations.md +45 -0
  121. data/spec/block_docs/math/inline.md +46 -0
  122. data/spec/block_docs/math/math2.md +45 -0
  123. data/spec/block_docs/math/notmath.md +25 -0
  124. data/spec/block_docs/math/table.md +25 -0
  125. data/spec/block_docs/math/table2.md +42 -0
  126. data/spec/block_docs/misc_sw.md +525 -0
  127. data/spec/block_docs/notyet/escape.md +21 -0
  128. data/spec/block_docs/notyet/header_after_par.md +58 -0
  129. data/spec/block_docs/notyet/ticks.md +18 -0
  130. data/spec/block_docs/notyet/triggering.md +157 -0
  131. data/spec/block_docs/olist.md +45 -0
  132. data/spec/block_docs/one.md +15 -0
  133. data/spec/block_docs/paragraph.md +16 -0
  134. data/spec/block_docs/paragraph_rules/dont_merge_ref.md +42 -0
  135. data/spec/block_docs/paragraph_rules/tab_is_blank.md +24 -0
  136. data/spec/block_docs/paragraphs.md +46 -0
  137. data/spec/block_docs/pending/amps.md +15 -0
  138. data/spec/block_docs/pending/empty_cells.md +37 -0
  139. data/spec/block_docs/pending/link.md +72 -0
  140. data/spec/block_docs/pending/ref.md +21 -0
  141. data/spec/block_docs/recover/recover_links.md +15 -0
  142. data/spec/block_docs/red_tests/abbrev.md +679 -0
  143. data/spec/block_docs/red_tests/lists7.md +32 -0
  144. data/spec/block_docs/red_tests/lists7b.md +65 -0
  145. data/spec/block_docs/red_tests/lists8.md +42 -0
  146. data/spec/block_docs/red_tests/ref.md +23 -0
  147. data/spec/block_docs/red_tests/xml.md +35 -0
  148. data/spec/block_docs/references/long_example.md +71 -0
  149. data/spec/block_docs/references/spaces_and_numbers.md +15 -0
  150. data/spec/block_docs/smartypants.md +114 -0
  151. data/spec/block_docs/syntax_hl.md +52 -0
  152. data/spec/block_docs/table_attributes.md +34 -0
  153. data/spec/block_docs/test.md +19 -0
  154. data/spec/block_docs/underscore_in_words.md +15 -0
  155. data/spec/block_docs/wrapping.md +67 -0
  156. data/spec/block_docs/xml2.md +19 -0
  157. data/spec/block_docs/xml3.md +26 -0
  158. data/spec/block_docs/xml_instruction.md +52 -0
  159. data/spec/block_spec.rb +49 -0
  160. data/spec/span_spec.rb +254 -0
  161. data/spec/spec_helper.rb +6 -0
  162. metadata +247 -0
@@ -0,0 +1,69 @@
1
+ # Copyright (C) 2006 Andrea Censi <andrea (at) rubyforge.org>
2
+ #
3
+ # This file is part of Maruku.
4
+ #
5
+ # Maruku is free software; you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation; either version 2 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # Maruku is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with Maruku; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
+
19
+
20
+ module MaRuKu
21
+
22
+ Globals = {
23
+ :unsafe_features => false,
24
+ :on_error => :warning,
25
+
26
+ :yaml_frontmatter => false,
27
+
28
+ :use_numbered_headers => false,
29
+
30
+ :maruku_signature => false,
31
+ :code_background_color => '#fef',
32
+ :code_show_spaces => false,
33
+
34
+ :filter_html => false,
35
+
36
+ :html_math_output_mathjax => true,
37
+ :html_math_output_span_equations => false,
38
+
39
+ :html_math_output_mathml => true, # also set :html_math_engine
40
+ :html_math_engine => 'none', #ritex, itex2mml
41
+
42
+ :html_math_output_png => false,
43
+ :html_png_engine => 'none',
44
+ :html_png_dir => 'pngs',
45
+ :html_png_url => 'pngs/',
46
+ :html_png_resolution => 200,
47
+
48
+ :html_use_syntax => false,
49
+
50
+ :latex_use_listings => false,
51
+ :latex_cjk => false,
52
+ :latex_cache_file => "blahtex_cache.pstore", # cache file for blahtex filter
53
+
54
+ :debug_keep_ials => false,
55
+ :doc_prefix => ''
56
+ }
57
+
58
+ class MDElement
59
+ def get_setting(sym)
60
+ return attributes[sym] if attributes.has_key?(sym)
61
+ return doc.attributes[sym] if doc && doc.attributes.has_key?(sym)
62
+ return MaRuKu::Globals[sym] if MaRuKu::Globals.has_key?(sym)
63
+
64
+ $stderr.puts "Bug: no default for #{sym.inspect}"
65
+ nil
66
+ end
67
+ end
68
+
69
+ end
@@ -0,0 +1,89 @@
1
+ # Copyright (C) 2006 Andrea Censi <andrea (at) rubyforge.org>
2
+ #
3
+ # This file is part of Maruku.
4
+ #
5
+ # Maruku is free software; you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation; either version 2 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # Maruku is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with Maruku; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
+
19
+
20
+ module MaRuKu
21
+ class Exception < RuntimeError; end
22
+
23
+ module Errors
24
+ FRAME_WIDTH = 75
25
+
26
+ # Properly handles a formatting error.
27
+ # All such errors go through this method.
28
+ #
29
+ # The behavior depends on {MaRuKu::Globals `MaRuKu::Globals[:on_error]`}.
30
+ # If this is `:warning`, this prints the error to stderr
31
+ # (or `@error_stream if` it's defined) and tries to continue.
32
+ # If `:on_error` is `:ignore`, this doesn't print anything
33
+ # and tries to continue. If it's `:raise`, this raises a {MaRuKu::Exception}.
34
+ #
35
+ # By default, `:on_error` is set to `:warning`.
36
+ #
37
+ # @overload def maruku_error(s, src = nil, con = nil)
38
+ # @param s [String] The text of the error
39
+ # @param src [#describe, nil] The source of the error
40
+ # @param con [#describe, nil] The context of the error
41
+ # @raise [MaRuKu::Exception] If `:on_error` is set to `:raise`
42
+ def maruku_error(*args)
43
+ policy = get_setting(:on_error)
44
+
45
+ case policy
46
+ when :ignore
47
+ when :raise
48
+ raise_error create_frame(describe_error(*args))
49
+ when :warning
50
+ tell_user create_frame(describe_error(*args))
51
+ else
52
+ raise "Unknown on_error policy: #{policy.inspect}"
53
+ end
54
+ end
55
+
56
+ def maruku_recover(*args)
57
+ tell_user create_frame(describe_error(*args))
58
+ end
59
+ alias error maruku_error
60
+
61
+ def raise_error(s)
62
+ raise MaRuKu::Exception, s, caller
63
+ end
64
+
65
+ def tell_user(s)
66
+ (self.attributes[:error_stream] || $stderr) << s
67
+ end
68
+
69
+ private
70
+
71
+ def create_frame(s)
72
+ "\n" + <<FRAME
73
+ #{"_" * FRAME_WIDTH}
74
+ | Maruku tells you:
75
+ +#{"-" * FRAME_WIDTH}
76
+ #{s.gsub(/^/, '| ').rstrip}
77
+ +#{"-" * FRAME_WIDTH}
78
+ #{caller[0...5].join("\n").gsub(/^/, '!')}
79
+ \\#{"_" * FRAME_WIDTH}
80
+ FRAME
81
+ end
82
+
83
+ def describe_error(s, src = nil, con = nil)
84
+ s += "\n#{src.describe}\n" if src
85
+ s += "\n#{con.describe}\n" if con
86
+ s
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,121 @@
1
+ #+-----------------------------------{.warning}------
2
+ #| this is the last warning!
3
+ #|
4
+ #| please, go away!
5
+ #|
6
+ #| +------------------------------------- {.menace} --
7
+ #| | or else terrible things will happen
8
+ #| +--------------------------------------------------
9
+ #+---------------------------------------------------
10
+
11
+ # TODO: Scope these properly
12
+ OpenDiv = /^[ ]{0,3}\+\-\-+\s*(\{([^{}]*?|".*?"|'.*?')*\})?\s*\-*\s*$/
13
+ CloseDiv = /^[ ]{0,3}\=\-\-+\s*(\{([^{}]*?|".*?"|'.*?')*\})?\s*\-*\s*$/
14
+ StartPipe = /^[ ]{0,3}\|(.*)$/ # $1 is rest of line
15
+ DecorativeClosing = OpenDiv
16
+
17
+ MaRuKu::In::Markdown.register_block_extension(
18
+ :regexp => OpenDiv,
19
+ :handler => lambda do |doc, src, context|
20
+ first = src.shift_line
21
+ ial_at_beginning = first[OpenDiv, 1]
22
+ ial_at_end = nil
23
+
24
+ lines = []
25
+ # if second line starts with "|"
26
+ if src.cur_line =~ StartPipe
27
+ # then we read until no more "|"
28
+ while src.cur_line && src.cur_line =~ StartPipe
29
+ lines.push $1
30
+ src.shift_line
31
+ end
32
+ if src.cur_line =~ DecorativeClosing
33
+ ial_at_end = $1
34
+ src.shift_line
35
+ end
36
+ else
37
+ # else we read until CloseDiv
38
+ divs_open = 1
39
+ while src.cur_line && divs_open > 0
40
+ if src.cur_line =~ CloseDiv
41
+ divs_open -= 1
42
+ if divs_open == 0
43
+ ial_at_end = $1
44
+ src.shift_line
45
+ break
46
+ else
47
+ lines.push src.shift_line
48
+ end
49
+ else
50
+ if src.cur_line =~ OpenDiv
51
+ divs_open += 1
52
+ end
53
+ lines.push src.shift_line
54
+ end
55
+ end
56
+
57
+ if divs_open > 0
58
+ doc.maruku_error("At end of input, I still have #{divs_open} DIVs open.",
59
+ src, context)
60
+ next true
61
+ end
62
+ end
63
+
64
+ ial_at_beginning = nil unless ial_at_beginning && ial_at_beginning.size > 0
65
+ ial_at_end = nil unless ial_at_end && ial_at_end.size > 0
66
+
67
+ if ial_at_beginning && ial_at_end
68
+ doc.maruku_error("Found two conflicting IALs: #{ial_at_beginning.inspect} and #{ial_at_end.inspect}",
69
+ src, context)
70
+ end
71
+
72
+ al_string = ial_at_beginning || ial_at_end
73
+ al = nil
74
+
75
+ if al_string =~ /^\{(.*)\}\s*$/
76
+ al = al_string && doc.read_attribute_list(
77
+ MaRuKu::In::Markdown::SpanLevelParser::CharSource.new($1),
78
+ nil, [nil])
79
+ end
80
+
81
+ context.push(
82
+ doc.md_div(
83
+ doc.parse_blocks(
84
+ MaRuKu::In::Markdown::BlockLevelParser::LineSource.new(lines)),
85
+ al))
86
+ true
87
+ end)
88
+
89
+ module MaRuKu
90
+ class MDElement
91
+ def md_div(children, al = nil)
92
+ type = label = num = nil
93
+ doc.refid2ref ||= {}
94
+ if al
95
+ al.each do |k, v|
96
+ case k
97
+ when :class; type = $1 if v =~ /^num_(\w*)/
98
+ when :id; label = v
99
+ end
100
+ end
101
+ end
102
+
103
+ if type
104
+ doc.refid2ref[type] ||= {}
105
+ num = doc.refid2ref[type].length + 1
106
+ end
107
+
108
+ e = self.md_el(:div, children, {:label => label, :type => type, :num => num}, al)
109
+ doc.refid2ref[type].update(label => e) if type && label
110
+ e
111
+ end
112
+ end
113
+
114
+ module Out
115
+ module HTML
116
+ def to_html_div
117
+ add_ws wrap_as_element('div')
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,78 @@
1
+ # fenced_code.rb -- Maruku extension for fenced code blocks
2
+ #
3
+ # Copyright (C) 2009 Jason R. Blevins
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # * Redistributions of source code must retain the above copyright notice,
10
+ # this list of conditions and the following disclaimer.
11
+ #
12
+ # * Redistributions in binary form must reproduce the above copyright notice,
13
+ # this list of conditions and the following disclaimer in the documentation
14
+ # and/or other materials provided with the distribution.
15
+ #
16
+ # * Neither the names of the copyright holders nor the names of any
17
+ # contributors may be used to endorse or promote products derived from this
18
+ # software without specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ # POSSIBILITY OF SUCH DAMAGE.
31
+ #
32
+ # Fenced code blocks begin with three or more tildes and are terminated
33
+ # by a closing line with at least as many tildes as the opening line.
34
+ # Optionally, an attribute list may appear at the end of the opening
35
+ # line. For example:
36
+ #
37
+ # ~~~~~~~~~~~~~ {: lang=ruby }
38
+ # puts 'Hello world'
39
+ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40
+
41
+ OpenFence = /^(~~~+?)\s*(\{([^{}]*?|".*?"|'.*?')*\})?\s*$/
42
+
43
+ MaRuKu::In::Markdown::register_block_extension(
44
+ :regexp => OpenFence,
45
+ :handler => lambda { |doc, src, context|
46
+
47
+ first = src.shift_line
48
+ first =~ OpenFence
49
+ close_fence = /^#{$1}~*$/
50
+ ial = $2
51
+
52
+ lines = []
53
+
54
+ # read until CloseFence
55
+ while src.cur_line
56
+ if src.cur_line =~ close_fence
57
+ src.shift_line
58
+ break
59
+ else
60
+ lines.push src.shift_line
61
+ end
62
+ end
63
+
64
+ ial = nil unless (ial && ial.size > 0)
65
+ al = nil
66
+
67
+ if ial =~ /^\{(.*?)\}\s*$/
68
+ inside = $1
69
+ cs = MaRuKu::In::Markdown::SpanLevelParser::CharSource
70
+ al = ial &&
71
+ doc.read_attribute_list(cs.new(inside),
72
+ its_context=nil, break_on=[nil])
73
+ end
74
+
75
+ source = lines.join("\n")
76
+ context.push doc.md_codeblock(source, al)
77
+ true
78
+ })
@@ -0,0 +1,37 @@
1
+ require 'maruku/ext/math/elements'
2
+ require 'maruku/ext/math/parsing'
3
+ require 'maruku/ext/math/to_latex'
4
+ require 'maruku/ext/math/to_html'
5
+
6
+ require 'maruku/ext/math/mathml_engines/none'
7
+ require 'maruku/ext/math/mathml_engines/ritex'
8
+ require 'maruku/ext/math/mathml_engines/itex2mml'
9
+ require 'maruku/ext/math/mathml_engines/blahtex'
10
+
11
+ =begin maruku_doc
12
+ Attribute: math_enabled
13
+ Scope: global, document
14
+ Summary: Enables parsing of LaTeX math
15
+
16
+ To explicitly disable the math parsing:
17
+
18
+ Maruku.new(string, {:math_enabled => false})
19
+ {:ruby}
20
+
21
+ =end
22
+
23
+ MaRuKu::Globals[:math_enabled] = true
24
+
25
+
26
+ =begin maruku_doc
27
+ Attribute: math_numbered
28
+ Scope: global, document
29
+ Summary: Math openings which should be numerated
30
+
31
+ Array containing any of `'\\['`, `'\\begin{equation}'`, `'$$'`.
32
+
33
+ MaRuKu::Globals[:math_numbered] = ['\\[']
34
+
35
+ =end
36
+
37
+ MaRuKu::Globals[:math_numbered] = []
@@ -0,0 +1,21 @@
1
+ module MaRuKu
2
+ class MDElement
3
+ def md_inline_math(math)
4
+ self.md_el(:inline_math, [], :math => math)
5
+ end
6
+
7
+ def md_equation(math, label, numerate)
8
+ reglabel = /\\label\{(\w+)\}/
9
+ math = math.gsub(reglabel, '') if label = math[reglabel, 1]
10
+ num = nil
11
+ if (label || numerate) && @doc # take number
12
+ @doc.eqid2eq ||= {}
13
+ num = @doc.eqid2eq.size + 1
14
+ label = "eq#{num}" if not label # TODO do id for document
15
+ end
16
+ e = self.md_el(:equation, [], :math => math, :label => label, :num => num)
17
+ @doc.eqid2eq[label] = e if label && @doc # take number
18
+ e
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ class String
2
+ # fix some LaTeX command-name clashes
3
+ def fix_latex
4
+ if #{html_math_engine} == 'itex2mml'
5
+ s = self.gsub("\\mathop{", "\\operatorname{")
6
+ s.gsub!(/\\begin\{svg\}.*?\\end\{svg\}/m, " ")
7
+ s.gsub("\\space{", "\\itexspace{")
8
+ else
9
+ self
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,93 @@
1
+ require 'tempfile'
2
+ require 'fileutils'
3
+ require 'digest/md5'
4
+ require 'pstore'
5
+
6
+ module MaRuKu
7
+ module Out
8
+ module HTML
9
+ PNG = Struct.new(:src, :depth, :height)
10
+
11
+ def convert_to_png_blahtex(kind, tex)
12
+ FileUtils.mkdir_p get_setting(:html_png_dir)
13
+
14
+ # first, we check whether this image has already been processed
15
+ md5sum = Digest::MD5.hexdigest(tex + " params: ")
16
+ result_file = File.join(get_setting(:html_png_dir), md5sum + ".txt")
17
+
18
+ if not File.exists?(result_file)
19
+ Tempfile.open('maruku_blahtex') do |tmp_in|
20
+ tmp_in.write tex
21
+ tmp_in.close
22
+
23
+ # It's important taht we don't replace *all* newlines,
24
+ # because newlines in arguments get escaped as "'\n'".
25
+ system <<COMMAND.gsub("\n ", " ")
26
+ blahtex --png --use-preview-package
27
+ --shell-dvipng #{shellescape("dvipng -D #{shellescape(get_setting(:html_png_resolution).to_s)}")}
28
+ #{'--displaymath' if kind == :equation}
29
+ --temp-directory #{shellescape(get_setting(:html_png_dir))}
30
+ --png-directory #{shellescape(get_setting(:html_png_dir))}
31
+ < #{shellescape(tmp_in.path)}
32
+ > #{shellescape(result_file)}
33
+ COMMAND
34
+ end
35
+ end
36
+
37
+ result = File.read(result_file)
38
+ if result.nil? || result.empty?
39
+ maruku_error "Blahtex error: empty output"
40
+ return
41
+ end
42
+
43
+ doc = Document.new(result, :respect_whitespace => :all)
44
+ png = doc.root.elements[1]
45
+ if png.name != 'png'
46
+ maruku_error "Blahtex error: \n#{doc}"
47
+ return
48
+ end
49
+
50
+ raise "No depth element in:\n #{doc}" unless depth = png.elements['depth']
51
+ raise "No height element in:\n #{doc}" unless height = png.elements['height']
52
+ raise "No md5 element in:\n #{doc}" unless md5 = png.elements['md5']
53
+
54
+ depth = depth.text.to_f
55
+ height = height.text.to_f # TODO: check != 0
56
+ md5 = md5.text
57
+
58
+ PNG.new("#{get_setting(:html_png_url)}#{md5}.png", depth, height)
59
+ rescue Exception => e
60
+ maruku_error "Error: #{e}"
61
+ end
62
+
63
+
64
+ def convert_to_mathml_blahtex(kind, tex)
65
+ @@BlahtexCache ||= PStore.new(get_setting(:latex_cache_file))
66
+
67
+ @@BlahtexCache.transaction do
68
+ if @@BlahtexCache[tex].nil?
69
+ Tempfile.open('maruku_blahtex') do |tmp_in|
70
+ tmp_in.write tex
71
+
72
+ Tempfile.new('maruku_blahtex') do |tmp_out|
73
+ system "blahtex --mathml < #{shellescape(tmp_in.path)} > #{shellescape(tmp_out.path)}"
74
+ @@BlahtexCache[tex] = tmp_out.read
75
+ end
76
+ end
77
+ end
78
+
79
+ blahtex = @@BlahtexCache[tex]
80
+ doc = Document.new(blahtex, :respect_whitespace => :all)
81
+ unless mathml = doc.root.elements['mathml']
82
+ maruku_error "Blahtex error: \n#{doc}"
83
+ return
84
+ end
85
+
86
+ return mathml
87
+ end
88
+ rescue Exception => e
89
+ maruku_error "Error: #{e}"
90
+ end
91
+ end
92
+ end
93
+ end