Debian Codegraph @

lib/Text/Markup/Asciidoc.pm

raw
1 package Text::Markup::Asciidoc;
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.42';
11
12 sub import {
13 # Replace the regex if passed one.
14 Text::Markup->register( asciidoc => $_[1] ) if $_[1];
15 }
16
17 my $ASCIIDOC = find_cmd([
18 (map { (WIN32 ? ("$_.exe", "$_.bat") : ($_)) } qw(asciidoc)),
19 'asciidoc.py',
20 ], '--version');
21
22 # Arguments to pass to asciidoc.
23 # Restore --safe if Asciidoc ever fixes it with the XHTML back end.
24 # https://groups.google.com/forum/#!topic/asciidoc/yEr5PqHm4-o
25 my @OPTIONS = qw(
26 --no-header-footer
27 --out-file -
28 --attribute newline=\\n
29 );
30
31 sub parser {
32 my ($file, $encoding, $opts) = @_;
33 my $html = do {
34 my $fh = open_pipe(
35 $ASCIIDOC, @OPTIONS,
36 '--attribute' => "encoding=$encoding",
37 $file
38 );
39
40 binmode $fh, ":encoding($encoding)";
41 local $/;
42 <$fh>;
43 };
44
45 # Make sure we have something.
46 return unless $html =~ /\S/;
47 utf8::encode $html;
48 return $html if { @{ $opts } }->{raw};
49 return qq{<html>
50 <head>
51 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
52 </head>
53 <body>
54 $html
55 </body>
56 </html>
57 };
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;
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 L<C<asciidoc>|https://asciidoc-py.github.io> command-line
80 application. See the L<installation docs|https://asciidoc-py.github.io/INSTALL.html>
81 for details, or use the command C<pip3 install asciidoc>.
82
83 Text::Markup::Asciidoc recognizes files with the following extensions as
84 Asciidoc:
85
86 =over
87
88 =item F<.asciidoc>
89
90 =item F<.asc>
91
92 =item F<.adoc>
93
94 =back
95
96 To change it the files it recognizes, load this module directly and pass a
97 regular expression matching the desired extension(s), like so:
98
99 use Text::Markup::Asciidoc qr{ski?doc};
100
101 Normally this parser returns the output of C<asciidoc> wrapped in a minimal
102 HTML page skeleton. If you would prefer to just get the exact output returned
103 by C<asciidoc>, you can pass in a true value for the C<raw> option.
104
105 =head1 Author
106
107 David E. Wheeler <[email protected]>
108
109 =head1 Copyright and License
110
111 Copyright (c) 2012-2025 David E. Wheeler. Some Rights Reserved.
112
113 This module is free software; you can redistribute it and/or modify it under
114 the same terms as Perl itself.
115
116 =cut