diff options
[File-lists] Simplified awk code significantly
Though this has slightly increased runtimes
1 files changed, 40 insertions, 72 deletions
| -rw-r--r-- | awk-edits/File-lists.awk | 112 |
diff --git a/awk-edits/File-lists.awk b/awk-edits/File-lists.awk index d3e0725..b952b55 100644 --- a/awk-edits/File-lists.awk +++ b/awk-edits/File-lists.awk @@ -84,84 +84,52 @@ # # &&/files -BEGIN { - fileStarted = 0 - noFolder = 1 - searchEndP = 0 # Used to remove trailing </p> in file row - - # Special formatting - flname[1] = "folder" - flrepl[1] = "<details class=\"file-wrapper\"><summary>" - flname[2] = "file" - flrepl[2] = "</tr><tr>" - flname[3] = "/files" # /files - flrepl[3] = "</tr></table></details>" - flname[4] = "fd" - flrepl[4] = "" # Not used, refer to fileSubstitute function - - flname[5] = "ft-photo" - flrepl[5] = "\\" - flname[6] = "ft-text" - flrepl[6] = "\\" - flname[7] = "ft-zip" - flrepl[7] = "\\" - flname[8] = "ft-movie" - flrepl[8] = "\\" - flname[9] = "ft-markdown" - flrepl[9] = "\\" - - flnamelength = 9 # POSIX compatibility: length(flname) is not valid - - for (i = 1; i <= flnamelength; i++) { - flpat[i] = "&&" flname[i] - if (index(flname[i], "ft") == 1) - flrepl[i] = "<td><div class=\"file-icon\">" flrepl[i] "</div></td>" - } +function flSub(name, repl, isft) { + if (isft) + repl = "<td><div class=\"file-icon\">" repl "</div></td>" + sub("&&" name, repl) + fl_rmPar = 1 } -function fileSubstitute(ind) { - gsub(/(<p>|<\/p>)/, "") +/&&folder/ { + flSub("folder", "<details class=\"file-wrapper\"><summary>") + fl_folder = 1 +} +/&&file/ { + # if inside folder and not the first &&file + if (fl_folder == 2) + flSub("file", "</tr><tr>") - # &&file - if (ind == 2 && fileStarted == 0) { - if (noFolder == 1) - sub("&&file", "<table class=\"file-list\"><tr>") - else - sub("&&file", "</summary><table class=\"file-list\"><tr>") - fileStarted = 1 - } - # &&/files - else if (ind == 3 && noFolder == 1) { - sub("&&/files", "</tr></table>") + # if inside folder and being the first &&file + else if (fl_folder == 1) { + flSub("file", "</summary><table class=\"file-list\"><tr>") + fl_folder = 2 } - # &&fd - else if (ind == 4) { - sub(/&&fd[ \t]*/, "<td>") - $0 = $0 "</td>" - } - else - sub(flpat[ind], flrepl[ind]) - # &&folder - if (ind == 1) { - noFolder = 0 - searchEndP = 1 - } - # &&/files - else if (ind == 3) { - fileStarted = 0 - noFolder = 1 - } + # if not inside folder (standalone) + else + flSub("file", "<table class=\"file-list\"><tr>") } - -/^[ \t]*(<p>|)&&/ { - for (i = 1; i <= flnamelength; i++) { - if (index($0, flpat[i]) != 0) - fileSubstitute(i) +/&&/files/ { + # if inside folder + if (fl_folder) { + flSub("/files", "</tr></table></details>") + fl_folder = 0 } -} -/<\/p>/ && searchEndP == 1 { - sub(/<\/p>/, "") - searchEndP = 0 + else + flSub("/files", "</tr></table>") } +/&&fd/ { + sub(/&&fd[ \t]*/, "<td>") + $0 = $0 "</td>" + fl_rmPar = 1 +} +/&&ft-photo/ { flSub("ft-photo", "\\", 1) } +/&&ft-text/ { flSub("ft-text", "\\", 1) } +/&&ft-zip/ { flSub("ft-zip", "\\", 1) } +/&&ft-movie/ { flSub("ft-movie", "\\", 1) } +/&&ft-markdown/ { flSub("ft-markdown", "\\", 1) } + +/<p>/ && fl_rmPar { sub(/<p>/, "") } +/<\/p>/ && fl_rmPar { sub(/<\/p>/, "") ; fl_rmPar = 0 } |