| 1 |
package Text::Markup; |
| 2 |
|
| 3 |
use 5.8.1; |
| 4 |
use strict; |
| 5 |
use warnings; |
| 6 |
use Text::Markup; |
| 7 |
use Text::Markup::None; |
| 8 |
use Carp; |
| 9 |
|
| 10 |
our $VERSION = '0.41'; |
| 11 |
|
| 12 |
my %_PARSER_FOR; |
| 13 |
my %REGEX_FOR = ( |
| 14 |
html => qr{x?html?}, |
| 15 |
markdown => qr{m(?:d(?:own)?|kdn?|arkdown)}, |
| 16 |
multimarkdown => qr{mm(?:d(?:own)?|kdn?|arkdown)}, |
| 17 |
pod => qr{p(?:od|m|l)}, |
| 18 |
textile => qr{textile}, |
| 19 |
trac => qr{tra?c}, |
| 20 |
mediawiki => qr{(?:m(?:edia)?)?wiki}, |
| 21 |
rest => qr{re?st}, |
| 22 |
asciidoc => qr{a(?:sc(?:iidoc)?|doc)?}, |
| 23 |
bbcode => qr{bb(?:code)?}, |
| 24 |
creole => qr{creole}, |
| 25 |
); |
| 26 |
|
| 27 |
sub register { |
| 28 |
my ($class, $name, $regex) = @_; |
| 29 |
my $pkg = caller; |
| 30 |
$REGEX_FOR{$name} = $regex; |
| 31 |
$_PARSER_FOR{$name} = $pkg->can('parser') |
| 32 |
or croak "No parser() function defind in $pkg"; |
| 33 |
} |
| 34 |
|
| 35 |
sub _parser_for { |
| 36 |
my ($self, $format) = @_; |
| 37 |
return Text::Markup::None->can('parser') unless $format; |
| 38 |
return $_PARSER_FOR{$format} if $_PARSER_FOR{$format}; |
| 39 |
my $pkg = __PACKAGE__ . '::' . ($format eq 'html' ? 'HTML' : ucfirst $format); |
| 40 |
eval "require $pkg; 1" or die $@; |
| 41 |
return $_PARSER_FOR{$format} = $pkg->can('parser') |
| 42 |
|| croak "No parser() function defind in $pkg"; |
| 43 |
} |
| 44 |
|
| 45 |
sub formats { |
| 46 |
sort keys %REGEX_FOR; |
| 47 |
} |
| 48 |
|
| 49 |
sub format_matchers { %REGEX_FOR } |
| 50 |
|
| 51 |
sub new { |
| 52 |
my $class = shift; |
| 53 |
bless { default_encoding => 'UTF-8', @_ } => $class; |
| 54 |
} |
| 55 |
|
| 56 |
sub parse { |
| 57 |
my $self = shift; |
| 58 |
my %p = @_; |
| 59 |
my $file = $p{file} or croak "No file parameter passed to parse()"; |
| 60 |
croak "$file does not exist" unless -e $file && !-d _; |
| 61 |
|
| 62 |
my $parser = $self->_get_parser(\%p); |
| 63 |
return $parser->( |
| 64 |
$file, |
| 65 |
$p{encoding} || $self->default_encoding, |
| 66 |
$p{options} || [], |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
sub default_format { |
| 71 |
my $self = shift; |
| 72 |
return $self->{default_format} unless @_; |
| 73 |
$self->{default_format} = shift; |
| 74 |
} |
| 75 |
|
| 76 |
sub default_encoding { |
| 77 |
my $self = shift; |
| 78 |
return $self->{default_encoding} unless @_; |
| 79 |
$self->{default_encoding} = shift; |
| 80 |
} |
| 81 |
|
| 82 |
sub _get_parser { |
| 83 |
my ($self, $p) = @_; |
| 84 |
my $format = $p->{format} |
| 85 |
|| $self->guess_format($p->{file}) |
| 86 |
|| $self->default_format; |
| 87 |
|
| 88 |
return $self->_parser_for($format); |
| 89 |
} |
| 90 |
|
| 91 |
sub guess_format { |
| 92 |
my ($self, $file) = @_; |
| 93 |
for my $format (keys %REGEX_FOR) { |
| 94 |
return $format if $file =~ qr{[.]$REGEX_FOR{$format}$}; |
| 95 |
} |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
1; |
| 100 |
__END__ |
| 101 |
|
| 102 |
=head1 Name |
| 103 |
|
| 104 |
Text::Markup - Parse text markup into HTML |
| 105 |
|
| 106 |
=head1 Synopsis |
| 107 |
|
| 108 |
my $parser = Text::Markup->new( |
| 109 |
default_format => 'markdown', |
| 110 |
default_encoding => 'UTF-8', |
| 111 |
); |
| 112 |
|
| 113 |
my $html = $parser->parse(file => $markup_file); |
| 114 |
|
| 115 |
=head1 Description |
| 116 |
|
| 117 |
This class is really simple. All it does is take the name of a file and return |
| 118 |
an HTML-formatted version of that file. The idea is that one might have files |
| 119 |
in lots of different markups, and not know or care what markups each uses. |
| 120 |
It's the job of this module to figure that out, parse it, and give you the |
| 121 |
resulting HTML. |
| 122 |
|
| 123 |
This distribution includes support for a number of markup formats: |
| 124 |
|
| 125 |
=over |
| 126 |
|
| 127 |
=item * L<Asciidoc|https://asciidoc.org> |
| 128 |
|
| 129 |
=item * L<BBcode|https://www.bbcode.org/> |
| 130 |
|
| 131 |
=item * L<Creole|https://www.wikicreole.org/> |
| 132 |
|
| 133 |
=item * L<HTML|https://whatwg.org/html> |
| 134 |
|
| 135 |
=item * L<Markdown|https://daringfireball.net/projects/markdown/> |
| 136 |
|
| 137 |
=item * L<MediaWiki|https://en.wikipedia.org/wiki/Help:Contents/Editing_Wikipedia> |
| 138 |
|
| 139 |
=item * L<MultiMarkdown|https://fletcherpenney.net/multimarkdown/> |
| 140 |
|
| 141 |
=item * L<Pod|perlpod> |
| 142 |
|
| 143 |
=item * L<reStructuredText|https://docutils.sourceforge.io/rst.html> |
| 144 |
|
| 145 |
=item * L<Textile|https://textile-lang.com/> |
| 146 |
|
| 147 |
=item * L<Trac|https://trac.edgewall.org/wiki/WikiFormatting> |
| 148 |
|
| 149 |
=back |
| 150 |
|
| 151 |
Modules under the Text::Markup namespace provide these parsers, and Text::Markup |
| 152 |
automatically loads them on recognizing file name suffixes documented for each |
| 153 |
module. To change the file extensions recognized for a particular parser (except |
| 154 |
for L<Text::Markup::None>), load it directly and pass a regular expression. For |
| 155 |
example, to have the Mediawiki parser recognized files with the suffixes |
| 156 |
C<truck>, C<truc>, C<track>, or C<trac>, load it like so: |
| 157 |
|
| 158 |
use Text::Markup::Mediawiki qr{tr[au]ck?}; |
| 159 |
|
| 160 |
Adding support for more markup languages is straight-forward, and patches |
| 161 |
adding them to this distribution are also welcome. See L</Add a Parser> for |
| 162 |
step-by-step instructions. |
| 163 |
|
| 164 |
Or if you just want to use this module, then read on! |
| 165 |
|
| 166 |
=head1 Interface |
| 167 |
|
| 168 |
=head2 Constructor |
| 169 |
|
| 170 |
=head3 C<new> |
| 171 |
|
| 172 |
my $parser = Text::Markup->new(default_format => 'markdown'); |
| 173 |
|
| 174 |
Supported parameters: |
| 175 |
|
| 176 |
=over |
| 177 |
|
| 178 |
=item C<default_format> |
| 179 |
|
| 180 |
The default format to use if one isn't passed to C<parse()> and one can't be |
| 181 |
guessed. |
| 182 |
|
| 183 |
=item C<default_encoding> |
| 184 |
|
| 185 |
The character encoding in which to assume a file is encoded if it's not |
| 186 |
otherwise explicitly determined by examination of the source file. Defaults to |
| 187 |
"UTF-8". |
| 188 |
|
| 189 |
=back |
| 190 |
|
| 191 |
=head2 Class Methods |
| 192 |
|
| 193 |
=head3 C<register> |
| 194 |
|
| 195 |
Text::Markup->register(foobar => qr{fb|foob(?:ar)?}); |
| 196 |
|
| 197 |
Registers a markup parser. You likely won't need to use this method unless |
| 198 |
you're creating a new markup parser and not contributing it back to the |
| 199 |
Text::Markup project. See L</Add a Parser> for details. |
| 200 |
|
| 201 |
=head3 C<formats> |
| 202 |
|
| 203 |
my @formats = Text::Markup->formats; |
| 204 |
|
| 205 |
Returns a list of all of the formats currently recognized by Text::Markup. |
| 206 |
This will include all core parsers (except for "None") and any that have been |
| 207 |
loaded elsewhere and that call C<register> to register themselves. |
| 208 |
|
| 209 |
=head3 C<format_matchers> |
| 210 |
|
| 211 |
my %matchers = Text::Markup->format_matchers; |
| 212 |
|
| 213 |
Returns a list of key/value pairs mapping all the formats returned by |
| 214 |
C<formats> to the regular expressions used to match them. |
| 215 |
|
| 216 |
=head2 Instance Methods |
| 217 |
|
| 218 |
=head3 C<parse> |
| 219 |
|
| 220 |
my $html = $parser->parse(file => $file_to_parse); |
| 221 |
|
| 222 |
Parses a file and return the generated HTML, or C<undef> if no markup was |
| 223 |
found in the file. Supported parameters: |
| 224 |
|
| 225 |
=over |
| 226 |
|
| 227 |
=item C<file> |
| 228 |
|
| 229 |
The file from which to read the markup to be parsed. Required. |
| 230 |
|
| 231 |
=item C<format> |
| 232 |
|
| 233 |
The markup format in the file, which determines the parser used to parse it. |
| 234 |
If not specified, Text::Markup will try to guess the format from the file's |
| 235 |
suffix. If it can't guess, it falls back on C<default_format>. And if that |
| 236 |
attribute is not set, it uses the C<none> parser, which simply encodes the |
| 237 |
entire file and wraps it in a C<< <pre> >> element. |
| 238 |
|
| 239 |
=item C<encoding> |
| 240 |
|
| 241 |
The character encoding to assume the source file is encoded in (if such cannot |
| 242 |
be determined by other means, such as a |
| 243 |
L<BOM|https://en.wikipedia.org/wiki/Byte_order_mark>). If not specified, the |
| 244 |
value of the C<default_encoding> attribute will be used, and if that attribute |
| 245 |
is not set, UTF-8 will be assumed. |
| 246 |
|
| 247 |
=item C<options> |
| 248 |
|
| 249 |
An array reference of options for the parser. See the documentation of the |
| 250 |
various parser modules for details. |
| 251 |
|
| 252 |
=back |
| 253 |
|
| 254 |
=head3 C<default_format> |
| 255 |
|
| 256 |
my $format = $parser->default_format; |
| 257 |
$parser->default_format('markdown'); |
| 258 |
|
| 259 |
An accessor for the default format attribute. |
| 260 |
|
| 261 |
=head3 C<default_encoding> |
| 262 |
|
| 263 |
my $encoding = $parser->default_encoding; |
| 264 |
$parser->default_encoding('Big5'); |
| 265 |
|
| 266 |
An accessor for the default encoding attribute. |
| 267 |
|
| 268 |
=head3 C<guess_format> |
| 269 |
|
| 270 |
my $format = $parser->guess_format($filename); |
| 271 |
|
| 272 |
Compares the passed file name's suffix to the regular expressions of all |
| 273 |
registered formatting parser and returns the first one that matches. Returns |
| 274 |
C<undef> if none matches. |
| 275 |
|
| 276 |
=head1 Add a Parser |
| 277 |
|
| 278 |
Adding support for markup formats not supported by the core Text::Markup |
| 279 |
distribution is a straight-forward exercise. Say you wanted to add a "FooBar" |
| 280 |
markup parser. Here are the steps to take: |
| 281 |
|
| 282 |
=over |
| 283 |
|
| 284 |
=item 1 |
| 285 |
|
| 286 |
Fork L<this project on GitHub|https://github.com/theory/text-markup/> |
| 287 |
|
| 288 |
=item 2 |
| 289 |
|
| 290 |
Clone your fork and create a new branch in which to work: |
| 291 |
|
| 292 |
git clone [email protected]:$USER/text-markup.git |
| 293 |
cd text-markup |
| 294 |
git checkout -b foobar |
| 295 |
|
| 296 |
=item 3 |
| 297 |
|
| 298 |
Create a new module named C<Text::Markup::FooBar>. The simplest thing to do is |
| 299 |
copy an existing module and modify it. The HTML parser is probably the simplest: |
| 300 |
|
| 301 |
cp lib/Text/Markup/HTML.pm lib/Text/Markup/FooBar.pm |
| 302 |
perl -i -pe 's{HTML}{FooBar}g' lib/Text/Markup/FooBar.pm |
| 303 |
perl -i -pe 's{html}{foobar}g' lib/Text/Markup/FooBar.pm |
| 304 |
|
| 305 |
=item 4 |
| 306 |
|
| 307 |
Implement the C<parser> function in your new module. If you were to use a |
| 308 |
C<Text::FooBar> module, it might look something like this: |
| 309 |
|
| 310 |
package Text::Markup::FooBar; |
| 311 |
|
| 312 |
use 5.8.1; |
| 313 |
use strict; |
| 314 |
use Text::FooBar (); |
| 315 |
use File::BOM qw(open_bom) |
| 316 |
|
| 317 |
sub import { |
| 318 |
# Replace the regex if passed one. |
| 319 |
Text::Markup->register( foobar => $_[1] ) if $_[1]; |
| 320 |
} |
| 321 |
|
| 322 |
sub parser { |
| 323 |
my ($file, $encoding, $opts) = @_; |
| 324 |
my $md = Text::FooBar->new(@{ $opts || [] }); |
| 325 |
open_bom my $fh, $file, ":encoding($encoding)"; |
| 326 |
local $/; |
| 327 |
my $html = $md->parse(<$fh>); |
| 328 |
return unless $html =~ /\S/; |
| 329 |
utf8::encode($html); |
| 330 |
return join( "\n", |
| 331 |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />', |
| 332 |
'</head>', |
| 333 |
'<body>', |
| 334 |
$html, |
| 335 |
'</body>', |
| 336 |
'</html>', |
| 337 |
); |
| 338 |
} |
| 339 |
|
| 340 |
Use the C<$encoding> argument as appropriate to read in the source file. If |
| 341 |
your parser requires that text be decoded to Perl's internal form, use of |
| 342 |
L<File::BOM> is recommended, so that an explicit BOM will determine the |
| 343 |
encoding. Otherwise, fall back on the specified encoding. Note that some |
| 344 |
parsers, such as an HTML parser, would want text encoded before it parsed it. |
| 345 |
In such a case, read in the file as raw bytes: |
| 346 |
|
| 347 |
open my $fh, '<:raw', $file or die "Cannot open $file: $!\n"; |
| 348 |
|
| 349 |
The returned HTML, however, B<must be encoded in UTF-8>. Please include an |
| 350 |
L<encoding declaration|https://en.wikipedia.org/wiki/Character_encodings_in_HTML>, |
| 351 |
such as a content-type C<< <meta> >> element: |
| 352 |
|
| 353 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 354 |
|
| 355 |
This will allow any consumers of the returned HTML to parse it correctly. |
| 356 |
If the parser parsed no content, C<parser()> should return C<undef>. |
| 357 |
|
| 358 |
=item 5 |
| 359 |
|
| 360 |
Edit F<lib/Text/Markup.pm> and add an entry to its C<%REGEX_FOR> hash for your |
| 361 |
new format. The key should be the name of the format (lowercase, the same as |
| 362 |
the last part of your module's name). The value should be a regular expression |
| 363 |
that matches the file extensions that suggest that a file is formatted in your |
| 364 |
parser's markup language. For our FooBar parser, the line might look like |
| 365 |
this: |
| 366 |
|
| 367 |
foobar => qr{fb|foob(?:ar)?}, |
| 368 |
|
| 369 |
=item 6 |
| 370 |
|
| 371 |
Add a file in your parser's markup language to F<t/markups>. It should be |
| 372 |
named for your parser and end in F<.txt>, that is, F<t/markups/foobar.txt>. |
| 373 |
|
| 374 |
=item 7 |
| 375 |
|
| 376 |
Add an HTML file, F<t/html/foobar.html>, which should be the expected output |
| 377 |
once F<t/markups/foobar.txt> is parsed into HTML. This will be used to test |
| 378 |
that your parser works correctly. |
| 379 |
|
| 380 |
=item 8 |
| 381 |
|
| 382 |
Edit F<t/formats.t> by adding a line to its C<__DATA__> section. The line |
| 383 |
should be a comma-separated list describing your parser. The columns are: |
| 384 |
|
| 385 |
=over |
| 386 |
|
| 387 |
=item * Format |
| 388 |
|
| 389 |
The lowercased name of the format. |
| 390 |
|
| 391 |
=item * Format Module |
| 392 |
|
| 393 |
The name of the parser module. |
| 394 |
|
| 395 |
=item * Required Module |
| 396 |
|
| 397 |
The name of a module that's required to be installed in order for your parser |
| 398 |
to load. |
| 399 |
|
| 400 |
=item * Extensions |
| 401 |
|
| 402 |
Additional comma-separated values should be a list of file extensions that |
| 403 |
your parser should recognize. |
| 404 |
|
| 405 |
=back |
| 406 |
|
| 407 |
So for our FooBar parser, it might look like this: |
| 408 |
|
| 409 |
markdown,Text::Markup::FooBar,Text::FooBar 0.22,fb,foob,foobar |
| 410 |
|
| 411 |
=item 9 |
| 412 |
|
| 413 |
Test your new parser by running |
| 414 |
|
| 415 |
prove -lv t/formats.t |
| 416 |
|
| 417 |
This will test I<all> included parsers, but of course you should only pay |
| 418 |
attention to how your parser works. Tweak until your tests pass. Note that one |
| 419 |
test has the parser parse a file with just a couple of empty lines, to ensure |
| 420 |
that the parser finds no content and returns C<undef>. |
| 421 |
|
| 422 |
=item 10 |
| 423 |
|
| 424 |
Don't forget to write the documentation in your new parser module! If you |
| 425 |
copied F<Text::Markup::HTML>, you can just modify as appropriate. |
| 426 |
|
| 427 |
=item 11 |
| 428 |
|
| 429 |
Add any new module requirements to the C<requires> section of F<Build.PL>. |
| 430 |
|
| 431 |
=item 12 |
| 432 |
|
| 433 |
Commit and push the branch to your fork on GitHub: |
| 434 |
|
| 435 |
git add . |
| 436 |
git commit -am 'Add great new FooBar parser!' |
| 437 |
git push origin -u foobar |
| 438 |
|
| 439 |
=item 13 |
| 440 |
|
| 441 |
And finally, submit a pull request to the upstream repository via the GitHub |
| 442 |
UI. |
| 443 |
|
| 444 |
=back |
| 445 |
|
| 446 |
If you don't want to submit your parser, you can still create and use one |
| 447 |
independently. Just omit editing the C<%REGEX_FOR> hash in this module and make |
| 448 |
sure you C<register> the parser manually with a default regular expression |
| 449 |
in the C<import> method, like so: |
| 450 |
|
| 451 |
package My::Markup::FooBar; |
| 452 |
use Text::Markup; |
| 453 |
sub import { |
| 454 |
Text::Markup->register( foobar => $_[1] || qr{fb|foob(?:ar)?} ); |
| 455 |
} |
| 456 |
|
| 457 |
This will be useful for creating private parsers you might not want to |
| 458 |
contribute, or that you'd want to distribute independently. |
| 459 |
|
| 460 |
=head1 See Also |
| 461 |
|
| 462 |
=over |
| 463 |
|
| 464 |
=item * |
| 465 |
|
| 466 |
The L<markup|https://github.com/github/markup> Ruby library -- the inspiration |
| 467 |
for this module -- provides similar functionality, and is used to parse |
| 468 |
F<README.your_favorite_markup> on GitHub. |
| 469 |
|
| 470 |
=item * |
| 471 |
|
| 472 |
L<Markup::Unified> offers similar functionality. |
| 473 |
|
| 474 |
=back |
| 475 |
|
| 476 |
=head1 Support |
| 477 |
|
| 478 |
This module is stored in an open |
| 479 |
L<GitHub repository|https://github.com/theory/text-markup/>. Feel free to |
| 480 |
fork and contribute! |
| 481 |
|
| 482 |
Please file bug reports via |
| 483 |
L<GitHub Issues|https://github.com/theory/text-markup/issues/>. |
| 484 |
|
| 485 |
=head1 Author |
| 486 |
|
| 487 |
David E. Wheeler <[email protected]> |
| 488 |
|
| 489 |
=head1 Copyright and License |
| 490 |
|
| 491 |
Copyright (c) 2011-2025 David E. Wheeler. Some Rights Reserved. |
| 492 |
|
| 493 |
This module is free software; you can redistribute it and/or modify it under |
| 494 |
the same terms as Perl itself. |
| 495 |
|
| 496 |
=cut |