forked from github/kensanata.oddmuse
markdown-rule.pl: improving the regexps
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#! /usr/bin/perl
|
||||
# Copyright (C) 2014 Alex Schroeder <alex@gnu.org>
|
||||
# Copyright (C) 2014–2-17 Alex Schroeder <alex@gnu.org>
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify it under
|
||||
# the terms of the GNU General Public License as published by the Free Software
|
||||
@@ -18,7 +18,7 @@ use v5.10;
|
||||
|
||||
AddModuleDescription('markdown-rule.pl', 'Markdown Rule Extension');
|
||||
|
||||
our ($q, $bol, %RuleOrder, @MyRules, $UrlProtocols, $FullUrlPattern);
|
||||
our ($q, $bol, %RuleOrder, @MyRules, $UrlProtocols, $FullUrlPattern, @HtmlStack);
|
||||
|
||||
push(@MyRules, \&MarkdownRule);
|
||||
# Since we want this package to be a simple add-on, we try and avoid
|
||||
@@ -31,8 +31,12 @@ $RuleOrder{\&MarkdownRule} = 200;
|
||||
# https://help.github.com/articles/github-flavored-markdown
|
||||
|
||||
sub MarkdownRule {
|
||||
# \escape
|
||||
if (m/\G\\([-#>*`=])/cg) {
|
||||
return $1;
|
||||
}
|
||||
# atx headers
|
||||
if ($bol and m~\G(\s*\n)*(#{1,6})[ \t]*~cg) {
|
||||
elsif ($bol and m~\G(\s*\n)*(#{1,6})[ \t]*~cg) {
|
||||
my $header_depth = length($2);
|
||||
return CloseHtmlEnvironments()
|
||||
. AddHtmlEnvironment("h" . $header_depth);
|
||||
@@ -56,6 +60,15 @@ sub MarkdownRule {
|
||||
return CloseHtmlEnvironments()
|
||||
. AddHtmlEnvironment('blockquote');
|
||||
}
|
||||
# ``` = code
|
||||
elsif ($bol and m/\G```[ \t]*\n(.*?)\n```[ \t]*(\n|$)/cgs) {
|
||||
return CloseHtmlEnvironments() . $q->pre($1)
|
||||
. AddHtmlEnvironment("p");
|
||||
}
|
||||
# ` = code
|
||||
elsif (m/\G`([^`].*?)`/cg) {
|
||||
return $q->code($1);
|
||||
}
|
||||
# ***bold and italic***
|
||||
elsif (not InElement('strong') and not InElement('em') and m/\G\*\*\*/cg) {
|
||||
return AddHtmlEnvironment('em') . AddHtmlEnvironment('strong');
|
||||
@@ -65,8 +78,11 @@ sub MarkdownRule {
|
||||
return AddOrCloseHtmlEnvironment('strong');
|
||||
}
|
||||
# *italic*
|
||||
elsif (m/\G\*/cg) {
|
||||
return AddOrCloseHtmlEnvironment('em');
|
||||
elsif ($bol and m/\G\*/cg or m/\G(?<=\s)\*/cg) {
|
||||
return AddHtmlEnvironment('em');
|
||||
}
|
||||
elsif (InElement('em') and m/\G\*/cg) {
|
||||
return CloseHtmlEnvironment('em');
|
||||
}
|
||||
# ~~strikethrough~~ (deleted)
|
||||
elsif (m/\G~~/cg) {
|
||||
@@ -124,11 +140,6 @@ sub MarkdownRule {
|
||||
}
|
||||
return OpenHtmlEnvironment('pre',1) . $str; # always level 1
|
||||
}
|
||||
# ``` = code
|
||||
elsif ($bol and m/\G```[ \t]*\n(.*?)\n```[ \t]*(\n|$)/cgs) {
|
||||
return CloseHtmlEnvironments() . $q->pre($1)
|
||||
. AddHtmlEnvironment("p");
|
||||
}
|
||||
# [an example](http://example.com/ "Title")
|
||||
elsif (m/\G\[(.+?)\]\($FullUrlPattern(\s+"(.+?)")?\)/cg) {
|
||||
my ($text, $url, $title) = ($1, $2, $4);
|
||||
@@ -141,3 +152,28 @@ sub MarkdownRule {
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
push(@MyRules, \&MarkdownExtraRule);
|
||||
|
||||
sub MarkdownExtraRule {
|
||||
# __italic underline__
|
||||
if (m/\G__/cg) {
|
||||
return AddOrCloseHtmlEnvironment('em', 'style="font-style: italic; text-decoration: underline"');
|
||||
}
|
||||
# _underline_
|
||||
elsif (m/\G_/cg) {
|
||||
return AddOrCloseHtmlEnvironment('em', 'style="font-style: normal; text-decoration: underline";');
|
||||
}
|
||||
# //italic//
|
||||
elsif (m/\G\/\//cg) {
|
||||
return AddOrCloseHtmlEnvironment('em');
|
||||
}
|
||||
# /italic/
|
||||
elsif ($bol and m/\G\//cg or m/\G(?<=\s)\//cg) {
|
||||
return AddHtmlEnvironment('em');
|
||||
}
|
||||
elsif (InElement('em') and m/\G\//cg) {
|
||||
return CloseHtmlEnvironment('em');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env perl
|
||||
# Copyright (C) 2014 Alex Schroeder <alex@gnu.org>
|
||||
# Copyright (C) 2014–2017 Alex Schroeder <alex@gnu.org>
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
require 't/test.pl';
|
||||
package OddMuse;
|
||||
use Test::More tests => 34;
|
||||
use Test::More tests => 35;
|
||||
|
||||
add_module('markdown-rule.pl');
|
||||
|
||||
@@ -78,6 +78,8 @@ bar <h2>foo</h2><p>bar</p>
|
||||
<pre>foo</pre>
|
||||
```\nfoo\n``` bar
|
||||
``` foo ``` bar
|
||||
`bar`
|
||||
<code>bar</code>
|
||||
|a|b|\n|c|d|\nbar
|
||||
<table><tr><th>a</th><th>b</th></tr><tr><td>c</td><td>d</td></tr></table><p>bar</p>
|
||||
|a|b|\n|c|d|
|
||||
|
||||
Reference in New Issue
Block a user