Implemented highlighter for web site; removed parser state.
h3rald h3rald@h3rald.com
Sat, 18 Nov 2023 17:47:52 +0100
4 files changed,
110 insertions(+),
8 deletions(-)
M
minpkg/core/parser.nim
→
minpkg/core/parser.nim
@@ -466,12 +466,10 @@ proc getToken*(my: var MinParser): MinTokenKind =
setLen(my.a, 0) case my.buf[my.bufpos] of ';': - add(my.a, my.buf[my.bufpos]) # skip line comment: if my.buf[my.bufpos+1] == ';': my.doc = true inc(my.bufpos, 1) - add(my.a, my.buf[my.bufpos]) inc(my.bufpos, 1) while true: case my.buf[my.bufpos]
M
site/assets/styles/min-lang.css
→
site/assets/styles/min-lang.css
@@ -122,7 +122,7 @@ dt {
margin-top: 1em; } -code, pre, .kwd, .cmd, .file, .dir, kbd, .kbd, .ext, .min-terminal { +code, pre, .kwd, .cmd, .file, .dir, kbd, .kbd, .ext, .min-terminal, .min-codeblock { font-family: 'Source Code Pro', monospace; font-size: 90%; }@@ -265,3 +265,52 @@ text-align: center;
margin: auto; padding: 5px; } + +.min-codeblock { + white-space: pre-wrap; + font-size: 80%; + background: #1a1c1a; + border: 1px solid #313631; + border-radius: 3px; + padding: 0.5rem; +} + +.min-codeblock .min-tkLineComment, +.min-codeblock .min-tkLineDocComment, +.min-codeblock .min-tkBlockComment, +.min-codeblock .min-tkBlockDocComment { + color: #04873f; +} + +.min-codeblock .min-tkBraceRi, +.min-codeblock .min-tkBracketRi, +.min-codeblock .min-tkSqBracketRi, +.min-codeblock .min-tkBraceLe, +.min-codeblock .min-tkBracketLe, +.min-codeblock .min-tkSqBracketLe { + color: #9c080f; +} + +.min-codeblock .min-tkInt, +.min-codeblock .min-tkFloat { + color: #f381f7; +} + +.min-codeblock .min-tkString { + color: #c7830e; +} + +.min-codeblock .min-tkCommand { + color: #c7830e; +} + +.min-codeblock .min-tkSymbol { + color: #9de9fa; +} + +.min-codeblock .min-tkNull, +.min-codeblock .min-tkTrue, +.min-codeblock .min-tkFalse { + color: #0cc9a0; +} +
M
site/contents/learn.md
→
site/contents/learn.md
@@ -10,10 +10,12 @@ {{m}} is a stack-based, concatenative programming language that uses postfix notation. If you already know [Forth](http://www.forth.org/), [Factor](http://factorcode.org/) or [Joy](http://www.kevinalbrecht.com/code/joy-mirror/), or if you ever used an [RPN](https://en.wikipedia.org/wiki/Reverse_Polish_notation) calculator, then min will look somewhat familiar to you.
If not, well, here's how a short min program looks like: - ; This is a comment - (1 2 3 4 5) (dup *) map - #| This is a... - ...multiline comment |# +```min +; This is a comment +(1 2 3 4 5) (dup *) map +#| This is a... +...multiline comment |# +``` This program returns a list containing the square values of the first five integer numbers:
M
site/rules.min
→
site/rules.min
@@ -21,6 +21,59 @@ ))
) case ) ^set-destination +; Syntax highlighter +( + symbol span + (str :class str :value ==> str :result) + ( + "min-$#" (class) =% @class + {} :attributes + attributes class %class @attributes + "span" xelement :sp + sp attributes %attributes @sp + value xtext :text + sp (text) => %children @sp + sp to-xml @result + ) +) :: + +( + symbol highlight + (dict :token ==> str :result) + ( + token /value :value + token /type :type + ( + ((type "tkEof" ==) ("" @result)) + ((type "tkSpace" ==) (value @result)) + ((type "tkBlockComment" ==) (type "#|$#|#" (value) =% span @result)) + ((type "tkBlockDocComment" ==) (type "#||$#||#" (value) =% span @result)) + ((type "tkLineComment" ==) (type ";$#" (value) =% span @result)) + ((type "tkLineDocComment" ==) (type ";;$#" (value) =% span @result)) + ((type "tkString" ==) (type "\"$#\"" (value) =% span @result)) + ((true) (type value span @result)) + ) case + ) +) :: + +( + symbol min-highlight + (str :code ==> str :result) + ( + code tokenize 'highlight map "" join :code + "<div class='min-codeblock'>$#</div>" (code) =% @result + ) +) :: + +( + symbol highlight-min-codeblocks + (str :markdown ==> str :result) + ( + markdown "```min\n([^`]+)\n```" + (1 get min-highlight) replace-apply @result + ) +) :: + ;Processing operators ( ('sym dict) expect -> :tpl :meta@@ -30,7 +83,7 @@ meta (
(input-fread @contents meta) (settings /title %site) (settings /version %version) - (:temp contents temp markdown @contents temp) + (:temp contents highlight-min-codeblocks temp markdown @contents temp) (contents %contents) (:temp tpl temp mustache @page temp) (page %contents)