Un traducteur md -> html minimal en awk - retour accueil
git clone git://bebou.netlib.re/katdown
Log | Files | Refs | README |
katdown (2960B)
1 #! /usr/bin/awk -f 2 # 3 # fork d'un script de Graham Marlow 4 # https://mgmarlow.com/ 5 6 # HR 7 # <hr> 8 /^---+$/ { print "<hr />"; next } 9 10 # BLOCS DE CODE 11 # <pre> 12 inqpre && /^```/ { flush(); print "</code></pre>"; intqre = 0; next } 13 /^```/ { print "<pre><code>"; inqpre = 1; next } 14 intpre && /^ / { print substr ($0,2); next } 15 intpre && /^[^ ]/ { flush(); print "</code></pre>"; intpre = 0 } 16 !inol && !inul && /^ / { print "<pre><code>"; intpre = 1; print substr ($0,2); next } 17 18 # TITRES 19 # <hN> 20 /^#+ / { l=length($1); printf "<h%d>%s</h%d>\n",l,render(substr($0, l+2)),l; next } 21 22 # LISTES 23 # <ul> 24 !inul && /^ +[-*] / { inul = 1; print "<ul>"; printf "<li>"; for (i=2; i<=NF; i++) collect($i); next } 25 inul && /^ +[-*] / { printf render(line) "</li>\n<li>"; line = sep = ""; for (i=2; i<=NF; i++) collect($i); next } 26 inul && /^$/ { print render(line) "</li>\n</ul>"; line = sep = ""; inul = 0; next } 27 # <ol> 28 !inol && /^ +[0-9]+\. / { inol = 1; print "<ol>"; printf "<li>"; for (i=2; i<=NF; i++) collect($i); next } 29 inol && /^ +[0-9]+\. / { printf render(line) "</li>\n<li>"; line=""; for (i=2; i<=NF; i++) collect($i); next } 30 inol && /^$/ { print render(line) "</li>\n</ol>"; line = sep = ""; inol = 0; next } 31 32 # CITATIONS 33 # <blockquote> 34 /^> / { if (!inquote) print "<blockquote>"; inquote = 1; print render(substr($0, 3)); next } 35 inquote && !/^> / { print "</blockquote>"; inquote = 0; next } 36 37 # PARAGRAPHES 38 # <p> 39 /./ { for (i=1; i<=NF; i++) collect($i) } 40 41 # VIDE 42 /^$/ { flushp() } 43 END { flushp(); flushtags() } 44 45 function collect(v) { 46 line = line sep v 47 sep = " " 48 } 49 50 function flush() { 51 if (line) { 52 print line 53 line = sep = "" 54 } 55 } 56 57 function flushp() { 58 if (line) { 59 if (inul || inol) print render(line) "</li>" 60 else print "<p>" render(line) "</p>" 61 line = sep = "" 62 } 63 } 64 65 function render(line) { 66 while (match(line, /\*\*[^*]+\*\*/)) { 67 sub(/\*\*([^*]+)\*\*/, sprintf("<strong>%s</strong>", substr(line, RSTART+2, RLENGTH-4)), line) 68 } 69 70 while (match(line, /[*_][^*_]+[*_]/)) { 71 sub(/[*_][^*_]+[*_]/, sprintf("<em>%s</em>", substr(line, RSTART+1, RLENGTH-2)), line) 72 } 73 74 while (match(line, /`[^`]+`/)) { 75 sub(/`[^`]+`/, sprintf("<code>%s</code>", substr(line, RSTART+1, RLENGTH-2)), line) 76 } 77 78 while (match(line, /\^[^ ]+( |$)/)) { 79 sub(/\^[^ ]+( |$)/, sprintf("<sup>%s</sup> ", substr(line, RSTART+1, RLENGTH-2)), line) 80 } 81 82 while (match(line, /\[[^]]+\]\([^)]+\)/)) { 83 inner = substr(line, RSTART+1, RLENGTH-2) 84 split(inner, spl, /\]\(/) 85 sub(/\[[^]]+\]\([^)]+\)/, sprintf("<a href=\"%s\">%s</a>", spl[2], spl[1]), line) 86 } 87 88 return line 89 } 90 91 function flushtags() { 92 if (inquote) print "</blockquote>" 93 if (inol) print "</ol>" 94 if (inul) print "</ul>" 95 if (intpre || inqpre) { print "</code></pre>"; intpre=0; inqpre=0 } 96 }