| 1 |
package Text::Markup::Rest; |
| 2 |
|
| 3 |
use 5.8.1; |
| 4 |
use strict; |
| 5 |
use warnings; |
| 6 |
use Text::Markup; |
| 7 |
use Text::Markup::Cmd; |
| 8 |
use File::Basename; |
| 9 |
|
| 10 |
our $VERSION = '0.42'; |
| 11 |
|
| 12 |
sub import { |
| 13 |
# Replace the regex if passed one. |
| 14 |
Text::Markup->register( rest => $_[1] ) if $_[1]; |
| 15 |
} |
| 16 |
|
| 17 |
# Find Python or die. |
| 18 |
my $PYTHON = find_cmd( |
| 19 |
[WIN32 ? 'python3.exe' : 'python3'], |
| 20 |
'--version', |
| 21 |
); |
| 22 |
|
| 23 |
# We have python, let's find out if we have docutils. |
| 24 |
exec_or_die( |
| 25 |
qq{Missing required Python "docutils" module}, |
| 26 |
$PYTHON, '-c', 'import docutils', |
| 27 |
); |
| 28 |
|
| 29 |
# We ship with our own rst2html that's lenient with unknown directives. |
| 30 |
my $RST2HTML = File::Spec->catfile(dirname(__FILE__), 'rst2html_lenient.py'); |
| 31 |
|
| 32 |
exec_or_die( |
| 33 |
"$RST2HTML will not execute", |
| 34 |
$PYTHON, $RST2HTML, '--test-patch', |
| 35 |
); |
| 36 |
|
| 37 |
# Optional arguments to pass to rst2html |
| 38 |
my @OPTIONS = qw( |
| 39 |
--no-raw |
| 40 |
--no-file-insertion |
| 41 |
--stylesheet= |
| 42 |
--cloak-email-address |
| 43 |
--no-generator |
| 44 |
--quiet |
| 45 |
); |
| 46 |
|
| 47 |
# Options to improve rendering of Sphinx documents |
| 48 |
my @SPHINX_OPTIONS = qw( |
| 49 |
--dir-ignore toctree |
| 50 |
--dir-ignore highlight |
| 51 |
--dir-ignore index |
| 52 |
--dir-ignore default-domain |
| 53 |
|
| 54 |
--dir-nested note |
| 55 |
--dir-nested warning |
| 56 |
--dir-nested versionadded |
| 57 |
--dir-nested versionchanged |
| 58 |
--dir-nested deprecated |
| 59 |
--dir-nested seealso |
| 60 |
--dir-nested hlist |
| 61 |
--dir-nested glossary |
| 62 |
|
| 63 |
--dir-notitle code-block |
| 64 |
|
| 65 |
--dir-nested module |
| 66 |
--dir-nested function |
| 67 |
--output-encoding utf-8 |
| 68 |
); |
| 69 |
# note: domains directive (last 2 options) incomplete |
| 70 |
|
| 71 |
sub parser { |
| 72 |
my ($file, $encoding, $opts) = @_; |
| 73 |
my $html = do { |
| 74 |
my $fh = open_pipe( |
| 75 |
$PYTHON, $RST2HTML, |
| 76 |
@OPTIONS, @SPHINX_OPTIONS, |
| 77 |
'--input-encoding', $encoding, |
| 78 |
$file |
| 79 |
); |
| 80 |
local $/; |
| 81 |
<$fh>; |
| 82 |
}; |
| 83 |
|
| 84 |
# Make sure we have something. |
| 85 |
return undef if $html =~ m{<div\s+class\s*=\s*(['"])document\1>\s+</div>}ms; |
| 86 |
|
| 87 |
# Alas, --no-generator does not remove the generator meta tag. :-( |
| 88 |
$html =~ s{^\s*<meta\s+name\s*=\s*(['"])generator\1[^>]+>\n}{}ms; |
| 89 |
|
| 90 |
return $html; |
| 91 |
} |
| 92 |
|
| 93 |
1; |
| 94 |
__END__ |
| 95 |
|
| 96 |
=head1 Name |
| 97 |
|
| 98 |
Text::Markup::Rest - reStructuredText parser for Text::Markup |
| 99 |
|
| 100 |
=head1 Synopsis |
| 101 |
|
| 102 |
use Text::Markup; |
| 103 |
my $html = Text::Markup->new->parse(file => 'hello.rst'); |
| 104 |
|
| 105 |
=head1 Description |
| 106 |
|
| 107 |
This is the |
| 108 |
L<reStructuredText|https://docutils.sourceforge.io/rst.html> parser for |
| 109 |
L<Text::Markup>. It depends on the C<docutils> Python package, which can be |
| 110 |
found as C<python3-docutils> in many Linux distributions, or installed using |
| 111 |
the command C<pip install docutils>. It recognizes files with the following |
| 112 |
extensions as reST: |
| 113 |
|
| 114 |
=over |
| 115 |
|
| 116 |
=item F<.rest> |
| 117 |
|
| 118 |
=item F<.rst> |
| 119 |
|
| 120 |
=back |
| 121 |
|
| 122 |
To change it the files it recognizes, load this module directly and pass a |
| 123 |
regular expression matching the desired extension(s), like so: |
| 124 |
|
| 125 |
use Text::Markup::Rest qr{re?st(?:aurant)}; |
| 126 |
|
| 127 |
=head1 Author |
| 128 |
|
| 129 |
Daniele Varrazzo <[email protected]> |
| 130 |
|
| 131 |
=head1 Copyright and License |
| 132 |
|
| 133 |
Copyright (c) 2011-2023 Daniele Varrazzo. Some Rights Reserved. |
| 134 |
|
| 135 |
This module is free software; you can redistribute it and/or modify it under |
| 136 |
the same terms as Perl itself. |
| 137 |
|
| 138 |
=cut |