Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit8ae9ca1c57b5f183ae8e98df3310346aadbc8ddf (patch)
Author
 Kamen Mladenov <kamen@syndamia.com>
2023-04-29 12:29:43 +0000
Committer
 Kamen Mladenov <kamen@syndamia.com>
2023-04-29 12:29:43 +0000
Files07fecb5c85a57195503057f099e193ed2fd3ed39
syndamia.com-8ae9ca1c57b5f183ae8e98df3310346aadbc8ddf.tar
syndamia.com-8ae9ca1c57b5f183ae8e98df3310346aadbc8ddf.tar.gz
syndamia.com-8ae9ca1c57b5f183ae8e98df3310346aadbc8ddf.zip
Parent5b2ac851b21fcb2a9f7c6cff47637be184881902 (diff)
Parenta9f641a6ec9d70ab4d78f6280ede0d5e64c3d50c (diff)
Merge branch 'better-syntax-highlighting' into 'master'
Reworked code highlighting See merge request Syndamia/syndamiadotcom!8
4 files changed, 265 insertions, 86 deletions
-rw-r--r--awk-edits/#strings.awk40
-rw-r--r--awk-edits/Syntax-highlighting.awk166
-rw-r--r--src/darkmode.css46
-rw-r--r--src/styles.css99
diff --git a/awk-edits/#strings.awk b/awk-edits/#strings.awk
index a34757e..3365df2 100644
--- a/awk-edits/#strings.awk
+++ b/awk-edits/#strings.awk
@@ -43,3 +43,43 @@ function gesub(regexp, exregexp, replacement, target) {
return result target;
}
+
+# glsub(lbregexp, regexp, largexp, replacement [, target])
+# Aka global sub with non-capturing look-around (lookbehind and lookahead)
+# Behaves kind of like gsub, however substitution of regexp with replacement
+# only happens, when regexp is preceded by lbregexp and succeeded by laregexp.
+# Only regexp get's replaced, the matches of lbregexp and laregexp are left
+# as-is. Overlapping of any form isn't supported.
+# This function is quite slow, try to use as little as possible.
+# Returns the result string, rather than doing it inplace.
+function glsub(lbregexp, regexp, laregexp, replacement, target) {
+ if (!length(target)) {
+ target = $0;
+ }
+
+ surlen = split(target, surrounds, lbregexp regexp laregexp);
+ result = surrounds[1];
+ # POSIX compliance, seps parameter in split isn't available
+ target = substr(target, length(surrounds[1]) + 1);
+ for (i = 2; i <= surlen; i++) {
+ if (surrounds[i] == "") {
+ match(target, lbregexp regexp laregexp);
+ si = RLENGTH + 1;
+ }
+ else {
+ si = index(target, surrounds[i]);
+ }
+ csep = substr(target, 1, si - 1);
+ target = substr(target, length(surrounds[i]) + si);
+
+ match(csep, regexp);
+ behind = substr(csep, 1, RSTART - 1);
+ ahead = substr(csep, RSTART + RLENGTH);
+ csep = substr(csep, RSTART, RLENGTH);
+
+ sub(/.*/, replacement, csep);
+ result = result behind csep ahead surrounds[i];
+ }
+
+ return result;
+}
diff --git a/awk-edits/Syntax-highlighting.awk b/awk-edits/Syntax-highlighting.awk
index e3a9be2..0e5250e 100644
--- a/awk-edits/Syntax-highlighting.awk
+++ b/awk-edits/Syntax-highlighting.awk
@@ -17,6 +17,24 @@ BEGIN {
syntaxhighlighting=0;
}
+function hi(pat, class) {
+ gsub(pat, "<span class=\"HI-" class "\">&</span>");
+}
+function hie(pat, epat, class) {
+ return gesub(pat, epat, "<span class=\"HI-" class "\">&</span>");
+}
+function hil(lbpat, pat, lrpat, class) {
+ return glsub(lbpat, pat, lrpat, "<span class=\"HI-" class "\">&</span>");
+}
+
+#
+# -- Global --
+#
+
+syntaxhighlighting > 0 || /class="language-/ {
+ $0 = gesub(@/=/, "<[^>]*>", "\\&#61;");
+}
+
#
# -- Grammars --
#
@@ -58,10 +76,10 @@ syntaxhighlighting == 1 {
}
for (i = 1; i <= grammarNamesI; i++) {
- $0 = gesub(grammarNames[i], "<span [^>]*>[^<]*</[^>]*>", "<span class=\"SH-fnc\">&</span>");
+ $0 = hie(grammarNames[i], @/<span [^>]*>[^<]*<\/[^>]*>/, "function");
}
- gsub(/[[:blank:]]*[^-\\]-&#62;/, "<span class=\"SH-kwd\">&</span>");
- gsub(/\|/, "<span class=\"SH-kwd2\">&</span>");
+ hi(@/-&#62;/, "statement");
+ hi(@/\|/, "statement");
}
#
@@ -76,11 +94,11 @@ syntaxhighlighting == 0 && /class="language-regex"/ {
syntaxhighlighting == 2 {
syntaxhighlighting = 2;
- gsub(/[\*]/, "<span class=\"SH-kwd\">&</span>");
- gsub(/[\(\)\|]/, "<span class=\"SH-kwd2\">&</span>");
+ hi(@/\*/, "repeat");
+ hi(@/[\(\)\|]/, "statement");
}
-#
+#
# -- Bash
#
# Supports "bash"
@@ -97,38 +115,46 @@ syntaxhighlighting == 2 {
}
syntaxhighlighting == 3 {
- gsub(/("[^"]*"|'[^']*')/, "<span class=\"SH-str\">&</span>");
- gsub(/\$(\(|\(\(|{).+(\)|\)\)|})/, "<span class=\"SH-kwd3\">&</span>");
- gsub(/\$[^[:blank:]]+/, "<span class=\"SH-kwd3\">&</span>");
- gsub(/[[:blank:]]+-[-_[:alnum:]]+/, "<span class=\"SH-kwd2\">&</span>");
- gsub(/(^|[[:blank:]]+)(function|if|fi|then|case|esac|in|disown|==|!=|-eq|-ne|-gt|-ge|-lt|-le|;|\|\||&#38;&#38;)($|[[:blank:]])/, "<span class=\"SH-kwd\">&</span>");
- gsub(/^[[:blank:]]*[[:alnum:]]+=/, "<span class=\"SH-cls\">&</span>");
- gsub(/[-_[:alnum:]]+[[:blank:]]*\([^\)]*\)/, "<span class=\"SH-fnc\">&</span>");
- gsub(/[\{\}]/, "<span class=\"SH-fnc\">&</span>");
- gsub(/[[:blank:]]#.*$/, "<span class=\"SH-com\">&</span>");
- gsub(/(\|)/, "<span class=\"SH-none\">&</span>");
+ $0 = hie(@/"[^"]*"/, "<[^>]*>", "string");
+ $0 = hil(@/[^\>&]/,
+ @/#.*$/,
+ "",
+ "comment");
+ hi(@/\$(\(|\(\(|{).+(\)|\)\)|})/, "structure");
+ hi(@/\$[[:alnum:]]+/, "structure");
+ $0 = hil(@/[^[:alnum:]#\$]/,
+ @/[[:digit:]]+/,
+ @/[^[:alnum:]]/,
+ "number");
+ $0 = hil(@/[^[:alnum:]#\$]/,
+ @/[[:digit:]]+\.[[:digit:]]+/,
+ @/[^[:alnum:]]/,
+ "float");
+ $0 = hil(@/[[:blank:]]+/, @/-[-_[:alnum:]]+/, "", "operator");
+ $0 = hil(@/(^|[^[:alnum:]])/,
+ @/(if|fi|then|case|esac|&#61;&#61;|!&#61;|-eq|-ne|-gt|-ge|-lt|-le|;;|;|\|\||&#38;&#38;)/,
+ @/([^[:alnum:]]|$)/,
+ "conditional");
+ $0 = hil(@/(^|[^[:alnum:]])/,
+ @/(for|while|until|in|do|done)/,
+ @/([^[:alnum:]]|$)/,
+ "repeat");
+ $0 = hil(@/(^|[[:blank:]]+)/,
+ @/[[:alnum:]]+/,
+ @/(|\+|-)&#61;/,
+ "identifier");
+ $0 = hil("",
+ @/[-_[:alnum:]]+[[:blank:]]*/,
+ @/\([^\)]*\)/,
+ "function");
+ hi(@/[\{\}]/, "function");
+ hi(@/^[[:blank:]]*[[:alnum:]]+/, "statement");
}
+
#
-# -- C --
+# -- C-like programming languages --
#
-# Supports "c" and is used in C-like programming languages
-
-/class="language-c"/ {
- syntaxhighlighting = 100;
-}
-
-syntaxhighlighting >= 100 {
- gsub(/([[:alnum:]]|[-_])+(&#60;[[:alnum:]]+&#62;)?\(/, "<span class=\"SH-fnc\">&</span>");
- gsub(/(&#47;&#47;.*|&#47;*.*&#47;)/, "<span class=\"SH-com\">&</span>");
- gsub(/[\(,\.]/, "<span class=\"SH-none\">&</span>");
- gsub(/(public|protected|private|override|new|if|else|while|for|return|struct|typedef|extern|const)[[:blank:]]/, "<span class=\"SH-kwd\">&</span>");
- gsub(/(&#38;&#38;|\|\||==|!=)/, "<span class=\"SH-kwd2\">&</span>");
- gsub(/#(include|if|ifndef|endif|define)/, "<span class=\"SH-kwd3\">&</span>");
- gsub(/(true|false)[[:blank:]]/, "<span class=\"SH-cst\">&</span>");
- gsub(/(int|void|double|bool|char)[[:blank:]]/, "<span class=\"SH-typ\">&</span>");
- $0 = gesub("(\"[^\"]*\"|'[^']*')", "<[^>]*>", "<span class=\"SH-str\">&</span>");
-}
#
# -- C++ --
@@ -140,7 +166,10 @@ syntaxhighlighting >= 100 {
}
syntaxhighlighting == 101 {
- gsub(/([[:blank:]]|\()*[[:upper:]]([[:upper:]]|[-_])*([[:blank:]]|\)|,|;)+/, "<span class=\"SH-cst\">&</span>");
+ $0 = hil(@/(^|[^[:alnum:]])/,
+ @/(string)/,
+ @/([^[:alnum:]]|$)/,
+ "type");
}
#
@@ -153,10 +182,12 @@ syntaxhighlighting == 101 {
}
syntaxhighlighting == 102 {
- gsub(/new ([[:alnum:]]|[-_])+[\(\.]/, "<span class=\"SH-cls\">&</span>");
- gsub(/(&#60;[[:alnum:]]+&#62;)/, "<span class=\"SH-kwd\">&</span>");
- gsub(/([[:blank:]]|\()*([[:upper:]]|[-_])+([[:blank:]]|\)|,|;)+/, "<span class=\"SH-cst\">&</span>");
- gsub(/(=&#62;)/, "<span class=\"SH-kwd2\">&</span>");
+ $0 = hil(@/(^|[^[:alnum:]])/,
+ @/(new|public|protected|private|override)/,
+ @/([^[:alnum:]]|$)/,
+ "keyword");
+ hi(@/&#60;[-_[:alpha:]]+&#62;/, "typedef");
+ $0 = hie(@/[_[:upper:]][_[:upper:]][_[:upper:]][_[:upper:]]+/, @/<[^>]*>/, "const");
}
#
@@ -169,7 +200,7 @@ syntaxhighlighting == 102 {
}
syntaxhighlighting == 103 {
- gsub(/%[^[:blank:]]+/, "<span class=\"SH-kwd\">&</span>");
+ hi(@/%[^[:blank:]]+/, "storageclass");
}
#
@@ -182,7 +213,60 @@ syntaxhighlighting == 103 {
}
syntaxhighlighting == 104 {
- gsub(/%[^[:blank:]]+/, "<span class=\"SH-kwd\">&</span>");
+ hi(@/%[^[:blank:]]+/, "storageclass");
+}
+
+#
+# -- C --
+#
+# Supports "c" and is used in C-like programming languages
+
+/class="language-c"/ {
+ syntaxhighlighting = 100;
+}
+
+syntaxhighlighting >= 100 {
+ $0 = hie(@/"[^"]*"/, "<[^>]*>", "string");
+ hi(@/(&#47;&#47;.*$|&#47;\*.*$|^[^\*]*\*&#47;)/, "comment");
+ $0 = hil(@/[^[:alnum:]#\$]/,
+ @/[[:digit:]]+/,
+ @/[^[:alnum:]]/,
+ "number");
+ $0 = hil(@/[^[:alnum:]#\$]/,
+ @/[[:digit:]]+\.[[:digit:]]+/,
+ @/[^[:alnum:]]/,
+ "float");
+ $0 = hil("",
+ @/[-_[:alnum:]]+/,
+ @/[[:blank:]]*(|\+|-|\*|&#47;|%|&#60;&#60;|&#62;&#62;|&|\^|\|)&#61;/,
+ "identifier");
+ $0 = hil("",
+ @/[-_[:alnum:]]+[[:blank:]]*/,
+ @/\(/,
+ "function");
+ hi(@/(&#61;&#61;|!&#61;|&#60;&#61;|&#62;&#61;|&#38;&#38;|\|\||!)/, "conditional");
+ $0 = hil(@/(^|[^-[:alnum:]])/,
+ @/(break|continue|do|for|goto|while)/,
+ @/([^[:alnum:]]|$)/,
+ "repeat");
+ $0 = hil(@/(^|[^-[:alnum:]])/,
+ @/(case|const|default|else|extern|if|return|sizeof|switch|typedef)/,
+ @/([^[:alnum:]]|$)/,
+ "keyword");
+ $0 = hil(@/(^|<.*>)[[:blank:]]*/,
+ @/#[[:alpha:]]+/,
+ "",
+ "preproc");
+ $0 = hil(@/(^|[^-[:alnum:]])/,
+ @/(auto|char|double|float|int|long|short|signed|unsigned|bool|void)/,
+ @/([^[:alnum:]]|$)/,
+ "type");
+ hi(@/&#39;[\\]?[ -~]&#39;/, "char");
+ hi(@/(true|false)/, "bool");
+ $0 = hil(@/(^|[^[:alnum:]])/,
+ @/(enum|struct|union)/,
+ @/([^[:alnum:]]|$)/,
+ "storageclass");
}
tagend != 0 {
diff --git a/src/darkmode.css b/src/darkmode.css
index 7a8ad3a..11eadf7 100644
--- a/src/darkmode.css
+++ b/src/darkmode.css
@@ -2,23 +2,35 @@
--bg-color: #262626;
--fg-color: #ffeede;
- --a-color: #cd81a7;
- --a-hover-color: #eeaacd;
- --accent-line-color: #d47408;
- --alternating-row-color: #353535;
- --bg-selection-color: #81949f;
- --blockquote-color: #919191;
- --blockquote-border-color: #919191;
- --body-bg: #000000;
- --border-color: #777;
- --code-bg-color: #414141;
- --em-color: #e17b08;
- --fg-selection-color: #001121;
- --header-color: #d49408;
- --pre-bg-color: #414141;
- --strong-color: #c35b5b;
- --table-border-color: #777;
- --tr-bg-color: #262626;
+ --a-color: #cd81a7;
+ --a-hover-color: #eeaacd;
+ --accent-line-color: #d47408;
+ --alternating-row-color: #353535;
+ --bg-selection-color: #81949f;
+ --blockquote-color: #919191;
+ --blockquote-border-color: var(--blockquote-color);
+ --body-bg: #000000;
+ --border-color: #777;
+ --code-bg-color: #414141;
+ --em-color: #e17b08;
+ --fg-selection-color: #001121;
+ --header-color: #d49408;
+ --pre-bg-color: #414141;
+ --strong-color: #c35b5b;
+ --table-border-color: var(--border-color);
+ --tr-bg-color: var(--bg-color);
+
+ /* https://github.com/morhetz/gruvbox/blob/master/autoload/gruvbox.vim */
+ --hi-fg: #fbf1c7;
+ --hi-bg: #1d2021;
+ --hic-gray: #928374;
+ --hic-red: #fb4934;
+ --hic-green: #b8bb26;
+ --hic-blue: #83a598;
+ --hic-yellow: #fabd2f;
+ --hic-purple: #d3869b;
+ --hic-aqua: #8ec07c;
+ --hic-orange: #fe8019;
}
/* This selects the link images, next to headings */
diff --git a/src/styles.css b/src/styles.css
index b24a355..6173c5e 100644
--- a/src/styles.css
+++ b/src/styles.css
@@ -55,23 +55,56 @@
--bg-color: white;
--fg-color: black;
- --a-color: #336700;
- --a-hover-color: #3ea100;
- --accent-line-color: #004080;
- --alternating-row-color: #eef0f2;
- --bg-selection-color: #338fff;
- --blockquote-color: #6a737d;
+ --a-color: #336700;
+ --a-hover-color: #3ea100;
+ --accent-line-color: #004080;
+ --alternating-row-color: #eef0f2;
+ --bg-selection-color: #338fff;
+ --blockquote-color: #6a737d;
--blockquote-border-color: #dfe2e5;
- --body-bg: #dbdbdb;
- --border-color: black;
- --code-bg-color: #e2e4e6;
- --em-color: black;
- --fg-selection-color: white;
- --header-color: #006b8f;
- --pre-bg-color: #f6f8fa;
- --strong-color: black;
- --table-border-color: black;
- --tr-bg-color: #fff;
+ --body-bg: #dbdbdb;
+ --border-color: black;
+ --code-bg-color: #e2e4e6;
+ --em-color: black;
+ --fg-selection-color: white;
+ --header-color: #006b8f;
+ --pre-bg-color: #f6f8fa;
+ --strong-color: black;
+ --table-border-color: black;
+ --tr-bg-color: white;
+
+ /* https://github.com/morhetz/gruvbox/blob/master/autoload/gruvbox.vim */
+ --hi-fg: #282828;
+ --hi-bg: #f9f5d7;
+ --hic-gray: #928374;
+ --hic-red: #fb4934;
+ --hic-green: #79740e;
+ --hic-blue: #076678;
+ --hic-yellow: #b57614;
+ --hic-purple: #8f3f71;
+ --hic-aqua: #427b58;
+ --hic-orange: #af3a03;
+
+ --hi-comment: var(--hic-gray);
+ --hi-const: var(--hic-purple);
+ --hi-string: var(--hic-green);
+ --hi-char: var(--hic-purple);
+ --hi-number: var(--hic-purple);
+ --hi-bool: var(--hic-purple);
+ --hi-float: var(--hic-purple);
+ --hi-identifier: var(--hic-blue);
+ --hi-function: var(--hic-green);
+ --hi-statement: var(--hic-red);
+ --hi-conditional: var(--hic-red);
+ --hi-repeat: var(--hic-red);
+ --hi-operator: var(--hi-fg);
+ --hi-keyword: var(--hic-red); /* Label and Exception are keywords */
+ --hi-preproc: var(--hic-aqua);
+ --hi-type: var(--hic-yellow);
+ --hi-storageclass: var(--hic-orange);
+ --hi-structure: var(--hic-aqua);
+ --hi-typedef: var(--hic-aqua);
+ --hi-ignore: var(--hi-fg);
}
/* Global */
@@ -187,7 +220,6 @@ h1, h2, h3, h4, h5, h6, b {
code, pre {
background-color: var(--pre-bg-color);
font-family: monospace;
- line-height: 1em;
}
pre {
overflow: auto;
@@ -202,7 +234,7 @@ code, pre {
}
pre code {
border: 0;
- display: inline;
+ display: inline-block;
overflow: visible;
padding: 0;
word-wrap: normal;
@@ -471,14 +503,25 @@ dt { font-size: 1.2em; }
padding: 0.2em 0 0.2em 0.2em !important;
}
-/* Syntax highlighting (colours are inspired from gruvbox) */
+/* Syntax highlighting */
-/* Constant, Chars */ .SH-cst, .SH-chr { color: #d787af; }
-/* Comment */ .SH-com { color: #8a8a8a; }
-/* Keyword */ .SH-kwd { color: #d75f5f; }
-/* Second keyword color */ .SH-kwd2 { color: #ff8700; }
-/* Third keyword color */ .SH-kwd3 { color: #87af87; }
-/* Type */ .SH-typ { color: #ffaf00; }
-/* Class */ .SH-cls { color: #87afaf; }
-/* Function names, Strings */ :not(.SH-cls) > .SH-fnc, .SH-str { color: #afaf00; }
-.SH-none { color: var(--fg-color); }
+.HI-comment { color: var(--hi-comment); }
+.HI-const { color: var(--hi-const); }
+.HI-string { color: var(--hi-string); }
+.HI-char { color: var(--hi-char); }
+.HI-number { color: var(--hi-number); }
+.HI-bool { color: var(--hi-bool); }
+.HI-float { color: var(--hi-float); }
+.HI-identifier { color: var(--hi-identifier); }
+.HI-function { color: var(--hi-function); }
+.HI-statement { color: var(--hi-statement); }
+.HI-conditional { color: var(--hi-conditional); }
+.HI-repeat { color: var(--hi-repeat); }
+.HI-operator { color: var(--hi-operator); }
+.HI-keyword { color: var(--hi-keyword); /* Label and Exception are keywords */ }
+.HI-preproc { color: var(--hi-preproc); }
+.HI-type { color: var(--hi-type); }
+.HI-storageclass { color: var(--hi-storageclass); }
+.HI-structure { color: var(--hi-structure); }
+.HI-typedef { color: var(--hi-typedef); }
+.HI-ignore { color: var(--hi-ignore); }