summaryrefslogtreecommitdiff
path: root/lib/VNDB/Func.pm
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2020-01-24 16:29:51 +0100
committerYorhel <git@yorhel.nl>2020-02-06 11:02:58 +0100
commitedd812bc90f9111b598efc9f4c25e26801767b20 (patch)
tree266886c3e212066e6851e2d189eb53e0e06fea16 /lib/VNDB/Func.pm
parentbc9bf70662fe35bcb4c6a89965d3b4f06bbbe783 (diff)
Cache Markdown-to-HTML in the database for doc pages
Strips about 500ms off the page generation time for d11. The previous in-memory cache kinda worked, too, but it still often happens that the cache is empty. This cache also applies for docs_hist, so browsing through earlier revisions is now faster as well.
Diffstat (limited to 'lib/VNDB/Func.pm')
-rw-r--r--lib/VNDB/Func.pm31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/VNDB/Func.pm b/lib/VNDB/Func.pm
index 94d1281e..2a169552 100644
--- a/lib/VNDB/Func.pm
+++ b/lib/VNDB/Func.pm
@@ -18,6 +18,7 @@ our @EXPORT = (@VNDBUtil::EXPORT, 'bb2html', 'bb2text', qw|
json_encode json_decode script_json
form_compare
query_encode
+ md2html
|);
@@ -317,5 +318,35 @@ sub query_encode {
} sort keys %$o;
}
+
+sub md2html {
+ require Text::MultiMarkdown;
+ my $html = Text::MultiMarkdown::markdown(shift, {
+ strip_metadata => 1,
+ img_ids => 0,
+ disable_footnotes => 1,
+ disable_bibliography => 1,
+ });
+
+ # Number sections and turn them into links
+ my($sec, $subsec) = (0,0);
+ $html =~ s{<h([1-2])[^>]+>(.*?)</h\1>}{
+ if($1 == 1) {
+ $sec++;
+ $subsec = 0;
+ qq{<h3><a href="#$sec" name="$sec">$sec. $2</a></h3>}
+ } elsif($1 == 2) {
+ $subsec++;
+ qq|<h4><a href="#$sec.$subsec" name="$sec.$subsec">$sec.$subsec. $2</a></h4>\n|
+ }
+ }ge;
+
+ # Text::MultiMarkdown doesn't handle fenced code blocks properly. The
+ # following solution breaks inline code blocks, but I don't use those anyway.
+ $html =~ s/<code>/<pre>/g;
+ $html =~ s#</code>#</pre>#g;
+ $html
+}
+
1;