Debian Codegraph @

lib/Text/Markup/None.pm

raw
1 package Text::Markup::None;
2
3 use 5.8.1;
4 use strict;
5 use warnings;
6 use Text::Markup;
7 use HTML::Entities;
8 use File::BOM qw(open_bom);
9
10 our $VERSION = '0.41';
11
12 sub import {
13 # Set a regex if passed one.
14 Text::Markup->register( none => $_[1] ) if $_[1];
15 }
16
17 sub parser {
18 my ($file, $encoding, $opts) = @_;
19 open_bom my $fh, $file, ":encoding($encoding)";
20 local $/;
21 my $html = encode_entities(<$fh>, '<>&"');
22 return undef unless $html =~ /\S/;
23 utf8::encode($html);
24 return $html if { @{ $opts } }->{raw};
25 return qq{<html>
26 <head>
27 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
28 </head>
29 <body>
30 <pre>$html</pre>
31 </body>
32 </html>
33 };
34 }
35
36 1;
37 __END__
38
39 =head1 Name
40
41 Text::Markup::None - Turn a file with no known markup into HTML
42
43 =head1 Synopsis
44
45 use Text::Markup;
46 my $html = Text::Markup->new->parse(file => 'README');
47 my $raw = Text::Markup->new->parse(
48 file => 'README',
49 options => [ raw => 1 ],
50 );
51
52 =head1 Description
53
54 This is the default parser used by L<Text::Markup> in the event that it cannot
55 determine the format of a text file. All it does is read the file in (relying
56 on a L<BOM|https://www.unicode.org/unicode/faq/utf_bom.html#BOM>, encodes all
57 entities, and then returns an HTML string with the file in a C<< <pre> >>
58 element. This will be handy for files that really are nothing but plain text,
59 like F<README> files.
60
61 By default this parser is not associated with any file extensions. To have
62 Text::Markup also recognize files for this module, load it directly and pass
63 a regular expression matching the desired extension(s), like so:
64
65 use Text::Markup::None qr{te?xt};
66
67 Normally this module returns the output wrapped in a minimal HTML document
68 skeleton. If you would like the raw output without the skeleton, you can pass
69 the C<raw> option to C<parse>.
70
71 =head1 Author
72
73 David E. Wheeler <[email protected]>
74
75 =head1 Copyright and License
76
77 Copyright (c) 2011-2025 David E. Wheeler. Some Rights Reserved.
78
79 This module is free software; you can redistribute it and/or modify it under
80 the same terms as Perl itself.
81
82 =cut