diff --git a/Markdown.pl b/Markdown.pl index 0625440..2026b22 100755 --- a/Markdown.pl +++ b/Markdown.pl @@ -46,8 +46,9 @@ exit(&_main(@ARGV)||0) unless caller; # # Global default settings: # -my ($g_empty_element_suffix, $g_indent_width, $g_tab_width); +my ($g_style_prefix, $g_empty_element_suffix, $g_indent_width, $g_tab_width); BEGIN { + $g_style_prefix = "_markdown-"; # Prefix for markdown css class styles $g_empty_element_suffix = " />"; # Change to ">" for HTML output $g_indent_width = 4; # Number of spaces considered new level $g_tab_width = 4; # Legacy even though it's wrong @@ -58,6 +59,9 @@ BEGIN { # Globals: # +# Style sheet template +my $g_style_sheet; + # Permanent block id table my %g_perm_block_ids; @@ -257,6 +261,7 @@ sub _main { 'htmlroot|r=s', 'imageroot|i=s', 'tabwidth|tab-width=s', + 'stylesheet|style-sheet', ); if ($cli_opts{'help'}) { pod2usage(-verbose => 2, -exitval => 0); @@ -289,8 +294,17 @@ sub _main { if ($cli_opts{'imageroot'}) { # Use image URL prefix $options{img_prefix} = $cli_opts{'imageroot'}; } + if ($cli_opts{'stylesheet'}) { # Display the style sheet + $options{show_styles} = 1; + } $options{tab_width} = 8 unless defined($options{tab_width}); + #### Show the style sheet if requested + if ($options{show_styles}) { + my $stylesheet = $g_style_sheet; + $stylesheet =~ s/%\(base\)/$g_style_prefix/g; + print $stylesheet; + } #### Process incoming text: ########################### for (;;) { @@ -322,6 +336,7 @@ sub Markdown { # hashref or a list of name, value paurs. %opt = ( # set initial defaults + style_prefix => $g_style_prefix, empty_element_suffix => $g_empty_element_suffix, tab_width => $g_tab_width, indent_width => $g_indent_width, @@ -382,7 +397,8 @@ sub Markdown { $text = _UnescapeSpecialChars($text); - return $text . "\n"; + $text .= "\n" unless $text eq ""; + return $text; } @@ -409,7 +425,8 @@ sub _HashBTCodeBlocks { $codeblock =~ s/\A\n+//; # trim leading newlines $codeblock =~ s/\s+\z//; # trim trailing whitespace $codeblock = _EncodeCode($codeblock); # or run highlighter here - $codeblock = "
" . $codeblock . "\n
"; + $codeblock = "
"
+		. $codeblock . "\n
"; my $key = block_id($codeblock); $g_html_blocks{$key} = $codeblock; @@ -1024,7 +1041,7 @@ sub _DoLists { # Turn double returns into triple returns, so that we can make a # paragraph for the last item in a list, if necessary: $list =~ s/\n\n/\n\n\n/g; - my $result = _ProcessListItems($list, $marker_any); + my $result = _ProcessListItems($list_type, $list, $marker_any); $result = "<$list_type>\n" . $result . "\n"; $result; }egmx; @@ -1050,7 +1067,7 @@ sub _DoLists { # Turn double returns into triple returns, so that we can make a # paragraph for the last item in a list, if necessary: $list =~ s/\n\n/\n\n\n/g; - my $result = _ProcessListItems($list, $marker_any); + my $result = _ProcessListItems($list_type, $list, $marker_any); $result = "<$list_type>\n" . $result . "\n"; $result; }egmx; @@ -1067,6 +1084,7 @@ sub _ProcessListItems { # into individual list items. # + my $list_type = shift; my $list_str = shift; my $marker_any = shift; @@ -1097,18 +1115,33 @@ sub _ProcessListItems { # trim trailing blank lines: $list_str =~ s/\n{2,}\z/\n/; - $list_str =~ s{ (\n)? # leading line = $1 (^[ ]*) # leading whitespace = $2 - ($marker_any) [ ]+ # list marker = $3 - ((?s:.+?) # list item text = $4 - (\n{1,2})) + ($marker_any) [ ] ([ ]*) # list marker = $3 leading item space = $4 + ((?s:.+?) # list item text = $5 + (?:\n{1,2})) (?= \n* (?: \z | \2 $marker_any [ ])) }{ - my $item = $4; + my $item = $5; my $leading_line = $1; my $leading_space = $2; + my $leading_item_space = $4; + my $liclass = ''; + my $checkbox = ''; + + if ($list_type eq "ul" && !$leading_item_space && $item =~ /^\[([ xX])\] +(.*)$/s) { + my $checkmark = lc $1; + $item = $2; + my ($checkbox_class, $checkbox_val); + if ($checkmark eq "x") { + ($checkbox_class, $checkbox_val) = ("checkbox-on", "x"); + } else { + ($checkbox_class, $checkbox_val) = ("checkbox-off", " "); + } + $liclass = " class=\"$opt{style_prefix}$checkbox_class\""; + $checkbox = "[$checkbox_val"; + } if ($leading_line or ($item =~ m/\n{2,}/)) { $item = _RunBlockGamut(_Outdent($item)); @@ -1120,7 +1153,7 @@ sub _ProcessListItems { $item = _RunSpanGamut($item); } - "
  • " . $item . "
  • \n"; + "" . $checkbox . $item . "\n"; }egmx; $g_list_level--; @@ -1153,7 +1186,8 @@ sub _DoCodeBlocks { $codeblock =~ s/\A\n+//; # trim leading newlines $codeblock =~ s/\s+\z//; # trim trailing whitespace - my $result = "
    " . $codeblock . "\n
    "; + my $result = "
    "
    +		. $codeblock . "\n
    "; my $key = block_id($result); $g_code_blocks{$key} = $result; "\n\n" . $key . "\n\n"; @@ -1530,12 +1564,107 @@ sub _PrefixURL { } -1; +BEGIN { + $g_style_sheet = <<'STYLESHEET'; + + -=pod +STYLESHEET + $g_style_sheet =~ s/^\s+//g; + $g_style_sheet =~ s/\s+$//g; + $g_style_sheet .= "\n"; +} + +1; + +__DATA__ =head1 NAME @@ -1545,7 +1674,7 @@ Markdown.pl - convert Markdown format text files to HTML B [B<--help>] [B<--html4tags>] [B<--htmlroot>=I] [B<--imageroot>=I] [B<--version>] [B<--shortversion>] - [B<--tabwidth>=I] [--] [I...] + [B<--tabwidth>=I] [B<--stylesheet>] [--] [I...] Options: -h show short usage help @@ -1559,6 +1688,7 @@ B [B<--help>] [B<--html4tags>] [B<--htmlroot>=I] -V | --version show version, authors, license and copyright -s | --shortversion show just the version number + --stylesheet output the fancy style sheet -- end options and treat next argument as file @@ -1632,6 +1762,16 @@ Display Markdown's version number and copyright information. Display the short-form version number. +=item B<--stylesheet> + +Include the fancy style sheet at the beginning of the output. This style +sheet makes fancy checkboxes and list numbering work. Without it things +will still look fine except that the fancy stuff won't be there. + +Use this option with no other arguments and redirect standard input to +/dev/null to get just the style sheet and nothing else. + + =item B<-h>, B<--help> Display Markdown's help. With B<--help> full help is shown, with B<-h> only