From f18058771d9dff4f889c5ef370e2671ab5fdf01c Mon Sep 17 00:00:00 2001 From: "David E. Wheeler" Date: Thu, 15 Feb 2024 14:57:49 -0500 Subject: [PATCH] Add extension registration to the None parser This allows it to be used for text files, for example. --- Changes | 4 ++++ lib/Text/Markup/None.pm | 12 ++++++++++++ t/formats.t | 14 +++++++++++--- t/html/none.html | 17 +++++++++++++++++ t/markups/none.txt | 9 +++++++++ 5 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 t/html/none.html create mode 100644 t/markups/none.txt diff --git a/Changes b/Changes index 84bc1d7..f847b8f 100644 --- a/Changes +++ b/Changes @@ -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 diff --git a/lib/Text/Markup/None.pm b/lib/Text/Markup/None.pm index f116e85..98566ae 100644 --- a/lib/Text/Markup/None.pm +++ b/lib/Text/Markup/None.pm @@ -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{ @@ -52,6 +58,12 @@ entities, and then returns an HTML string with the file in a C<<
 >>
 element. This will be handy for files that really are nothing but plain text,
 like F 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 option to C.
diff --git a/t/formats.t b/t/formats.t
index 7db5d2a..b6ce485 100644
--- a/t/formats.t
+++ b/t/formats.t
@@ -63,9 +63,10 @@ while (my $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) {
@@ -103,6 +104,12 @@ while (my $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";
+        }
     }
 }
 
@@ -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,,
diff --git a/t/html/none.html b/t/html/none.html
new file mode 100644
index 0000000..6d87abe
--- /dev/null
+++ b/t/html/none.html
@@ -0,0 +1,17 @@
+
+
+
+
+
+
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")
+    }
+
+ + diff --git a/t/markups/none.txt b/t/markups/none.txt new file mode 100644 index 0000000..6b25701 --- /dev/null +++ b/t/markups/none.txt @@ -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") + }