tttml-fmt - tttml - converters for a simpler syntax than markdown HTML git clone git://bitreich.org/tttml git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/tttml DIR Log DIR Files DIR Refs DIR Tags DIR README --- tttml-fmt (2362B) --- 1 #!/usr/bin/awk -f 2 3 function fold(blk, first, prefix) 4 { 5 len = length(prefix); 6 gsub("\t", " ", blk); 7 gsub(" +", " ", blk); 8 sub("^ *", "", blk); sub(" *$", "", blk); 9 gsub("[.!?] ", "& ", blk); 10 11 if (match(blk, /^[0-9.]+ /)) 12 blk = substr(blk, RMATCH, RLENGTH - 1) substr(blk, RLENGTH + 1); 13 14 for (p = first; (line = substr(blk, 1, 80 - len)) != ""; p = prefix) { 15 if (length(line) == 80 - len) 16 sub(" +[^ ]*$", "", line); 17 print(p line); 18 blk = substr(blk, length(line) + 1); 19 sub("^ *", "", blk); 20 } 21 } 22 23 function tag(blk) 24 { 25 match(blk, /^\*[^*]*\*:/); 26 print(substr(blk, 1, RLENGTH)); 27 blk = substr(blk, RLENGTH + 1); 28 fold(blk, "", ""); 29 } 30 31 function link(blk) 32 { 33 match(blk, /^\[[^]]*\]: [^ \t]*/) 34 print(substr(blk, 0, RLENGTH)); 35 fold(substr(blk, RLENGTH + 1), "", ""); 36 } 37 38 function literal() 39 { 40 print(""); 41 do { 42 print($0); 43 } while (getline && match($0, /^\t/)); 44 } 45 46 function title(blk) 47 { 48 fold(blk, "", ""); 49 half = "========================================"; 50 print(half half); 51 } 52 53 function heading(blk) 54 { 55 print("\n"); 56 fold(blk, "", ""); 57 half = "----------------------------------------"; 58 print(half half); 59 } 60 61 function subheading(str) 62 { 63 print("\n\n### " str); 64 } 65 66 function printblk(blk) 67 { 68 if (blk) print(""); 69 70 if (type == PARAGRAPH) fold(blk, "", ""); 71 else if (type == QUOTE) fold(blk, "> ", "> "); 72 else if (type == LIST) fold(blk, "- ", " "); 73 else if (type == TAG) tag(blk); 74 else if (type == LINK) link(blk); 75 } 76 77 BEGIN { 78 PARAGRAPH = 1; QUOTE = 2; LIST = 3; TAG = 4; LINK = 5; 79 } 80 81 # print the append line set type or skip append 82 # last block to current print the to block 83 # right now block or not current block (see the end) 84 85 /^[ \t]*$/ { 86 printblk(blk); blk = $0; type = PARAGRAPH; next; 87 } 88 89 sub(/^[-*] /, "") { 90 printblk(blk); blk = $0; type = LIST; next; 91 } 92 93 /^\[[^]]*\]: / { 94 printblk(blk); blk = $0; type = LINK; next; 95 } 96 97 /^\*[^*]*\*:/ { 98 printblk(blk); blk = $0; type = TAG; next; 99 } 100 101 sub(/^> */, "") { 102 type = QUOTE; 103 } 104 105 /^\t/ { 106 printblk(blk); blk = ""; literal(); next; 107 } 108 109 sub(/^# +/, "") { 110 printblk(blk); blk = ""; title($0); next; 111 } 112 113 sub(/^## +/, "") { 114 printblk(blk); blk = ""; heading($0); next; 115 } 116 117 sub(/^###+ */, "") { 118 printblk(blk); blk = ""; subheading($0); next; 119 } 120 121 /^=+$/ { 122 title(blk); blk = ""; next; 123 } 124 125 /^-+$/ { 126 heading(blk); blk = ""; next; 127 } 128 129 { 130 blk = blk " " $0; 131 } 132 133 END { 134 printblk(blk); 135 }