Skip to content

Commit

Permalink
Add extension registration to the None parser
Browse files Browse the repository at this point in the history
This allows it to be used for text files, for example.
  • Loading branch information
theory committed Feb 15, 2024
1 parent d666284 commit f180587
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Revision history for Perl extension Text-Markup.

0.33
- Added support for registering a regular expression for Text::Markup::None
by passing it in the `use` statement. The None parser otherwise is only
the fallback parser used by `parse()` when Text::Markup cannot determine
which parser to use.

0.32 2024-02-08T03:25:18Z
- Added the ability to change the regular expression for a format by
Expand Down
12 changes: 12 additions & 0 deletions lib/Text/Markup/None.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ use File::BOM qw(open_bom);

our $VERSION = '0.33';

sub import {
# Set a regex if passed one.
Text::Markup->register( none => $_[1] ) if $_[1];
}

sub parser {
my ($file, $encoding, $opts) = @_;
open_bom my $fh, $file, ":encoding($encoding)";
local $/;
my $html = encode_entities(<$fh>, '<>&"');
return undef unless $html =~ /\S/;
utf8::encode($html);
return $html if { @{ $opts } }->{raw};
return qq{<html>
Expand Down Expand Up @@ -52,6 +58,12 @@ entities, and then returns an HTML string with the file in a C<< <pre> >>
element. This will be handy for files that really are nothing but plain text,
like F<README> files.
By default this parser is not associated with any file extensions. To have
Text::Markup also recognize files for this module, load it directly and pass
a regular expression matching the desired extension(s), like so:
use Text::Markup::None qr{te?xt};
Normally this module returns the output wrapped in a minimal HTML document
skeleton. If you would like the raw output without the skeleton, you can pass
the C<raw> option to C<parse>.
Expand Down
14 changes: 11 additions & 3 deletions t/formats.t
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ while (my $data = <DATA>) {
plan tests => @exts + 7;
use_ok $module or next;

push @loaded => $format unless grep { $_ eq $format } @loaded;
is_deeply [Text::Markup->formats], \@loaded,
"$format should be loaded";
if ($format ne 'none') { # Not loaded yet; tested below.
is_deeply [Text::Markup->formats], \@loaded,
"$format should be loaded";
}

my $parser = new_ok 'Text::Markup';
for my $ext (@exts) {
Expand Down Expand Up @@ -103,6 +104,12 @@ while (my $data = <DATA>) {
is $parser->guess_format('hello.txt'), $format,
'Now guess_format should match .txt';
$module->import($regex_for{$format});

if ($format eq 'none') {
@loaded = sort (@loaded, 'none');
is_deeply [Text::Markup->formats], \@loaded,
"$format should be loaded";
}
}
}

Expand All @@ -123,3 +130,4 @@ asciidoc,asciidoc,Text::Markup::Asciidoc,Text::Markup::Asciidoc,asciidoc,asc,ado
asciidoctor,asciidoc,Text::Markup::Asciidoctor,Text::Markup::Asciidoctor,asciidoc,asc,adoc
bbcode,bbcode,Text::Markup::Bbcode,Parse::BBCode,bbcode,bb
creole,creole,Text::Markup::Creole,Text::WikiCreole,creole
none,none,Text::Markup::None,,
17 changes: 17 additions & 0 deletions t/html/none.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<pre>No Format Test File
===================

This file tests the the default parser used if Text::Markup can't figure out the
type of parser to use. Powered by Text::Markup::None. Öy.

func main() {
fmt.Println(&quot;hello&quot;)
}
</pre>
</body>
</html>
9 changes: 9 additions & 0 deletions t/markups/none.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
No Format Test File
===================

This file tests the the default parser used if Text::Markup can't figure out the
type of parser to use. Powered by Text::Markup::None. Öy.

func main() {
fmt.Println("hello")
}

0 comments on commit f180587

Please sign in to comment.