Browse Source

Markdown.pl: add --stub option

When the --stub option is used, the output is wrapped in a full
HTML document stub.

This makes it much easier to test and validate the output even
if ultimately it will not be used with the --stub option.

Signed-off-by: Kyle J. McKay <mackyle@gmail.com>
master
Kyle J. McKay 8 years ago
parent
commit
3bd54ef41a
  1. 95
      Markdown.pl

95
Markdown.pl

@ -262,6 +262,8 @@ sub _main {
'imageroot|i=s', 'imageroot|i=s',
'tabwidth|tab-width=s', 'tabwidth|tab-width=s',
'stylesheet|style-sheet', 'stylesheet|style-sheet',
'no-stylesheet|no-style-sheet',
'stub',
); );
if ($cli_opts{'help'}) { if ($cli_opts{'help'}) {
pod2usage(-verbose => 2, -exitval => 0); pod2usage(-verbose => 2, -exitval => 0);
@ -279,8 +281,13 @@ sub _main {
print $VERSION; print $VERSION;
exit 0; exit 0;
} }
my $stub = 0;
if ($cli_opts{'stub'}) {
$stub = 1;
}
if ($cli_opts{'html4tags'}) { # Use HTML tag style instead of XHTML if ($cli_opts{'html4tags'}) { # Use HTML tag style instead of XHTML
$options{empty_element_suffix} = ">"; $options{empty_element_suffix} = ">";
$stub = -$stub;
} }
if ($cli_opts{'tabwidth'}) { if ($cli_opts{'tabwidth'}) {
my $tw = $cli_opts{'tabwidth'}; my $tw = $cli_opts{'tabwidth'};
@ -297,16 +304,47 @@ sub _main {
if ($cli_opts{'stylesheet'}) { # Display the style sheet if ($cli_opts{'stylesheet'}) { # Display the style sheet
$options{show_styles} = 1; $options{show_styles} = 1;
} }
if ($cli_opts{'no-stylesheet'}) { # Do not display the style sheet
$options{show_styles} = 0;
}
$options{show_styles} = 1 if $stub && !defined($options{show_styles});
$options{tab_width} = 8 unless defined($options{tab_width}); $options{tab_width} = 8 unless defined($options{tab_width});
#### Show the style sheet if requested my $hdr = sub {
if ($stub > 0) {
print <<'HTML5';
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
HTML5
} elsif ($stub < 0) {
print <<'HTML4';
<html>
<head>
HTML4
}
if ($stub && ($options{title} || $options{h1})) {
my $title = $options{title};
defined($title) && $title ne "" or $title = $options{h1};
if (defined($title) && $title ne "") {
$title =~ s/&/&amp;/g;
$title =~ s/</&lt;/g;
print "<title>$title</title>\n";
}
}
if ($options{show_styles}) { if ($options{show_styles}) {
my $stylesheet = $g_style_sheet; my $stylesheet = $g_style_sheet;
$stylesheet =~ s/%\(base\)/$g_style_prefix/g; $stylesheet =~ s/%\(base\)/$g_style_prefix/g;
print $stylesheet; print $stylesheet;
} }
if ($stub) {
print "</head>\n<body style=\"text-align:center\">\n",
"<div style=\"display:inline-block;text-align:left;max-width:42pc\">\n";
}
};
#### Process incoming text: ########################### #### Process incoming text: ###########################
my $didhdr;
for (;;) { for (;;) {
local $_; local $_;
{ {
@ -314,9 +352,17 @@ sub _main {
$_ = <>; $_ = <>;
} }
defined($_) or last; defined($_) or last;
print Markdown($_, \%options); my $result = Markdown($_, \%options);
if ($result ne "") {
if (!$didhdr) {
&$hdr();
$didhdr = 1;
} }
print $result;
}
}
&$hdr() unless $didhdr;
print "</div>\n</body>\n</html>\n" if $stub;
exit 0; exit 0;
} }
@ -398,6 +444,9 @@ sub Markdown {
$text = _UnescapeSpecialChars($text); $text = _UnescapeSpecialChars($text);
$text .= "\n" unless $text eq ""; $text .= "\n" unless $text eq "";
${$_[0]}{h1} = $opt{h1}
if defined($opt{h1}) && $opt{h1} ne "" && ref($_[0]) eq "HASH";
return $text; return $text;
} }
@ -911,6 +960,15 @@ sub _GetNewAnchorId {
sub _DoHeaders { sub _DoHeaders {
my ($text, $anchors) = @_; my ($text, $anchors) = @_;
my $h1;
my $geth1 = $anchors && !defined($opt{h1}) ? sub {
return unless !defined($h1);
my $h = shift;
$h =~ s/^\s+//;
$h =~ s/\s+$//;
$h =~ s/\s+/ /g;
$h1 = $h if $h ne "";
} : sub {};
# Setext-style headers: # Setext-style headers:
# Header 1 # Header 1
@ -925,6 +983,7 @@ sub _DoHeaders {
$text =~ s{ ^(?:=+[ ]*\n)?(.+)[ ]*\n=+[ ]*\n+ }{ $text =~ s{ ^(?:=+[ ]*\n)?(.+)[ ]*\n=+[ ]*\n+ }{
my $h = $1; my $h = $1;
my $id = _GetNewAnchorId($h); my $id = _GetNewAnchorId($h);
&$geth1($h);
$id = " id=\"$id\"" if $id ne ""; $id = " id=\"$id\"" if $id ne "";
"<h1$id>" . _RunSpanGamut($h) . "</h1>\n\n"; "<h1$id>" . _RunSpanGamut($h) . "</h1>\n\n";
}egmx; }egmx;
@ -962,10 +1021,12 @@ sub _DoHeaders {
my $h = $2; my $h = $2;
my $h_level = length($1); my $h_level = length($1);
my $id = $h_level <= 3 ? _GetNewAnchorId($h) : ''; my $id = $h_level <= 3 ? _GetNewAnchorId($h) : '';
&$geth1($h) if $h_level == 1;
$id = " id=\"$id\"" if $id ne ""; $id = " id=\"$id\"" if $id ne "";
"<h$h_level$id>" . _RunSpanGamut($h) . "</h$h_level>\n\n"; "<h$h_level$id>" . _RunSpanGamut($h) . "</h$h_level>\n\n";
}egmx; }egmx;
$opt{h1} = $h1 if defined($h1) && $h1 ne "";
return $text; return $text;
} }
@ -1945,7 +2006,8 @@ Markdown.pl - convert Markdown format text files to HTML
B<Markdown.pl> [B<--help>] [B<--html4tags>] [B<--htmlroot>=I<prefix>] B<Markdown.pl> [B<--help>] [B<--html4tags>] [B<--htmlroot>=I<prefix>]
[B<--imageroot>=I<prefix>] [B<--version>] [B<--shortversion>] [B<--imageroot>=I<prefix>] [B<--version>] [B<--shortversion>]
[B<--tabwidth>=I<num>] [B<--stylesheet>] [--] [I<file>...] [B<--tabwidth>=I<num>] [B<--stylesheet>] [B<--stub>] [--]
[I<file>...]
Options: Options:
-h show short usage help -h show short usage help
@ -1960,6 +2022,9 @@ B<Markdown.pl> [B<--help>] [B<--html4tags>] [B<--htmlroot>=I<prefix>]
and copyright and copyright
-s | --shortversion show just the version number -s | --shortversion show just the version number
--stylesheet output the fancy style sheet --stylesheet output the fancy style sheet
--no-stylesheet do not output fancy style sheet
--stub wrap output in stub document
implies --stylesheet
-- end options and treat next -- end options and treat next
argument as file argument as file
@ -2035,15 +2100,29 @@ Display the short-form version number.
=item B<--stylesheet> =item B<--stylesheet>
Include the fancy style sheet at the beginning of the output. This style Include the fancy style sheet at the beginning of the output (or in the
sheet makes fancy checkboxes and makes a right parenthesis C<)> show instead C<head> section with B<--stub>). This style sheet makes fancy checkboxes
of a C<.> for ordered lists that use them. Without it things will still look and makes a right parenthesis C<)> show instead of a C<.> for ordered lists
fine except that the fancy stuff won't be there. that use them. 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 Use this option with no other arguments and redirect standard input to
/dev/null to get just the style sheet and nothing else. /dev/null to get just the style sheet and nothing else.
=item B<--no-stylesheet>
Overrides a previous B<--stylesheet> and disables implicit inclusion
of the style sheet by the B<--stub> option.
=item B<--stub>
Wrap the output in a full document stub (i.e. has C<html>, C<head> and C<body>
tags). The style sheet I<will> be included in the C<head> section unless the
B<--no-stylesheet> option is also used.
=item B<-h>, B<--help> =item B<-h>, B<--help>
Display Markdown's help. With B<--help> full help is shown, with B<-h> only Display Markdown's help. With B<--help> full help is shown, with B<-h> only

Loading…
Cancel
Save