WikiCreole 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/COPYING +340 -0
  2. data/Changelog +2 -0
  3. data/LICENSE +52 -0
  4. data/README +13 -0
  5. data/lib/wiki_creole.rb +1023 -0
  6. metadata +57 -0
data/COPYING ADDED
@@ -0,0 +1,340 @@
1
+ GNU GENERAL PUBLIC LICENSE
2
+ Version 2, June 1991
3
+
4
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
5
+ 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
6
+ Everyone is permitted to copy and distribute verbatim copies
7
+ of this license document, but changing it is not allowed.
8
+
9
+ Preamble
10
+
11
+ The licenses for most software are designed to take away your
12
+ freedom to share and change it. By contrast, the GNU General Public
13
+ License is intended to guarantee your freedom to share and change free
14
+ software--to make sure the software is free for all its users. This
15
+ General Public License applies to most of the Free Software
16
+ Foundation's software and to any other program whose authors commit to
17
+ using it. (Some other Free Software Foundation software is covered by
18
+ the GNU Library General Public License instead.) You can apply it to
19
+ your programs, too.
20
+
21
+ When we speak of free software, we are referring to freedom, not
22
+ price. Our General Public Licenses are designed to make sure that you
23
+ have the freedom to distribute copies of free software (and charge for
24
+ this service if you wish), that you receive source code or can get it
25
+ if you want it, that you can change the software or use pieces of it
26
+ in new free programs; and that you know you can do these things.
27
+
28
+ To protect your rights, we need to make restrictions that forbid
29
+ anyone to deny you these rights or to ask you to surrender the rights.
30
+ These restrictions translate to certain responsibilities for you if you
31
+ distribute copies of the software, or if you modify it.
32
+
33
+ For example, if you distribute copies of such a program, whether
34
+ gratis or for a fee, you must give the recipients all the rights that
35
+ you have. You must make sure that they, too, receive or can get the
36
+ source code. And you must show them these terms so they know their
37
+ rights.
38
+
39
+ We protect your rights with two steps: (1) copyright the software, and
40
+ (2) offer you this license which gives you legal permission to copy,
41
+ distribute and/or modify the software.
42
+
43
+ Also, for each author's protection and ours, we want to make certain
44
+ that everyone understands that there is no warranty for this free
45
+ software. If the software is modified by someone else and passed on, we
46
+ want its recipients to know that what they have is not the original, so
47
+ that any problems introduced by others will not reflect on the original
48
+ authors' reputations.
49
+
50
+ Finally, any free program is threatened constantly by software
51
+ patents. We wish to avoid the danger that redistributors of a free
52
+ program will individually obtain patent licenses, in effect making the
53
+ program proprietary. To prevent this, we have made it clear that any
54
+ patent must be licensed for everyone's free use or not licensed at all.
55
+
56
+ The precise terms and conditions for copying, distribution and
57
+ modification follow.
58
+
59
+ GNU GENERAL PUBLIC LICENSE
60
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61
+
62
+ 0. This License applies to any program or other work which contains
63
+ a notice placed by the copyright holder saying it may be distributed
64
+ under the terms of this General Public License. The "Program", below,
65
+ refers to any such program or work, and a "work based on the Program"
66
+ means either the Program or any derivative work under copyright law:
67
+ that is to say, a work containing the Program or a portion of it,
68
+ either verbatim or with modifications and/or translated into another
69
+ language. (Hereinafter, translation is included without limitation in
70
+ the term "modification".) Each licensee is addressed as "you".
71
+
72
+ Activities other than copying, distribution and modification are not
73
+ covered by this License; they are outside its scope. The act of
74
+ running the Program is not restricted, and the output from the Program
75
+ is covered only if its contents constitute a work based on the
76
+ Program (independent of having been made by running the Program).
77
+ Whether that is true depends on what the Program does.
78
+
79
+ 1. You may copy and distribute verbatim copies of the Program's
80
+ source code as you receive it, in any medium, provided that you
81
+ conspicuously and appropriately publish on each copy an appropriate
82
+ copyright notice and disclaimer of warranty; keep intact all the
83
+ notices that refer to this License and to the absence of any warranty;
84
+ and give any other recipients of the Program a copy of this License
85
+ along with the Program.
86
+
87
+ You may charge a fee for the physical act of transferring a copy, and
88
+ you may at your option offer warranty protection in exchange for a fee.
89
+
90
+ 2. You may modify your copy or copies of the Program or any portion
91
+ of it, thus forming a work based on the Program, and copy and
92
+ distribute such modifications or work under the terms of Section 1
93
+ above, provided that you also meet all of these conditions:
94
+
95
+ a) You must cause the modified files to carry prominent notices
96
+ stating that you changed the files and the date of any change.
97
+
98
+ b) You must cause any work that you distribute or publish, that in
99
+ whole or in part contains or is derived from the Program or any
100
+ part thereof, to be licensed as a whole at no charge to all third
101
+ parties under the terms of this License.
102
+
103
+ c) If the modified program normally reads commands interactively
104
+ when run, you must cause it, when started running for such
105
+ interactive use in the most ordinary way, to print or display an
106
+ announcement including an appropriate copyright notice and a
107
+ notice that there is no warranty (or else, saying that you provide
108
+ a warranty) and that users may redistribute the program under
109
+ these conditions, and telling the user how to view a copy of this
110
+ License. (Exception: if the Program itself is interactive but
111
+ does not normally print such an announcement, your work based on
112
+ the Program is not required to print an announcement.)
113
+
114
+ These requirements apply to the modified work as a whole. If
115
+ identifiable sections of that work are not derived from the Program,
116
+ and can be reasonably considered independent and separate works in
117
+ themselves, then this License, and its terms, do not apply to those
118
+ sections when you distribute them as separate works. But when you
119
+ distribute the same sections as part of a whole which is a work based
120
+ on the Program, the distribution of the whole must be on the terms of
121
+ this License, whose permissions for other licensees extend to the
122
+ entire whole, and thus to each and every part regardless of who wrote it.
123
+
124
+ Thus, it is not the intent of this section to claim rights or contest
125
+ your rights to work written entirely by you; rather, the intent is to
126
+ exercise the right to control the distribution of derivative or
127
+ collective works based on the Program.
128
+
129
+ In addition, mere aggregation of another work not based on the Program
130
+ with the Program (or with a work based on the Program) on a volume of
131
+ a storage or distribution medium does not bring the other work under
132
+ the scope of this License.
133
+
134
+ 3. You may copy and distribute the Program (or a work based on it,
135
+ under Section 2) in object code or executable form under the terms of
136
+ Sections 1 and 2 above provided that you also do one of the following:
137
+
138
+ a) Accompany it with the complete corresponding machine-readable
139
+ source code, which must be distributed under the terms of Sections
140
+ 1 and 2 above on a medium customarily used for software interchange; or,
141
+
142
+ b) Accompany it with a written offer, valid for at least three
143
+ years, to give any third party, for a charge no more than your
144
+ cost of physically performing source distribution, a complete
145
+ machine-readable copy of the corresponding source code, to be
146
+ distributed under the terms of Sections 1 and 2 above on a medium
147
+ customarily used for software interchange; or,
148
+
149
+ c) Accompany it with the information you received as to the offer
150
+ to distribute corresponding source code. (This alternative is
151
+ allowed only for noncommercial distribution and only if you
152
+ received the program in object code or executable form with such
153
+ an offer, in accord with Subsection b above.)
154
+
155
+ The source code for a work means the preferred form of the work for
156
+ making modifications to it. For an executable work, complete source
157
+ code means all the source code for all modules it contains, plus any
158
+ associated interface definition files, plus the scripts used to
159
+ control compilation and installation of the executable. However, as a
160
+ special exception, the source code distributed need not include
161
+ anything that is normally distributed (in either source or binary
162
+ form) with the major components (compiler, kernel, and so on) of the
163
+ operating system on which the executable runs, unless that component
164
+ itself accompanies the executable.
165
+
166
+ If distribution of executable or object code is made by offering
167
+ access to copy from a designated place, then offering equivalent
168
+ access to copy the source code from the same place counts as
169
+ distribution of the source code, even though third parties are not
170
+ compelled to copy the source along with the object code.
171
+
172
+ 4. You may not copy, modify, sublicense, or distribute the Program
173
+ except as expressly provided under this License. Any attempt
174
+ otherwise to copy, modify, sublicense or distribute the Program is
175
+ void, and will automatically terminate your rights under this License.
176
+ However, parties who have received copies, or rights, from you under
177
+ this License will not have their licenses terminated so long as such
178
+ parties remain in full compliance.
179
+
180
+ 5. You are not required to accept this License, since you have not
181
+ signed it. However, nothing else grants you permission to modify or
182
+ distribute the Program or its derivative works. These actions are
183
+ prohibited by law if you do not accept this License. Therefore, by
184
+ modifying or distributing the Program (or any work based on the
185
+ Program), you indicate your acceptance of this License to do so, and
186
+ all its terms and conditions for copying, distributing or modifying
187
+ the Program or works based on it.
188
+
189
+ 6. Each time you redistribute the Program (or any work based on the
190
+ Program), the recipient automatically receives a license from the
191
+ original licensor to copy, distribute or modify the Program subject to
192
+ these terms and conditions. You may not impose any further
193
+ restrictions on the recipients' exercise of the rights granted herein.
194
+ You are not responsible for enforcing compliance by third parties to
195
+ this License.
196
+
197
+ 7. If, as a consequence of a court judgment or allegation of patent
198
+ infringement or for any other reason (not limited to patent issues),
199
+ conditions are imposed on you (whether by court order, agreement or
200
+ otherwise) that contradict the conditions of this License, they do not
201
+ excuse you from the conditions of this License. If you cannot
202
+ distribute so as to satisfy simultaneously your obligations under this
203
+ License and any other pertinent obligations, then as a consequence you
204
+ may not distribute the Program at all. For example, if a patent
205
+ license would not permit royalty-free redistribution of the Program by
206
+ all those who receive copies directly or indirectly through you, then
207
+ the only way you could satisfy both it and this License would be to
208
+ refrain entirely from distribution of the Program.
209
+
210
+ If any portion of this section is held invalid or unenforceable under
211
+ any particular circumstance, the balance of the section is intended to
212
+ apply and the section as a whole is intended to apply in other
213
+ circumstances.
214
+
215
+ It is not the purpose of this section to induce you to infringe any
216
+ patents or other property right claims or to contest validity of any
217
+ such claims; this section has the sole purpose of protecting the
218
+ integrity of the free software distribution system, which is
219
+ implemented by public license practices. Many people have made
220
+ generous contributions to the wide range of software distributed
221
+ through that system in reliance on consistent application of that
222
+ system; it is up to the author/donor to decide if he or she is willing
223
+ to distribute software through any other system and a licensee cannot
224
+ impose that choice.
225
+
226
+ This section is intended to make thoroughly clear what is believed to
227
+ be a consequence of the rest of this License.
228
+
229
+ 8. If the distribution and/or use of the Program is restricted in
230
+ certain countries either by patents or by copyrighted interfaces, the
231
+ original copyright holder who places the Program under this License
232
+ may add an explicit geographical distribution limitation excluding
233
+ those countries, so that distribution is permitted only in or among
234
+ countries not thus excluded. In such case, this License incorporates
235
+ the limitation as if written in the body of this License.
236
+
237
+ 9. The Free Software Foundation may publish revised and/or new versions
238
+ of the General Public License from time to time. Such new versions will
239
+ be similar in spirit to the present version, but may differ in detail to
240
+ address new problems or concerns.
241
+
242
+ Each version is given a distinguishing version number. If the Program
243
+ specifies a version number of this License which applies to it and "any
244
+ later version", you have the option of following the terms and conditions
245
+ either of that version or of any later version published by the Free
246
+ Software Foundation. If the Program does not specify a version number of
247
+ this License, you may choose any version ever published by the Free Software
248
+ Foundation.
249
+
250
+ 10. If you wish to incorporate parts of the Program into other free
251
+ programs whose distribution conditions are different, write to the author
252
+ to ask for permission. For software which is copyrighted by the Free
253
+ Software Foundation, write to the Free Software Foundation; we sometimes
254
+ make exceptions for this. Our decision will be guided by the two goals
255
+ of preserving the free status of all derivatives of our free software and
256
+ of promoting the sharing and reuse of software generally.
257
+
258
+ NO WARRANTY
259
+
260
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261
+ FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262
+ OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263
+ PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264
+ OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266
+ TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267
+ PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268
+ REPAIR OR CORRECTION.
269
+
270
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272
+ REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273
+ INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274
+ OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275
+ TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276
+ YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277
+ PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278
+ POSSIBILITY OF SUCH DAMAGES.
279
+
280
+ END OF TERMS AND CONDITIONS
281
+
282
+ How to Apply These Terms to Your New Programs
283
+
284
+ If you develop a new program, and you want it to be of the greatest
285
+ possible use to the public, the best way to achieve this is to make it
286
+ free software which everyone can redistribute and change under these terms.
287
+
288
+ To do so, attach the following notices to the program. It is safest
289
+ to attach them to the start of each source file to most effectively
290
+ convey the exclusion of warranty; and each file should have at least
291
+ the "copyright" line and a pointer to where the full notice is found.
292
+
293
+ <one line to give the program's name and a brief idea of what it does.>
294
+ Copyright (C) <year> <name of author>
295
+
296
+ This program is free software; you can redistribute it and/or modify
297
+ it under the terms of the GNU General Public License as published by
298
+ the Free Software Foundation; either version 2 of the License, or
299
+ (at your option) any later version.
300
+
301
+ This program is distributed in the hope that it will be useful,
302
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
303
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304
+ GNU General Public License for more details.
305
+
306
+ You should have received a copy of the GNU General Public License
307
+ along with this program; if not, write to the Free Software
308
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
309
+
310
+
311
+ Also add information on how to contact you by electronic and paper mail.
312
+
313
+ If the program is interactive, make it output a short notice like this
314
+ when it starts in an interactive mode:
315
+
316
+ Gnomovision version 69, Copyright (C) year name of author
317
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
318
+ This is free software, and you are welcome to redistribute it
319
+ under certain conditions; type `show c' for details.
320
+
321
+ The hypothetical commands `show w' and `show c' should show the appropriate
322
+ parts of the General Public License. Of course, the commands you use may
323
+ be called something other than `show w' and `show c'; they could even be
324
+ mouse-clicks or menu items--whatever suits your program.
325
+
326
+ You should also get your employer (if you work as a programmer) or your
327
+ school, if any, to sign a "copyright disclaimer" for the program, if
328
+ necessary. Here is a sample; alter the names:
329
+
330
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
331
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
332
+
333
+ <signature of Ty Coon>, 1 April 1989
334
+ Ty Coon, President of Vice
335
+
336
+ This General Public License does not permit incorporating your program into
337
+ proprietary programs. If your program is a subroutine library, you may
338
+ consider it more useful to permit linking proprietary applications with the
339
+ library. If this is what you want to do, use the GNU Library General
340
+ Public License instead of this License.
data/Changelog ADDED
@@ -0,0 +1,2 @@
1
+ 2008-12-10 (0.1.0)
2
+ * Initial Rubyforge Version
data/LICENSE ADDED
@@ -0,0 +1,52 @@
1
+ WikiCreole is copyrighted free software by Gordon McCreight
2
+ <gordon@mccreight.com>. You can redistribute it and/or modify it under either
3
+ the terms of the GPL (see COPYING file), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) rename any non-standard executables so the names do not conflict
21
+ with standard executables, which must also be provided.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or executable
26
+ form, provided that you do at least ONE of the following:
27
+
28
+ a) distribute the executables and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c) give non-standard executables non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial).
42
+
43
+ 5. The scripts and library files supplied as input to or produced as
44
+ output from the software do not automatically fall under the
45
+ copyright of the software, but belong to whomever generated them,
46
+ and may be sold commercially, and may be aggregated with this
47
+ software.
48
+
49
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
50
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
51
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52
+ PURPOSE.
data/README ADDED
@@ -0,0 +1,13 @@
1
+ == WikiCreole
2
+ WikiCreole is a Creole-to-XHTML converter written in pure Ruby. It supports
3
+ Creole 1.0 as well as extensions. Check out the rdoc documentation by issuing
4
+ the following command:
5
+ ri WikiCreole
6
+
7
+ == Synopsis
8
+ require 'wiki_creole'
9
+ xhtml = WikiCreole.creole_parse(creole_markup_string)
10
+
11
+ == Contribute
12
+ http://github.com/gmccreight/wikicreole/tree/master
13
+ or send an email to creole.to.gordon@mccreight.com
@@ -0,0 +1,1023 @@
1
+ # WikiCreole implements the Wiki Creole markup language,
2
+ # version 1.0, as described at http://www.wikicreole.org. It
3
+ # reads Creole 1.0 markup and returns XHTML.
4
+ #
5
+ # Author:: Gordon McCreight (mailto:wikicreole.to.gordon@mccreight.com)
6
+ # Copyright:: Copyright (c) 2008 Gordon McCreight
7
+ # License:: Distributes under the same terms as Ruby (see the LICENCE file)
8
+ #
9
+ # == Synopsis
10
+ # Most likely you'll just want to do:
11
+ # require 'wiki_creole'
12
+ # xhtml = WikiCreole.creole_parse(wiki_creole_markup)
13
+ # If you want to override the default behaviors, make sure to look at the other
14
+ # public methods.
15
+ #
16
+ # == Official Markup
17
+ #
18
+ # Here is a summary of the official Creole 1.0 markup
19
+ # elements. See http://www.wikicreole.org for the full
20
+ # details.
21
+ #
22
+ # Headings:
23
+ # = heading 1 -> <h1>heading 1</h1>
24
+ # == heading 2 -> <h2>heading 2</h2>
25
+ # ...
26
+ # ====== heading 6 -> <h6>heading 6</h6>
27
+ #
28
+ # Various inline markup:
29
+ # ** bold ** -> <strong> bold </strong>
30
+ # // italics // -> <em> italics </em>
31
+ # **// both //** -> <strong><em> both </em></strong>
32
+ # [[ link ]] -> <a href="link">link</a>
33
+ # [[ link | text ]] -> <a href="link">text</a>
34
+ # http://cpan.org -> <a href="http://cpan.org">http://cpan.org</a>
35
+ # line \\ break -> line <br /> break
36
+ # {{img.jpg|alt}} -> <img src="img.jpg" alt="alt">
37
+ #
38
+ # Lists:
39
+ # * unordered list <ul><li>unordered list</li>
40
+ # * second item <li>second item</li>
41
+ # ## nested ordered -> <ol><li>nested ordered</li>
42
+ # *** uber-nested <ul><li>uber-nested</li></ul>
43
+ # * back to level 1 </ol><li>back to level 1</li></ul>
44
+ #
45
+ # Tables:
46
+ # |= h1 |= h2 -> <table><tr><th>h1</th><th>h2</th></tr>
47
+ # | c1 | c2 <tr><td>c1</td><td>c2</td></tr></table>
48
+ #
49
+ # Nowiki (Preformatted):
50
+ # {{{ <pre>
51
+ # ** not bold ** ** not bold **
52
+ # escaped HTML: -> escaped HTML:
53
+ # <i> test </i> &lt;i&gt; test &lt;/i&gt;
54
+ # }}} <pre>
55
+ #
56
+ # {{{ inline\\also }}} -> <tt>inline\\also</tt>
57
+ #
58
+ # Escape Character:
59
+ # ~** not bold ** -> ** not bold **
60
+ # tilde: ~~ -> tilde: ~
61
+ #
62
+ # Paragraphs are separated by other blocks and blank lines.
63
+ # Inline markup can usually be combined, overlapped, etc. List
64
+ # items and plugin text can span lines.
65
+ #
66
+ # == Extended Markup
67
+ #
68
+ # In addition to OFFICIAL MARKUP, Text::WikiCreole also supports
69
+ # the following markup:
70
+ #
71
+ # Plugins:
72
+ # << plugin >> -> whatever you want (see WikiCreole.creole_plugin)
73
+ # <<< plugin >>> -> whatever you want (see WikiCreole.creole_plugin)
74
+ # Triple-bracket syntax has priority, in order to allow you to embed
75
+ # double-brackets in plugins, such as to embed Perl code.
76
+ #
77
+ # Inline:
78
+ # ## monospace ## -> <tt> monospace </tt>
79
+ # ^^ superscript ^^ -> <sup> superscript </sup>
80
+ # ,, subscript ,, -> <sub> subscript </sub>
81
+ # __ underline __ -> <u> underline </u>
82
+ # (TM) -> &trade;
83
+ # (R) -> &reg;
84
+ # (C) -> &copy;
85
+ # ... -> &hellip;
86
+ # -- -> &ndash;
87
+ #
88
+ # Indented Paragraphs:
89
+ # :this -> <div style="margin-left:2em"><p>this
90
+ # is indented is indented</p>
91
+ # :: more indented <div style="margin-left:2em"><p> more
92
+ # indented</div></div>
93
+ #
94
+ # Definition Lists:
95
+ # ; Title -> <dl><dt>Title</dt>
96
+ # : item 1 : item 2 <dd>item 1</dd><dd>item 2</dd>
97
+ # ; Title 2 : item2a <dt>Title 2</dt><dd>item 2a</dd></dl>
98
+ #
99
+ # == Acknowledgements
100
+ # Most of this code is ported from Jason Burnett's excellent Perl-based
101
+ # converter which can be found here:
102
+ # http://search.cpan.org/~jburnett/Text-WikiCreole/
103
+ # He, in turn, acknowledges the Document::Parser perl module.
104
+ #
105
+ # Also, some of the tests are taken from Lars Christensen's implementation of
106
+ # the Creole parser. You can find his code at:
107
+ # http://github.com/larsch/creole/tree/master
108
+ #
109
+ # Other test come from the wikicreole website itself, here:
110
+ # http://www.wikicreole.org/
111
+
112
+ class WikiCreole
113
+
114
+ # Reads Creole 1.0 markup and return XHTML.
115
+ #
116
+ # xhtml = WikiCreole.creole_parse(wiki_creole_markup)
117
+ def self.creole_parse(s)
118
+ return "" if s.class.to_s != "String"
119
+ return "" if s.length < 1
120
+
121
+ init
122
+ return parse(s, :top)
123
+ end
124
+
125
+ # Creole 1.0 supports two plugin syntaxes: << plugin content >> and
126
+ # <<< plugin content >>>
127
+ #
128
+ # Write a function that receives the text between the <<>>
129
+ # delimiters (not including the delimiters) and
130
+ # returns the text to be displayed. For example, here is a
131
+ # simple plugin that converts plugin text to uppercase:
132
+ #
133
+ # uppercase = Proc.new {|s|
134
+ # s.upcase!
135
+ # s
136
+ # }
137
+ # WikiCreole.creole_plugin(uppercase)
138
+ #
139
+ # If you do not register a plugin function, plugin markup will be left
140
+ # as is, including the surrounding << >>.
141
+ def self.creole_plugin(func)
142
+ @plugin_function = func
143
+ end
144
+
145
+ # You may wish to customize [[ links ]], such as to prefix a hostname,
146
+ # port, etc.
147
+ #
148
+ # Write a function, similar to the plugin function, which receives the
149
+ # URL part of the link (with leading and trailing whitespace stripped)
150
+ # and returns the customized link. For example, to prepend
151
+ # http://my.domain/
152
+ # to pagename:
153
+ #
154
+ # mylink = Proc.new {|s|
155
+ # s = "http://my.domain/" + s
156
+ # s
157
+ # }
158
+ # WikiCreole.creole_link(mylink)
159
+ def self.creole_link(func)
160
+ @link_function = func
161
+ end
162
+
163
+ # Same purpose as creole_link, but for "bare" link markup. Bare links are
164
+ # the links which are in the text but not surrounded by brackets.
165
+ #
166
+ # mybarelink = Proc.new {|s|
167
+ # s = s + ".html"
168
+ # s
169
+ # }
170
+ # WikiCreole.creole_barelink(mybarelink)
171
+ def self.creole_barelink(func)
172
+ @barelink_function = func
173
+ end
174
+
175
+ # Same purpose as creole_link, but for image URLs.
176
+ #
177
+ # myimg = Proc.new {|s|
178
+ # s = "http://my.domain/" + s
179
+ # s
180
+ # }
181
+ # WikiCreole.creole_img(myimg)
182
+ def self.creole_img(func)
183
+ @img_function = func
184
+ end
185
+
186
+ # If you want complete control over links, rather than just modifying
187
+ # the URL, register your link markup function with WikiCreole.creole_link()
188
+ # as above and then call creole_customlinks(). Now your function will receive
189
+ # the entire link markup chunk, such as <tt>[[ some_wiki_page | page description ]]</tt>
190
+ # and must return HTML.
191
+ #
192
+ # This has no effect on "bare" link markup, such as
193
+ # http://cpan.org
194
+ def self.creole_customlinks
195
+ @@chunks_hash[:href][:open] = ""
196
+ @@chunks_hash[:href][:close] = ""
197
+ @@chunks_hash[:link][:open] = ""
198
+ @@chunks_hash[:link][:close] = ""
199
+ @@chunks_hash[:link].delete(:contains)
200
+ @@chunks_hash[:link][:filter] = Proc.new {|s|
201
+ if @link_function
202
+ s = @link_function.call(s)
203
+ end
204
+ s
205
+ }
206
+ end
207
+
208
+ # Same purpose as creole_customlinks, but for "bare" link markup.
209
+ def self.creole_custombarelinks
210
+ @@chunks_hash[:ilink][:open] = ""
211
+ @@chunks_hash[:ilink][:close] = ""
212
+ @@chunks_hash[:ilink][:filter] = Proc.new {|s|
213
+ if @barelink_function
214
+ s = @barelink_function.call(s)
215
+ end
216
+ s
217
+ }
218
+ end
219
+
220
+ # Similar to creole_customlinks, but for images.
221
+ def self.creole_customimgs
222
+ @@chunks_hash[:img][:open] = ""
223
+ @@chunks_hash[:img][:close] = ""
224
+ @@chunks_hash[:img].delete(:contains)
225
+ @@chunks_hash[:img][:filter] = Proc.new {|s|
226
+ if @img_function
227
+ s = @img_function.call(s)
228
+ end
229
+ s
230
+ }
231
+ end
232
+
233
+ # You may wish to customize the opening and/or closing tags
234
+ # for the various bits of Creole markup. For example, to
235
+ # assign a CSS class to list items:
236
+ # WikiCreole.creole_tag(:li, :open, "<li class=myclass>")
237
+ #
238
+ # Or to see all current tags:
239
+ # puts WikiCreole.creole_tag()
240
+ #
241
+ # The tags that may be of interest are:
242
+ #
243
+ # br dd dl
244
+ # dt em h1
245
+ # h2 h3 h4
246
+ # h5 h6 hr
247
+ # ilink img inowiki
248
+ # ip li link
249
+ # mono nowiki ol
250
+ # p strong sub
251
+ # sup table td
252
+ # th tr u
253
+ # ul
254
+ #
255
+ # Those should be self-explanatory, except for inowiki (inline nowiki),
256
+ # ilink (bare links, e.g.
257
+ # http://www.cpan.org
258
+ # and ip (indented paragraph).
259
+ def self.creole_tag(*args)
260
+
261
+ # I bet a good Ruby hacker would know a way around this little chunk...
262
+ tag = args.length > 0 ? args[0] : nil
263
+ type = args.length > 1 ? args[1] : nil
264
+ text = args.length > 2 ? args[2] : nil
265
+
266
+ if tag.nil?
267
+ tags = ""
268
+ for key in @@chunks_hash.keys.collect{|x| x.to_s}.sort
269
+ key = key.to_sym
270
+ o = @@chunks_hash[key][:open]
271
+ c = @@chunks_hash[key][:close]
272
+ next if o.nil? || !o.index(/</m)
273
+ o = o.gsub(/\n/m,"\\n")
274
+ c = c.gsub(/\n/m,"\\n") if c
275
+ c = "" if c.nil?
276
+ this_tag = "#{key}: open(#{o}) close(#{c})\n"
277
+ tags += this_tag
278
+ end
279
+ return tags
280
+ else
281
+ return if ! type
282
+ type = type.to_sym
283
+ return if type != :open && type != :close
284
+ return if !@@chunks_hash.has_key?(tag)
285
+ @@chunks_hash[tag][type] = text ? text : ""
286
+ end
287
+ end
288
+
289
+ private
290
+
291
+ # characters that may indicate inline wiki markup
292
+ @@specialchars = ['^', '\\', '*', '/', '_', ',', '{', '[',
293
+ '<', '~', '|', "\n", '#', ':', ';', '(', '-', '.']
294
+
295
+ # plain characters - auto-generated below (ascii printable minus @specialchars)
296
+ @@plainchars = []
297
+
298
+ # non-plain text inline widgets
299
+ @@inline = %w{strong em br esc img link ilink inowiki
300
+ sub sup mono u plug plug2 tm reg copy ndash ellipsis amp}
301
+
302
+ @@all_inline = [@@inline, 'plain', 'any'].flatten # including plain text
303
+
304
+ @@blocks = %w{h1 h2 h3 hr nowiki h4 h5 h6 ul ol table p ip dl plug plug2 blank}
305
+
306
+ # handy - used several times in %chunks
307
+ @@eol = '(?:\n|$)'; # end of line (or string)
308
+
309
+ @plugin_function = nil
310
+ @barelink_function = nil
311
+ @link_function = nil
312
+ @img_function = nil
313
+
314
+ @is_initialized = false
315
+
316
+ @@chunks_hash = {
317
+ :top => {
318
+ :contains => @@blocks,
319
+ },
320
+ :blank => {
321
+ :curpat => "(?= *#{@@eol})",
322
+ :fwpat => "(?=(?:^|\n) *#{@@eol})",
323
+ :stops => '(?=\S)',
324
+ :hint => ["\n"],
325
+ :filter => Proc.new { "" }, # whitespace into the bit bucket
326
+ :open => "", :close => "",
327
+ },
328
+ :p => {
329
+ :curpat => '(?=.)',
330
+ :stops => ['blank', 'ip', 'h', 'hr', 'nowiki', 'ul', 'ol', 'dl', 'table'],
331
+ :hint => @@plainchars,
332
+ :contains => @@all_inline,
333
+ :filter => Proc.new {|s| s.chomp },
334
+ :open => "<p>", :close => "</p>\n\n",
335
+ },
336
+ :ip => {
337
+ :curpat => '(?=:)',
338
+ :fwpat => '\n(?=:)',
339
+ :stops => ['blank', 'h', 'hr', 'nowiki', 'ul', 'ol', 'dl', 'table'],
340
+ :hint => [':'],
341
+ :contains => ['p', 'ip'],
342
+ :filter => Proc.new {|s|
343
+ s.sub!(/:/, '')
344
+ s.sub!(/\n:/m, "\n")
345
+ s
346
+ },
347
+ :open => "<div style=\"margin-left: 2em\">", :close => "</div>\n",
348
+ },
349
+ :dl => {
350
+ :curpat => '(?=;)',
351
+ :fwpat => '\n(?=;)',
352
+ :stops => ['blank', 'h', 'hr', 'nowiki', 'ul', 'ol', 'table'],
353
+ :hint => [';'],
354
+ :contains => ['dt', 'dd'],
355
+ :open => "<dl>\n", :close => "</dl>\n",
356
+ },
357
+ :dt => {
358
+ :curpat => '(?=;)',
359
+ :fwpat => '\n(?=;)',
360
+ :stops => '(?=:|\n)',
361
+ :hint => [';'],
362
+ :contains => @@all_inline,
363
+ :filter => Proc.new {|s|
364
+ s.sub!(/^;\s*/, '')
365
+ s
366
+ },
367
+ :open => " <dt>", :close => "</dt>\n",
368
+ },
369
+ :dd => {
370
+ :curpat => '(?=\n|:)',
371
+ :fwpat => '(?:\n|:)',
372
+ :stops => '(?=:)|\n(?=;)',
373
+ :hint => [':', "\n"],
374
+ :contains => @@all_inline,
375
+ :filter => Proc.new {|s|
376
+ s.sub!(/(?:\n|:)\s*/m, '')
377
+ s.sub!(/\s*$/m, '')
378
+ s
379
+ },
380
+ :open => " <dd>", :close => "</dd>\n",
381
+ },
382
+ :table => {
383
+ :curpat => '(?= *\|.)',
384
+ :fwpat => '\n(?= *\|.)',
385
+ :stops => '\n(?= *[^\|])',
386
+ :contains => ['tr'],
387
+ :hint => ['|', ' '],
388
+ :open => "<table>\n", :close => "</table>\n\n",
389
+ },
390
+ :tr => {
391
+ :curpat => '(?= *\|)',
392
+ :stops => '\n',
393
+ :contains => ['td', 'th'],
394
+ :hint => ['|', ' '],
395
+ :filter => Proc.new {|s|
396
+ s.sub!(/^ */, '')
397
+ s.sub!(/\| *$/, '')
398
+ s
399
+ },
400
+ :open => " <tr>\n", :close => " </tr>\n",
401
+ },
402
+ :td => {
403
+ :curpat => '(?=\|[^=])',
404
+ # this gnarly regex fixes ambiguous '|' for links/imgs/nowiki in tables
405
+ :stops => '[^~](?=\|(?!(?:[^\[]*\]\])|(?:[^\{]*\}\})))',
406
+ :contains => @@all_inline,
407
+ :hint => ['|'],
408
+ :filter => Proc.new {|s|
409
+ s.sub!(/^ *\| */, '')
410
+ s.sub!(/\s*$/m, '')
411
+ s
412
+ },
413
+ :open => " <td>", :close => "</td>\n",
414
+ },
415
+ :th => {
416
+ :curpat => '(?=\|=)',
417
+ # this gnarly regex fixes ambiguous '|' for links/imgs/nowiki in tables
418
+ :stops => '[^~](?=\|(?!(?:[^\[]*\]\])|(?:[^\{]*\}\})))',
419
+ :contains => @@all_inline,
420
+ :hint => ['|'],
421
+ :filter => Proc.new {|s|
422
+ s.sub!(/^ *\|= */, '')
423
+ s.sub!(/\s*$/m, '')
424
+ s
425
+ },
426
+ :open => " <th>", :close => "</th>\n",
427
+ },
428
+ :ul => {
429
+ :curpat => '(?=(?:`| *)\*[^\*])',
430
+ :fwpat => '(?=\n(?:`| *)\*[^\*])',
431
+ :stops => ['blank', 'ip', 'h', 'nowiki', 'li', 'table', 'hr', 'dl'],
432
+ :contains => ['ul', 'ol', 'li'],
433
+ :hint => ['*', ' '],
434
+ :filter => Proc.new {|s|
435
+ s = strip_list(s)
436
+ s
437
+ },
438
+ :open => "<ul>\n", :close => "</ul>\n",
439
+ },
440
+ :ol => {
441
+ :curpat => '(?=(?:`| *)\#[^\#])',
442
+ :fwpat => '(?=\n(?:`| *)\#[^\#])',
443
+ :stops => ['blank', 'ip', 'h', 'nowiki', 'li', 'table', 'hr', 'dl'],
444
+ :contains => ['ul', 'ol', 'li'],
445
+ :hint => ['#', ' '],
446
+ :filter => Proc.new {|s|
447
+ s = strip_list(s)
448
+ s
449
+ },
450
+ :open => "<ol>\n", :close => "</ol>\n",
451
+ },
452
+ :li => {
453
+ :curpat => '(?=`[^\*\#])',
454
+ :fwpat => '\n(?=`[^\*\#])',
455
+ :stops => '\n(?=`)',
456
+ :hint => ['`'],
457
+ :filter => Proc.new {|s|
458
+ s.sub!(/` */, '')
459
+ s.chomp!
460
+ s
461
+ },
462
+ :contains => @@all_inline,
463
+ :open => " <li>", :close => "</li>\n",
464
+ },
465
+ :nowiki => {
466
+ :curpat => '(?=\{\{\{ *\n)',
467
+ :fwpat => '\n(?=\{\{\{ *\n)',
468
+ :stops => "\n\\}\\}\\} *#{@@eol}",
469
+ :hint => ['{'],
470
+ :filter => Proc.new {|s|
471
+ s[0,3] = ''
472
+ s.sub!(/\}{3}\s*$/, '')
473
+ s.gsub!(/&/, '&amp;')
474
+ s.gsub!(/</, '&lt;')
475
+ s.gsub!(/>/, '&gt;')
476
+ s
477
+ },
478
+ :open => "<pre>", :close => "</pre>\n\n",
479
+ },
480
+ :hr => {
481
+ :curpat => "(?= *-{4,} *#{@@eol})",
482
+ :fwpat => "\n(?= *-{4,} *#{@@eol})",
483
+ :hint => ['-', ' '],
484
+ :stops => @@eol,
485
+ :open => "<hr />\n\n", :close => "",
486
+ :filter => Proc.new { "" } # ----- into the bit bucket
487
+ },
488
+ :h => { :curpat => '(?=(?:^|\n) *=)' }, # matches any heading
489
+ :h1 => {
490
+ :curpat => '(?= *=[^=])',
491
+ :hint => ['=', ' '],
492
+ :stops => '\n',
493
+ :contains => @@all_inline,
494
+ :open => "<h1>", :close => "</h1>\n\n",
495
+ :filter => Proc.new {|s|
496
+ s = strip_leading_and_trailing_eq_and_whitespace(s)
497
+ s
498
+ },
499
+ },
500
+ :h2 => {
501
+ :curpat => '(?= *={2}[^=])',
502
+ :hint => ['=', ' '],
503
+ :stops => '\n',
504
+ :contains => @@all_inline,
505
+ :open => "<h2>", :close => "</h2>\n\n",
506
+ :filter => Proc.new {|s|
507
+ s = strip_leading_and_trailing_eq_and_whitespace(s)
508
+ s
509
+ },
510
+ },
511
+ :h3 => {
512
+ :curpat => '(?= *={3}[^=])',
513
+ :hint => ['=', ' '],
514
+ :stops => '\n',
515
+ :contains => @@all_inline,
516
+ :open => "<h3>", :close => "</h3>\n\n",
517
+ :filter => Proc.new {|s|
518
+ s = strip_leading_and_trailing_eq_and_whitespace(s)
519
+ s
520
+ },
521
+ },
522
+ :h4 => {
523
+ :curpat => '(?= *={4}[^=])',
524
+ :hint => ['=', ' '],
525
+ :stops => '\n',
526
+ :contains => @@all_inline,
527
+ :open => "<h4>", :close => "</h4>\n\n",
528
+ :filter => Proc.new {|s|
529
+ s = strip_leading_and_trailing_eq_and_whitespace(s)
530
+ s
531
+ },
532
+ },
533
+ :h5 => {
534
+ :curpat => '(?= *={5}[^=])',
535
+ :hint => ['=', ' '],
536
+ :stops => '\n',
537
+ :contains => @@all_inline,
538
+ :open => "<h5>", :close => "</h5>\n\n",
539
+ :filter => Proc.new {|s|
540
+ s = strip_leading_and_trailing_eq_and_whitespace(s)
541
+ s
542
+ },
543
+ },
544
+ :h6 => {
545
+ :curpat => '(?= *={6,})',
546
+ :hint => ['=', ' '],
547
+ :stops => '\n',
548
+ :contains => @@all_inline,
549
+ :open => "<h6>", :close => "</h6>\n\n",
550
+ :filter => Proc.new {|s|
551
+ s = strip_leading_and_trailing_eq_and_whitespace(s)
552
+ s
553
+ },
554
+ },
555
+ :plain => {
556
+ :curpat => '(?=[^\*\/_\,\^\\\\{\[\<\|])',
557
+ :stops => @@inline,
558
+ :hint => @@plainchars,
559
+ :open => '', :close => ''
560
+ },
561
+ :any => { # catch-all
562
+ :curpat => '(?=.)',
563
+ :stops => @@inline,
564
+ :open => '', :close => ''
565
+ },
566
+ :br => {
567
+ :curpat => '(?=\\\\\\\\)',
568
+ :stops => '\\\\\\\\',
569
+ :hint => ['\\'],
570
+ :filter => Proc.new { "" },
571
+ :open => '<br />', :close => '',
572
+ },
573
+ :esc => {
574
+ :curpat => '(?=~[\S])',
575
+ :stops => '~.',
576
+ :hint => ['~'],
577
+ :filter => Proc.new {|s|
578
+ s.sub!(/^./m, '')
579
+ s
580
+ },
581
+ :open => '', :close => '',
582
+ },
583
+ :inowiki => {
584
+ :curpat => '(?=\{{3}.*?\}*\}{3})',
585
+ :stops => '.*?\}*\}{3}',
586
+ :hint => ['{'],
587
+ :filter => Proc.new {|s|
588
+ s[0,3] = ''
589
+ s.sub!(/\}{3}\s*$/, '')
590
+ s.gsub!(/&/, '&amp;')
591
+ s.gsub!(/</, '&lt;')
592
+ s.gsub!(/>/, '&gt;')
593
+ s
594
+ },
595
+ :open => "<tt>", :close => "</tt>",
596
+ },
597
+ :plug => {
598
+ :curpat => '(?=\<{3}.*?\>*\>{3})',
599
+ :stops => '.*?\>*\>{3}',
600
+ :hint => ['<'],
601
+ :filter => Proc.new {|s|
602
+ s[0,3] = ''
603
+ s.sub!(/\>{3}$/, '')
604
+ if !@plugin_function.nil?
605
+ s = @plugin_function.call(s)
606
+ else
607
+ s = "<<<#{s}>>>"
608
+ end
609
+ s
610
+ },
611
+ :open => "", :close => "",
612
+ },
613
+ :plug2 => {
614
+ :curpat => '(?=\<{2}.*?\>*\>{2})',
615
+ :stops => '.*?\>*\>{2}',
616
+ :hint => ['<'],
617
+ :filter => Proc.new {|s|
618
+ s[0,2] = ''
619
+ s.sub!(/\>{2}$/, '')
620
+ if !@plugin_function.nil?
621
+ s = @plugin_function.call(s)
622
+ else
623
+ s = "<<#{s}>>"
624
+ end
625
+ s
626
+ },
627
+ :open => "", :close => "",
628
+ },
629
+ :ilink => {
630
+ :curpat => '(?=(?:https?|ftp):\/\/)',
631
+ # This following is the [:punct:] character class with the / and ? removed
632
+ # so that URLs like http://www.somesite.com/ will match the trailing
633
+ # slash. URLs with a trailing ? will also work. Trailing ? is sometimes
634
+ # used to ensure that browsers don't cache the page.
635
+ :stops => '(?=[!"#$%&\'()*+,-.:;<=>@\[\\]^_`{|}~]?(?:\s|$))',
636
+ :hint => ['h', 'f'],
637
+ :filter => Proc.new {|s|
638
+ s.sub!(/^\s*/, '')
639
+ s.sub!(/\s*$/, '')
640
+ if !@barelink_function.nil?
641
+ s = @barelink_function.call(s)
642
+ end
643
+ s = "href=\"#{s}\">#{s}"
644
+ s
645
+ },
646
+ :open => "<a ", :close=> "</a>",
647
+ },
648
+ :link => {
649
+ :curpat => '(?=\[\[[^\n]+?\]\])',
650
+ :stops => '\]\]',
651
+ :hint => ['['],
652
+ :contains => ['href', 'atext'],
653
+ :filter => Proc.new {|s|
654
+ s[0,2] = ''
655
+ s[-2,2] = ''
656
+ s += "|#{s}" if ! s.index(/\|/) # text = url unless given
657
+ s
658
+ },
659
+ :open => "<a ", :close => "</a>",
660
+ },
661
+ :href => {
662
+ :curpat => '(?=[^\|])',
663
+ :stops => '(?=\|)',
664
+ :filter => Proc.new {|s|
665
+ s.sub!(/^\s*/, '')
666
+ s.sub!(/\s*$/, '')
667
+ if !@link_function.nil?
668
+ s = @link_function.call(s)
669
+ end
670
+ s
671
+ },
672
+ :open => 'href="', :close => '">',
673
+ },
674
+ :atext => {
675
+ :curpat => '(?=\|)',
676
+ :stops => '\n',
677
+ :hint => ['|'],
678
+ :contains => @@all_inline,
679
+ :filter => Proc.new {|s|
680
+ s.sub!(/^\|\s*/, '')
681
+ s.sub!(/\s*$/, '')
682
+ s
683
+ },
684
+ :open => '', :close => '',
685
+ },
686
+ :img => {
687
+ :curpat => '(?=\{\{[^\{][^\n]*?\}\})',
688
+ :stops => '\}\}',
689
+ :hint => ['{'],
690
+ :contains => ['imgsrc', 'imgalt'],
691
+ :filter => Proc.new {|s|
692
+ s[0,2] = ''
693
+ s.sub!(/\}\}$/, '')
694
+ s
695
+ },
696
+ :open => "<img ", :close => " />",
697
+ },
698
+ :imgalt => {
699
+ :curpat => '(?=\|)',
700
+ :stops => '\n',
701
+ :hint => ['|'],
702
+ :filter => Proc.new {|s|
703
+ s.sub!(/^\|\s*/, '')
704
+ s.sub!(/\s*$/, '')
705
+ s
706
+ },
707
+ :open => ' alt="', :close => '"',
708
+ },
709
+ :imgsrc => {
710
+ :curpat => '(?=[^\|])',
711
+ :stops => '(?=\|)',
712
+ :filter => Proc.new {|s|
713
+ s.sub!(/^\|\s*/, '')
714
+ s.sub!(/\s*$/, '')
715
+ if !@img_function.nil?
716
+ s = @img_function.call(s)
717
+ end
718
+ s
719
+ },
720
+ :open => 'src="', :close => '"',
721
+ },
722
+ :strong => {
723
+ :curpat => '(?=\*\*)',
724
+ :stops => '\*\*.*?\*\*',
725
+ :hint => ['*'],
726
+ :contains => @@all_inline,
727
+ :filter => Proc.new {|s|
728
+ s[0,2] = ''
729
+ s.sub!(/\*\*$/, '')
730
+ s
731
+ },
732
+ :open => "<strong>", :close => "</strong>",
733
+ },
734
+ :em => {
735
+ # This could use a negative lookback assertion to let you know whether
736
+ # it's part of a URL or not. That would be helpful if the URL had been
737
+ # escaped. Currently, it will just become italic after the // since
738
+ # it didn't process the URL.
739
+ :curpat => '(?=\/\/)',
740
+ # Removed a negative lookback assertion (?<!:) from the Perl version
741
+ # and replaced it with [^:] Not sure of the consequences, however, as
742
+ # of this version, Ruby does not have negative lookback assertions, so
743
+ # I had to do it.
744
+ :stops => '\/\/.*?[^:]\/\/',
745
+ :hint => ['/'],
746
+ :contains => @@all_inline,
747
+ :filter => Proc.new {|s|
748
+ s[0,2] = ''
749
+ s.sub!(/\/\/$/, '')
750
+ s
751
+ },
752
+ :open => "<em>", :close => "</em>",
753
+ },
754
+ :mono => {
755
+ :curpat => '(?=\#\#)',
756
+ :stops => '\#\#.*?\#\#',
757
+ :hint => ['#'],
758
+ :contains => @@all_inline,
759
+ :filter => Proc.new {|s|
760
+ s[0,2] = ''
761
+ s.sub!(/\#\#$/, '')
762
+ s
763
+ },
764
+ :open => "<tt>", :close => "</tt>",
765
+ },
766
+ :sub => {
767
+ :curpat => '(?=,,)',
768
+ :stops => ',,.*?,,',
769
+ :hint => [','],
770
+ :contains => @@all_inline,
771
+ :filter => Proc.new {|s|
772
+ s[0,2] = ''
773
+ s.sub!(/\,\,$/, '')
774
+ s
775
+ },
776
+ :open => "<sub>", :close => "</sub>",
777
+ },
778
+ :sup => {
779
+ :curpat => '(?=\^\^)',
780
+ :stops => '\^\^.*?\^\^',
781
+ :hint => ['^'],
782
+ :contains => @@all_inline,
783
+ :filter => Proc.new {|s|
784
+ s[0,2] = ''
785
+ s.sub!(/\^\^$/, '')
786
+ s
787
+ },
788
+ :open => "<sup>", :close => "</sup>",
789
+ },
790
+ :u => {
791
+ :curpat => '(?=__)',
792
+ :stops => '__.*?__',
793
+ :hint => ['_'],
794
+ :contains => @@all_inline,
795
+ :filter => Proc.new {|s|
796
+ s[0,2] = ''
797
+ s.sub!(/__$/, '')
798
+ s
799
+ },
800
+ :open => "<u>", :close => "</u>",
801
+ },
802
+ :amp => {
803
+ :curpat => '(?=\&(?!\w+\;))',
804
+ :stops => '.',
805
+ :hint => ['&'],
806
+ :filter => Proc.new { "&amp;" },
807
+ :open => "", :close => "",
808
+ },
809
+ :tm => {
810
+ :curpat => '(?=\(TM\))',
811
+ :stops => '\(TM\)',
812
+ :hint => ['('],
813
+ :filter => Proc.new { "&trade;" },
814
+ :open => "", :close => "",
815
+ },
816
+ :reg => {
817
+ :curpat => '(?=\(R\))',
818
+ :stops => '\(R\)',
819
+ :hint => ['('],
820
+ :filter => Proc.new { "&reg;" },
821
+ :open => "", :close => "",
822
+ },
823
+ :copy => {
824
+ :curpat => '(?=\(C\))',
825
+ :stops => '\(C\)',
826
+ :hint => ['('],
827
+ :filter => Proc.new { "&copy;" },
828
+ :open => "", :close => "",
829
+ },
830
+ :ndash => {
831
+ :curpat => '(?=--)',
832
+ :stops => '--',
833
+ :hint => ['-'],
834
+ :filter => Proc.new { "&ndash;" },
835
+ :open => "", :close => "",
836
+ },
837
+ :ellipsis => {
838
+ :curpat => '(?=\.\.\.)',
839
+ :stops => '\.\.\.',
840
+ :hint => ['.'],
841
+ :filter => Proc.new { "&hellip;" },
842
+ :open => "", :close => "",
843
+ },
844
+ }
845
+
846
+ def self.strip_leading_and_trailing_eq_and_whitespace(s)
847
+ s.sub!(/^\s*=*\s*/, '')
848
+ s.sub!(/\s*=*\s*$/, '')
849
+ return s
850
+ end
851
+
852
+ def self.strip_list(s)
853
+ s.sub!(/(?:`*| *)[\*\#]/, '`')
854
+ s.gsub!(/\n(?:`*| *)[\*\#]/m, "\n`")
855
+ return s
856
+ end
857
+
858
+ def self.filter_string_x_with_chunk_filter_y(str, chunk)
859
+ return @@chunks_hash[chunk][:filter].call(str)
860
+ end
861
+
862
+ def self.parse(tref, chunk)
863
+
864
+ sub_chunk = nil
865
+ pos = 0
866
+ last_pos = 0
867
+ html = ""
868
+
869
+ loop do
870
+
871
+ if sub_chunk # we've determined what type of sub_chunk this is
872
+
873
+ if sub_chunk == :dd
874
+ # Yuck... I don't exactly understand why I need this section, but
875
+ # without it the parser will go into an infinite loop on the :dd's in
876
+ # the test suite. Please, if you're a most excellent Ruby hacker,
877
+ # find the issue, clean this up, and remove the comment here, m'kay?
878
+
879
+ while tref.index(Regexp.compile('\G.*' + @@chunks_hash[sub_chunk][:delim], Regexp::MULTILINE), pos)
880
+ end_of_match = Regexp.last_match.end(0)
881
+ if end_of_match == pos
882
+ break
883
+ else
884
+ pos = end_of_match
885
+ end
886
+ end
887
+
888
+ if pos == last_pos
889
+ pos = tref.length
890
+ end
891
+ else
892
+ # This is a little slower than it could be. The delim should be
893
+ # pre-compiled, but see the issue in the comment above.
894
+ if tref.index(Regexp.compile(@@chunks_hash[sub_chunk][:delim], Regexp::MULTILINE), pos)
895
+ pos = Regexp.last_match.end(0)
896
+ else
897
+ pos = tref.length
898
+ end
899
+ end
900
+
901
+ html += @@chunks_hash[sub_chunk][:open]
902
+
903
+ t = tref[last_pos, pos - last_pos] # grab the chunk
904
+
905
+ if @@chunks_hash[sub_chunk].has_key?(:filter) # filter it, if applicable
906
+ t = @@chunks_hash[sub_chunk][:filter].call(t)
907
+ end
908
+
909
+ last_pos = pos # remember where this chunk ends (where next begins)
910
+
911
+ if t && @@chunks_hash[sub_chunk].has_key?(:contains) # if it contains other chunks...
912
+ html += parse(t, sub_chunk) # recurse.
913
+ else
914
+ html += t # otherwise, print it
915
+ end
916
+
917
+ html += @@chunks_hash[sub_chunk][:close] # print the close tag
918
+
919
+ end
920
+
921
+ if pos && pos == tref.length # we've eaten the whole string
922
+ break
923
+ else ## more string to come
924
+ sub_chunk = get_sub_chunk_for(tref, chunk, pos)
925
+ end
926
+
927
+ end
928
+
929
+ return html
930
+ end
931
+
932
+ def self.get_sub_chunk_for(tref, chunk, pos)
933
+
934
+ first_char = tref[pos, 1] # get a hint about the next chunk
935
+ for chunk_hinted_at in @@chunks_hash[chunk][:calculated_hint_array_for][first_char].to_a
936
+ #puts "trying hint #{chunk_hinted_at} for -#{first_char}- on -" + tref[pos, 2] + "-\n"
937
+ if tref.index(@@chunks_hash[chunk_hinted_at][:curpatcmp], pos) # hint helped id the chunk
938
+ return chunk_hinted_at
939
+ end
940
+ end
941
+
942
+ # the hint didn't help. Check all the chunk types which this chunk contains
943
+ for contained_chunk in @@chunks_hash[chunk][:contains].to_a
944
+ #puts "trying contained chunk #{contained_chunk} on -" + tref[pos, 2] + "- within chunk #{chunk.to_s}\n"
945
+ if tref.index(@@chunks_hash[contained_chunk.to_sym][:curpatcmp], pos) # found one
946
+ return contained_chunk.to_sym
947
+ end
948
+ end
949
+
950
+ return nil
951
+ end
952
+
953
+ # compile a regex that matches any of the patterns that interrupt the
954
+ # current chunk.
955
+ def self.delim(chunk)
956
+ chunk = @@chunks_hash[chunk]
957
+ if chunk[:stops].class.to_s == "Array"
958
+ regex = ''
959
+ for stop in chunk[:stops]
960
+ stop = stop.to_sym
961
+ if @@chunks_hash[stop].has_key?(:fwpat)
962
+ regex += @@chunks_hash[stop][:fwpat] + "|"
963
+ else
964
+ regex += @@chunks_hash[stop][:curpat] + "|"
965
+ end
966
+ end
967
+ regex.chop!
968
+ return regex
969
+ else
970
+ return chunk[:stops]
971
+ end
972
+ end
973
+
974
+ # one-time optimization of the grammar - speeds the parser up a ton
975
+ def self.init
976
+ return if @is_initialized
977
+
978
+ @is_initialized = true
979
+
980
+ # build an array of "plain content" characters by subtracting @specialchars
981
+ # from ascii printable (ascii 32 to 126)
982
+ for charnum in 32..126 do
983
+ char = charnum.chr
984
+ if @@specialchars.index(char).nil?
985
+ @@plainchars << char
986
+ end
987
+ end
988
+
989
+ # precompile a bunch of regexes
990
+ for k in @@chunks_hash.keys do
991
+ c = @@chunks_hash[k]
992
+ if c.has_key?(:curpat)
993
+ c[:curpatcmp] = Regexp.compile('\G' + c[:curpat], Regexp::MULTILINE)
994
+ end
995
+
996
+ if c.has_key?(:stops)
997
+ c[:delim] = delim(k)
998
+ end
999
+
1000
+ if c.has_key?(:contains) # store hints about each chunk to speed id
1001
+ c[:calculated_hint_array_for] = {}
1002
+
1003
+ for ct in c[:contains]
1004
+ ct = ct.to_sym
1005
+
1006
+ if @@chunks_hash[ct].has_key?(:hint)
1007
+ for hint in @@chunks_hash[ct][:hint]
1008
+ if !c[:calculated_hint_array_for].has_key?(hint)
1009
+ c[:calculated_hint_array_for][hint] = []
1010
+ end
1011
+ c[:calculated_hint_array_for][hint] << ct
1012
+ end
1013
+ end
1014
+
1015
+ end
1016
+ end
1017
+ end
1018
+
1019
+ end
1020
+
1021
+
1022
+
1023
+ end