remi-maruku 0.5.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (183) hide show
  1. data/Rakefile +73 -0
  2. data/bin/marudown +29 -0
  3. data/bin/maruku +176 -0
  4. data/bin/marutest +338 -0
  5. data/bin/marutex +31 -0
  6. data/docs/changelog.md +334 -0
  7. data/docs/div_syntax.md +36 -0
  8. data/docs/entity_test.md +23 -0
  9. data/docs/markdown_syntax.md +899 -0
  10. data/docs/maruku.md +346 -0
  11. data/docs/math.md +194 -0
  12. data/docs/other_stuff.md +51 -0
  13. data/docs/proposal.md +309 -0
  14. data/lib/maruku.rb +141 -0
  15. data/lib/maruku/attributes.rb +227 -0
  16. data/lib/maruku/defaults.rb +70 -0
  17. data/lib/maruku/errors_management.rb +92 -0
  18. data/lib/maruku/ext/div.rb +100 -0
  19. data/lib/maruku/ext/math.rb +41 -0
  20. data/lib/maruku/ext/math/elements.rb +27 -0
  21. data/lib/maruku/ext/math/latex_fix.rb +11 -0
  22. data/lib/maruku/ext/math/mathml_engines/blahtex.rb +104 -0
  23. data/lib/maruku/ext/math/mathml_engines/itex2mml.rb +29 -0
  24. data/lib/maruku/ext/math/mathml_engines/none.rb +20 -0
  25. data/lib/maruku/ext/math/mathml_engines/ritex.rb +24 -0
  26. data/lib/maruku/ext/math/parsing.rb +105 -0
  27. data/lib/maruku/ext/math/to_html.rb +170 -0
  28. data/lib/maruku/ext/math/to_latex.rb +22 -0
  29. data/lib/maruku/helpers.rb +260 -0
  30. data/lib/maruku/input/charsource.rb +326 -0
  31. data/lib/maruku/input/extensions.rb +69 -0
  32. data/lib/maruku/input/html_helper.rb +189 -0
  33. data/lib/maruku/input/linesource.rb +111 -0
  34. data/lib/maruku/input/parse_block.rb +613 -0
  35. data/lib/maruku/input/parse_doc.rb +227 -0
  36. data/lib/maruku/input/parse_span_better.rb +732 -0
  37. data/lib/maruku/input/rubypants.rb +225 -0
  38. data/lib/maruku/input/type_detection.rb +144 -0
  39. data/lib/maruku/input_textile2/t2_parser.rb +163 -0
  40. data/lib/maruku/maruku.rb +33 -0
  41. data/lib/maruku/output/s5/fancy.rb +756 -0
  42. data/lib/maruku/output/s5/to_s5.rb +125 -0
  43. data/lib/maruku/output/to_html.rb +971 -0
  44. data/lib/maruku/output/to_latex.rb +563 -0
  45. data/lib/maruku/output/to_latex_entities.rb +367 -0
  46. data/lib/maruku/output/to_latex_strings.rb +64 -0
  47. data/lib/maruku/output/to_markdown.rb +164 -0
  48. data/lib/maruku/output/to_s.rb +53 -0
  49. data/lib/maruku/string_utils.rb +191 -0
  50. data/lib/maruku/structures.rb +165 -0
  51. data/lib/maruku/structures_inspect.rb +87 -0
  52. data/lib/maruku/structures_iterators.rb +61 -0
  53. data/lib/maruku/tests/benchmark.rb +82 -0
  54. data/lib/maruku/tests/new_parser.rb +370 -0
  55. data/lib/maruku/tests/tests.rb +136 -0
  56. data/lib/maruku/textile2.rb +1 -0
  57. data/lib/maruku/toc.rb +199 -0
  58. data/lib/maruku/usage/example1.rb +33 -0
  59. data/lib/maruku/version.rb +40 -0
  60. data/tests/bugs/code_in_links.md +16 -0
  61. data/tests/bugs/complex_escaping.md +4 -0
  62. data/tests/math/syntax.md +46 -0
  63. data/tests/math_usage/document.md +13 -0
  64. data/tests/others/abbreviations.md +11 -0
  65. data/tests/others/blank.md +4 -0
  66. data/tests/others/code.md +5 -0
  67. data/tests/others/code2.md +8 -0
  68. data/tests/others/code3.md +16 -0
  69. data/tests/others/email.md +4 -0
  70. data/tests/others/entities.md +19 -0
  71. data/tests/others/escaping.md +16 -0
  72. data/tests/others/extra_dl.md +101 -0
  73. data/tests/others/extra_header_id.md +13 -0
  74. data/tests/others/extra_table1.md +40 -0
  75. data/tests/others/footnotes.md +17 -0
  76. data/tests/others/headers.md +10 -0
  77. data/tests/others/hrule.md +10 -0
  78. data/tests/others/images.md +20 -0
  79. data/tests/others/inline_html.md +42 -0
  80. data/tests/others/links.md +38 -0
  81. data/tests/others/list1.md +4 -0
  82. data/tests/others/list2.md +5 -0
  83. data/tests/others/list3.md +8 -0
  84. data/tests/others/lists.md +32 -0
  85. data/tests/others/lists_after_paragraph.md +44 -0
  86. data/tests/others/lists_ol.md +39 -0
  87. data/tests/others/misc_sw.md +105 -0
  88. data/tests/others/one.md +1 -0
  89. data/tests/others/paragraphs.md +13 -0
  90. data/tests/others/sss06.md +352 -0
  91. data/tests/others/test.md +4 -0
  92. data/tests/s5/s5profiling.md +48 -0
  93. data/tests/unittest/abbreviations.md +72 -0
  94. data/tests/unittest/alt.md +30 -0
  95. data/tests/unittest/attributes/att2.md +34 -0
  96. data/tests/unittest/attributes/att3.md +45 -0
  97. data/tests/unittest/attributes/attributes.md +82 -0
  98. data/tests/unittest/attributes/circular.md +43 -0
  99. data/tests/unittest/attributes/default.md +38 -0
  100. data/tests/unittest/blank.md +39 -0
  101. data/tests/unittest/blanks_in_code.md +106 -0
  102. data/tests/unittest/bug_def.md +29 -0
  103. data/tests/unittest/bug_table.md +67 -0
  104. data/tests/unittest/code.md +53 -0
  105. data/tests/unittest/code2.md +46 -0
  106. data/tests/unittest/code3.md +102 -0
  107. data/tests/unittest/data_loss.md +42 -0
  108. data/tests/unittest/divs/div1.md +204 -0
  109. data/tests/unittest/divs/div2.md +34 -0
  110. data/tests/unittest/divs/div3_nest.md +62 -0
  111. data/tests/unittest/easy.md +28 -0
  112. data/tests/unittest/email.md +33 -0
  113. data/tests/unittest/encoding/iso-8859-1.md +38 -0
  114. data/tests/unittest/encoding/utf-8.md +33 -0
  115. data/tests/unittest/entities.md +124 -0
  116. data/tests/unittest/escaping.md +89 -0
  117. data/tests/unittest/extra_dl.md +72 -0
  118. data/tests/unittest/extra_header_id.md +86 -0
  119. data/tests/unittest/extra_table1.md +55 -0
  120. data/tests/unittest/footnotes.md +126 -0
  121. data/tests/unittest/headers.md +54 -0
  122. data/tests/unittest/hex_entities.md +50 -0
  123. data/tests/unittest/hrule.md +60 -0
  124. data/tests/unittest/html2.md +38 -0
  125. data/tests/unittest/html3.md +47 -0
  126. data/tests/unittest/html4.md +42 -0
  127. data/tests/unittest/html5.md +38 -0
  128. data/tests/unittest/ie.md +82 -0
  129. data/tests/unittest/images.md +114 -0
  130. data/tests/unittest/images2.md +46 -0
  131. data/tests/unittest/inline_html.md +260 -0
  132. data/tests/unittest/inline_html2.md +36 -0
  133. data/tests/unittest/links.md +197 -0
  134. data/tests/unittest/list1.md +66 -0
  135. data/tests/unittest/list2.md +76 -0
  136. data/tests/unittest/list3.md +88 -0
  137. data/tests/unittest/list4.md +116 -0
  138. data/tests/unittest/lists.md +241 -0
  139. data/tests/unittest/lists11.md +31 -0
  140. data/tests/unittest/lists6.md +54 -0
  141. data/tests/unittest/lists7.md +79 -0
  142. data/tests/unittest/lists7b.md +136 -0
  143. data/tests/unittest/lists8.md +83 -0
  144. data/tests/unittest/lists9.md +85 -0
  145. data/tests/unittest/lists_after_paragraph.md +268 -0
  146. data/tests/unittest/lists_ol.md +324 -0
  147. data/tests/unittest/loss.md +29 -0
  148. data/tests/unittest/math/equations.md +69 -0
  149. data/tests/unittest/math/inline.md +66 -0
  150. data/tests/unittest/math/math2.md +110 -0
  151. data/tests/unittest/math/notmath.md +40 -0
  152. data/tests/unittest/math/table.md +43 -0
  153. data/tests/unittest/math/table2.md +60 -0
  154. data/tests/unittest/misc_sw.md +615 -0
  155. data/tests/unittest/notyet/escape.md +36 -0
  156. data/tests/unittest/notyet/header_after_par.md +81 -0
  157. data/tests/unittest/notyet/ticks.md +31 -0
  158. data/tests/unittest/notyet/triggering.md +206 -0
  159. data/tests/unittest/olist.md +64 -0
  160. data/tests/unittest/one.md +28 -0
  161. data/tests/unittest/paragraph.md +29 -0
  162. data/tests/unittest/paragraph_rules/dont_merge_ref.md +57 -0
  163. data/tests/unittest/paragraph_rules/tab_is_blank.md +39 -0
  164. data/tests/unittest/paragraphs.md +66 -0
  165. data/tests/unittest/pending/amps.md +29 -0
  166. data/tests/unittest/pending/empty_cells.md +53 -0
  167. data/tests/unittest/pending/link.md +103 -0
  168. data/tests/unittest/recover/recover_links.md +28 -0
  169. data/tests/unittest/references/long_example.md +88 -0
  170. data/tests/unittest/references/spaces_and_numbers.md +28 -0
  171. data/tests/unittest/smartypants.md +171 -0
  172. data/tests/unittest/syntax_hl.md +80 -0
  173. data/tests/unittest/table_attributes.md +52 -0
  174. data/tests/unittest/test.md +32 -0
  175. data/tests/unittest/wrapping.md +88 -0
  176. data/tests/unittest/xml.md +54 -0
  177. data/tests/unittest/xml2.md +34 -0
  178. data/tests/unittest/xml3.md +44 -0
  179. data/tests/unittest/xml_instruction.md +72 -0
  180. data/tests/utf8-files/simple.md +1 -0
  181. data/unit_test_block.sh +5 -0
  182. data/unit_test_span.sh +2 -0
  183. metadata +243 -0
