fixed server to compile with current Nim

This commit is contained in:
Felix Krause 2021-05-17 19:50:10 +02:00
parent 329e18e44c
commit 2b0e20abd0
2 changed files with 19 additions and 17 deletions

View File

@ -5,13 +5,12 @@
# distribution, for details about the copyright.
import jester, asyncdispatch, json, streams, strutils
import packages.docutils.rstgen, packages.docutils.highlite
import packages.docutils.rstgen, packages.docutils.highlite, options
import ../yaml
routes:
get "/":
headers["Content-Type"] = "text/plain"
resp "I am a friendly NimYAML parser webservice."
resp(Http200, [("Content-Type", "text/plain")], "I am a friendly NimYAML parser webservice.")
post "/":
var
style: PresentationStyle
@ -19,10 +18,8 @@ routes:
msg: string
retStatus = Http200
contentType = "application/json"
headers["Access-Control-Allow-Origin"] = "*"
headers["Pragma"] = "no-cache"
headers["Cache-Control"] = "no-cache"
headers["Expires"] = "0"
headers = @[("Access-Control-Allow-Origin", "*"), ("Pragma", "no-cache"),
("Cache-Control", "no-cache"), ("Expires", "0")]
try:
case @"style"
of "minimal": style = psMinimal
@ -33,7 +30,7 @@ routes:
of "tokens":
var
output = "+STR\n"
parser = newYamlParser()
parser = initYamlParser(false)
events = parser.parse(newStringStream(@"input"))
for event in events: output.add(parser.display(event) & "\n")
output &= "-STR"
@ -44,7 +41,7 @@ routes:
retStatus = Http400
msg = "Invalid style: " & escape(@"style")
contentType = "text/plain;charset=utf8"
if isNil(msg):
if len(msg) == 0:
var
output = newStringStream()
highlighted = ""
@ -60,9 +57,9 @@ routes:
of gtNone, gtWhitespace:
highlighted.add(substr(output.data, g.start, g.length + g.start - 1))
else:
highlighted.addf("<span class=\"$2\">$1</span>", "\\span$2{$1}", [
highlighted.addf("<span class=\"$2\">$1</span>", "\\span$2{$1}",
esc(outHtml, substr(output.data, g.start, g.length+g.start-1)),
tokenClassToStr[g.kind]])
tokenClassToStr[g.kind])
resultNode["code"] = %0
resultNode["output"] = %highlighted
@ -70,8 +67,8 @@ routes:
except YamlParserError:
let e = (ref YamlParserError)(getCurrentException())
resultNode["code"] = %1
resultNode["line"] = %e.line
resultNode["column"] = %e.column
resultNode["line"] = %e.mark.line
resultNode["column"] = %e.mark.column
resultNode["message"] = %e.msg
resultNode["detail"] = %e.lineContent
msg = resultNode.pretty
@ -86,5 +83,6 @@ routes:
"\nTrace:\n" & e.getStackTrace
retStatus = Http500
contentType = "text/plain;charset=utf-8"
resp retStatus, msg, contentType
headers.add(("Content-Type", contentType))
resp retStatus, headers, msg
runForever()

View File

@ -805,7 +805,8 @@ proc transform*(input: Stream | string, output: Stream,
## while resolving non-specific tags to the ones in the YAML core tag
## library. If ``resolveToCoreYamlTags`` is ``true``, non-specific tags will
## be replaced by specific tags according to the YAML core schema.
doTransform(genInput(input), output, options, resolveToCoreYamlTags)
var c = Context(target: output, options: options)
doTransform(c, genInput(input), resolveToCoreYamlTags)
proc transform*(input: Stream | string,
options: PresentationOptions = defaultPresentationOptions,
@ -817,5 +818,8 @@ proc transform*(input: Stream | string,
## YAML string that represents the stream. If ``resolveToCoreYamlTags`` is
## ``true``, non-specific tags will be replaced by specific tags according to
## the YAML core schema.
result = ""
doTransform(genInput(input), addr result, options, resolveToCoreYamlTags)
var
ss = newStringStream()
c = Context(target: ss, options: options)
doTransform(c, genInput(input), resolveToCoreYamlTags)
return ss.data