BEGIN {
env = "none";
text = "";
}
/^!\[.+\] *\(.+\)/ {
split($0, a, /\] *\(/);
split(a[1], b, /\[/);
imgtext = b[2];
split(a[2], b, /\)/);
imgaddr = b[1];
print "

\n";
text = "";
next;
}
/\] *\(/ {
do {
na = split($0, a, /\] *\(/);
split(a[1], b, "[");
linktext = b[2];
nc = split(a[2], c, ")");
linkaddr = c[1];
text = text b[1] "" linktext "" c[2];
for(i = 3; i <= nc; i++)
text = text ")" c[i];
for(i = 3; i <= na; i++)
text = text "](" a[i];
$0 = text;;
text = "";
}
while (na > 2);
}
/^=+[ \t]*$/ {
Headers++
print "" text "
\n";
Contents = Contents "\n" ""
text = "";
next;
}
/^-+[ \t]*$/ {
Headers++
print "" text "
\n";
Contents = Contents "\n" ""
text = "";
next;
}
/^__+[ \t]*$/ {
Headers++
print "" text "
\n";
Contents = Contents "\n" ""
text = "";
next;
}
/^\.+[ \t]*$/ {
Headers++
print "" text "
\n";
Contents = Contents "\n" ""
text = "";
next;
}
/^#/ {
match($0, /#+/);
n = RLENGTH;
if(n > 6)
n = 6;
print "" substr($0, RLENGTH + 1) "\n";
next;
}
/`/ {
while (match($0, /`/) != 0) {
if (env == "tt") {
sub(/`/, "");
env = pcenv;
}
else {
sub(/`/, "");
pcenv = env;
env = "tt";
} } }
/_/ {
while (match($0, /_/) != 0) {
if (env == "em") {
sub(/_/, "");
env = peenv;
}
else {
sub(/_/, "");
peenv = env;
env = "em";
} } }
/^[ \t]*[+].*:/ {
if (env == "none") {
env = "dl";
print "";
}
if (text) print text ""
sub(/^[ \t]*[+]/,"");
sub(/:/,"- ");
print "
- " $0 ;
text = "";
next;
}
/^[ \t]*[+]/ {
if (env == "none") {
env = "ul";
print "
";
}
if (text) print text
sub(/^[ \t]*[+]/,"");
print "- " $0 ;
text = "";
next;
}
/^[0-9]./ {
if (env == "none") {
env = "ol";
print "
";
}
print "- " substr($0, 3) "
";
next;
}
/^[ \t]*$/ {
if (env != "none") {
if (text)
print text;
text = "";
print "" env ">\n";
env = "none";
} }
/^[ \t]*$/ {
paraOrPre()
next;
}
// {
text = (text ? text "\n" : "" ) $0;
}
END {
if (env != "none") {
if (text)
print text;
text = "";
print "" env ">\n";
env = "none";
}
paraOrPre()
text = "";
if (Contents)
print "\n" Contents "\n
">Toc
}
function paraOrPre() {
if (text) {
if (text ~ /^[ \t]/)
print "" text "
\n"
else
print "" text "
\n"
}
text = "";
}