diff --git a/Markdown.pl b/Markdown.pl index f90b55d..5d5f00c 100755 --- a/Markdown.pl +++ b/Markdown.pl @@ -14,11 +14,18 @@ use strict; use warnings; close(DATA) if fileno(DATA); +require Exporter; use Digest::MD5 qw(md5_hex); -use vars qw($VERSION); +use File::Basename qw(basename); +use vars qw($VERSION @ISA @EXPORT_OK); +@ISA = qw(Exporter); +@EXPORT_OK = qw(Markdown); +$INC{__PACKAGE__.'.pm'} = $INC{basename(__FILE__)} unless exists $INC{__PACKAGE__.'.pm'}; + $VERSION = '1.0.3'; # Sun 06 Sep 2015 + ## Disabled; causes problems under Perl 5.6.1: # use utf8; # binmode( STDOUT, ":utf8" ); # c.f.: http://acis.openlib.org/dev/perl-unicode-struggle.html @@ -28,8 +35,6 @@ $VERSION = '1.0.3'; # Global default settings: # my $g_empty_element_suffix = " />"; # Change to ">" for HTML output -my $g_url_prefix = ""; # Prefixed to non-absolute URLs -my $g_img_prefix = ""; # Prefixed to non-absolute image URLs my $g_tab_width = 4; @@ -62,6 +67,7 @@ foreach my $char (split //, "\\\`*_{}[]()>#+-.!~") { my %g_urls; my %g_titles; my %g_html_blocks; +my %opt; # Used to track when we're inside an ordered or unordered list # (see _ProcessListItems() for details): @@ -191,6 +197,7 @@ elsif (!caller) { use warnings; #### Check for command-line switches: ################# + my %options = (); my %cli_opts; use Getopt::Long; Getopt::Long::Configure('pass_through'); @@ -216,13 +223,13 @@ elsif (!caller) { exit 0; } if ($cli_opts{'html4tags'}) { # Use HTML tag style instead of XHTML - $g_empty_element_suffix = ">"; + $options{empty_element_suffix} = ">"; } if ($cli_opts{'htmlroot'}) { # Use URL prefix - $g_url_prefix = $cli_opts{'htmlroot'}; + $options{url_prefix} = $cli_opts{'htmlroot'}; } if ($cli_opts{'imageroot'}) { # Use image URL prefix - $g_img_prefix = $cli_opts{'imageroot'}; + $options{img_prefix} = $cli_opts{'imageroot'}; } @@ -232,7 +239,7 @@ elsif (!caller) { local $/; # Slurp the whole file $text = <>; } - print Markdown($text); + print Markdown($text, \%options); } } @@ -246,15 +253,35 @@ sub Markdown { # and tags get encoded. # my $text = shift; + defined $text or $text=''; + + # Any remaining arguments after the first are options; either a single + # hashref or a list of name, value paurs. + %opt = ( + # set initial defaults + empty_element_suffix => $g_empty_element_suffix, + tab_width => $g_tab_width, + url_prefix => "", # Prefixed to non-absolute URLs + img_prefix => "", # Prefixed to non-absolute image URLs + ); + my %args = (); + if (ref($_[0]) eq "HASH") { + %args = %{$_[0]}; + } else { + %args = @_; + } + while (my ($k,$v) = each %args) { + $opt{$k} = $v; + } - # Clear the global hashes. If we don't clear these, you get conflicts + # Clear the globals. 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): %g_urls = (); %g_titles = (); %g_html_blocks = (); - + $g_list_level = 0; # Standardize line endings: $text =~ s{\r\n}{\n}g; # DOS to Unix @@ -292,7 +319,7 @@ sub _StripLinkDefinitions { # hash references. # my $text = shift; - my $less_than_tab = $g_tab_width - 1; + my $less_than_tab = $opt{tab_width} - 1; # Link defs are in the form: ^[id]: url "optional title" while ($text =~ s{ @@ -327,7 +354,7 @@ sub _StripLinkDefinitions { sub _HashHTMLBlocks { my $text = shift; - my $less_than_tab = $g_tab_width - 1; + my $less_than_tab = $opt{tab_width} - 1; # Hashify HTML blocks: # We only want to do this for block-level HTML tags, such as headers, @@ -445,9 +472,9 @@ sub _RunBlockGamut { $text = _DoHeaders($text); # Do Horizontal Rules: - $text =~ s{^[ ]{0,2}([ ]?\*[ ]?){3,}[ \t]*$}{\n