maiku 0.6.1.maiku

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/lib/maruku.rb +141 -0
  2. data/lib/maruku/attributes.rb +175 -0
  3. data/lib/maruku/defaults.rb +71 -0
  4. data/lib/maruku/errors_management.rb +92 -0
  5. data/lib/maruku/ext/div.rb +133 -0
  6. data/lib/maruku/ext/math.rb +41 -0
  7. data/lib/maruku/ext/math/elements.rb +27 -0
  8. data/lib/maruku/ext/math/latex_fix.rb +12 -0
  9. data/lib/maruku/ext/math/mathml_engines/blahtex.rb +107 -0
  10. data/lib/maruku/ext/math/mathml_engines/itex2mml.rb +29 -0
  11. data/lib/maruku/ext/math/mathml_engines/none.rb +20 -0
  12. data/lib/maruku/ext/math/mathml_engines/ritex.rb +24 -0
  13. data/lib/maruku/ext/math/parsing.rb +119 -0
  14. data/lib/maruku/ext/math/to_html.rb +187 -0
  15. data/lib/maruku/ext/math/to_latex.rb +26 -0
  16. data/lib/maruku/helpers.rb +260 -0
  17. data/lib/maruku/input/charsource.rb +326 -0
  18. data/lib/maruku/input/extensions.rb +69 -0
  19. data/lib/maruku/input/html_helper.rb +189 -0
  20. data/lib/maruku/input/linesource.rb +111 -0
  21. data/lib/maruku/input/parse_block.rb +616 -0
  22. data/lib/maruku/input/parse_doc.rb +232 -0
  23. data/lib/maruku/input/parse_span_better.rb +746 -0
  24. data/lib/maruku/input/rubypants.rb +225 -0
  25. data/lib/maruku/input/type_detection.rb +147 -0
  26. data/lib/maruku/input_textile2/t2_parser.rb +163 -0
  27. data/lib/maruku/maruku.rb +33 -0
  28. data/lib/maruku/output/s5/fancy.rb +756 -0
  29. data/lib/maruku/output/s5/to_s5.rb +138 -0
  30. data/lib/maruku/output/to_html.rb +991 -0
  31. data/lib/maruku/output/to_latex.rb +590 -0
  32. data/lib/maruku/output/to_latex_entities.rb +367 -0
  33. data/lib/maruku/output/to_latex_strings.rb +64 -0
  34. data/lib/maruku/output/to_markdown.rb +164 -0
  35. data/lib/maruku/output/to_s.rb +56 -0
  36. data/lib/maruku/string_utils.rb +201 -0
  37. data/lib/maruku/structures.rb +167 -0
  38. data/lib/maruku/structures_inspect.rb +87 -0
  39. data/lib/maruku/structures_iterators.rb +61 -0
  40. data/lib/maruku/textile2.rb +1 -0
  41. data/lib/maruku/toc.rb +199 -0
  42. data/lib/maruku/usage/example1.rb +33 -0
  43. data/lib/maruku/version.rb +39 -0
  44. metadata +167 -0
