Debian Codegraph @

lib/Text/Markup/Pod.pm

raw
1 package Text::Markup::Pod;
2
3 use 5.8.1;
4 use strict;
5 use warnings;
6 use Text::Markup;
7 use Pod::Simple::XHTML 3.15;
8
9 sub import {
10 # Replace the regex if passed one.
11 Text::Markup->register( pod => $_[1] ) if $_[1];
12 }
13
14 # Disable the use of HTML::Entities.
15 $Pod::Simple::XHTML::HAS_HTML_ENTITIES = 0;
16
17 our $VERSION = '0.42';
18
19 sub parser {
20 my ($file, $encoding, $opts) = @_;
21 my $p = Pod::Simple::XHTML->new;
22 # Output everything as UTF-8.
23 $p->html_header_tags('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />');
24 $p->strip_verbatim_indent(sub { (sort map { /^(\s+)/ } @{$_[0]})[0] });
25 $p->html_encode_chars('&<>">');
26 $p->output_string(\my $html);
27 # Want user supplied options to override even these default behaviors,
28 # if necessary
29 my $opt = $opts ? { @$opts } : {};
30 foreach my $method ( keys %$opt ) {
31 my $v = $opt->{$method};
32 $p->$method($v);
33 }
34 $p->parse_file($file);
35 return unless $p->content_seen;
36 utf8::encode($html);
37 return $html;
38 }
39
40 1;
41 __END__
42
43 =head1 Name
44
45 Text::Markup::Pod - Pod parser for Text::Markup
46
47 =head1 Synopsis
48
49 use Text::Markup;
50 my $pod = Text::Markup->new->parse(file => 'README.pod');
51
52 =head1 Description
53
54 This is the L<Pod|perlpod> parser for L<Text::Markup>. It runs the file
55 through L<Pod::Simple::XHTML> and returns the result. If the Pod contains any
56 non-ASCII characters, the encoding must be declared either via a BOM or via
57 the C<=encoding> tag. Text::Markup::Pod recognizes files with the following
58 extensions as Pod:
59
60 =over
61
62 =item F<.pod>
63
64 =item F<.pm>
65
66 =item F<.pl>
67
68 =back
69
70 To change it the files it recognizes, load this module directly and pass a
71 regular expression matching the desired extension(s), like so:
72
73 use Text::Markup::Pod qr{cgi};
74
75 =head1 Options
76
77 You may pass an arrayref of settings to this parser which changes the output returned. For example,
78 to suppress an HTML header and footer, pass:
79
80 my $pod_fragment = Text::Markup->new->parse(
81 file => 'README.pod',
82 options => [
83 html_header => '',
84 html_footer => '',
85 ]
86 );
87
88 This implementation makes method calls to the L<Pod::Simple::XHTML> parser using the key as the method
89 name and the value as the parameter list to pass.
90
91 See L<Pod::Simple::XHTML> and L<Pod::Simple> for the full list of options and inherited options
92 which can be manipulated.
93
94 =head1 Author
95
96 David E. Wheeler <[email protected]>
97
98 =head1 Copyright and License
99
100 Copyright (c) 2011-2025 David E. Wheeler. Some Rights Reserved.
101
102 This module is free software; you can redistribute it and/or modify it under
103 the same terms as Perl itself.
104
105 =cut