mirror of https://github.com/status-im/NimYAML.git
Server: Added possibility to show token stream
This commit is contained in:
parent
a028b43858
commit
7eca93f988
|
@ -80,6 +80,10 @@
|
|||
<input type="radio" name="style" id="style-json" value="json"/>
|
||||
<label for="style-json">JSON</label>
|
||||
</div>
|
||||
<div class="style-option">
|
||||
<input type="radio" name="style" id="style-tokens" value="tokens"/>
|
||||
<label for="style-tokens">Tokens</label>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<script type="text/javascript">
|
||||
|
|
|
@ -12,26 +12,50 @@ routes:
|
|||
headers["Content-Type"] = "text/plain"
|
||||
resp "I am a friendly NimYAML parser webservice."
|
||||
post "/":
|
||||
var style: PresentationStyle
|
||||
var
|
||||
style: PresentationStyle
|
||||
resultNode = newJObject()
|
||||
tokens = false
|
||||
headers["Access-Control-Allow-Origin"] = "*"
|
||||
headers["Pragma"] = "no-cache"
|
||||
headers["Cache-Control"] = "no-cache"
|
||||
headers["Expires"] = "0"
|
||||
case @"style"
|
||||
of "minimal": style = psMinimal
|
||||
of "canonical": style = psCanonical
|
||||
of "default": style = psDefault
|
||||
of "json": style = psJson
|
||||
of "block": style = psBlockOnly
|
||||
var
|
||||
of "tokens":
|
||||
var
|
||||
output = ""
|
||||
parser = newYamlParser()
|
||||
events = parser.parse(newStringStream(@"input"))
|
||||
try:
|
||||
for event in events:
|
||||
output.add($event & "\n")
|
||||
resultNode["code"] = %0
|
||||
resultNode["output"] = %output
|
||||
resp resultNode.pretty, "application/json"
|
||||
tokens = true
|
||||
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
|
||||
resp resultNode.pretty, "application/json"
|
||||
if not tokens:
|
||||
echo "outputting YAML"
|
||||
var
|
||||
output = newStringStream()
|
||||
resultNode = newJObject()
|
||||
headers["Access-Control-Allow-Origin"] = "*"
|
||||
headers["Pragma"] = "no-cache"
|
||||
headers["Cache-Control"] = "no-cache"
|
||||
headers["Expires"] = "0"
|
||||
try:
|
||||
try:
|
||||
transform(newStringStream(@"input"), output, style)
|
||||
resultNode["code"] = %0
|
||||
resultNode["output"] = %output.data
|
||||
resp resultNode.pretty, "application/json"
|
||||
except YamlParserError:
|
||||
except YamlParserError:
|
||||
let e = (ref YamlParserError)(getCurrentException())
|
||||
resultNode["code"] = %1
|
||||
resultNode["line"] = %e.line
|
||||
|
@ -39,13 +63,13 @@ routes:
|
|||
resultNode["message"] = %e.msg
|
||||
resultNode["detail"] = %e.lineContent
|
||||
resp resultNode.pretty, "application/json"
|
||||
except YamlPresenterJsonError:
|
||||
except YamlPresenterJsonError:
|
||||
let e = getCurrentException()
|
||||
resultNode["code"] = %2
|
||||
resultNode["message"] = %e.msg
|
||||
headers["Content-Type"] = "application/json"
|
||||
resp resultNode.pretty, "application/json"
|
||||
except:
|
||||
except:
|
||||
let e = getCurrentException()
|
||||
let msg = "Name: " & $e.name & "\nMessage: " & e.msg &
|
||||
"\nTrace:\n" & e.getStackTrace
|
||||
|
|
Loading…
Reference in New Issue