Debian Codegraph @

lib/Text/Markup/Mediawiki.pm

raw
1 package Text::Markup::Mediawiki;
2
3 use 5.8.1;
4 use strict;
5 use warnings;
6 use Text::Markup;
7 use File::BOM qw(open_bom);
8 use Text::MediawikiFormat 1.0;
9
10 our $VERSION = '0.41';
11
12 sub import {
13 # Replace the regex if passed one.
14 Text::Markup->register( mediawiki => $_[1] ) if $_[1];
15 }
16
17 sub parser {
18 my ($file, $encoding, $opts) = @_;
19 open_bom my $fh, $file, ":encoding($encoding)";
20 local $/;
21 my $html = Text::MediawikiFormat::format(<$fh>, @{ $opts });
22 return unless $html =~ /\S/;
23 utf8::encode($html);
24 return $html if $opts->[1] && $opts->[1]->{raw};
25 return qq{<html>
26 <head>
27 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
28 </head>
29 <body>
30 $html
31 </body>
32 </html>
33 };
34
35 }
36
37 1;
38 __END__
39
40 =head1 Name
41
42 Text::Markup::Mediawiki - MediaWiki syntax parser for Text::Markup
43
44 =head1 Synopsis
45
46 my $html = Text::Markup->new->parse(file => 'README.mediawiki');
47 my $raw = Text::Markup->new->parse(
48 file => 'README.mediawiki',
49 options => [ {}, { raw => 1 } ],
50 );
51
52 =head1 Description
53
54 This is the L<MediaWiki
55 syntax|https://en.wikipedia.org/wiki/Help:Contents/Editing_Wikipedia> parser
56 for L<Text::Markup>. It reads in the file (relying on a
57 L<BOM|https://www.unicode.org/unicode/faq/utf_bom.html#BOM>), hands it off to
58 L<Text::MediawikiFormat> for parsing, and then returns the generated HTML as
59 an encoded UTF-8 string with an C<http-equiv="Content-Type"> element
60 identifying the encoding as UTF-8.
61
62 It recognizes files with the following extensions as MediaWiki:
63
64 =over
65
66 =item F<.mediawiki>
67
68 =item F<.mwiki>
69
70 =item F<.wiki>
71
72 =back
73
74 To change it the files it recognizes, load this module directly and pass a
75 regular expression matching the desired extension(s), like so:
76
77 use Text::Markup::Mediawiki qr{kwiki?};
78
79 Text::Markup::Mediawiki supports the two
80 L<Text::MediawikiFormat arguments|Text::MediawikiFormat/format>, a hash
81 reference for tags and a hash reference of options. The supported options
82 include:
83
84 =over
85
86 =item C<prefix>
87
88 =item C<extended>
89
90 =item C<implicit_links>
91
92 =item C<absolute_links>
93
94 =item C<process_html>
95
96 =back
97
98 Normally this module returns the output wrapped in a minimal HTML document
99 skeleton. If you would like the raw output without the skeleton, you can pass
100 the C<raw> option via that second hash reference of options.
101
102 =head1 Author
103
104 David E. Wheeler <[email protected]>
105
106 =head1 Copyright and License
107
108 Copyright (c) 2011-2025 David E. Wheeler. Some Rights Reserved.
109
110 This module is free software; you can redistribute it and/or modify it under
111 the same terms as Perl itself.
112
113 =cut