package Text::MultiMarkdown;
require 5.008_000;
use strict;
use warnings;
use re 'eval';
use Digest::MD5 qw(md5_hex);
use Encode qw();
use Carp qw(croak);
use base qw(Text::Markdown);
our $VERSION = '1.000034'; # 1.0.34
$VERSION = eval $VERSION;
our @EXPORT_OK = qw(markdown);
=head1 NAME
Text::MultiMarkdown - Convert MultiMarkdown syntax to (X)HTML
=head1 SYNOPSIS
use Text::MultiMarkdown 'markdown';
my $html = markdown($text);
use Text::MultiMarkdown 'markdown';
my $html = markdown( $text, {
empty_element_suffix => '>',
tab_width => 2,
use_wikilinks => 1,
} );
use Text::MultiMarkdown;
my $m = Text::MultiMarkdown->new;
my $html = $m->markdown($text);
use Text::MultiMarkdown;
my $m = Text::MultiMarkdown->new(
empty_element_suffix => '>',
tab_width => 2,
use_wikilinks => 1,
);
my $html = $m->markdown( $text );
=head1 DESCRIPTION
Markdown is a text-to-HTML filter; it translates an easy-to-read /
easy-to-write structured text format into HTML. Markdown's text format
is most similar to that of plain text email, and supports features such
as headers, *emphasis*, code blocks, blockquotes, and links.
Markdown's syntax is designed not as a generic markup language, but
specifically to serve as a front-end to (X)HTML. You can use span-level
HTML tags anywhere in a Markdown document, and you can use block level
HTML tags (C<<
>>, C<<
>> etc.). Note that by default
Markdown isn't interpreted in HTML block-level elements, unless you add
a C attribute to the element. See L for
details.
This module implements the MultiMarkdown markdown syntax extensions from:
http://fletcherpenney.net/multimarkdown/
=head1 SYNTAX
For more information about (original) Markdown's syntax, see:
http://daringfireball.net/projects/markdown/
This module implements MultiMarkdown, which is an extension to Markdown..
The extension is documented at:
http://fletcherpenney.net/multimarkdown/
and borrows from php-markdown, which lives at:
http://michelf.com/projects/php-markdown/extra/
This documentation is going to be moved/copied into this module for clearer reading in a future release..
=head1 OPTIONS
MultiMarkdown supports a number of options to it's processor which control the behaviour of the output document.
These options can be supplied to the constructor, on in a hash with the individual calls to the markdown method.
See the synopsis for examples of both of the above styles.
The options for the processor are:
=over
=item use_metadata
Controls the metadata options below.
=item strip_metadata
If true, any metadata in the input document is removed from the output document (note - does not take effect in complete document format).
=item empty element suffix
This option can be used to generate normal HTML output. By default, it is ' />', which is xHTML, change to '>' for normal HTML.
=item img_ids
Controls if
tags generated have an id attribute. Defaults to true.
Turn off for compatibility with the original markdown.
=item heading_ids
Controls if tags generated have an id attribute. Defaults to true.
Turn off for compatibility with the original markdown.
=item bibliography_title
The title of the generated bibliography, defaults to 'Bibliography'.
=item tab_width
Controls indent width in the generated markup, defaults to 4
=item disable_tables
If true, this disables the MultiMarkdown table handling.
=item disable_footnotes
If true, this disables the MultiMarkdown footnotes handling.
=item disable_bibliography
If true, this disables the MultiMarkdown bibliography/citation handling.
=item disable_definition_lists
If true, this disables the MultiMarkdown definition list handling.
=back
A number of possible items of metadata can also be supplied as options.
Note that if the use_metadata is true then the metadata in the document will overwrite the settings on command line.
Metadata options supported are:
=over
=item document_format
=item use_wikilinks
=item base_url
=back
=head1 METADATA
MultiMarkdown supports the concept of 'metadata', which allows you to specify a number of formatting options
within the document itself. Metadata should be placed in the top few lines of a file, on value per line as colon separated key/value pairs.
The metadata should be separated from the document with a blank line.
Most metadata keys are also supported as options to the constructor, or options
to the markdown method itself. (Note, as metadata, keys contain space, whereas options the keys are underscore separated.)
You can attach arbitrary metadata to a document, which is output in HTML tags if unknown, see t/11document_format.t for more info.
A list of 'known' metadata keys, and their effects are listed below:
=over
=item document format
If set to 'complete', MultiMarkdown will render an entire xHTML page, otherwise it will render a document fragment
=over
=item css
Sets a CSS file for the file, if in 'complete' document format.
=item title
Sets the page title, if in 'complete' document format.
=back
=item use wikilinks
If set to '1' or 'on', causes links that are WikiWords to automatically be processed into links.
=item base url
This is the base URL for referencing wiki pages. In this is not supplied, all wiki links are relative.
=back
=head1 METHODS
=head2 new
A simple constructor, see the SYNTAX and OPTIONS sections for more information.
=cut
sub new {
my ($class, %p) = @_;
# Default metadata to 1
$p{use_metadata} = 1 unless exists $p{use_metadata};
# Squash value to [01]
$p{use_metadata} = $p{use_metadata} ? 1 : 0;
$p{base_url} ||= ''; # This is the base url to be used for WikiLinks
$p{tab_width} = 4 unless (defined $p{tab_width} and $p{tab_width} =~ m/^\d+$/);
$p{document_format} ||= '';
$p{empty_element_suffix} ||= ' />'; # Change to ">" for HTML output
#$p{heading_ids} = defined $p{heading_ids} ? $p{heading_ids} : 1;
# For use with WikiWords and [[Wiki Links]]
# NOTE: You can use \WikiWord to prevent a WikiWord from being treated as a link
$p{use_wikilinks} = $p{use_wikilinks} ? 1 : 0;
$p{heading_ids} = defined $p{heading_ids} ? $p{heading_ids} : 1;
$p{img_ids} = defined $p{img_ids} ? $p{img_ids} : 1;
$p{bibliography_title} ||= 'Bibliography'; # FIXME - Test and document, can also be in metadata!
my $self = { params => \%p };
bless $self, ref($class) || $class;
return $self;
}
=head2 markdown
The main function as far as the outside world is concerned. See the SYNOPSIS
for details on use.
=cut
sub markdown {
my ( $self, $text, $options ) = @_;
# Detect functional mode, and create an instance for this run..
unless (ref $self) {
if ( $self ne __PACKAGE__ ) {
my $ob = __PACKAGE__->new();
# $self is text, $text is options
return $ob->markdown($self, $text);
}
else {
croak('Calling ' . $self . '->markdown (as a class method) is not supported.');
}
}
$options ||= {};
%$self = (%{ $self->{params} }, %$options, params => $self->{params});
$self->_CleanUpRunData($options);
return $self->_Markdown($text);
}
sub _CleanUpRunData {
my ($self, $options) = @_;
# Clear the global hashes. If we don't clear these, you get conflicts
# from other articles when generating a page which contains more than
# one article (e.g. an index page that shows the N most recent
# articles):
$self->{_crossrefs} = {};
$self->{_footnotes} = {};
$self->{_references} = {};
$self->{_used_footnotes} = []; # Why do we need 2 data structures for footnotes? FIXME
$self->{_used_references} = []; # Ditto for references
$self->{_citation_counter} = 0;
$self->{_metadata} = {};
$self->{_attributes} = {}; # Used for extra attributes on links / images.
$self->SUPER::_CleanUpRunData($options);
}
sub _Markdown {
#
# Main function. The order in which other subs are called here is
# essential. Link and image substitutions need to happen before
# _EscapeSpecialChars(), so that any *'s or _'s in the
# and
tags get encoded.
#
# Can't think of any good way to make this inherit from the Markdown version as ordering is so important, so I've left it.
my ($self, $text) = @_;
$text = $self->_CleanUpDoc($text);
# MMD only. Strip out MetaData
$text = $self->_ParseMetaData($text) if ($self->{use_metadata} || $self->{strip_metadata});
# Turn block-level HTML blocks into hash entries
$text = $self->_HashHTMLBlocks($text, {interpret_markdown_on_attribute => 1});
$text = $self->_StripLinkDefinitions($text);
# MMD only
$text = $self->_StripMarkdownReferences($text);
$text = $self->_RunBlockGamut($text, {wrap_in_p_tags => 1});
# MMD Only
$text = $self->_DoMarkdownCitations($text) unless $self->{disable_bibliography};
$text = $self->_DoFootnotes($text) unless $self->{disable_footnotes};
$text = $self->_UnescapeSpecialChars($text);
# MMD Only
# This must follow _UnescapeSpecialChars
$text = $self->_UnescapeWikiWords($text);
$text = $self->_FixFootnoteParagraphs($text) unless $self->{disable_footnotes}; # TODO: remove. Doesn't make any difference to test suite pass/failure
$text .= $self->_PrintFootnotes() unless $self->{disable_footnotes};
$text .= $self->_PrintMarkdownBibliography() unless $self->{disable_bibliography};
$text = $self->_ConvertCopyright($text);
# MMD Only
if (lc($self->{document_format}) =~ /^complete\s*$/) {
return $self->_xhtmlMetaData() . "\n" . $text . "\n\n