1Text::WikiFormat(3) User Contributed Perl Documentation Text::WikiFormat(3)
2
3
4
6 Text::WikiFormat - module for translating Wiki formatted text into
7 other formats
8
10 use Text::WikiFormat;
11 my $html = Text::WikiFormat::format($raw);
12
14 The original Wiki web site had a very simple interface to edit and to
15 add pages. Its formatting rules are simple and easy to use. They are
16 also easy to translate into other, more complicated markup languages
17 with this module. It creates HTML by default, but can produce valid
18 POD, DocBook, XML, or any other format imaginable.
19
20 The most important function is "format()". It is not exported by
21 default.
22
23 format()
24
25 "format()" takes one required argument, the text to convert, and
26 returns the converted text. It allows two optional arguments. The
27 first is a reference to a hash of tags. Anything passed in here will
28 override the default tag behavior. The second argument is a hash ref‐
29 erence of options. They are currently:
30
31 * prefix
32 The prefix of any links. In HTML mode, this is the path to the
33 Wiki. The actual linked item itself will be appended to the pre‐
34 fix. This is useful to create full URIs:
35
36 { prefix => 'http://example.com/wiki.pl?page=' }
37
38 * extended
39 A boolean flag, false by default, to use extended linking seman‐
40 tics. This comes from the Everything Engine (<http://everyde‐
41 vel.com/>), which marks links with square brackets. An optional
42 title may occur after the link target, preceded by an open pipe.
43 These are valid extended links:
44
45 [a valid link]
46 [link⎪title]
47
48 Where the linking semantics of the destination format allow it, the
49 result will display the title instead of the URI. In HTML terms,
50 the title is the content of an "A" element (not the content of its
51 "HREF" attribute).
52
53 You can use delimiters other than single square brackets for mark‐
54 ing extended links by passing a value for "extended_link_delim‐
55 iters" in the %tags hash when calling "format".
56
57 * implicit_links
58 A boolean flag, true by default, to create links from Studly‐
59 CapsStringsNote that if you disable this flag, you should probably
60 enable the "extended" one also, or there will be no way of creating
61 links in your documents. To disable it, use the pair:
62
63 { implicit_links => 0 }
64
65 * absolute_links
66 A boolean flag, false by default, which treats any links that are
67 absolute URIs (such as http://www.cpan.org/) specially. Any prefix
68 will not apply and the URIs aren't quoted. Use this in conjunction
69 with the "extended" option to detect the link.
70
71 A link is any text that starts with a known schema followed by a
72 colon and one or more non-whitespace characters. This is a dis‐
73 tinct subset of what URI recognizes as a URI, but is a good first-
74 order approximation. If you need to recognize more complex URIs,
75 use the standard wiki formatting explained earlier.
76
77 The recognized schemas are those defined in the "schema" value in
78 the %tags hash. The defaults are "http", "https", "ftp", "mailto",
79 and "gopher".
80
81 Wiki Format
82
83 Wiki formatting is very simple. An item wrapped in three single quotes
84 is strong. An item wrapped in two single quotes is emphasized. Any
85 word with multiple CapitalLetters (e. g., StudlyCaps) will become a
86 link. Four or more hyphen characters at the start of a line create a
87 horizontal line. Newlines turn into the appropriate tags. Headers are
88 matching equals signs around the header text -- the more signs, the
89 lesser the header.
90
91 Lists are indented text, by one tab or four spaces by default. You may
92 disable indentation. In unordered lists, where each item has its own
93 bullet point, each item needs a leading asterisk and space. Ordered
94 lists consist of items marked with combination of one or more alphanu‐
95 meric characters followed by a period and an optional space. Any
96 indented text without either marking is code, handled literally. You
97 can nest lists.
98
99 The following is valid Wiki formatting, with an extended link as
100 marked.
101
102 = my interesting text =
103
104 ANormalLink
105 [let the Sun shine⎪AnExtendedLink]
106
107 == my interesting lists ==
108
109 * unordered one
110 * unordered two
111
112 1. ordered one
113 2. ordered two
114 a. nested one
115 b. nested two
116
117 code one
118 code two
119
120 The first line of a normal paragraph.
121 The second line of a normal paragraph. Whee.
122
124 If you'd like to make your life more convenient, you can optionally
125 import a subroutine that already has default tags and options set up.
126 This is especially handy if you use a prefix:
127
128 use Text::WikiFormat prefix => 'http://www.example.com/';
129 wikiformat( 'some text' );
130
131 Tags are interpreted as, well, tags, except for five special keys:
132
133 * "prefix", interpreted as a link prefix
134 * "extended", interpreted as the extended link flag
135 * "implicit_links", interpreted as the flag to control implicit links
136 * "absolute_links", interpreted as the flag to control absolute links
137 * "as", interpreted as an alias for the imported function
138
139 Use the "as" flag to control the name by which your code calls the
140 imported functionFor example,
141
142 use Text::WikiFormat as => 'formatTextInWikiStyle';
143 formatTextInWikiStyle( 'some text' );
144
145 You might choose a better name, though.
146
147 The calling semantics are effectively the same as those of the format()
148 function. Any additional tags or options to the imported function will
149 override the defaults. This code:
150
151 use Text::WikiFormat as => 'wf', extended => 0;
152 wf( 'some text', {}, { extended => 1 });
153
154 enables extended links, though the default is to disable them.
155
156 Tony Bowden <tony@kasei.com> suggested this feature, but all implemen‐
157 tation blame rests solely with me. Kate L Pugh (<kake@earth.li>)
158 pointed out that it didn't work, with tests. It works now.
159
161 Tags
162
163 There are two types of Wiki markup: line items and blocks. Blocks
164 include lists, which are made up of lines and can also contain other
165 lists.
166
167 Line items
168
169 There are two classes of line items: simple tags, and tags that contain
170 data. The simple tags are "newline" and "line". The module inserts a
171 newline tag whenever it encounters a newline character ("\n"). It
172 inserts a line tag whenever four or more dash characters ("----") occur
173 at the start of a line. No whitespace is allowed. These default to
174 the <br> and <hr> HTML tags, respectively. To override either, simply
175 pass tags such as:
176
177 my $html = format($text, { newline => "\n" });
178
179 The three line items are more complex, and require subroutine refer‐
180 ences. This category includes the "strong" and "emphasized" tags as
181 well as "link"s. The first argument passed to the subref will be the
182 data found in between the marks. The second argument is the $opts hash
183 reference. The default action for a strong tag is equivalent to:
184
185 my $html = format($text, { strong => sub { "<b>$_[0]</b>" } });
186
187 As of version 0.70, you can change the regular expressions used to find
188 strong and emphasized tags:
189
190 %tags = (
191 strong_tag => qr/\*(.+?)\*/,
192 emphasized_tag => qr⎪(?<!<)/(.+?)/⎪,
193 );
194
195 $wikitext = 'this is *strong*, /emphasized/, and */emphasized strong/*';
196 $htmltext = Text::WikiFormat::format( $wikitext, \%tags, {} );
197
198 Be aware that using forward slashes to mark anything leads to the hairy
199 regular expression -- use something else. This interface is experimen‐
200 tal and may change if I find something better. It's nice to be able to
201 override those tags, though.
202
203 Finally, there are "extended_link_delimiters", which allow you to use
204 delimiters other than single square brackets for marking extended
205 links. Pass the tags as:
206
207 my $html = format( $text, { extended_link_delimiters => [ '[[', ']]' ] });
208
209 This allows you to use double square brackets as UseMod supports:
210
211 [[an extended link]]
212 [[a titled extended link⎪title]]
213
214 Blocks
215
216 There are five default block types: "paragraph", "header", "code",
217 "unordered", and "ordered". The parser usually finds these by indenta‐
218 tion, either one or more tabs or four or more whitespace characters.
219 (This does not include newlines, however.) Any line that does not fall
220 in any of these three categories is a "paragraph".
221
222 Code, unordered, and ordered blocks do not require indentation, but the
223 parser uses it to control nesting in lists. Be careful. To mark a
224 block as requiring indentation, use the "indented" tag, which contains
225 a reference to a hash:
226
227 my $html = format($text, {
228 indented => { map { $_ => 1 } qw( ordered unordered code )}
229 });
230
231 Block entries in the tag hashes must contain array references. The
232 first two items are the tags used at the start and end of the block.
233 The last items contain the tags used at the start and end of each line.
234 Where there needs to be more processing of individual lines, use a sub‐
235 ref as the third item. This is how the module numbers ordered lines in
236 HTML lists:
237
238 my $html = format($text, { ordered => [ '<ol>', "</ol>\n",
239 sub { qq⎪<li value="$_[2]">$_[0]</li>\n⎪ } ] });
240
241 The first argument to these subrefs is the post-processed text of the
242 line itself. (Processing removes the indentation and tokens used to
243 mark this as a list and checks the rest of the line for other line for‐
244 mattings.) The second argument is the indentation level. The subse‐
245 quent arguments are captured variables in the regular expression used
246 to find this list type. The regexp for ordered lists is:
247
248 qr/^([\dA-Za-z]+)\.\s*/;
249
250 The module processes indentation first, if applicable, and stores the
251 indentation level (the length of the indentation removed). The line
252 must contain one or more alphanumeric character followed by a single
253 period and optional whitespace to be an ordered list item. The module
254 saves the contents of this last group, the value of the list item, and
255 passes it to the subref as the third argument.
256
257 Lists automatically start and end as necessary.
258
259 Because of the indentation issue, there is a specific blocks processing
260 in a specific order. The "blockorder" tag governs this order. It con‐
261 tains a reference to an array of the names of the appropriate blocks to
262 process. If you add a block type, be sure to add an entry for it in
263 "blockorder":
264
265 my $html = format($text, {
266 escaped => [ '', '', '', '' ],
267 blocks => {
268 invisible => qr!^--(.*?)--$!,
269 },
270 blockorder =>
271 [qw( header line ordered unordered code paragraph invisible )],
272 });
273
274 Finding blocks
275
276 Text::WikiFormat uses regular expressions to find blocks. These are in
277 the %tags hash under the "blocks" key. To change the regular expres‐
278 sion to find code block items, use:
279
280 my $html = format($wikitext, {
281 blocks => {
282 code => qr/^:\s+/,
283 },
284 indented => {
285 code => 1,
286 },
287 );
288
289 This will require indentation and a colon to mark code lines. A poten‐
290 tial shortcut is to use the "indent" tag to match or to change the
291 indentation marker.
292
293 Note: if you want to mark a block type as non-indented, you cannot use
294 an empty regex such as "qr//". Use a mostly-empty, always-true regex
295 such as "qr/^/" instead.
296
297 Finding Blocks in the Correct Order
298
299 As intrepid bug reporter Tom Hukins pointed out in CPAN RT bug #671,
300 the order in which Text::WikiFormat searches for blocks varies by plat‐
301 form and version of Perl. Because some block-finding regular expres‐
302 sions are more specific than others, what you intend to be one type of
303 block may turn into a different list type.
304
305 If you're adding new block types, be aware of this. The "blockorder"
306 entry in %tags exists to force Text::WikiFormat to apply its regexes
307 from most specific to least specific. It contains an array reference.
308 By default, it looks for ordered lists first, unordered lists second,
309 and code references at the end.
310
312 chromatic, "chromatic@wgz.org", with much input from the Jellybean team
313 (including Jonathan Paulett). Kate L Pugh has also provided several
314 patches, many failing tests, and is usually the driving force behind
315 new features and releases. If you think this module is worth buying me
316 a beer, she deserves at least half of it.
317
318 Alex Vandiver added a nice patch and tests for extended links.
319
320 Tony Bowden, Tom Hukins, and Andy H. all suggested useful features that
321 are now implemented.
322
323 Sam Vilain, Chris Winters, Paul Schmidt, and Art Henry have all found
324 and reported silly bugs.
325
326 Blame me for the implementation.
327
329 The link checker in "format_line()" may fail to detect existing links
330 that do not follow HTML, XML, or SGML style. They may die with some
331 SGML styles too. Sic transit gloria mundi.
332
334 * Find a nicer way to mark list as having unformatted lines
335 * Optimize "format_line()" to work on a list of lines
336 * Handle nested "strong" and "emphasized" markings better
337
339 Brian "Ingy" Ingerson's CGI::Kwiki has a fairly nice parser.
340
341 John McNamara's Pod::Simple::Wiki looks like a good project.
342
343 Matt Sergeant keeps threatening to write a nice SAX-throwing Wiki for‐
344 matter.
345
347 Copyright (c) 2002 - 2006, chromatic. All rights reserved. This mod‐
348 ule is distributed under the same terms as Perl itself.
349
350
351
352perl v5.8.8 2006-03-31 Text::WikiFormat(3)