Skip to content

Instantly share code, notes, and snippets.

@Yardanico
Last active March 8, 2021 13:18
Show Gist options
  • Save Yardanico/144caea455c43917a409a22b56f34c03 to your computer and use it in GitHub Desktop.
Save Yardanico/144caea455c43917a409a22b56f34c03 to your computer and use it in GitHub Desktop.
Fix escaping
func nimNormalize(s: string): string =
# Copied from strutils.normalize
result = newString(s.len)
var j = 0
for i in 0 .. len(s) - 1:
# First char is case-sensitive
if i == 0:
result[j] = s[i]
inc j
elif s[i] in {'A'..'Z'}:
result[j] = chr(ord(s[i]) + (ord('a') - ord('A')))
inc j
elif s[i] != '_':
result[j] = s[i]
inc j
if j != s.len: setLen(result, j)
const builtin = [
"int", "int8", "int16", "int32", "int64",
"uint", "uint8", "uint16", "uint32", "uint64",
"float", "float32", "float64",
"bool", "char", "string", "cstring", "pointer",
"expr", "stmt", "untyped", "typed", "void", "auto",
"any", "range", "openArray", "varargs", "seq", "set",
"clong", "culong", "cchar", "cschar", "cshort", "cint", "csize",
"clonglong", "cfloat", "cdouble", "clongdouble", "cuchar", "cushort",
"cuint", "culonglong", "cstringArray", "array"
]
# TODO: Do we really need shared and guarded?
const literal = [
"shared", "guarded", "stdin", "stdout", "stderr", "result", "true",
"false", "Inf", "NegInf", "NaN", "nil"
]
const commonFuncs = [
"new", "await", "assert", "echo", "defined", "declared",
"newException", "countup", "countdown", "high", "low"
]
proc tokClass(str: string, kind: TokenClass): string =
let norm = nimNormalize(str)
debugecho norm
case norm
of builtin: "hljs-built_in"
of literal: "hljs-literal"
of commonFuncs: "hljs-keyword"
else:
case kind
of gtKeyword: "hljs-keyword"
of gtComment, gtLongComment: "hljs-comment"
of gtStringLit, gtLongStringLit, gtCharLit: "hljs-string"
of gtDecNumber..gtFloatNumber: "hljs-number"
of gtRegularExpression: "hljs-regexp"
# TODO: Currently nimib doesn't handle escape sequences correctly
of gtEscapeSequence: "hljs-meta"
of gtIdentifier:
# Most types are PascalCase (start with an upper-case letter)
if norm.len > 0 and norm[0] in {'A'..'Z'}: "hljs-type"
else: ""
else: ""
proc highlightNim(code: string): string =
var g: GeneralTokenizer
g.initGeneralTokenizer(code)
while true:
g.getNextToken(langNim)
var istr = substr(code, g.start, g.length + g.start - 1)
case g.kind
of gtEof: break
of gtNone, gtWhitespace:
result.add istr
else:
let cls = tokClass(istr, g.kind)
istr = istr.xmlEncode()
result.add if cls != "":
"<span class=\"$2\">$1</span>" % [istr, cls]
else:
istr
g.deinitGeneralTokenizer()
proc renderHtmlCodeBody*(code: string): string =
#fmt"""<pre><code class="nim">{code.strip}</code></pre>""" & "\n"
let highlit = highlightNim readFile("/home/dian/Projects/nimib/test.nim")
result = fmt"""<pre><code class="nim hljs">{highlit.strip}</code></pre>""" & "\n"
debugecho result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment