Browse Source

Markdown.pl: support strike through with ~~

Signed-off-by: Kyle J. McKay <mackyle@gmail.com>
master
Kyle J. McKay 9 years ago
parent
commit
be20ff3c3a
  1. 30
      Markdown.pl
  2. 4
      basics.text
  3. 17
      syntax.text

30
Markdown.pl

@ -50,7 +50,7 @@ $g_nested_brackets = qr{
# Table of hash values for escaped characters: # Table of hash values for escaped characters:
my %g_escape_table; my %g_escape_table;
foreach my $char (split //, '\\`*_{}[]()>#+-.!') { foreach my $char (split //, '\\`*_{}[]()>#+-.!~') {
$g_escape_table{$char} = md5_hex($char); $g_escape_table{$char} = md5_hex($char);
} }
@ -480,7 +480,7 @@ sub _RunSpanGamut {
$text = _EncodeAmpsAndAngles($text); $text = _EncodeAmpsAndAngles($text);
$text = _DoItalicsAndBold($text); $text = _DoItalicsAndBoldAndStrike($text);
# Do hard breaks: # Do hard breaks:
$text =~ s/ {2,}\n/ <br$g_empty_element_suffix\n/g; $text =~ s/ {2,}\n/ <br$g_empty_element_suffix\n/g;
@ -499,7 +499,7 @@ sub _EscapeSpecialChars {
foreach my $cur_token (@$tokens) { foreach my $cur_token (@$tokens) {
if ($cur_token->[0] eq "tag") { if ($cur_token->[0] eq "tag") {
# Within tags, encode * and _ so they don't conflict # Within tags, encode *, _ and ~ so they don't conflict
# with their use in Markdown for italics and strong. # with their use in Markdown for italics and strong.
# We're replacing each such character with its # We're replacing each such character with its
# corresponding MD5 checksum value; this is likely # corresponding MD5 checksum value; this is likely
@ -507,6 +507,7 @@ sub _EscapeSpecialChars {
# with the escape values by accident. # with the escape values by accident.
$cur_token->[1] =~ s! \* !$g_escape_table{'*'}!gx; $cur_token->[1] =~ s! \* !$g_escape_table{'*'}!gx;
$cur_token->[1] =~ s! _ !$g_escape_table{'_'}!gx; $cur_token->[1] =~ s! _ !$g_escape_table{'_'}!gx;
$cur_token->[1] =~ s! ~ !$g_escape_table{'~'}!gx;
$text .= $cur_token->[1]; $text .= $cur_token->[1];
} else { } else {
my $t = $cur_token->[1]; my $t = $cur_token->[1];
@ -553,12 +554,14 @@ sub _DoAnchors {
if (defined $g_urls{$link_id}) { if (defined $g_urls{$link_id}) {
my $url = $g_urls{$link_id}; my $url = $g_urls{$link_id};
$url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid $url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid
$url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics/bold. $url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics, bold
$url =~ s! ~ !$g_escape_table{'~'}!gx; # and strike through.
$result = "<a href=\"$url\""; $result = "<a href=\"$url\"";
if ( defined $g_titles{$link_id} ) { if ( defined $g_titles{$link_id} ) {
my $title = $g_titles{$link_id}; my $title = $g_titles{$link_id};
$title =~ s! \* !$g_escape_table{'*'}!gx; $title =~ s! \* !$g_escape_table{'*'}!gx;
$title =~ s! _ !$g_escape_table{'_'}!gx; $title =~ s! _ !$g_escape_table{'_'}!gx;
$title =~ s! ~ !$g_escape_table{'~'}!gx;
$result .= " title=\"$title\""; $result .= " title=\"$title\"";
} }
$result .= ">$link_text</a>"; $result .= ">$link_text</a>";
@ -596,13 +599,15 @@ sub _DoAnchors {
my $title = $6; my $title = $6;
$url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid $url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid
$url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics/bold. $url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics, bold
$url =~ s! ~ !$g_escape_table{'~'}!gx; # and strike through.
$result = "<a href=\"$url\""; $result = "<a href=\"$url\"";
if (defined $title) { if (defined $title) {
$title =~ s/"/&quot;/g; $title =~ s/"/&quot;/g;
$title =~ s! \* !$g_escape_table{'*'}!gx; $title =~ s! \* !$g_escape_table{'*'}!gx;
$title =~ s! _ !$g_escape_table{'_'}!gx; $title =~ s! _ !$g_escape_table{'_'}!gx;
$title =~ s! ~ !$g_escape_table{'~'}!gx;
$result .= " title=\"$title\""; $result .= " title=\"$title\"";
} }
@ -652,12 +657,14 @@ sub _DoImages {
if (defined $g_urls{$link_id}) { if (defined $g_urls{$link_id}) {
my $url = $g_urls{$link_id}; my $url = $g_urls{$link_id};
$url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid $url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid
$url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics/bold. $url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics, bold
$url =~ s! ~ !$g_escape_table{'~'}!gx; # and strike through.
$result = "<img src=\"$url\" alt=\"$alt_text\""; $result = "<img src=\"$url\" alt=\"$alt_text\"";
if (defined $g_titles{$link_id}) { if (defined $g_titles{$link_id}) {
my $title = $g_titles{$link_id}; my $title = $g_titles{$link_id};
$title =~ s! \* !$g_escape_table{'*'}!gx; $title =~ s! \* !$g_escape_table{'*'}!gx;
$title =~ s! _ !$g_escape_table{'_'}!gx; $title =~ s! _ !$g_escape_table{'_'}!gx;
$title =~ s! ~ !$g_escape_table{'~'}!gx;
$result .= " title=\"$title\""; $result .= " title=\"$title\"";
} }
$result .= $g_empty_element_suffix; $result .= $g_empty_element_suffix;
@ -704,11 +711,13 @@ sub _DoImages {
$alt_text =~ s/"/&quot;/g; $alt_text =~ s/"/&quot;/g;
$title =~ s/"/&quot;/g; $title =~ s/"/&quot;/g;
$url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid $url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid
$url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics/bold. $url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics, bold
$url =~ s! ~ !$g_escape_table{'~'}!gx; # and strike through.
$result = "<img src=\"$url\" alt=\"$alt_text\""; $result = "<img src=\"$url\" alt=\"$alt_text\"";
if (defined $title) { if (defined $title) {
$title =~ s! \* !$g_escape_table{'*'}!gx; $title =~ s! \* !$g_escape_table{'*'}!gx;
$title =~ s! _ !$g_escape_table{'_'}!gx; $title =~ s! _ !$g_escape_table{'_'}!gx;
$title =~ s! ~ !$g_escape_table{'~'}!gx;
$result .= " title=\"$title\""; $result .= " title=\"$title\"";
} }
$result .= $g_empty_element_suffix; $result .= $g_empty_element_suffix;
@ -1034,6 +1043,7 @@ sub _EncodeCode {
# Now, escape characters that are magic in Markdown: # Now, escape characters that are magic in Markdown:
s! \* !$g_escape_table{'*'}!gx; s! \* !$g_escape_table{'*'}!gx;
s! _ !$g_escape_table{'_'}!gx; s! _ !$g_escape_table{'_'}!gx;
s! ~ !$g_escape_table{'~'}!gx;
s! { !$g_escape_table{'{'}!gx; s! { !$g_escape_table{'{'}!gx;
s! } !$g_escape_table{'}'}!gx; s! } !$g_escape_table{'}'}!gx;
s! \[ !$g_escape_table{'['}!gx; s! \[ !$g_escape_table{'['}!gx;
@ -1044,7 +1054,7 @@ sub _EncodeCode {
} }
sub _DoItalicsAndBold { sub _DoItalicsAndBoldAndStrike {
my $text = shift; my $text = shift;
# <strong> must go first: # <strong> must go first:
@ -1053,6 +1063,9 @@ sub _DoItalicsAndBold {
$text =~ s{ (?<!\w) __ (?=\S) (.+?[*_]*) (?<=\S) __ (?!\w) } $text =~ s{ (?<!\w) __ (?=\S) (.+?[*_]*) (?<=\S) __ (?!\w) }
{<strong>$1</strong>}gsx; {<strong>$1</strong>}gsx;
$text =~ s{ ~~ (?=\S) (.+?[*_]*) (?<=\S) ~~ }
{<strike>$1</strike>}gsx;
$text =~ s{ \* (?=\S) (.+?) (?<=\S) \* } $text =~ s{ \* (?=\S) (.+?) (?<=\S) \* }
{<em>$1</em>}gsx; {<em>$1</em>}gsx;
$text =~ s{ (?<!\w) _ (?=\S) (.+?) (?<=\S) _ (?!\w) } $text =~ s{ (?<!\w) _ (?=\S) (.+?) (?<=\S) _ (?!\w) }
@ -1163,6 +1176,7 @@ sub _EncodeBackslashEscapes {
s! \\` !$g_escape_table{'`'}!gx; s! \\` !$g_escape_table{'`'}!gx;
s! \\\* !$g_escape_table{'*'}!gx; s! \\\* !$g_escape_table{'*'}!gx;
s! \\_ !$g_escape_table{'_'}!gx; s! \\_ !$g_escape_table{'_'}!gx;
s! \\~ !$g_escape_table{'~'}!gx;
s! \\\{ !$g_escape_table{'{'}!gx; s! \\\{ !$g_escape_table{'{'}!gx;
s! \\\} !$g_escape_table{'}'}!gx; s! \\\} !$g_escape_table{'}'}!gx;
s! \\\[ !$g_escape_table{'['}!gx; s! \\\[ !$g_escape_table{'['}!gx;

4
basics.text

@ -116,6 +116,7 @@ Markdown:
Use two asterisks for **strong emphasis**. Use two asterisks for **strong emphasis**.
Or, if you prefer, __use two underscores instead__. Or, if you prefer, __use two underscores instead__.
Or, even, ~~strike through instead~~.
Output: Output:
@ -123,7 +124,8 @@ Output:
Some of these words <em>are emphasized also</em>.</p> Some of these words <em>are emphasized also</em>.</p>
<p>Use two asterisks for <strong>strong emphasis</strong>. <p>Use two asterisks for <strong>strong emphasis</strong>.
Or, if you prefer, <strong>use two underscores instead</strong>.</p> Or, if you prefer, <strong>use two underscores instead</strong>.
Or, even, <strke>strike through instead</strike>.</p>

17
syntax.text

@ -718,7 +718,8 @@ prose.
Markdown treats asterisks (`*`) and underscores (`_`) as indicators of Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
emphasis. Text wrapped with one `*` or `_` will be wrapped with an emphasis. Text wrapped with one `*` or `_` will be wrapped with an
HTML `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML HTML `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML
`<strong>` tag. E.g., this input: `<strong>` tag. Double `~`'s will be wrapped with an HTML `<strike>` tag.
E.g., this input:
*single asterisks* *single asterisks*
@ -728,6 +729,8 @@ HTML `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML
__double underscores__ __double underscores__
~~strike through~~
will produce: will produce:
<em>single asterisks</em> <em>single asterisks</em>
@ -738,18 +741,20 @@ will produce:
<strong>double underscores</strong> <strong>double underscores</strong>
<strike>strike through</strike>
You can use whichever style you prefer; the lone restriction is that You can use whichever style you prefer; the lone restriction is that
the same character must be used to open and close an emphasis span. the same character must be used to open and close an emphasis span.
Additionally `_` and double `_` are not recognized within words. Additionally `_` and double `_` are not recognized within words.
Emphasis using `*` can be used in the middle of a word: Emphasis using `*`'s or `~`'s can be used in the middle of a word:
un*frigging*believable un*frigging*believable fan~~frigging~~tastic
But if you surround an `*` or `_` with spaces, it'll be treated as a But if you surround an `*`, `_` or `~` with spaces, it'll be treated as a
literal asterisk or underscore. literal asterisk, underscore or tilde.
To produce a literal asterisk or underscore at a position where it To produce a literal asterisk, underscore or tilde at a position where it
would otherwise be used as an emphasis delimiter, you can backslash would otherwise be used as an emphasis delimiter, you can backslash
escape it: escape it:

Loading…
Cancel
Save