-
Notifications
You must be signed in to change notification settings - Fork 34
/
md2html.pl
executable file
·79 lines (66 loc) · 1.4 KB
/
md2html.pl
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
#!/usr/bin/perl
my $src = "";
my $id = "";
my $title = "";
my $section = "";
my $section_id = "";
my $is_list = 0;
my $topic = "";
my $current_level = 0;
my $prev_level = 0;
$src = $ARGV[0];
$id = $src;
$id =~ s/\.md$//;
open(IN, ${src}) || die "${src}: $!\n";
while (<IN>) {
chomp;
s/</</g;
s/>/>/g;
s/\r$//;
while ($_ =~ /\[.*\]\(.*\)/g) {
$_ =~ s/\[(.*)\]\((.*)\)/<a href="$2">$1<\/a>/;
}
if (/^## Title: (.*)$/) {
$current_level = 2;
${title} = $1;
print "<section id=\"${id}\">\n";
print "<h2>${title}</h2>\n";
print "<dl>\n";
$is_list = 0;
} elsif (/^### (.*)$/) {
$current_level = 3;
${section} = $1;
${section} =~ s/:([\s]+|)$//;
${section_id} = $section;
${section_id} =~ s/\(s\)//;
${section_id} =~ tr/A-Z/a-z/;
${id} =~ tr/A-Z/a-z/;
if ($prev_level >= $current_level) {
print "<\/dd>\n";
}
print "<dt>${section}</dt>\n";
print "<dd>\n";
$prev_level = $current_level;
$is_list = 0;
} elsif (/^#### (.*)$/) {
print "<p>$_</p>\n";
$is_list = 0;
} elsif (/^(\-|\*) (.*)$/) {
${topic} = $2;
if ($is_list eq 0) {
print "<ul>\n";
}
print "<li>${topic}</li>\n";
$is_list = 1;
} else {
if (($is_list eq 1) && (/^$/)) {
print "</ul>\n";
$is_list = 0;
}
print "$_\n";
}
}
print "<\/dd>\n";
print "<\/dl>\n";
print "<\/section>\n";
close(IN);