Summary Files Commits Branches @commit @diff @stats
path: root / awk-edits / Syntax-highlighting.awk
diff options
context:
space:
mode:
Commit573fedf949116b0965dcdfcc7188c9fbbf580489 (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-03-28 11:48:09 +0300
Committer
 Syndamia <kamen@syndamia.com>
2023-03-28 11:48:09 +0300
Filese4401ace8ee39665980e6a5146f8c9eb40cd904d /awk-edits/Syntax-highlighting.awk
syndamia.com-573fedf949116b0965dcdfcc7188c9fbbf580489.tar
syndamia.com-573fedf949116b0965dcdfcc7188c9fbbf580489.tar.gz
syndamia.com-573fedf949116b0965dcdfcc7188c9fbbf580489.zip
Parente7f4d0924d39e849325bdda49062b4200dc91ab5 (diff)
[Syntax-highlighting] Added awk-edits script for syntax highlighting
1 files changed, 76 insertions, 0 deletions
-rw-r--r--awk-edits/Syntax-highlighting.awk76
diff --git a/awk-edits/Syntax-highlighting.awk b/awk-edits/Syntax-highlighting.awk
new file mode 100644
index 0000000..26720d9
--- /dev/null
+++ b/awk-edits/Syntax-highlighting.awk
@@ -0,0 +1,76 @@
+#
+# == About ==
+#
+# This script wraps certain code block patterns in spans, enabling code syntax highlighting.
+# The styling classes are defined in the bottom of styles.css
+# You'll need to specify the language in markdown, like this
+#
+# ```cpp
+# code here
+# ```
+#
+# where cpp should be your language of choice.
+
+BEGIN {
+ # This variable specifies what language is currently being highlighted.
+ # 0 is none, and >0 is the index/id of the language.
+ syntaxhighlighting=0;
+ grammarNamesI = 0;
+}
+
+#
+# -- Grammars --
+#
+# Code in the form
+# name -> expression | expression | ...
+#
+# Supports grammar and grammar+, the difference being that the first resets
+# what grammer names have been found in predvious grammar code blocks,
+# while the second doesn't.
+# The idea is that you could want to define a grammar in one code block,
+# do some explanations and then continue the definition with other rules.
+#
+
+syntaxhighlighting == 1 || /class="language-grammar(|\+)"/ {
+ if (syntaxhighlighting == 0 && match($0, /class="language-grammar"/))
+ grammarNamesI = 0;
+ syntaxhighlighting = 1;
+
+ # Extract the name of the current grammar in the array grammarNames
+ # Since a grammar name could appear in multiple different rule expressions
+ grammarDef = match($0, /[[:alnum:]]+[[:blank:]]*-&#62/);
+ grammarNames[++grammarNamesI] = substr($0, grammarDef, match($0, /[[:blank:]]+-&#62;[[:blank:]]+/) - grammarDef);
+ # If we couldn't find a name in the current line, or the rule name already exists,
+ # make sure not to "add" it. grammarNamesI acts as an array size, so decrementing it
+ # effectively "removes" the current string.
+ if (grammarDef == 0 || grammarNames[grammarNamesI] == "") grammarNamesI--;
+ else {
+ for (i = 1; i < grammarNamesI; i++) {
+ if (grammarNames[i] == grammarNames[grammarNamesI]) {
+ grammarNamesI--;
+ break;
+ }
+ }
+ }
+
+ for (i = 1; i <= grammarNamesI; i++) {
+ gsub(grammarNames[i], "<span class=\"SH-fnc\">&</span>");
+ }
+ gsub(/[[:blank:]]*[^-\\]-&#62;/, "<span class=\"SH-kwd\">&</span>");
+ gsub(/\|/, "<span class=\"SH-kwd2\">&</span>");
+}
+
+#
+# -- Regexes --
+#
+
+syntaxhighlighting == 2 || /class="language-regex"/ {
+ syntaxhighlighting = 2;
+
+ gsub(/[\|\*]/, "<span class=\"SH-kwd\">&</span>");
+ gsub(/[\(\)]/, "<span class=\"SH-kwd2\">&</span>");
+}
+
+syntaxhighlighting > 0 && /<\/code>/ {
+ syntaxhighlighting = 0;
+}