Debian Codegraph @

lib/Text/Markup/Asciidoctor.pm

raw
1 package Text::Markup::Asciidoctor;
2
3 use 5.8.1;
4 use strict;
5 use warnings;
6 use Text::Markup;
7 use Text::Markup::Cmd;
8 use utf8;
9
10 our $VERSION = '0.41';
11
12 sub import {
13 # Replace Text::Markup::Asciidoc.
14 Text::Markup->register( asciidoc => $_[1] || qr{a(?:sc(?:iidoc)?|doc)?} );
15 }
16
17 # Find Asciidoc.
18 my $ASCIIDOC = find_cmd([
19 (map { (WIN32 ? ("$_.exe", "$_.bat") : ($_)) } qw(asciidoctor)),
20 'asciidoctor.py',
21 ], '--version');
22
23 # Arguments to pass to asciidoc.
24 # Restore --safe if Asciidoc ever fixes it with the XHTML back end.
25 # https://groups.google.com/forum/#!topic/asciidoc/yEr5PqHm4-o
26 my @OPTIONS = qw(
27 --no-header-footer
28 --out-file -
29 --attribute newline=\\n
30 );
31
32 sub parser {
33 my ($file, $encoding, $opts) = @_;
34 my $html = do {
35 my $fh = open_pipe(
36 $ASCIIDOC, @OPTIONS,
37 '--attribute' => "encoding=$encoding",
38 $file
39 );
40
41 binmode $fh, ":encoding($encoding)";
42 local $/;
43 <$fh>;
44 };
45
46 # Make sure we have something.
47 return unless $html =~ /\S/;
48 utf8::encode $html;
49 return $html if { @{ $opts } }->{raw};
50 return qq{<html>
51 <head>
52 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
53 </head>
54 <body>
55 $html
56 </body>
57 </html>
58 };
59 }
60 1;
61 __END__
62
63 =head1 Name
64
65 Text::Markup::Asciidoc - Asciidoc parser for Text::Markup
66
67 =head1 Synopsis
68
69 use Text::Markup::Asciidoctor;
70 my $html = Text::Markup->new->parse(file => 'hello.adoc');
71 my $raw = Text::Markup->new->parse(
72 file => 'hello.adoc',
73 options => [raw => 1],
74 );
75
76 =head1 Description
77
78 This is the L<Asciidoc|https://asciidoc.org/> parser for L<Text::Markup>. It
79 depends on the C<asciidoctor> command-line application; see the
80 L<installation docs|https://asciidoctor.org/#installation> for details, or
81 use the command C<gem install asciidoctor>. Note that L<Text::Markup> does
82 not load this module by default, but when loaded manually will replace
83 Text::Markup::Asciidoc as preferred Asciidoc parser.
84
85 Text::Markup::Asciidoctor reads in the file (relying on a
86 L<BOM|https://www.unicode.org/unicode/faq/utf_bom.html#BOM>), hands it off to
87 L<C<asciidoctor>|https://asciidoctor.org> for parsing, and then returns the
88 generated HTML as an encoded UTF-8 string with an C<http-equiv="Content-Type">
89 element identifying the encoding as UTF-8.
90
91 Text::Markup::Asciidoctor recognizes files with the following extensions as
92 Asciidoc:
93
94 =over
95
96 =item F<.asciidoc>
97
98 =item F<.asc>
99
100 =item F<.adoc>
101
102 =back
103
104 To change it the files it recognizes, load this module directly and pass a
105 regular expression matching the desired extension(s), like so:
106
107 use Text::Markup::AsciiDoctor qr{ski?doc};
108
109 Normally this parser returns the output of C<asciidoctor> wrapped in a minimal
110 HTML page skeleton. If you would prefer to just get the exact output returned
111 by C<asciidoctor>, you can pass in a true value for the C<raw> option.
112
113 =head1 Author
114
115 David E. Wheeler <[email protected]>
116
117 =head1 Copyright and License
118
119 Copyright (c) 2012-2025 David E. Wheeler. 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