@@ -0,0 +1,225 @@
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
+ # NOTA BENE:
23
+ #
24
+ # The following algorithm is a rip-off of RubyPants written by
25
+ # Christian Neukirchen.
26
+ #
27
+ # RubyPants is a Ruby port of SmartyPants written by John Gruber.
28
+ #
29
+ # This file is distributed under the GPL, which I guess is compatible
30
+ # with the terms of the RubyPants license.
31
+ #
32
+ # -- Andrea Censi
33
+
34
+
35
+ # = RubyPants -- SmartyPants ported to Ruby
36
+ #
37
+ # Ported by Christian Neukirchen <mailto:chneukirchen@gmail.com>
38
+ # Copyright (C) 2004 Christian Neukirchen
39
+ #
40
+ # Incooporates ideas, comments and documentation by Chad Miller
41
+ # Copyright (C) 2004 Chad Miller
42
+ #
43
+ # Original SmartyPants by John Gruber
44
+ # Copyright (C) 2003 John Gruber
45
+ #
46
+
47
+ #
48
+ # = RubyPants -- SmartyPants ported to Ruby
49
+ #
50
+ #
51
+ # [snip]
52
+ #
53
+ # == Authors
54
+ #
55
+ # John Gruber did all of the hard work of writing this software in
56
+ # Perl for Movable Type and almost all of this useful documentation.
57
+ # Chad Miller ported it to Python to use with Pyblosxom.
58
+ #
59
+ # Christian Neukirchen provided the Ruby port, as a general-purpose
60
+ # library that follows the *Cloth API.
61
+ #
62
+ #
63
+ # == Copyright and License
64
+ #
65
+ # === SmartyPants license:
66
+ #
67
+ # Copyright (c) 2003 John Gruber
68
+ # (http://daringfireball.net)
69
+ # All rights reserved.
70
+ #
71
+ # Redistribution and use in source and binary forms, with or without
72
+ # modification, are permitted provided that the following conditions
73
+ # are met:
74
+ #
75
+ # * Redistributions of source code must retain the above copyright
76
+ # notice, this list of conditions and the following disclaimer.
77
+ #
78
+ # * Redistributions in binary form must reproduce the above copyright
79
+ # notice, this list of conditions and the following disclaimer in
80
+ # the documentation and/or other materials provided with the
81
+ # distribution.
82
+ #
83
+ # * Neither the name "SmartyPants" nor the names of its contributors
84
+ # may be used to endorse or promote products derived from this
85
+ # software without specific prior written permission.
86
+ #
87
+ # This software is provided by the copyright holders and contributors
88
+ # "as is" and any express or implied warranties, including, but not
89
+ # limited to, the implied warranties of merchantability and fitness
90
+ # for a particular purpose are disclaimed. In no event shall the
91
+ # copyright owner or contributors be liable for any direct, indirect,
92
+ # incidental, special, exemplary, or consequential damages (including,
93
+ # but not limited to, procurement of substitute goods or services;
94
+ # loss of use, data, or profits; or business interruption) however
95
+ # caused and on any theory of liability, whether in contract, strict
96
+ # liability, or tort (including negligence or otherwise) arising in
97
+ # any way out of the use of this software, even if advised of the
98
+ # possibility of such damage.
99
+ #
100
+ # === RubyPants license
101
+ #
102
+ # RubyPants is a derivative work of SmartyPants and smartypants.py.
103
+ #
104
+ # Redistribution and use in source and binary forms, with or without
105
+ # modification, are permitted provided that the following conditions
106
+ # are met:
107
+ #
108
+ # * Redistributions of source code must retain the above copyright
109
+ # notice, this list of conditions and the following disclaimer.
110
+ #
111
+ # * Redistributions in binary form must reproduce the above copyright
112
+ # notice, this list of conditions and the following disclaimer in
113
+ # the documentation and/or other materials provided with the
114
+ # distribution.
115
+ #
116
+ # This software is provided by the copyright holders and contributors
117
+ # "as is" and any express or implied warranties, including, but not
118
+ # limited to, the implied warranties of merchantability and fitness
119
+ # for a particular purpose are disclaimed. In no event shall the
120
+ # copyright owner or contributors be liable for any direct, indirect,
121
+ # incidental, special, exemplary, or consequential damages (including,
122
+ # but not limited to, procurement of substitute goods or services;
123
+ # loss of use, data, or profits; or business interruption) however
124
+ # caused and on any theory of liability, whether in contract, strict
125
+ # liability, or tort (including negligence or otherwise) arising in
126
+ # any way out of the use of this software, even if advised of the
127
+ # possibility of such damage.
128
+ #
129
+ #
130
+ # == Links
131
+ #
132
+ # John Gruber:: http://daringfireball.net
133
+ # SmartyPants:: http://daringfireball.net/projects/smartypants
134
+ #
135
+ # Chad Miller:: http://web.chad.org
136
+ #
137
+ # Christian Neukirchen:: http://kronavita.de/chris
138
+
139
+
140
+ module MaRuKu; module In; module Markdown; module SpanLevelParser
141
+ Punct_class = '[!"#\$\%\'()*+,\-.\/:;<=>?\@\[\\\\\]\^_`{|}~]'
142
+ Close_class = %![^\ \t\r\n\\[\{\(\-]!
143
+
144
+ Rules = [
145
+ [/---/, :mdash ],
146
+ [/--/, :ndash ],
147
+ ['...', :hellip ],
148
+ ['. . .', :hellip ],
149
+ ["``", :ldquo ],
150
+ ["''", :rdquo ],
151
+ [/<<\s/, [:laquo, :nbsp] ],
152
+ [/\s>>/, [:nbsp, :raquo] ],
153
+ [/<</, :laquo ],
154
+ [/>>/, :raquo ],
155
+
156
+ # def educate_single_backticks(str)
157
+ # ["`", :lsquo]
158
+ # ["'", :rsquo]
159
+
160
+ # Special case if the very first character is a quote followed by
161
+ # punctuation at a non-word-break. Close the quotes by brute
162
+ # force:
163
+ [/^'(?=#{Punct_class}\B)/, :rsquo],
164
+ [/^"(?=#{Punct_class}\B)/, :rdquo],
165
+ # Special case for double sets of quotes, e.g.:
166
+ # <p>He said, "'Quoted' words in a larger quote."</p>
167
+ [/"'(?=\w)/, [:ldquo, :lsquo] ],
168
+ [/'"(?=\w)/, [:lsquo, :ldquo] ],
169
+ # Special case for decade abbreviations (the '80s):
170
+ [/'(?=\d\ds)/, :rsquo ],
171
+ # Get most opening single quotes:
172
+ [/(\s)'(?=\w)/, [:one, :lsquo] ],
173
+ # Single closing quotes:
174
+ [/(#{Close_class})'/, [:one, :rsquo]],
175
+ [/'(\s|s\b|$)/, [:rsquo, :one]],
176
+ # Any remaining single quotes should be opening ones:
177
+ [/'/, :lsquo],
178
+ # Get most opening double quotes:
179
+ [/(\s)"(?=\w)/, [:one, :ldquo]],
180
+ # Double closing quotes:
181
+ [/(#{Close_class})"/, [:one, :rdquo]],
182
+ [/"(\s|s\b|$)/, [:rdquo, :one]],
183
+ # Any remaining quotes should be opening ones:
184
+ [/"/, :ldquo]
185
+ ].
186
+ map{|reg, subst| # People should do the thinking, machines should do the work.
187
+ reg = Regexp.new(Regexp.escape(reg)) if not reg.kind_of? Regexp
188
+ subst = [subst] if not subst.kind_of?Array
189
+ [reg, subst]}
190
+
191
+ # note: input will be destroyed
192
+ def apply_one_rule(reg, subst, input)
193
+ output = []
194
+ while first = input.shift
195
+ if first.kind_of?(String) && (m = reg.match(first))
196
+ output.push m. pre_match if m. pre_match.size > 0
197
+ input.unshift m.post_match if m.post_match.size > 0
198
+ subst.reverse.each do |x|
199
+ input.unshift( x == :one ? m[1] : md_entity(x.to_s) ) end
200
+ else
201
+ output.push first
202
+ end
203
+ end
204
+ return output
205
+ end
206
+
207
+ def educate(elements)
208
+ Rules.each do |reg, subst|
209
+ elements = apply_one_rule(reg, subst, elements)
210
+ end
211
+ # strips empty strings
212
+ elements.delete_if {|x| x.kind_of?(String) && x.size == 0}
213
+ final = []
214
+ # join consecutive strings
215
+ elements.each do |x|
216
+ if x.kind_of?(String) && final.last.kind_of?(String)
217
+ final.last << x
218
+ else
219
+ final << x
220
+ end
221
+ end
222
+ return final
223
+ end
224
+
225
+ end end end end
@@ -0,0 +1,144 @@
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
+ class String
22
+ include MaRuKu::Strings
23
+ def md_type()
24
+ @md_type ||= line_md_type(self)
25
+ end
26
+ end
27
+
28
+ class NilClass
29
+ def md_type() nil end
30
+
31
+ end
32
+
33
+ # This code does the classification of lines for block-level parsing.
34
+ module MaRuKu; module Strings
35
+
36
+ def line_md_type(l)
37
+ # The order of evaluation is important (:text is a catch-all)
38
+ return :text if l =~ /^[a-zA-Z]/
39
+ return :code if number_of_leading_spaces(l)>=4
40
+ return :empty if l =~ /^\s*$/
41
+ return :footnote_text if l =~ FootnoteText
42
+ return :ref_definition if l =~ LinkRegex or l=~ IncompleteLink
43
+ return :abbreviation if l =~ Abbreviation
44
+ return :definition if l =~ Definition
45
+ # I had a bug with emails and urls at the beginning of the
46
+ # line that were mistaken for raw_html
47
+ return :text if l=~ /^[ ]{0,3}#{EMailAddress}/
48
+ return :text if l=~ /^[ ]{0,3}<http:/
49
+ # raw html is like PHP Markdown Extra: at most three spaces before
50
+ return :xml_instr if l =~ %r{^\s*<\?}
51
+ return :raw_html if l =~ %r{^[ ]?[ ]?[ ]?</?\s*\w+}
52
+ return :raw_html if l =~ %r{^[ ]?[ ]?[ ]?<\!\-\-}
53
+ # Something is wrong with how we parse lists! :-(
54
+ #return :ulist if l =~ /^[ ]{0,3}([\*\-\+])\s+.*\w+/
55
+ #return :olist if l =~ /^[ ]{0,3}\d+\..*\w+/
56
+ return :ulist if l =~ /^[ ]{0,1}([\*\-\+])\s+.*\w+/
57
+ return :olist if l =~ /^[ ]{0,1}\d+\..*\w+/
58
+ return :header1 if l =~ /^(=)+/
59
+ return :header2 if l =~ /^([-\s])+$/
60
+ return :header3 if l =~ /^(#)+\s*\S+/
61
+ # at least three asterisks on a line, and only whitespace
62
+ return :hrule if l =~ /^(\s*\*\s*){3,1000}$/
63
+ return :hrule if l =~ /^(\s*-\s*){3,1000}$/ # or hyphens
64
+ return :hrule if l =~ /^(\s*_\s*){3,1000}$/ # or underscores
65
+ return :quote if l =~ /^>/
66
+ return :metadata if l =~ /^@/
67
+ # if @@new_meta_data?
68
+ return :ald if l =~ AttributeDefinitionList
69
+ return :ial if l =~ InlineAttributeList
70
+ # end
71
+ # return :equation_end if l =~ EquationEnd
72
+ return :text # else, it's just text
73
+ end
74
+
75
+
76
+ # $1 = id $2 = attribute list
77
+ AttributeDefinitionList = /^\s{0,3}\{([\w\d\s]+)\}:\s*(.*)\s*$/
78
+ #
79
+ InlineAttributeList = /^\s{0,3}\{([:#\.].*)\}\s*$/
80
+ # Example:
81
+ # ^:blah blah
82
+ # ^: blah blah
83
+ # ^ : blah blah
84
+ Definition = %r{
85
+ ^ # begin of line
86
+ [ ]{0,3} # up to 3 spaces
87
+ : # colon
88
+ \s* # whitespace
89
+ (\S.*) # the text = $1
90
+ $ # end of line
91
+ }x
92
+
93
+ # Example:
94
+ # *[HTML]: Hyper Text Markup Language
95
+ Abbreviation = %r{
96
+ ^ # begin of line
97
+ \* # one asterisk
98
+ \[ # opening bracket
99
+ ([^\]]+) # any non-closing bracket: id = $1
100
+ \] # closing bracket
101
+ : # colon
102
+ \s* # whitespace
103
+ (\S.*\S)* # definition=$2
104
+ \s* # strip this whitespace
105
+ $ # end of line
106
+ }x
107
+
108
+ FootnoteText = %r{
109
+ ^\s*\[(\^.+)\]: # id = $1 (including '^')
110
+ \s*(\S.*)?$ # text = $2 (not obb.)
111
+ }x
112
+
113
+ # This regex is taken from BlueCloth sources
114
+ # Link defs are in the form: ^[id]: \n? url "optional title"
115
+ LinkRegex = %r{
116
+ ^[ ]{0,3}\[([^\[\]]+)\]: # id = $1
117
+ [ ]*
118
+ <?(\S+)>? # url = $2
119
+ [ ]*
120
+ (?:# Titles are delimited by "quotes" or (parens).
121
+ ["(']
122
+ (.+?) # title = $3
123
+ [")'] # Matching ) or "
124
+ \s*(.+)? # stuff = $4
125
+ )? # title is optional
126
+ }x
127
+
128
+ IncompleteLink = %r{^[ ]{0,3}\[([^\[\]]+)\]:\s*$}
129
+
130
+ HeaderWithId = /^(.*)\{\#([\w_-]+)\}\s*$/
131
+
132
+ HeaderWithAttributes = /^(.*)\{(.*)\}\s*$/
133
+
134
+
135
+ # if contains a pipe, it could be a table header
136
+ MightBeTableHeader = %r{\|}
137
+ # -------------:
138
+ Sep = /\s*(\:)?\s*-+\s*(\:)?\s*/
139
+ # | -------------:| ------------------------------ |
140
+ TableSeparator = %r{^(\|?#{Sep}\|?)+\s*$}
141
+
142
+
143
+ EMailAddress = /<([^:]+@[^:]+)>/
144
+ end end
@@ -0,0 +1,163 @@
1
+ class String
2
+ Textile2_EmptyLine = /^\s*$/
3
+ Textile2_Signature = /^(\S+\.?)\.\s(.*)/
4
+
5
+ def t2_empty?
6
+ self =~ Textile2_EmptyLine
7
+ end
8
+
9
+ def t2_contains_signature?
10
+ self =~ Textile2_Signature
11
+ end
12
+
13
+ # sig, rest = t2_get_signature
14
+ def t2_get_signature
15
+ self =~ Textile2_Signature
16
+ return Textile2Signature.new($1), $2
17
+ end
18
+ end
19
+
20
+ class Textile2Signature
21
+ Reg = %r{
22
+ ^
23
+ # block name is 1
24
+ ([A-Za-z0-9]+)?
25
+ # style is 2
26
+ (
27
+ \{ # open bracket
28
+ ([^\}]+) # style spec is 3
29
+ \} # close bracket
30
+ )?
31
+ # language is 4
32
+ (\[(\w+)\])? # value is 5
33
+ # class and id
34
+ (?:
35
+ \( # open par
36
+ (\w+)? # optional class specification is 6
37
+ (?:\#(\w+))? # optional id is 7
38
+ \) # close par
39
+ )?
40
+ # alignment is 8
41
+ (\<|\>|\<\>|\=)?
42
+ # padding
43
+ (\(+)? # left is 9
44
+ (\)+)? # right is 10
45
+ # filters is 11
46
+ (\|
47
+ (?:(?:\w+)\|)+
48
+ )?
49
+ # optional final dot is 12
50
+ (\.)?
51
+ $
52
+ }x
53
+
54
+
55
+ def initialize(s)
56
+ if m = Reg.match(s)
57
+ self.block_name = m[1]
58
+ self.style = m[3]
59
+ self.lang = m[4]
60
+ self.css_class = m[6]
61
+ self.css_id = m[7]
62
+ self.text_align = {nil=>nil,'>'=>'right','<'=>'left',
63
+ '<>'=>'center','='=>'justified'}[m[8]]
64
+ self.num_left_pad = m[9] && m[9].size
65
+ self.num_right_pad = m[10] && m[10].size
66
+ self.filters = m[11] && m[11].split('|')
67
+ self.double_dot = m[12] && true
68
+ end
69
+ end
70
+
71
+
72
+ attr_accessor :block_name # or nil
73
+ attr_accessor :style # or nil
74
+ attr_accessor :lang # or nil
75
+ attr_accessor :css_class # or nil
76
+ attr_accessor :css_id # or nil
77
+ attr_accessor :text_align # {nil, 'left', 'right', 'center', 'justified'}
78
+ attr_accessor :num_left_pad # nil or 1..
79
+ attr_accessor :num_right_pad # nil or 1..
80
+ attr_accessor :filters # nil [], array of strings
81
+ attr_accessor :double_dot # nil or true
82
+
83
+
84
+ end
85
+
86
+ module MaRuKu
87
+
88
+ def self.textile2(source, params)
89
+ m = Maruku.new
90
+ m.t2_parse(source, params)
91
+ end
92
+
93
+
94
+ class MDDocument
95
+ def t2_parse(source, params)
96
+ src = LineSource.new(source)
97
+ output = BlockContext.new
98
+ t2_parse_blocks(src, output)
99
+ self.children = output.elements
100
+ end
101
+
102
+ Handling = Struct.new(:method, :parse_lines)
103
+ T2_Handling = {
104
+ nil => Handling.new(:t2_block_paragraph, true),
105
+ 'p' => Handling.new(:t2_block_paragraph, true)
106
+ }
107
+
108
+ # Input is a LineSource
109
+ def t2_parse_blocks(src, output)
110
+ while src.cur_line
111
+ l = src.shift_line
112
+
113
+ # ignore empty line
114
+ if l.t2_empty? then
115
+ src.shift_line
116
+ next
117
+ end
118
+
119
+ # TODO: lists
120
+ # TODO: xml
121
+ # TODO: `==`
122
+
123
+ signature, l =
124
+ if l.t2_contains_signature?
125
+ l.t2_get_signature
126
+ else
127
+ [Textile2Signature.new, l]
128
+ end
129
+
130
+ if handling = T2_Handling.has_key?(signature.block_name)
131
+ if self.responds_to? handling.method
132
+ # read as many non-empty lines that you can
133
+ lines = [l]
134
+ if handling.parse_lines
135
+ while not src.cur_line.t2_empty?
136
+ lines.push src.shift_line
137
+ end
138
+ end
139
+
140
+ self.send(handling.method, src, output, signature, lines)
141
+ else
142
+ maruku_error("We don't know about method #{handling.method.inspect}")
143
+ next
144
+ end
145
+ end
146
+
147
+
148
+ end
149
+ end
150
+
151
+ def t2_block_paragraph(src, output, signature, lines)
152
+ paragraph = lines.join("\n")
153
+ src2 = CharSource.new(paragraph, src)
154
+ # output =
155
+ end
156
+
157
+ def t2_parse_span(src, output)
158
+
159
+ end
160
+
161
+ end # MDDocument
162
+
163
+ end