@@ -0,0 +1,187 @@
1
+
2
+ =begin maruku_doc
3
+ Extension: math
4
+ Attribute: html_math_engine
5
+ Scope: document, element
6
+ Output: html
7
+ Summary: Select the rendering engine for MathML.
8
+ Default: <?mrk Globals[:html_math_engine].to_s ?>
9
+
10
+ Select the rendering engine for math.
11
+
12
+ If you want to use your custom engine `foo`, then set:
13
+
14
+ HTML math engine: foo
15
+ {:lang=markdown}
16
+
17
+ and then implement two functions:
18
+
19
+ def convert_to_mathml_foo(kind, tex)
20
+ ...
21
+ end
22
+ =end
23
+
24
+ =begin maruku_doc
25
+ Extension: math
26
+ Attribute: html_png_engine
27
+ Scope: document, element
28
+ Output: html
29
+ Summary: Select the rendering engine for math.
30
+ Default: <?mrk Globals[:html_math_engine].to_s ?>
31
+
32
+ Same thing as `html_math_engine`, only for PNG output.
33
+
34
+ def convert_to_png_foo(kind, tex)
35
+ # same thing
36
+ ...
37
+ end
38
+ {:lang=ruby}
39
+
40
+ =end
41
+
42
+ module MaRuKu; module Out; module HTML
43
+
44
+
45
+
46
+ # Creates an xml Mathml document of self.math
47
+ def render_mathml(kind, tex)
48
+ engine = get_setting(:html_math_engine)
49
+ method = "convert_to_mathml_#{engine}".to_sym
50
+ if self.respond_to? method
51
+ mathml = self.send(method, kind, tex)
52
+ return mathml || convert_to_mathml_none(kind, tex)
53
+ else
54
+ puts "A method called #{method} should be defined."
55
+ return convert_to_mathml_none(kind, tex)
56
+ end
57
+ end
58
+
59
+ # Creates an xml Mathml document of self.math
60
+ def render_png(kind, tex)
61
+ engine = get_setting(:html_png_engine)
62
+ method = "convert_to_png_#{engine}".to_sym
63
+ if self.respond_to? method
64
+ return self.send(method, kind, tex)
65
+ else
66
+ puts "A method called #{method} should be defined."
67
+ return nil
68
+ end
69
+ end
70
+
71
+ def pixels_per_ex
72
+ if not $pixels_per_ex
73
+ x = render_png(:inline, "x")
74
+ $pixels_per_ex = x.height # + x.depth
75
+ end
76
+ $pixels_per_ex
77
+ end
78
+
79
+ def adjust_png(png, use_depth)
80
+ src = png.src
81
+
82
+ height_in_px = png.height
83
+ depth_in_px = png.depth
84
+ height_in_ex = height_in_px / pixels_per_ex
85
+ depth_in_ex = depth_in_px / pixels_per_ex
86
+ total_height_in_ex = height_in_ex + depth_in_ex
87
+ style = ""
88
+ style += "vertical-align: -#{depth_in_ex}ex;" if use_depth
89
+ style += "height: #{total_height_in_ex}ex;"
90
+ img = Element.new 'img'
91
+ img.attributes['src'] = src
92
+ img.attributes['style'] = style
93
+ img.attributes['alt'] = "$#{self.math.strip}$"
94
+ img
95
+ end
96
+
97
+ def to_html_inline_math
98
+ mathml = get_setting(:html_math_output_mathml) && render_mathml(:inline, self.math)
99
+ png = get_setting(:html_math_output_png) && render_png(:inline, self.math)
100
+
101
+ span = create_html_element 'span'
102
+ add_class_to(span, 'maruku-inline')
103
+
104
+ if mathml
105
+ add_class_to(mathml, 'maruku-mathml')
106
+ return mathml
107
+ end
108
+
109
+ if png
110
+ img = adjust_png(png, use_depth=true)
111
+ add_class_to(img, 'maruku-png')
112
+ span << img
113
+ end
114
+ span
115
+
116
+ end
117
+
118
+ def to_html_equation
119
+ mathml = get_setting(:html_math_output_mathml) && render_mathml(:equation, self.math)
120
+ png = get_setting(:html_math_output_png) && render_png(:equation, self.math)
121
+
122
+ div = create_html_element 'div'
123
+ add_class_to(div, 'maruku-equation')
124
+ if mathml
125
+ add_class_to(mathml, 'maruku-mathml')
126
+ div << mathml
127
+ end
128
+
129
+ if png
130
+ img = adjust_png(png, use_depth=false)
131
+ add_class_to(img, 'maruku-png')
132
+ div << img
133
+ end
134
+
135
+ source_span = Element.new 'span'
136
+ add_class_to(source_span, 'maruku-eq-tex')
137
+ code = convert_to_mathml_none(:equation, self.math.strip)
138
+ code.attributes['style'] = 'display: none'
139
+ source_span << code
140
+ div << source_span
141
+
142
+ if self.label # then numerate
143
+ span = Element.new 'span'
144
+ span.attributes['class'] = 'maruku-eq-number'
145
+ num = self.num
146
+ span << Text.new("(#{num})")
147
+ div << span
148
+ div.attributes['id'] = "eq:#{self.label}"
149
+ end
150
+ div
151
+ end
152
+
153
+ def to_html_eqref
154
+ if eq = self.doc.eqid2eq[self.eqid]
155
+ num = eq.num
156
+ a = Element.new 'a'
157
+ a.attributes['class'] = 'maruku-eqref'
158
+ a.attributes['href'] = "#eq:#{self.eqid}"
159
+ a << Text.new("(#{num})")
160
+ a
161
+ else
162
+ maruku_error "Cannot find equation #{self.eqid.inspect}"
163
+ Text.new "(eq:#{self.eqid})"
164
+ end
165
+ end
166
+
167
+ def to_html_divref
168
+ ref= nil
169
+ self.doc.refid2ref.each_value { |h|
170
+ ref = h[self.refid] if h.has_key?(self.refid)
171
+ }
172
+ if ref
173
+ num = ref.num
174
+ a = Element.new 'a'
175
+ a.attributes['class'] = 'maruku-ref'
176
+ a.attributes['href'] = "#" + self.refid
177
+ a << Text.new(num.to_s)
178
+ a
179
+ else
180
+ maruku_error "Cannot find div #{self.refid.inspect}"
181
+ Text.new "\\ref{#{self.refid}}"
182
+ end
183
+ end
184
+
185
+ end end end
186
+
187
+
@@ -0,0 +1,26 @@
1
+ require 'maruku/ext/math/latex_fix'
2
+
3
+ module MaRuKu; module Out; module Latex
4
+
5
+ def to_latex_inline_math
6
+ "$#{self.math.strip}$".fix_latex
7
+ end
8
+
9
+ def to_latex_equation
10
+ if self.label
11
+ l = "\\label{#{self.label}}"
12
+ "\\begin{equation}\n#{self.math.strip}\n#{l}\\end{equation}\n".fix_latex
13
+ else
14
+ "\\begin{displaymath}\n#{self.math.strip}\n\\end{displaymath}\n".fix_latex
15
+ end
16
+ end
17
+
18
+ def to_latex_eqref
19
+ "\\eqref{#{self.eqid}}"
20
+ end
21
+
22
+ def to_latex_divref
23
+ "\\ref{#{self.refid}}"
24
+ end
25
+
26
+ end end end
@@ -0,0 +1,260 @@
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
+
22
+
23
+
24
+ # A series of helper functions for creating elements: they hide the
25
+ # particular internal representation.
26
+ #
27
+ # Please, always use these instead of creating MDElement.
28
+ #
29
+
30
+ module MaRuKu
31
+ module Helpers
32
+
33
+ # if the first is a md_ial, it is used as such
34
+ def md_el(node_type, children=[], meta={}, al=nil)
35
+ if (e=children.first).kind_of?(MDElement) and
36
+ e.node_type == :ial then
37
+ if al
38
+ al += e.ial
39
+ else
40
+ al = e.ial
41
+ end
42
+ children.shift
43
+ end
44
+ e = MDElement.new(node_type, children, meta, al)
45
+ e.doc = @doc
46
+ return e
47
+ end
48
+
49
+ def md_header(level, children, al=nil)
50
+ md_el(:header, children, {:level => level}, al)
51
+ end
52
+
53
+ # Inline code
54
+ def md_code(code, al=nil)
55
+ md_el(:inline_code, [], {:raw_code => code}, al)
56
+ end
57
+
58
+ # Code block
59
+ def md_codeblock(source, al=nil)
60
+ md_el(:code, [], {:raw_code => source}, al)
61
+ end
62
+
63
+ def md_quote(children, al=nil)
64
+ md_el(:quote, children, {}, al)
65
+ end
66
+
67
+ def md_li(children, want_my_par, al=nil)
68
+ md_el(:li, children, {:want_my_paragraph=>want_my_par}, al)
69
+ end
70
+
71
+ def md_footnote(footnote_id, children, al=nil)
72
+ md_el(:footnote, children, {:footnote_id=>footnote_id}, al)
73
+ end
74
+
75
+ def md_abbr_def(abbr, text, al=nil)
76
+ md_el(:abbr_def, [], {:abbr=>abbr, :text=>text}, al)
77
+ end
78
+
79
+ def md_abbr(abbr, title)
80
+ md_el(:abbr, [abbr], {:title=>title})
81
+ end
82
+
83
+ def md_html(raw_html, al=nil)
84
+ e = md_el(:raw_html, [], {:raw_html=>raw_html})
85
+ begin
86
+ # remove newlines and whitespace at begin
87
+ # end end of string, or else REXML gets confused
88
+ raw_html = raw_html.gsub(/\A\s*</,'<').
89
+ gsub(/>[\s\n]*\Z/,'>')
90
+
91
+ raw_html = "<marukuwrap>#{raw_html}</marukuwrap>"
92
+ e.instance_variable_set :@parsed_html,
93
+ REXML::Document.new(raw_html)
94
+ rescue REXML::ParseException => ex
95
+ e.instance_variable_set :@parsed_html, nil
96
+ maruku_recover "REXML cannot parse this block of HTML/XML:\n"+
97
+ add_tabs(raw_html,1,'|') + "\n"+ex.inspect
98
+ # " #{raw_html.inspect}\n\n"+ex.inspect
99
+ end
100
+ e
101
+ end
102
+
103
+ def md_link(children, ref_id, al=nil)
104
+ md_el(:link, children, {:ref_id=>ref_id.downcase}, al)
105
+ end
106
+
107
+ def md_im_link(children, url, title=nil, al=nil)
108
+ md_el(:im_link, children, {:url=>url,:title=>title}, al)
109
+ end
110
+
111
+ def md_image(children, ref_id, al=nil)
112
+ md_el(:image, children, {:ref_id=>ref_id}, al)
113
+ end
114
+
115
+ def md_im_image(children, url, title=nil, al=nil)
116
+ md_el(:im_image, children, {:url=>url,:title=>title},al)
117
+ end
118
+
119
+ def md_em(children, al=nil)
120
+ md_el(:emphasis, [children].flatten, {}, al)
121
+ end
122
+
123
+ def md_br()
124
+ md_el(:linebreak, [], {}, nil)
125
+ end
126
+
127
+ def md_hrule()
128
+ md_el(:hrule, [], {}, nil)
129
+ end
130
+
131
+ def md_strong(children, al=nil)
132
+ md_el(:strong, [children].flatten, {}, al)
133
+ end
134
+
135
+ def md_emstrong(children, al=nil)
136
+ md_strong(md_em(children), al)
137
+ end
138
+
139
+ # <http://www.example.com/>
140
+ def md_url(url, al=nil)
141
+ md_el(:immediate_link, [], {:url=>url}, al)
142
+ end
143
+
144
+ # <andrea@rubyforge.org>
145
+ # <mailto:andrea@rubyforge.org>
146
+ def md_email(email, al=nil)
147
+ md_el(:email_address, [], {:email=>email}, al)
148
+ end
149
+
150
+ def md_entity(entity_name, al=nil)
151
+ md_el(:entity, [], {:entity_name=>entity_name}, al)
152
+ end
153
+
154
+ # Markdown extra
155
+ def md_foot_ref(ref_id, al=nil)
156
+ md_el(:footnote_reference, [], {:footnote_id=>ref_id}, al)
157
+ end
158
+
159
+ def md_par(children, al=nil)
160
+ md_el(:paragraph, children, meta={}, al)
161
+ end
162
+
163
+ # [1]: http://url [properties]
164
+ def md_ref_def(ref_id, url, title=nil, meta={}, al=nil)
165
+ meta[:url] = url
166
+ meta[:ref_id] = ref_id
167
+ meta[:title] = title if title
168
+ md_el(:ref_definition, [], meta, al)
169
+ end
170
+
171
+ # inline attribute list
172
+ def md_ial(al)
173
+ al = Maruku::AttributeList.new(al) if
174
+ not al.kind_of?Maruku::AttributeList
175
+ md_el(:ial, [], {:ial=>al})
176
+ end
177
+
178
+ # Attribute list definition
179
+ def md_ald(id, al)
180
+ md_el(:ald, [], {:ald_id=>id,:ald=>al})
181
+ end
182
+
183
+ # Server directive <?target code... ?>
184
+ def md_xml_instr(target, code)
185
+ md_el(:xml_instr, [], {:target=>target, :code=>code})
186
+ end
187
+
188
+ end
189
+ end
190
+
191
+ module MaRuKu
192
+
193
+ class MDElement
194
+ # outputs abbreviated form (this should be eval()uable to get the document)
195
+ def inspect2
196
+ s =
197
+ case @node_type
198
+ when :paragraph
199
+ "md_par(%s)" % children_inspect
200
+ when :footnote_reference
201
+ "md_foot_ref(%s)" % self.footnote_id.inspect
202
+ when :entity
203
+ "md_entity(%s)" % self.entity_name.inspect
204
+ when :email_address
205
+ "md_email(%s)" % self.email.inspect
206
+ when :inline_code
207
+ "md_code(%s)" % self.raw_code.inspect
208
+ when :raw_html
209
+ "md_html(%s)" % self.raw_html.inspect
210
+ when :emphasis
211
+ "md_em(%s)" % children_inspect
212
+ when :strong
213
+ "md_strong(%s)" % children_inspect
214
+ when :immediate_link
215
+ "md_url(%s)" % self.url.inspect
216
+ when :image
217
+ "md_image(%s, %s)" % [
218
+ children_inspect,
219
+ self.ref_id.inspect]
220
+ when :im_image
221
+ "md_im_image(%s, %s, %s)" % [
222
+ children_inspect,
223
+ self.url.inspect,
224
+ self.title.inspect]
225
+ when :link
226
+ "md_link(%s,%s)" % [
227
+ children_inspect, self.ref_id.inspect]
228
+ when :im_link
229
+ "md_im_link(%s, %s, %s)" % [
230
+ children_inspect,
231
+ self.url.inspect,
232
+ self.title.inspect,
233
+ ]
234
+ when :ref_definition
235
+ "md_ref_def(%s, %s, %s)" % [
236
+ self.ref_id.inspect,
237
+ self.url.inspect,
238
+ self.title.inspect
239
+ ]
240
+ when :ial
241
+ "md_ial(%s)" % self.ial.inspect
242
+ else
243
+ return nil
244
+ end
245
+ if @al and not @al.empty? then
246
+ s = s.chop + ", #{@al.inspect})"
247
+ end
248
+ s
249
+ end
250
+
251
+ end
252
+
253
+ end
254
+
255
+
256
+
257
+
258
+
259
+
260
+