Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commitefa4e784bafeb146dd35ea09f2f481aaedc7e69f (patch)
Author
 Syndamia <kamen@syndamia.com>
2024-02-23 21:29:18 +0200
Committer
 Syndamia <kamen@syndamia.com>
2024-02-23 21:29:18 +0200
Filesc5f4a2b91a874ecbb9a31cf5ee1e09a425c7eb30
syndamia.com-efa4e784bafeb146dd35ea09f2f481aaedc7e69f.tar
syndamia.com-efa4e784bafeb146dd35ea09f2f481aaedc7e69f.tar.gz
syndamia.com-efa4e784bafeb146dd35ea09f2f481aaedc7e69f.zip
Parent795d04783d25c0ada4ca3a5a4b56dbc18ff34bd6 (diff)
(awk-edits) Added initial MEL parser
2 files changed, 104 insertions, 0 deletions
-rw-r--r--awk-edits/#stack.awk26
-rw-r--r--awk-edits/MEL-parser.awk78
diff --git a/awk-edits/#stack.awk b/awk-edits/#stack.awk
new file mode 100644
index 0000000..a8b0b2a
--- /dev/null
+++ b/awk-edits/#stack.awk
@@ -0,0 +1,26 @@
+function newStack(name) {
+ __stacks[name, "length"] = 0
+}
+
+function topStack(name) {
+ return __stacks[name, __stacks[name, "length"] - 1]
+}
+
+function lengthStack(name) {
+ return __stacks[name, "length"]
+}
+
+function pushStack(name, value) {
+ __stacks[name, __stacks[name, "length"]++] = value
+}
+
+function popStack(name, value) {
+ delete __stacks[name, --__stacks[name, "length"]]
+}
+
+function deleteStack(name) {
+ for (i = 0; i < __stacks[name, "length"]; i++) {
+ delete __stacks[name, i]
+ }
+ delete __stacks[name, "length"]
+}
diff --git a/awk-edits/MEL-parser.awk b/awk-edits/MEL-parser.awk
new file mode 100644
index 0000000..ebd68bd
--- /dev/null
+++ b/awk-edits/MEL-parser.awk
@@ -0,0 +1,78 @@
+# .*(slides|teaching)/.*html
+#
+# == About ==
+#
+# MEL is the "Markdown Extensions Language"
+# It's a general metalanguage (I made) for adding custom syntax to Markdown (via awk)
+# The idea is it provides parsing for general constructs, and you only need to define
+# the names of those constructs and with what they get replaced with
+#
+# Slightly based on ftags, this system allows you to easily implement the following construts:
+# &(name ... &) Multiline "tag", everything inside is formatted by "name"
+# &{name ... &} In practice, it just replaces "&[name" and "&]" with the appropriate HTML
+# &[name ... &] All three can be used interchangeably, though it's preferrable if
+# you use the matching braces and semantically, in your "language"
+# square brackets are used for the largest components
+# curly for structural components and normal braces for styling
+# &:name Format/encapsulate everything until the end of the paragraph
+# &::name Format/encapsulate everything until the end of the line
+# &.name Insert standalone element as-is
+#
+# Though it's not decided (or implemented) yet, potentially there will also be:
+# &)-( Used to simplify cases of &(name1 ... &) &(name1 ... &) &(name1 ... &)
+# &}-{ into &(name1 ... &)-( ... &)-( ... &)
+# &]-[ Or maybe for special cases, where rather than doing something like
+# &{elementsList &(element1 ... &) &(element2 ... &) &}
+# you could simply do
+# &{elementsList ... &}-{ ... &}-{ ... &}
+# So it will surf as a separator between direct subelements
+
+BEGIN {
+ newStack("mel")
+ MELSINGLE = "s"
+ MELOPEN = "o"
+ MELCLOSE = "c"
+}
+
+/<code>/ {
+ melInsideCode = 1
+}
+
+/<\/code>/ {
+ melInsideCode = 0
+}
+
+melInsideCode == 0 && /^[ \t]*(<p>)?&#38;/ {
+ newLine = ""
+ lineCopy = $0
+ while (match(lineCopy, /&#38;(((::|[[{(:.])[^[:space:].:[{(<>)}\]&]+)|[\]})])/)) {
+ RSTART = RSTART + 5
+ RLENGTH = RLENGTH - 5
+
+ # Inside closing tag
+ if (RLENGTH == 1) {
+ replaceWith = MEL[topStack("mel"), MELCLOSE]
+ popStack("mel")
+ }
+ # Inside opening tag
+ else if (substr(lineCopy, RSTART, 1) ~ /[[{(]/) {
+ name = substr(lineCopy, RSTART + 1, RLENGTH - 1)
+
+ replaceWith = MEL[name, MELOPEN]
+ pushStack("mel", name)
+ }
+ # Inside paragraph/one line/insert
+ # TODO: currently all work as insert
+ else {
+ nameStart = 1 + (substr(lineCopy, RSTART + 1, 1) == ":")
+ name = substr(lineCopy, RSTART + nameStart, RLENGTH - nameStart)
+
+ replaceWith = MEL[name, MELSINGLE]
+ }
+ newLine = newLine substr(lineCopy, 0, RSTART - 6) replaceWith
+ lineCopy = substr(lineCopy, RSTART + RLENGTH)
+ }
+ $0 = newLine lineCopy
+ if (length(newLine) > 0)
+ gsub(/(<p>|<\/p>)/, "")
+}