drb.awk (5093B)
1 BEGIN { 2 # $1 Book name 3 # $2 Book abbreviation 4 # $3 Book number 5 # $4 Chapter number 6 # $5 Verse number 7 # $6 Verse 8 FS = "\t" 9 10 MAX_WIDTH = 80 11 if (ENVIRON["DRB_MAX_WIDTH"] ~ /^[0-9]+$/) { 12 if (int(ENVIRON["DRB_MAX_WIDTH"]) < MAX_WIDTH) { 13 MAX_WIDTH = int(ENVIRON["DRB_MAX_WIDTH"]) 14 } 15 } 16 17 if (cmd == "ref") { 18 mode = parseref(ref, p) 19 p["book"] = cleanbook(p["book"]) 20 } 21 } 22 23 cmd == "list" { 24 if (!($2 in seen_books)) { 25 printf("%s (%s)\n", $1, $2) 26 seen_books[$2] = 1 27 } 28 } 29 30 function parseref(ref, arr) { 31 # 1. <book> 32 # 2. <book>:?<chapter> 33 # 3. <book>:?<chapter>:<verse> 34 # 3a. <book>:?<chapter>:<verse>[,<verse>]... 35 # 4. <book>:?<chapter>-<chapter> 36 # 5. <book>:?<chapter>:<verse>-<verse> 37 # 6. <book>:?<chapter>:<verse>-<chapter>:<verse> 38 # 7. /<search> 39 # 8. <book>/search 40 # 9. <book>:?<chapter>/search 41 42 if (match(ref, "^[1-9]?[a-zA-Z ]+")) { 43 # 1, 2, 3, 3a, 4, 5, 6, 8, 9 44 arr["book"] = substr(ref, 1, RLENGTH) 45 ref = substr(ref, RLENGTH + 1) 46 } else if (match(ref, "^/")) { 47 # 7 48 arr["search"] = substr(ref, 2) 49 return "search" 50 } else { 51 return "unknown" 52 } 53 54 if (match(ref, "^:?[1-9]+[0-9]*")) { 55 # 2, 3, 3a, 4, 5, 6, 9 56 if (sub("^:", "", ref)) { 57 arr["chapter"] = int(substr(ref, 1, RLENGTH - 1)) 58 ref = substr(ref, RLENGTH) 59 } else { 60 arr["chapter"] = int(substr(ref, 1, RLENGTH)) 61 ref = substr(ref, RLENGTH + 1) 62 } 63 } else if (match(ref, "^/")) { 64 # 8 65 arr["search"] = substr(ref, 2) 66 return "search" 67 } else if (ref == "") { 68 # 1 69 return "exact" 70 } else { 71 return "unknown" 72 } 73 74 if (match(ref, "^:[1-9]+[0-9]*")) { 75 # 3, 3a, 5, 6 76 arr["verse"] = int(substr(ref, 2, RLENGTH - 1)) 77 ref = substr(ref, RLENGTH + 1) 78 } else if (match(ref, "^-[1-9]+[0-9]*$")) { 79 # 4 80 arr["chapter_end"] = int(substr(ref, 2)) 81 return "range" 82 } else if (match(ref, "^/")) { 83 # 9 84 arr["search"] = substr(ref, 2) 85 return "search" 86 } else if (ref == "") { 87 # 2 88 return "exact" 89 } else { 90 return "unknown" 91 } 92 93 if (match(ref, "^-[1-9]+[0-9]*$")) { 94 # 5 95 arr["verse_end"] = int(substr(ref, 2)) 96 return "range" 97 } else if (match(ref, "-[1-9]+[0-9]*")) { 98 # 6 99 arr["chapter_end"] = int(substr(ref, 2, RLENGTH - 1)) 100 ref = substr(ref, RLENGTH + 1) 101 } else if (ref == "") { 102 # 3 103 return "exact" 104 } else if (match(ref, "^,[1-9]+[0-9]*")) { 105 # 3a 106 arr["verse", arr["verse"]] = 1 107 delete arr["verse"] 108 do { 109 arr["verse", substr(ref, 2, RLENGTH - 1)] = 1 110 ref = substr(ref, RLENGTH + 1) 111 } while (match(ref, "^,[1-9]+[0-9]*")) 112 113 if (ref != "") { 114 return "unknown" 115 } 116 117 return "exact_set" 118 } else { 119 return "unknown" 120 } 121 122 if (match(ref, "^:[1-9]+[0-9]*$")) { 123 # 6 124 arr["verse_end"] = int(substr(ref, 2)) 125 return "range_ext" 126 } else { 127 return "unknown" 128 } 129 } 130 131 function cleanbook(book) { 132 book = tolower(book) 133 gsub(" +", "", book) 134 return book 135 } 136 137 function bookmatches(book, bookabbr, query) { 138 book = cleanbook(book) 139 if (book == query) { 140 return book 141 } 142 143 bookabbr = cleanbook(bookabbr) 144 if (bookabbr == query) { 145 return book 146 } 147 148 if (substr(book, 1, length(query)) == query) { 149 return book 150 } 151 } 152 153 function printverse(verse, word_count, characters_printed) { 154 if (ENVIRON["DRB_NOLINEWRAP"] != "" && ENVIRON["DRB_NOLINEWRAP"] != "0") { 155 printf("%s\n", verse) 156 return 157 } 158 159 word_count = split(verse, words, " ") 160 for (i = 1; i <= word_count; i++) { 161 if (characters_printed + length(words[i]) + (characters_printed > 0 ? 1 : 0) > MAX_WIDTH - 8) { 162 printf("\n\t") 163 characters_printed = 0 164 } 165 if (characters_printed > 0) { 166 printf(" ") 167 characters_printed++ 168 } 169 printf("%s", words[i]) 170 characters_printed += length(words[i]) 171 } 172 printf("\n") 173 } 174 175 function processline() { 176 if (last_book_printed != $2) { 177 print $1 178 last_book_printed = $2 179 } 180 181 printf("%d:%d\t", $4, $5) 182 printverse($6) 183 outputted_records++ 184 } 185 186 cmd == "ref" && mode == "exact" && bookmatches($1, $2, p["book"]) && (p["chapter"] == "" || $4 == p["chapter"]) && (p["verse"] == "" || $5 == p["verse"]) { 187 processline() 188 } 189 190 cmd == "ref" && mode == "exact_set" && bookmatches($1, $2, p["book"]) && (p["chapter"] == "" || $4 == p["chapter"]) && p["verse", $5] { 191 processline() 192 } 193 194 cmd == "ref" && mode == "range" && bookmatches($1, $2, p["book"]) && ((p["chapter_end"] == "" && $4 == p["chapter"]) || ($4 >= p["chapter"] && $4 <= p["chapter_end"])) && (p["verse"] == "" || $5 >= p["verse"]) && (p["verse_end"] == "" || $5 <= p["verse_end"]) { 195 processline() 196 } 197 198 cmd == "ref" && mode == "range_ext" && bookmatches($1, $2, p["book"]) && (($4 == p["chapter"] && $5 >= p["verse"] && p["chapter"] != p["chapter_end"]) || ($4 > p["chapter"] && $4 < p["chapter_end"]) || ($4 == p["chapter_end"] && $5 <= p["verse_end"] && p["chapter"] != p["chapter_end"]) || (p["chapter"] == p["chapter_end"] && $4 == p["chapter"] && $5 >= p["verse"] && $5 <= p["verse_end"])) { 199 processline() 200 } 201 202 cmd == "ref" && mode == "search" && (p["book"] == "" || bookmatches($1, $2, p["book"])) && (p["chapter"] == "" || $4 == p["chapter"]) && match(tolower($6), tolower(p["search"])) { 203 processline() 204 } 205 206 END { 207 if (cmd == "ref" && outputted_records == 0) { 208 print "Unknown reference: " ref 209 } 210 }