#.H1 Markdown.awk #.H2 Synopsis #.P awk -f markdown.awk file.txt > file.html #.H2 Download #.P #Download from #.URL http://lawker.googlecode.com/svn/fridge/gawk/text/markdown.awk LAWKER. #.H2 Description #.P # (Note: this code was orginally called txt2html.awk by its author but that caused a name # clash inside LAWKER. Hence, I've taken the liberty of renamining it. --Timm) #.P The following code implements a subset of John Gruber's Markdown langauge: a widely-used, ultra light-weight markup language for html. #.UL #.LI Paragraghs- denoted by a leading blank line. #.LI #Links:
An [example](http://url.com/ "Title")#.LI #Images:
![alt text](/path/img.jpg "Title")#.LI #Emphasis: **To be in italics** #.LI #Code: `<code>` spans are delimited by backticks. #.LI #Headings (Setex style) #.PRE #Level 1 Header #=============== # #Level 2 Header #-------------- # #Level 3 Header #______________ #./PRE #.LI #Heaings (Atx style): #.P Number of leading "#" codes the heading level: #.PRE ## Level 1 Header ##### Level 4 Header #./PRE #.LI Unordered lists #.PRE #- List item 1 #- List item 2 #./PRE #.P Note: beginnging and end of list are automatically inferred, maybe not always correctly. #.LI Ordered lists #.P Denoted by a number at start-of-line. #.PRE #1 A numbered list item #./PRE #./UL #.H2 Code #.P # The following code demonstrates a "exception-style" of Awk programming. Note #how all the processing relating to each mark-up tag is localized (exception, carrying #round prior text and environments). The modularity of the following code should make it #easily hackable. #.H3 Globals #.PRE BEGIN { env = "none"; text = ""; } #./PRE #.H3 Images #.PRE /^!\[.+\] *\(.+\)/ { split($0, a, /\] *\(/); split(a[1], b, /\[/); imgtext = b[2]; split(a[2], b, /\)/); imgaddr = b[1]; print "\n"; text = ""; next; } #./PRE #.H3 Links #.PRE /\] *\(/ { 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); } #./PRE #.H3 Code #.PRE /`/ { while (match($0, /`/) != 0) { if (env == "code") { sub(/`/, ""); env = pcenv; } else { sub(/`/, "
");
pcenv = env;
env = "code";
}
}
}
#./PRE
#.H3 Emphasis
#.PRE
/\*\*/ {
while (match($0, /\*\*/) != 0) {
if (env == "emph") {
sub(//, "");
env = peenv;
}
else {
sub(/\*\*/, "");
peenv = env;
env = "emph";
}
}
}
#./PRE
#.H3 Setex-style Headers
#.P (Plus h3 with underscores.)
#.PRE
/^=+$/ {
print "" text "
\n";
text = "";
next;
}
/^-+$/ {
print "" text "
\n";
text = "";
next;
}
/^_+$/ {
print "" text "
\n";
text = "";
next;
}
#./PRE
#.H3 Atx-style headers
#.PRE
/^#/ {
match($0, /#+/);
n = RLENGTH;
if(n > 6)
n = 6;
print "" substr($0, RLENGTH + 1) " \n";
next;
}
#./PRE
#.H3 Unordered Lists
#.PRE
/^[*-+]/ {
if (env == "none") {
env = "ul";
print "";
}
print "- " substr($0, 3) "
";
text = "";
next;
}
/^[0-9]./ {
if (env == "none") {
env = "ol";
print "";
}
print "- " substr($0, 3) "
";
next;
}
#./PRE
#.H3 Paragraphs
#.PRE
/^[ t]*$/ {
if (env != "none") {
if (text)
print text;
text = "";
print "" env ">\n";
env = "none";
}
if (text)
print "" text "
\n";
text = "";
next;
}
#./PRE
#.H3 Default
#.PRE
// {
text = text $0;
}
#./PRE
#.H3 End
#.PRE
END {
if (env != "none") {
if (text)
print text;
text = "";
print "" env ">\n";
env = "none";
}
if (text)
print "" text "
\n";
text = "";
}
#./PRE
#.H2 Bugs
#.P Does not implement the full Markdown syntax.
#.H2 Author
#.P Jesus Galan (yiyus) 2006
# <yiyu DOT jgl AT gmail DOT com>