| 1 |
package Text::Markup::Bbcode; |
| 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 Parse::BBCode; |
| 9 |
|
| 10 |
our $VERSION = '0.42'; |
| 11 |
|
| 12 |
sub import { |
| 13 |
# Replace the regex if passed one. |
| 14 |
Text::Markup->register( bbcode => $_[1] ) if $_[1]; |
| 15 |
} |
| 16 |
|
| 17 |
sub parser { |
| 18 |
my ($file, $encoding, $opts) = @_; |
| 19 |
my %params = @{ $opts }; |
| 20 |
my $parse = Parse::BBCode->new(\%params); |
| 21 |
open_bom my $fh, $file, ":encoding($encoding)"; |
| 22 |
local $/; |
| 23 |
my $html = $parse->render(<$fh>); |
| 24 |
return unless $html =~ /\S/; |
| 25 |
utf8::encode($html); |
| 26 |
return $html if $params{raw}; |
| 27 |
return qq{<html> |
| 28 |
<head> |
| 29 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 30 |
</head> |
| 31 |
<body> |
| 32 |
$html |
| 33 |
</body> |
| 34 |
</html> |
| 35 |
}; |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
1; |
| 40 |
__END__ |
| 41 |
|
| 42 |
=head1 Name |
| 43 |
|
| 44 |
Text::Markup::Bbcode - BBcode parser for Text::Markup |
| 45 |
|
| 46 |
=head1 Synopsis |
| 47 |
|
| 48 |
my $html = Text::Markup->new->parse(file => 'file.bbcode'); |
| 49 |
my $raw = Text::Markup->new->parse( |
| 50 |
file => 'file.bbcode', |
| 51 |
options => [ raw => 1 ], |
| 52 |
); |
| 53 |
|
| 54 |
=head1 Description |
| 55 |
|
| 56 |
This is the L<BBcode|https://www.bbcode.org/> parser for L<Text::Markup>. It |
| 57 |
reads in the file (relying on a |
| 58 |
L<BOM|https://www.unicode.org/unicode/faq/utf_bom.html#BOM>), hands it off to |
| 59 |
L<Parse::BBCode> for parsing, and then returns the generated HTML as an |
| 60 |
encoded UTF-8 string with an C<http-equiv="Content-Type"> element identifying |
| 61 |
the encoding as UTF-8. |
| 62 |
|
| 63 |
It recognizes files with the following extensions as BBcode: |
| 64 |
|
| 65 |
=over |
| 66 |
|
| 67 |
=item F<.bb> |
| 68 |
|
| 69 |
=item F<.bbcode> |
| 70 |
|
| 71 |
=back |
| 72 |
|
| 73 |
To change it the files it recognizes, load this module directly and pass a |
| 74 |
regular expression matching the desired extension(s), like so: |
| 75 |
|
| 76 |
use Text::Markup::Bbcode qr{beebee}; |
| 77 |
|
| 78 |
Normally this module returns the output wrapped in a minimal HTML document |
| 79 |
skeleton. If you would like the raw output with the raw skeleton, you can pass |
| 80 |
the C<raw> option to C<parse>. |
| 81 |
|
| 82 |
In addition Text::Markup::Bbcode supports all of the |
| 83 |
L<Parse::BBCode options|Parse::BBCode/METHODS>, including: |
| 84 |
|
| 85 |
=over |
| 86 |
|
| 87 |
=item C<tags> |
| 88 |
|
| 89 |
=item C<escapes> |
| 90 |
|
| 91 |
=item C<url_finder> |
| 92 |
|
| 93 |
=item C<smileys> |
| 94 |
|
| 95 |
=item C<linebreaks> |
| 96 |
|
| 97 |
=item C<text_processor> |
| 98 |
|
| 99 |
=item C<close_open_tags> |
| 100 |
|
| 101 |
=item C<strict_attributes> |
| 102 |
|
| 103 |
=item C<direct_attributes> |
| 104 |
|
| 105 |
=item C<attribute_quote> |
| 106 |
|
| 107 |
=item C<attribute_parser> |
| 108 |
|
| 109 |
=item C<strip_linebreaks> |
| 110 |
|
| 111 |
=back |
| 112 |
|
| 113 |
=head1 Author |
| 114 |
|
| 115 |
Lucas Kanashiro <[email protected]> |
| 116 |
|
| 117 |
=head1 Copyright and License |
| 118 |
|
| 119 |
Copyright (c) 2011-2023 Lucas Kanashiro. Some Rights Reserved. |
| 120 |
|
| 121 |
This module is free software; you can redistribute it and/or modify it under |
| 122 |
the same terms as Perl itself. |
| 123 |
|
| 124 |
=cut |