2016-01-05 20:34:07 +00:00
|
|
|
# NimYAML - YAML implementation in Nim
|
|
|
|
# (c) Copyright 2015 Felix Krause
|
|
|
|
#
|
|
|
|
# See the file "copying.txt", included in this
|
|
|
|
# distribution, for details about the copyright.
|
|
|
|
|
2016-05-12 13:35:29 +00:00
|
|
|
import jester, asyncdispatch, json, streams, strutils
|
|
|
|
import packages.docutils.rstgen, packages.docutils.highlite
|
2016-10-01 12:49:32 +00:00
|
|
|
import ../yaml
|
2016-01-05 20:34:07 +00:00
|
|
|
|
|
|
|
routes:
|
2016-04-02 15:48:22 +00:00
|
|
|
get "/":
|
|
|
|
headers["Content-Type"] = "text/plain"
|
|
|
|
resp "I am a friendly NimYAML parser webservice."
|
|
|
|
post "/":
|
|
|
|
var
|
|
|
|
style: PresentationStyle
|
|
|
|
resultNode = newJObject()
|
2016-10-01 12:49:32 +00:00
|
|
|
msg: string
|
|
|
|
retStatus = Http200
|
|
|
|
contentType = "application/json"
|
2016-04-02 15:48:22 +00:00
|
|
|
headers["Access-Control-Allow-Origin"] = "*"
|
|
|
|
headers["Pragma"] = "no-cache"
|
|
|
|
headers["Cache-Control"] = "no-cache"
|
|
|
|
headers["Expires"] = "0"
|
|
|
|
try:
|
|
|
|
case @"style"
|
|
|
|
of "minimal": style = psMinimal
|
|
|
|
of "canonical": style = psCanonical
|
|
|
|
of "default": style = psDefault
|
|
|
|
of "json": style = psJson
|
|
|
|
of "block": style = psBlockOnly
|
|
|
|
of "tokens":
|
2016-02-18 14:52:19 +00:00
|
|
|
var
|
2016-04-02 15:48:22 +00:00
|
|
|
output = ""
|
|
|
|
parser = newYamlParser()
|
|
|
|
events = parser.parse(newStringStream(@"input"))
|
|
|
|
for event in events: output.add($event & "\n")
|
|
|
|
resultNode["code"] = %0
|
|
|
|
resultNode["output"] = %output
|
2016-10-01 12:49:32 +00:00
|
|
|
msg = resultNode.pretty
|
|
|
|
else:
|
|
|
|
retStatus = Http400
|
|
|
|
msg = "Invalid style: " & escape(@"style")
|
|
|
|
contentType = "text/plain;charset=utf8"
|
|
|
|
if isNil(msg):
|
2016-05-12 13:35:29 +00:00
|
|
|
var
|
|
|
|
output = newStringStream()
|
|
|
|
highlighted = ""
|
2016-04-02 15:48:22 +00:00
|
|
|
transform(newStringStream(@"input"), output, defineOptions(style))
|
2016-10-01 12:49:32 +00:00
|
|
|
|
2016-05-12 13:35:29 +00:00
|
|
|
# syntax highlighting (stolen and modified from stlib's rstgen)
|
|
|
|
var g: GeneralTokenizer
|
|
|
|
g.initGeneralTokenizer(output.data)
|
|
|
|
while true:
|
|
|
|
g.getNextToken(langYaml)
|
|
|
|
case g.kind
|
|
|
|
of gtEof: break
|
|
|
|
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}", [
|
|
|
|
esc(outHtml, substr(output.data, g.start, g.length+g.start-1)),
|
|
|
|
tokenClassToStr[g.kind]])
|
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
resultNode["code"] = %0
|
2016-05-12 13:35:29 +00:00
|
|
|
resultNode["output"] = %highlighted
|
2016-10-01 12:49:32 +00:00
|
|
|
msg = resultNode.pretty
|
2016-04-02 15:48:22 +00:00
|
|
|
except YamlParserError:
|
|
|
|
let e = (ref YamlParserError)(getCurrentException())
|
|
|
|
resultNode["code"] = %1
|
|
|
|
resultNode["line"] = %e.line
|
|
|
|
resultNode["column"] = %e.column
|
|
|
|
resultNode["message"] = %e.msg
|
|
|
|
resultNode["detail"] = %e.lineContent
|
2016-10-01 12:49:32 +00:00
|
|
|
msg = resultNode.pretty
|
2016-04-02 15:48:22 +00:00
|
|
|
except YamlPresenterJsonError:
|
|
|
|
let e = getCurrentException()
|
|
|
|
resultNode["code"] = %2
|
|
|
|
resultNode["message"] = %e.msg
|
2016-10-01 12:49:32 +00:00
|
|
|
msg = resultNode.pretty
|
2016-04-02 15:48:22 +00:00
|
|
|
except:
|
|
|
|
let e = getCurrentException()
|
2016-10-01 12:49:32 +00:00
|
|
|
msg = "Name: " & $e.name & "\nMessage: " & e.msg &
|
|
|
|
"\nTrace:\n" & e.getStackTrace
|
|
|
|
retStatus = Http500
|
|
|
|
contentType = "text/plain;charset=utf-8"
|
|
|
|
resp retStatus, msg, contentType
|
2016-01-05 22:19:00 +00:00
|
|
|
runForever()
|