Text::Markup(3pm)
β¨ NAME β¨
Text::Markup β parse text markup into HTML π
(yes bestie, this is the manual page for Text::Markup(3pm), the Perl module that looks at a file, figures out what kind of markup it's written in, and hands you back HTML. it's giving main character energy.)
π SYNOPSIS π
my $parser = Text::Markup->new(
default_format => 'markdown',
default_encoding => 'UTF-8',
);
my $html = $parser->parse(file => $markup_file);
π DESCRIPTION π
okay so this class is genuinely simple. like, uncomplicated. it takes the name of a file and gives you back an HTML-formatted version of that file. periodt.
the whole idea is: you might have files sitting around in a whole bunch of different markup languages, and you do not know β and honestly could not care less β which markup each one is using. so that's the job of this module: figure it out, parse it, and hand you the resulting HTML. π«Ά
this distribution comes with support for a bunch of markup formats:
- Asciidoc β
https://asciidoc.org - BBcode β
https://www.bbcode.org/ - Creole β
https://www.wikicreole.org/ - HTML β
https://whatwg.org/html - Markdown β
https://daringfireball.net/projects/markdown/ - MediaWiki β
https://en.wikipedia.org/wiki/Help:Contents/Editing_Wikipedia - MultiMarkdown β
https://fletcherpenney.net/multimarkdown/ - Pod
- reStructuredText β
https://docutils.sourceforge.io/rst.html - Textile β
https://textile-lang.com/ - Trac β
https://trac.edgewall.org/wiki/WikiFormatting
modules living under the Text::Markup namespace are the ones providing these parsers, and Text::Markup automatically loads them the second it recognizes file name suffixes documented for each module. π
so if you want to change which file extensions get recognized for a particular parser (heads up: this does not apply to Text::Markup::None), just load that parser directly and pass it a regular expression. like, say you wanted the Mediawiki parser to recognize files with the suffixes truck, truc, track, or trac β you'd load it like this:
use Text::Markup::Mediawiki qr{tr[au]ck?};
adding support for even more markup languages is straight-forward, and ngl, patches that add them to this distribution are super welcome. check out "Add a Parser" for the step-by-step.
or, if you're just here to use this module, then read on bestie π
π INTERFACE π
π§ CONSTRUCTOR
new
my $parser = Text::Markup->new(default_format => 'markdown');
here are the parameters it supports:
default_formatβ the default format to use if one isn't passed toparse()and one can't be guessed.default_encodingβ the character encoding in which to assume a file is encoded if it's not otherwise explicitly determined by examination of the source file. defaults toUTF-8.
π CLASS METHODS
register
Text::Markup->register(foobar => qr{fb|foob(?:ar)?});
registers a markup parser. realistically you won't need this method unless you're creating a brand new markup parser and not contributing it back to the Text::Markup project. details are in "Add a Parser".
formats
my @formats = Text::Markup->formats;
returns a list of every format Text::Markup currently recognizes. that includes all the core parsers (except for None) plus any that got loaded somewhere else and called register to register themselves. π
format_matchers
my %matchers = Text::Markup->format_matchers;
returns a list of key/value pairs mapping all the formats returned by formats to the regular expressions used to match them.
π INSTANCE METHODS
parse
my $html = $parser->parse(file => $file_to_parse);
parses a file and returns the generated HTML β or undef if no markup was found in the file. π here's what it supports:
fileβ the file from which to read the markup to be parsed. Required.formatβ the markup format in the file, which determines the parser used to parse it. if you don't specify it,Text::Markupwill try to guess the format from the file's suffix. if it can't guess, it falls back ondefault_format. and if that attribute isn't set either, it uses thenoneparser, which just encodes the entire file and wraps it in a<pre>element.encodingβ the character encoding to assume the source file is encoded in (if that can't be determined by other means, like a BOM βhttps://en.wikipedia.org/wiki/Byte_order_mark). if not specified, the value of thedefault_encodingattribute gets used, and if that attribute isn't set, UTF-8 is assumed.optionsβ an array reference of options for the parser. go read the documentation of the various parser modules for the deets.
default_format
my $format = $parser->default_format;
$parser->default_format('markdown');
an accessor for the default format attribute.
default_encoding
my $encoding = $parser->default_encoding;
$parser->default_encoding('Big5');
an accessor for the default encoding attribute.
guess_format
my $format = $parser->guess_format($filename);
compares the passed file name's suffix against the regular expressions of all registered formatting parsers and returns the first one that matches. returns undef if none of them match. π
π¦ ADD A PARSER π¦
adding support for markup formats that the core Text::Markup distribution doesn't support is a straight-forward exercise, fr fr. say you wanted to add a FooBar markup parser. here are the steps to take:
- Fork this project on GitHub β
https://github.com/theory/text-markup/ - Clone your fork and create a new branch in which to work:
git clone git@github.com:$USER/text-markup.git
cd text-markup
git checkout -b foobar
- Create a new module named
Text::Markup::FooBar. the simplest thing to do is copy an existing module and modify it. the HTML parser is probably the simplest one:
cp lib/Text/Markup/HTML.pm lib/Text/Markup/FooBar.pm
perl -i -pe 's{HTML}{FooBar}g' lib/Text/Markup/FooBar.pm
perl -i -pe 's{html}{foobar}g' lib/Text/Markup/FooBar.pm
- Implement the
parserfunction in your new module. if you were to use aText::FooBarmodule, it might look something like this:
package Text::Markup::FooBar;
use 5.8.1;
use strict;
use Text::FooBar ();
use File::BOM qw(open_bom)
sub import {
# Replace the regex if passed one.
Text::Markup->register( foobar => $_[1] ) if $_[1];
}
sub parser {
my ($file, $encoding, $opts) = @_;
my $md = Text::FooBar->new(@{ $opts || [] });
open_bom my $fh, $file, ":encoding($encoding)";
local $/;
my $html = $md->parse(<$fh>);
return unless $html =~ /\S/;
utf8::encode($html);
return join( "\n",
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />',
'</head>',
'<body>',
$html,
'</body>',
'</html>',
);
}
use the $encoding argument as appropriate to read in the source file. if your parser requires that text be decoded to Perl's internal form, using File::BOM is recommended, so that an explicit BOM will determine the encoding. otherwise, fall back on the specified encoding. heads up tho: some parsers, like an HTML parser, would want text encoded before it parsed it. in that case, read in the file as raw bytes:
open my $fh, '<:raw', $file or die "Cannot open $file: $!\n";
the returned HTML, however, must be encoded in UTF-8. please include an encoding declaration β https://en.wikipedia.org/wiki/Character_encodings_in_HTML β like a content-type <meta> element:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
this will let any consumers of the returned HTML parse it correctly. and if the parser parsed no content, parser() should return undef.
- Edit
lib/Text/Markup.pmand add an entry to its%REGEX_FORhash for your new format. the key should be the name of the format (lowercase, the same as the last part of your module's name). the value should be a regular expression that matches the file extensions that suggest a file is formatted in your parser's markup language. for our FooBar parser, the line might look like this:
foobar => qr{fb|foob(?:ar)?},
- Add a file in your parser's markup language to
t/markups. it should be named for your parser and end in.txt, that is,t/markups/foobar.txt. - Add an HTML file,
t/html/foobar.html, which should be the expected output oncet/markups/foobar.txtis parsed into HTML. this gets used to test that your parser works correctly. β - Edit
t/formats.tby adding a line to its__DATA__section. the line should be a comma-separated list describing your parser. the columns are:- Format β the lowercased name of the format.
- Format Module β the name of the parser module.
- Required Module β the name of a module that's required to be installed in order for your parser to load.
- Extensions β additional comma-separated values should be a list of file extensions that your parser should recognize.
so for our FooBar parser, it might look like this:
markdown,Text::Markup::FooBar,Text::FooBar 0.22,fb,foob,foobar
- Test your new parser by running
prove -lv t/formats.t
this tests all the included parsers, but obviously you should only be paying attention to how your parser works. tweak until your tests pass. note that one test has the parser parse a file with just a couple of empty lines, to make sure the parser finds no content and returns undef.
- Don't forget to write the documentation in your new parser module! if you copied
Text::Markup::HTML, you can just modify as appropriate. π - Add any new module requirements to the
requiressection ofBuild.PL. - Commit and push the branch to your fork on GitHub:
git add .
git commit -am 'Add great new FooBar parser!'
git push origin -u foobar
- And finally, submit a pull request to the upstream repository via the GitHub UI. π
if you don't want to submit your parser, you can still create and use one independently. just skip editing the %REGEX_FOR hash in this module and make sure you register the parser manually with a default regular expression in the import method, like so:
package My::Markup::FooBar;
use Text::Markup;
sub import {
Text::Markup->register( foobar => $_[1] || qr{fb|foob(?:ar)?} );
}
this will be useful for creating private parsers you might not want to contribute, or ones you'd want to distribute independently. π
β¨ See Also β¨
- The
markupRuby library βhttps://github.com/github/markupβ this is literally the inspiration for this module, bestie. It provides similar functionality, and it's used to parseREADME.your_favorite_markupon GitHub. π Markup::Unifiedβ offers similar functionality, so if you vibe with that, go off. π¦
π«Ά Support π«Ά
This module is stored in an open GitHub repository β https://github.com/theory/text-markup/ β feel free to fork and contribute! No gatekeeping here. π
Please file bug reports via GitHub Issues β https://github.com/theory/text-markup/issues/ β and that's the move, periodt. π
π Author π
David E. Wheeler david@justatheory.com β that's the icon, the moment, the whole vibe. β¨
π Copyright and License π
Copyright (c) 2011-2025 David E. Wheeler. Some Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. So yes, it's giving open source, but keep the license intact, bestie. π
This page was brought to you by perl v5.40.1, dated 2025-12-18, and it's Text::Markup(3pm) signing off β periodt. π β¨
π¦ where this page comes from
- ubuntu resolute: libtext-markup-perl 0.41-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtext-markup-perl 0.41-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms