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. # distribution, for details about the copyright.
import jester, asyncdispatch, json, streams, strutils import jester, asyncdispatch, json, streams, strutils
import packages.docutils.rstgen, packages.docutils.highlite import packages.docutils.rstgen, packages.docutils.highlite, options
import ../yaml import ../yaml
routes: routes:
get "/": get "/":
headers["Content-Type"] = "text/plain" resp(Http200, [("Content-Type", "text/plain")], "I am a friendly NimYAML parser webservice.")
resp "I am a friendly NimYAML parser webservice."
post "/": post "/":
var var
style: PresentationStyle style: PresentationStyle
@ -19,10 +18,8 @@ routes:
msg: string msg: string
retStatus = Http200 retStatus = Http200
contentType = "application/json" contentType = "application/json"
headers["Access-Control-Allow-Origin"] = "*" headers = @[("Access-Control-Allow-Origin", "*"), ("Pragma", "no-cache"),
headers["Pragma"] = "no-cache" ("Cache-Control", "no-cache"), ("Expires", "0")]
headers["Cache-Control"] = "no-cache"
headers["Expires"] = "0"
try: try:
case @"style" case @"style"
of "minimal": style = psMinimal of "minimal": style = psMinimal
@ -33,7 +30,7 @@ routes:
of "tokens": of "tokens":
var var
output = "+STR\n" output = "+STR\n"
parser = newYamlParser() parser = initYamlParser(false)
events = parser.parse(newStringStream(@"input")) events = parser.parse(newStringStream(@"input"))
for event in events: output.add(parser.display(event) & "\n") for event in events: output.add(parser.display(event) & "\n")
output &= "-STR" output &= "-STR"
@ -44,7 +41,7 @@ routes:
retStatus = Http400 retStatus = Http400
msg = "Invalid style: " & escape(@"style") msg = "Invalid style: " & escape(@"style")
contentType = "text/plain;charset=utf8" contentType = "text/plain;charset=utf8"
if isNil(msg): if len(msg) == 0:
var var
output = newStringStream() output = newStringStream()
highlighted = "" highlighted = ""
@ -60,9 +57,9 @@ routes:
of gtNone, gtWhitespace: of gtNone, gtWhitespace:
highlighted.add(substr(output.data, g.start, g.length + g.start - 1)) highlighted.add(substr(output.data, g.start, g.length + g.start - 1))
else: 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)), esc(outHtml, substr(output.data, g.start, g.length+g.start-1)),
tokenClassToStr[g.kind]]) tokenClassToStr[g.kind])
resultNode["code"] = %0 resultNode["code"] = %0
resultNode["output"] = %highlighted resultNode["output"] = %highlighted
@ -70,8 +67,8 @@ routes:
except YamlParserError: except YamlParserError:
let e = (ref YamlParserError)(getCurrentException()) let e = (ref YamlParserError)(getCurrentException())
resultNode["code"] = %1 resultNode["code"] = %1
resultNode["line"] = %e.line resultNode["line"] = %e.mark.line
resultNode["column"] = %e.column resultNode["column"] = %e.mark.column
resultNode["message"] = %e.msg resultNode["message"] = %e.msg
resultNode["detail"] = %e.lineContent resultNode["detail"] = %e.lineContent
msg = resultNode.pretty msg = resultNode.pretty
@ -86,5 +83,6 @@ routes:
"\nTrace:\n" & e.getStackTrace "\nTrace:\n" & e.getStackTrace
retStatus = Http500 retStatus = Http500
contentType = "text/plain;charset=utf-8" contentType = "text/plain;charset=utf-8"
resp retStatus, msg, contentType headers.add(("Content-Type", contentType))
resp retStatus, headers, msg
runForever() 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 ## while resolving non-specific tags to the ones in the YAML core tag
## library. If ``resolveToCoreYamlTags`` is ``true``, non-specific tags will ## library. If ``resolveToCoreYamlTags`` is ``true``, non-specific tags will
## be replaced by specific tags according to the YAML core schema. ## 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, proc transform*(input: Stream | string,
options: PresentationOptions = defaultPresentationOptions, options: PresentationOptions = defaultPresentationOptions,
@ -817,5 +818,8 @@ proc transform*(input: Stream | string,
## YAML string that represents the stream. If ``resolveToCoreYamlTags`` is ## YAML string that represents the stream. If ``resolveToCoreYamlTags`` is
## ``true``, non-specific tags will be replaced by specific tags according to ## ``true``, non-specific tags will be replaced by specific tags according to
## the YAML core schema. ## the YAML core schema.
result = "" var
doTransform(genInput(input), addr result, options, resolveToCoreYamlTags) ss = newStringStream()
c = Context(target: ss, options: options)
doTransform(c, genInput(input), resolveToCoreYamlTags)
return ss.data