#txt2html.awk #© Jesus Galan (yiyus) 2006 # #Usage: awk -f txt2html.awk file.txt > file.html BEGIN { env = "none"; text = ""; } # images /^!\[.+\] *\(.+\)/ { split($0, a, /\] *\(/); split(a[1], b, /\[/); imgtext = b[2]; split(a[2], b, /\)/); imgaddr = b[1]; print "

\""

\n"; text = ""; next; } # links /\] *\(/ { 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); } # code /`/ { while (match($0, /`/) != 0) { if (env == "code") { sub(/`/, ""); env = pcenv; } else { sub(/`/, ""); pcenv = env; env = "code"; } } } # emph /\*\*/ { while (match($0, /\*\*/) != 0) { if (env == "emph") { sub(//, ""); env = peenv; } else { sub(/\*\*/, ""); peenv = env; env = "emph"; } } } # setex-style headers (plus h3 with underscores) /^=+$/ { print "

" text "

\n"; text = ""; next; } /^-+$/ { print "

" text "

\n"; text = ""; next; } /^_+$/ { print "

" text "

\n"; text = ""; next; } # atx-style headers /^#/ { match($0, /#+/); n = RLENGTH; if(n > 6) n = 6; print "" substr($0, RLENGTH + 1) "\n"; next; } # unordered lists /^[*-+]/ { if (env == "none") { env = "ul"; print "
    "; } print "
  • " substr($0, 3) "
  • "; text = ""; next; } /^[0-9]./ { if (env == "none") { env = "ol"; print "
      "; } print "
    1. " substr($0, 3) "
    2. "; next; } # paragraph /^$/ { if (env != "none") { if (text) print text; text = ""; print "\n"; env = "none"; } if (text) print "

      " text "

      \n"; text = ""; next; } # default // { text = text $0; } END { }