Summary Files Commits Branches @commit @diff @stats
path: root / awk-edits / File-lists.awk
diff options
context:
space:
mode:
Commit8ebd7c53abcba01743b9336d10617372dd4e064a (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-09-11 12:23:57 +0300
Committer
 Syndamia <kamen@syndamia.com>
2023-09-11 12:23:57 +0300
Files88bb7db82df9e71101af82b8e0c9416db5f234ff /awk-edits/File-lists.awk
syndamia.com-8ebd7c53abcba01743b9336d10617372dd4e064a.tar
syndamia.com-8ebd7c53abcba01743b9336d10617372dd4e064a.tar.gz
syndamia.com-8ebd7c53abcba01743b9336d10617372dd4e064a.zip
Parent05dc55368b1a8a56d18d9f6bdf96f3c7d933942c (diff)
[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.awk112
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] = "&#47;files" # /files
- flrepl[3] = "</tr></table></details>"
- flname[4] = "fd"
- flrepl[4] = "" # Not used, refer to fileSubstitute function
-
- flname[5] = "ft-photo"
- flrepl[5] = "\\&#xeb0a;"
- flname[6] = "ft-text"
- flrepl[6] = "\\&#xeaa2;"
- flname[7] = "ft-zip"
- flrepl[7] = "\\&#xed4e;"
- flname[8] = "ft-movie"
- flrepl[8] = "\\&#xeafa;"
- flname[9] = "ft-markdown"
- flrepl[9] = "\\&#xec41;"
-
- flnamelength = 9 # POSIX compatibility: length(flname) is not valid
-
- for (i = 1; i <= flnamelength; i++) {
- flpat[i] = "&#38;&#38;" 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("&#38;&#38;" name, repl)
+ fl_rmPar = 1
}
-function fileSubstitute(ind) {
- gsub(/(<p>|<\/p>)/, "")
+/&#38;&#38;folder/ {
+ flSub("folder", "<details class=\"file-wrapper\"><summary>")
+ fl_folder = 1
+}
+/&#38;&#38;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("&#38;&#38;file", "<table class=\"file-list\"><tr>")
- else
- sub("&#38;&#38;file", "</summary><table class=\"file-list\"><tr>")
- fileStarted = 1
- }
- # &&/files
- else if (ind == 3 && noFolder == 1) {
- sub("&#38;&#38;&#47;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(/&#38;&#38;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>|)&#38;&#38;/ {
- for (i = 1; i <= flnamelength; i++) {
- if (index($0, flpat[i]) != 0)
- fileSubstitute(i)
+/&#38;&#38;&#47;files/ {
+ # if inside folder
+ if (fl_folder) {
+ flSub("&#47;files", "</tr></table></details>")
+ fl_folder = 0
}
-}
-/<\/p>/ && searchEndP == 1 {
- sub(/<\/p>/, "")
- searchEndP = 0
+ else
+ flSub("&#47;files", "</tr></table>")
}
+/&#38;&#38;fd/ {
+ sub(/&#38;&#38;fd[ \t]*/, "<td>")
+ $0 = $0 "</td>"
+ fl_rmPar = 1
+}
+/&#38;&#38;ft-photo/ { flSub("ft-photo", "\\&#xeb0a;", 1) }
+/&#38;&#38;ft-text/ { flSub("ft-text", "\\&#xeaa2;", 1) }
+/&#38;&#38;ft-zip/ { flSub("ft-zip", "\\&#xed4e;", 1) }
+/&#38;&#38;ft-movie/ { flSub("ft-movie", "\\&#xeafa;", 1) }
+/&#38;&#38;ft-markdown/ { flSub("ft-markdown", "\\&#xec41;", 1) }
+
+/<p>/ && fl_rmPar { sub(/<p>/, "") }
+/<\/p>/ && fl_rmPar { sub(/<\/p>/, "") ; fl_rmPar = 0 }