-
Notifications
You must be signed in to change notification settings - Fork 0
/
peg_markdown.pm
94 lines (78 loc) · 2.05 KB
/
peg_markdown.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/perl
package IkiWiki::Plugin::peg_markdown;
use warnings;
use strict;
use IkiWiki 2.00;
use FileHandle;
use IPC::Open2;
sub import {
my $markdown_ext = $config{peg_markdown_ext} || "mdwn";
hook(type => "getsetup", id => "peg_markdown", call => \&getsetup);
hook(type => "htmlize", id => $markdown_ext,
call => sub { htmlize("markdown", @_) });
}
sub getsetup () {
return
plugin => {
safe => 1,
rebuild => 1,
},
peg_markdown_command => {
type => "string",
example => "/usr/local/bin/peg-markdown",
description => "Path to peg-markdown executable",
safe => 0,
rebuild => 0,
},
peg_markdown_ext => {
type => "string",
example => "mdwn",
description => "File extension for Markdown files",
safe => 1,
rebuild => 1,
},
peg_markdown_smart => {
type => "boolean",
example => 1,
description => "Use smart quotes, dashes, and ellipses",
safe => 1,
rebuild => 1,
},
peg_markdown_notes => {
type => "boolean",
example => 1,
description => "Enable peg-markdown footnote extension",
safe => 1,
rebuild => 1,
}
}
sub htmlize ($@) {
my $format = shift;
my %params = @_;
my $page = $params{page};
local(*IN, *OUT);
my @args;
my $command = $config{peg_markdown_command}
|| "/usr/local/bin/peg-markdown";
# Extensions
if ($config{peg_markdown_smart}) {
push @args, '--smart';
}
if ($config{peg_markdown_notes}) {
push @args, '--notes';
}
# Open a bi-directional pipe with peg-markdown
my $pid = open2(*IN, *OUT, $command, @args);
error("Unable to open $command") unless $pid;
# Workaround for perl bug (#376329)
require Encode;
my $content = Encode::encode_utf8($params{content});
print OUT $content;
close OUT;
my @html = <IN>;
close IN;
waitpid $pid, 0;
$content = Encode::decode_utf8(join('', @html));
return $content;
}
1;