2015-12-28 21:24:05 +00:00
|
|
|
# NimYAML - YAML implementation in Nim
|
2016-09-21 19:20:57 +00:00
|
|
|
# (c) Copyright 2016 Felix Krause
|
2015-12-28 21:24:05 +00:00
|
|
|
#
|
|
|
|
# See the file "copying.txt", included in this
|
|
|
|
# distribution, for details about the copyright.
|
|
|
|
|
2016-09-21 19:20:57 +00:00
|
|
|
## =====================
|
2021-05-17 22:31:47 +00:00
|
|
|
## Module yaml/presenter
|
2016-09-21 19:20:57 +00:00
|
|
|
## =====================
|
|
|
|
##
|
|
|
|
## This is the presenter API, used for generating YAML character streams.
|
|
|
|
|
2022-09-07 13:11:59 +00:00
|
|
|
import std / [streams, deques, strutils, options]
|
2021-10-22 15:46:05 +00:00
|
|
|
import data, taglib, stream, private/internal, hints, parser
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2015-12-27 15:36:32 +00:00
|
|
|
type
|
2016-09-20 19:53:38 +00:00
|
|
|
PresentationStyle* = enum
|
|
|
|
## Different styles for YAML character stream output.
|
|
|
|
##
|
|
|
|
## - ``ypsMinimal``: Single-line flow-only output which tries to
|
|
|
|
## use as few characters as possible.
|
|
|
|
## - ``ypsCanonical``: Canonical YAML output. Writes all tags except
|
|
|
|
## for the non-specific tags ``?`` and ``!``, uses flow style, quotes
|
|
|
|
## all string scalars.
|
|
|
|
## - ``ypsDefault``: Tries to be as human-readable as possible. Uses
|
|
|
|
## block style by default, but tries to condense mappings and
|
|
|
|
## sequences which only contain scalar nodes into a single line using
|
|
|
|
## flow style.
|
|
|
|
## - ``ypsJson``: Omits the ``%YAML`` directive and the ``---``
|
|
|
|
## marker. Uses flow style. Flattens anchors and aliases, omits tags.
|
|
|
|
## Output will be parseable as JSON. ``YamlStream`` to dump may only
|
|
|
|
## contain one document.
|
|
|
|
## - ``ypsBlockOnly``: Formats all output in block style, does not use
|
|
|
|
## flow style at all.
|
|
|
|
psMinimal, psCanonical, psDefault, psJson, psBlockOnly
|
|
|
|
|
|
|
|
TagStyle* = enum
|
|
|
|
## Whether object should be serialized with explicit tags.
|
|
|
|
##
|
|
|
|
## - ``tsNone``: No tags will be outputted unless necessary.
|
|
|
|
## - ``tsRootOnly``: A tag will only be outputted for the root tag and
|
|
|
|
## where necessary.
|
|
|
|
## - ``tsAll``: Tags will be outputted for every object.
|
|
|
|
tsNone, tsRootOnly, tsAll
|
|
|
|
|
|
|
|
AnchorStyle* = enum
|
|
|
|
## How ref object should be serialized.
|
|
|
|
##
|
2022-09-07 14:23:50 +00:00
|
|
|
## - ``asNone``: No anchors will be written. Values present at
|
2016-09-20 19:53:38 +00:00
|
|
|
## multiple places in the content that should be serialized will be
|
|
|
|
## fully serialized at every occurence. If the content is cyclic, this
|
2022-09-07 14:23:50 +00:00
|
|
|
## will raise a YamlSerializationError.
|
2016-09-20 19:53:38 +00:00
|
|
|
## - ``asTidy``: Anchors will only be generated for objects that
|
|
|
|
## actually occur more than once in the content to be serialized.
|
|
|
|
## This is a bit slower and needs more memory than ``asAlways``.
|
|
|
|
## - ``asAlways``: Achors will be generated for every ref object in the
|
|
|
|
## content to be serialized, regardless of whether the object is
|
|
|
|
## referenced again afterwards
|
|
|
|
asNone, asTidy, asAlways
|
|
|
|
|
|
|
|
NewLineStyle* = enum
|
|
|
|
## What kind of newline sequence is used when presenting.
|
|
|
|
##
|
|
|
|
## - ``nlLF``: Use a single linefeed char as newline.
|
|
|
|
## - ``nlCRLF``: Use a sequence of carriage return and linefeed as
|
|
|
|
## newline.
|
|
|
|
## - ``nlOSDefault``: Use the target operation system's default newline
|
|
|
|
## sequence (CRLF on Windows, LF everywhere else).
|
|
|
|
nlLF, nlCRLF, nlOSDefault
|
|
|
|
|
|
|
|
OutputYamlVersion* = enum
|
|
|
|
## Specify which YAML version number the presenter shall emit. The
|
|
|
|
## presenter will always emit content that is valid YAML 1.1, but by
|
|
|
|
## default will write a directive ``%YAML 1.2``. For compatibility with
|
|
|
|
## other YAML implementations, it is possible to change this here.
|
|
|
|
##
|
|
|
|
## It is also possible to specify that the presenter shall not emit any
|
|
|
|
## YAML version. The generated content is then guaranteed to be valid
|
|
|
|
## YAML 1.1 and 1.2 (but not 1.0 or any newer YAML version).
|
|
|
|
ov1_2, ov1_1, ovNone
|
|
|
|
|
|
|
|
PresentationOptions* = object
|
|
|
|
## Options for generating a YAML character stream
|
|
|
|
style*: PresentationStyle
|
|
|
|
indentationStep*: int
|
|
|
|
newlines*: NewLineStyle
|
|
|
|
outputVersion*: OutputYamlVersion
|
2022-09-07 13:11:59 +00:00
|
|
|
maxLineLength*: Option[int]
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2020-11-03 21:08:21 +00:00
|
|
|
YamlPresenterJsonError* = object of ValueError
|
2016-09-20 19:53:38 +00:00
|
|
|
## Exception that may be raised by the YAML presenter when it is
|
|
|
|
## instructed to output JSON, but is unable to do so. This may occur if:
|
|
|
|
##
|
|
|
|
## - The given `YamlStream <#YamlStream>`_ contains a map which has any
|
|
|
|
## non-scalar type as key.
|
|
|
|
## - Any float scalar bears a ``NaN`` or positive/negative infinity value
|
|
|
|
|
2020-11-03 21:08:21 +00:00
|
|
|
YamlPresenterOutputError* = object of ValueError
|
2016-09-20 19:53:38 +00:00
|
|
|
## Exception that may be raised by the YAML presenter. This occurs if
|
|
|
|
## writing character data to the output stream raises any exception.
|
|
|
|
## The error that has occurred is available from ``parent``.
|
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
DumperState = enum
|
|
|
|
dBlockExplicitMapKey, dBlockImplicitMapKey, dBlockMapValue,
|
|
|
|
dBlockInlineMap, dBlockSequenceItem, dFlowImplicitMapKey, dFlowMapValue,
|
|
|
|
dFlowExplicitMapKey, dFlowSequenceItem, dFlowMapStart, dFlowSequenceStart
|
2016-08-30 20:15:29 +00:00
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
ScalarStyle = enum
|
|
|
|
sLiteral, sFolded, sPlain, sDoubleQuoted
|
2015-12-27 15:36:32 +00:00
|
|
|
|
2020-11-06 20:39:50 +00:00
|
|
|
Context = object
|
|
|
|
target: Stream
|
|
|
|
options: PresentationOptions
|
|
|
|
handles: seq[tuple[handle, uriPrefix: string]]
|
|
|
|
levels: seq[DumperState]
|
2016-09-19 17:33:29 +00:00
|
|
|
|
2016-09-20 19:53:38 +00:00
|
|
|
const
|
|
|
|
defaultPresentationOptions* =
|
2019-01-23 07:32:59 +00:00
|
|
|
PresentationOptions(style: psDefault, indentationStep: 2,
|
2022-09-07 13:11:59 +00:00
|
|
|
newlines: nlOSDefault, maxLineLength: some(80))
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-02-26 20:55:59 +00:00
|
|
|
proc defineOptions*(style: PresentationStyle = psDefault,
|
2016-03-20 09:27:09 +00:00
|
|
|
indentationStep: int = 2,
|
|
|
|
newlines: NewLineStyle = nlOSDefault,
|
2022-09-07 13:11:59 +00:00
|
|
|
outputVersion: OutputYamlVersion = ov1_2,
|
|
|
|
maxLineLength: Option[int] = some(80)):
|
2016-09-20 19:53:38 +00:00
|
|
|
PresentationOptions {.raises: [].} =
|
|
|
|
## Define a set of options for presentation. Convenience proc that requires
|
|
|
|
## you to only set those values that should not equal the default.
|
2016-04-02 15:48:22 +00:00
|
|
|
PresentationOptions(style: style, indentationStep: indentationStep,
|
2022-09-07 13:11:59 +00:00
|
|
|
newlines: newlines, outputVersion: outputVersion,
|
|
|
|
maxLineLength: maxLineLength)
|
2016-02-26 20:55:59 +00:00
|
|
|
|
2020-11-06 20:39:50 +00:00
|
|
|
proc state(c: Context): DumperState = c.levels[^1]
|
|
|
|
|
|
|
|
proc `state=`(c: var Context, v: DumperState) =
|
|
|
|
c.levels[^1] = v
|
|
|
|
|
|
|
|
proc searchHandle(c: Context, tag: string):
|
|
|
|
tuple[handle: string, len: int] {.raises: [].} =
|
|
|
|
## search in the registered tag handles for one whose prefix matches the start
|
|
|
|
## of the given tag. If multiple registered handles match, the one with the
|
|
|
|
## longest prefix is returned. If no registered handle matches, ("", 0) is
|
|
|
|
## returned.
|
|
|
|
result.len = 0
|
|
|
|
for item in c.handles:
|
|
|
|
if item.uriPrefix.len > result.len:
|
|
|
|
if tag.startsWith(item.uriPrefix):
|
|
|
|
result.len = item.uriPrefix.len
|
|
|
|
result.handle = item.handle
|
|
|
|
|
2016-02-25 20:04:44 +00:00
|
|
|
proc inspect(scalar: string, indentation: int,
|
2022-09-07 13:11:59 +00:00
|
|
|
words, lines: var seq[tuple[start, finish: int]],
|
|
|
|
lineLength: Option[int]):
|
2016-04-02 15:48:22 +00:00
|
|
|
ScalarStyle {.raises: [].} =
|
|
|
|
var
|
|
|
|
inLine = false
|
|
|
|
inWord = false
|
|
|
|
multipleSpaces = true
|
|
|
|
curWord, curLine: tuple[start, finish: int]
|
|
|
|
canUseFolded = true
|
|
|
|
canUseLiteral = true
|
|
|
|
canUsePlain = scalar.len > 0 and
|
|
|
|
scalar[0] notin {'@', '`', '|', '>', '&', '*', '!', ' ', '\t'}
|
|
|
|
for i, c in scalar:
|
|
|
|
case c
|
|
|
|
of ' ':
|
|
|
|
if inWord:
|
|
|
|
if not multipleSpaces:
|
|
|
|
curWord.finish = i - 1
|
|
|
|
inWord = false
|
|
|
|
else:
|
2016-02-25 20:04:44 +00:00
|
|
|
multipleSpaces = true
|
2016-04-02 15:48:22 +00:00
|
|
|
inWord = true
|
|
|
|
if not inLine:
|
|
|
|
inLine = true
|
|
|
|
curLine.start = i
|
|
|
|
# space at beginning of line will preserve previous and next
|
|
|
|
# linebreak. that is currently too complex to handle.
|
|
|
|
canUseFolded = false
|
|
|
|
of '\l':
|
2019-01-23 07:32:59 +00:00
|
|
|
canUsePlain = false # we don't use multiline plain scalars
|
2016-04-02 15:48:22 +00:00
|
|
|
curWord.finish = i - 1
|
2022-09-07 13:11:59 +00:00
|
|
|
if lineLength.isSome and
|
|
|
|
curWord.finish - curWord.start + 1 > lineLength.get() - indentation:
|
2016-04-02 15:48:22 +00:00
|
|
|
return if canUsePlain: sPlain else: sDoubleQuoted
|
|
|
|
words.add(curWord)
|
|
|
|
inWord = false
|
|
|
|
curWord.start = i + 1
|
|
|
|
multipleSpaces = true
|
|
|
|
if not inLine: curLine.start = i
|
|
|
|
inLine = false
|
|
|
|
curLine.finish = i - 1
|
2022-09-07 13:11:59 +00:00
|
|
|
if lineLength.isSome and
|
|
|
|
curLine.finish - curLine.start + 1 > lineLength.get() - indentation:
|
2016-04-02 15:48:22 +00:00
|
|
|
canUseLiteral = false
|
|
|
|
lines.add(curLine)
|
|
|
|
else:
|
|
|
|
if c in {'{', '}', '[', ']', ',', '#', '-', ':', '?', '%', '"', '\''} or
|
|
|
|
c.ord < 32: canUsePlain = false
|
|
|
|
if not inLine:
|
|
|
|
curLine.start = i
|
|
|
|
inLine = true
|
|
|
|
if not inWord:
|
|
|
|
if not multipleSpaces:
|
2022-09-07 13:11:59 +00:00
|
|
|
if lineLength.isSome and
|
|
|
|
curWord.finish - curWord.start + 1 > lineLength.get() - indentation:
|
2016-02-25 20:04:44 +00:00
|
|
|
return if canUsePlain: sPlain else: sDoubleQuoted
|
2016-04-02 15:48:22 +00:00
|
|
|
words.add(curWord)
|
|
|
|
curWord.start = i
|
|
|
|
inWord = true
|
|
|
|
multipleSpaces = false
|
|
|
|
if inWord:
|
|
|
|
curWord.finish = scalar.len - 1
|
2022-09-07 13:11:59 +00:00
|
|
|
if lineLength.isSome and
|
|
|
|
curWord.finish - curWord.start + 1 > lineLength.get() - indentation:
|
2016-04-02 15:48:22 +00:00
|
|
|
return if canUsePlain: sPlain else: sDoubleQuoted
|
|
|
|
words.add(curWord)
|
|
|
|
if inLine:
|
|
|
|
curLine.finish = scalar.len - 1
|
2022-09-07 13:11:59 +00:00
|
|
|
if lineLength.isSome and
|
|
|
|
curLine.finish - curLine.start + 1 > lineLength.get() - indentation:
|
2016-04-02 15:48:22 +00:00
|
|
|
canUseLiteral = false
|
|
|
|
lines.add(curLine)
|
2022-09-07 13:11:59 +00:00
|
|
|
if lineLength.isNone or scalar.len <= lineLength.get() - indentation:
|
2016-04-02 15:48:22 +00:00
|
|
|
result = if canUsePlain: sPlain else: sDoubleQuoted
|
|
|
|
elif canUseLiteral: result = sLiteral
|
|
|
|
elif canUseFolded: result = sFolded
|
|
|
|
elif canUsePlain: result = sPlain
|
|
|
|
else: result = sDoubleQuoted
|
2016-08-30 20:15:29 +00:00
|
|
|
|
2016-09-19 17:33:29 +00:00
|
|
|
template append(target: Stream, val: string | char) =
|
|
|
|
target.write(val)
|
|
|
|
|
|
|
|
template append(target: ptr[string], val: string | char) =
|
|
|
|
target[].add(val)
|
|
|
|
|
2020-11-06 20:39:50 +00:00
|
|
|
proc writeDoubleQuoted(c: Context, scalar: string, indentation: int,
|
2022-09-07 13:11:59 +00:00
|
|
|
newline: string, lineLength: Option[int])
|
2016-01-05 18:58:46 +00:00
|
|
|
{.raises: [YamlPresenterOutputError].} =
|
2016-04-02 15:48:22 +00:00
|
|
|
var curPos = indentation
|
2020-11-06 20:39:50 +00:00
|
|
|
let t = c.target
|
2016-04-02 15:48:22 +00:00
|
|
|
try:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append('"')
|
2016-04-02 15:48:22 +00:00
|
|
|
curPos.inc()
|
|
|
|
for c in scalar:
|
2022-09-07 13:11:59 +00:00
|
|
|
if lineLength.isSome and curPos == lineLength.get() - 1:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append('\\')
|
|
|
|
t.append(newline)
|
|
|
|
t.append(repeat(' ', indentation))
|
2016-04-02 15:48:22 +00:00
|
|
|
curPos = indentation
|
|
|
|
if c == ' ':
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append('\\')
|
2016-04-02 15:48:22 +00:00
|
|
|
curPos.inc()
|
|
|
|
case c
|
|
|
|
of '"':
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append("\\\"")
|
2016-04-02 15:48:22 +00:00
|
|
|
curPos.inc(2)
|
|
|
|
of '\l':
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append("\\n")
|
2016-04-02 15:48:22 +00:00
|
|
|
curPos.inc(2)
|
|
|
|
of '\t':
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append("\\t")
|
2016-04-02 15:48:22 +00:00
|
|
|
curPos.inc(2)
|
|
|
|
of '\\':
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append("\\\\")
|
2016-04-02 15:48:22 +00:00
|
|
|
curPos.inc(2)
|
|
|
|
else:
|
|
|
|
if ord(c) < 32:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append("\\x" & toHex(ord(c), 2))
|
2016-04-02 15:48:22 +00:00
|
|
|
curPos.inc(4)
|
|
|
|
else:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append(c)
|
2016-04-02 15:48:22 +00:00
|
|
|
curPos.inc()
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append('"')
|
2016-04-02 15:48:22 +00:00
|
|
|
except:
|
|
|
|
var e = newException(YamlPresenterOutputError,
|
|
|
|
"Error while writing to output stream")
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2016-02-25 21:32:50 +00:00
|
|
|
|
2020-11-06 20:39:50 +00:00
|
|
|
proc writeDoubleQuotedJson(c: Context, scalar: string)
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlPresenterOutputError].} =
|
2020-11-06 20:39:50 +00:00
|
|
|
let t = c.target
|
2016-04-02 15:48:22 +00:00
|
|
|
try:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append('"')
|
2016-04-02 15:48:22 +00:00
|
|
|
for c in scalar:
|
|
|
|
case c
|
2020-11-06 20:39:50 +00:00
|
|
|
of '"': t.append("\\\"")
|
|
|
|
of '\\': t.append("\\\\")
|
|
|
|
of '\l': t.append("\\n")
|
|
|
|
of '\t': t.append("\\t")
|
|
|
|
of '\f': t.append("\\f")
|
|
|
|
of '\b': t.append("\\b")
|
2016-04-02 15:48:22 +00:00
|
|
|
else:
|
2020-11-06 20:39:50 +00:00
|
|
|
if ord(c) < 32: t.append("\\u" & toHex(ord(c), 4)) else: t.append(c)
|
|
|
|
t.append('"')
|
2016-04-02 15:48:22 +00:00
|
|
|
except:
|
|
|
|
var e = newException(YamlPresenterOutputError,
|
|
|
|
"Error while writing to output stream")
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2016-02-25 20:04:44 +00:00
|
|
|
|
2020-11-06 20:39:50 +00:00
|
|
|
proc writeLiteral(c: Context, scalar: string, indentation, indentStep: int,
|
|
|
|
lines: seq[tuple[start, finish: int]], newline: string)
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlPresenterOutputError].} =
|
2020-11-06 20:39:50 +00:00
|
|
|
let t = c.target
|
2016-04-02 15:48:22 +00:00
|
|
|
try:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append('|')
|
|
|
|
if scalar[^1] != '\l': t.append('-')
|
|
|
|
if scalar[0] in [' ', '\t']: t.append($indentStep)
|
2016-04-02 15:48:22 +00:00
|
|
|
for line in lines:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append(newline)
|
|
|
|
t.append(repeat(' ', indentation + indentStep))
|
2016-04-02 15:48:22 +00:00
|
|
|
if line.finish >= line.start:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append(scalar[line.start .. line.finish])
|
2016-04-02 15:48:22 +00:00
|
|
|
except:
|
|
|
|
var e = newException(YamlPresenterOutputError,
|
|
|
|
"Error while writing to output stream")
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2016-02-25 20:04:44 +00:00
|
|
|
|
2020-11-06 20:39:50 +00:00
|
|
|
proc writeFolded(c: Context, scalar: string, indentation, indentStep: int,
|
|
|
|
words: seq[tuple[start, finish: int]],
|
2016-09-19 17:33:29 +00:00
|
|
|
newline: string)
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlPresenterOutputError].} =
|
2020-11-06 20:39:50 +00:00
|
|
|
let t = c.target
|
2022-09-07 13:11:59 +00:00
|
|
|
let lineLength = (
|
|
|
|
if c.options.maxLineLength.isSome: c.options.maxLineLength.get() else: 1024)
|
2016-04-02 15:48:22 +00:00
|
|
|
try:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append(">")
|
|
|
|
if scalar[^1] != '\l': t.append('-')
|
|
|
|
if scalar[0] in [' ', '\t']: t.append($indentStep)
|
2022-09-07 13:11:59 +00:00
|
|
|
var curPos = lineLength
|
2016-04-02 15:48:22 +00:00
|
|
|
for word in words:
|
|
|
|
if word.start > 0 and scalar[word.start - 1] == '\l':
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append(newline & newline)
|
|
|
|
t.append(repeat(' ', indentation + indentStep))
|
2016-04-02 15:48:22 +00:00
|
|
|
curPos = indentation + indentStep
|
2022-09-07 13:11:59 +00:00
|
|
|
elif curPos + (word.finish - word.start + 1) > lineLength:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append(newline)
|
|
|
|
t.append(repeat(' ', indentation + indentStep))
|
2016-04-02 15:48:22 +00:00
|
|
|
curPos = indentation + indentStep
|
|
|
|
else:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append(' ')
|
2016-04-02 15:48:22 +00:00
|
|
|
curPos.inc()
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append(scalar[word.start .. word.finish])
|
2016-04-02 15:48:22 +00:00
|
|
|
curPos += word.finish - word.start + 1
|
|
|
|
except:
|
|
|
|
var e = newException(YamlPresenterOutputError,
|
|
|
|
"Error while writing to output stream")
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2016-01-05 18:58:46 +00:00
|
|
|
|
2020-11-06 20:39:50 +00:00
|
|
|
template safeWrite(c: Context, s: string or char) =
|
|
|
|
try: c.target.append(s)
|
2016-04-02 15:48:22 +00:00
|
|
|
except:
|
|
|
|
var e = newException(YamlPresenterOutputError, "")
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2015-12-27 15:36:32 +00:00
|
|
|
|
2020-11-06 20:39:50 +00:00
|
|
|
proc startItem(c: var Context, indentation: int, isObject: bool,
|
2016-09-19 17:33:29 +00:00
|
|
|
newline: string) {.raises: [YamlPresenterOutputError].} =
|
2020-11-06 20:39:50 +00:00
|
|
|
let t = c.target
|
2016-04-02 15:48:22 +00:00
|
|
|
try:
|
2020-11-06 20:39:50 +00:00
|
|
|
case c.state
|
2016-04-02 15:48:22 +00:00
|
|
|
of dBlockMapValue:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append(newline)
|
|
|
|
t.append(repeat(' ', indentation))
|
|
|
|
if isObject or c.options.style == psCanonical:
|
|
|
|
t.append("? ")
|
|
|
|
c.state = dBlockExplicitMapKey
|
|
|
|
else: c.state = dBlockImplicitMapKey
|
|
|
|
of dBlockInlineMap: c.state = dBlockImplicitMapKey
|
2016-04-02 15:48:22 +00:00
|
|
|
of dBlockExplicitMapKey:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append(newline)
|
|
|
|
t.append(repeat(' ', indentation))
|
|
|
|
t.append(": ")
|
|
|
|
c.state = dBlockMapValue
|
2016-04-02 15:48:22 +00:00
|
|
|
of dBlockImplicitMapKey:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append(": ")
|
|
|
|
c.state = dBlockMapValue
|
2016-04-02 15:48:22 +00:00
|
|
|
of dFlowExplicitMapKey:
|
2020-11-06 20:39:50 +00:00
|
|
|
if c.options.style != psMinimal:
|
|
|
|
t.append(newline)
|
|
|
|
t.append(repeat(' ', indentation))
|
|
|
|
t.append(": ")
|
|
|
|
c.state = dFlowMapValue
|
2016-04-02 15:48:22 +00:00
|
|
|
of dFlowMapValue:
|
2020-11-06 20:39:50 +00:00
|
|
|
if (isObject and c.options.style != psMinimal) or c.options.style in [psJson, psCanonical]:
|
|
|
|
t.append(',' & newline & repeat(' ', indentation))
|
|
|
|
if c.options.style == psJson: c.state = dFlowImplicitMapKey
|
2016-04-02 15:48:22 +00:00
|
|
|
else:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append("? ")
|
|
|
|
c.state = dFlowExplicitMapKey
|
|
|
|
elif isObject and c.options.style == psMinimal:
|
|
|
|
t.append(", ? ")
|
|
|
|
c.state = dFlowExplicitMapKey
|
2016-04-02 15:48:22 +00:00
|
|
|
else:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append(", ")
|
|
|
|
c.state = dFlowImplicitMapKey
|
2016-04-02 15:48:22 +00:00
|
|
|
of dFlowMapStart:
|
2020-11-06 20:39:50 +00:00
|
|
|
if (isObject and c.options.style != psMinimal) or c.options.style in [psJson, psCanonical]:
|
|
|
|
t.append(newline & repeat(' ', indentation))
|
|
|
|
if c.options.style == psJson: c.state = dFlowImplicitMapKey
|
2016-04-02 15:48:22 +00:00
|
|
|
else:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append("? ")
|
|
|
|
c.state = dFlowExplicitMapKey
|
|
|
|
else: c.state = dFlowImplicitMapKey
|
2016-04-02 15:48:22 +00:00
|
|
|
of dFlowImplicitMapKey:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append(": ")
|
|
|
|
c.state = dFlowMapValue
|
2016-04-02 15:48:22 +00:00
|
|
|
of dBlockSequenceItem:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append(newline)
|
|
|
|
t.append(repeat(' ', indentation))
|
|
|
|
t.append("- ")
|
2016-04-02 15:48:22 +00:00
|
|
|
of dFlowSequenceStart:
|
2020-11-06 20:39:50 +00:00
|
|
|
case c.options.style
|
2016-04-02 15:48:22 +00:00
|
|
|
of psMinimal, psDefault: discard
|
|
|
|
of psCanonical, psJson:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append(newline)
|
|
|
|
t.append(repeat(' ', indentation))
|
2016-04-02 15:48:22 +00:00
|
|
|
of psBlockOnly: discard # can never happen
|
2020-11-06 20:39:50 +00:00
|
|
|
c.state = dFlowSequenceItem
|
2016-04-02 15:48:22 +00:00
|
|
|
of dFlowSequenceItem:
|
2020-11-06 20:39:50 +00:00
|
|
|
case c.options.style
|
|
|
|
of psMinimal, psDefault: t.append(", ")
|
2016-04-02 15:48:22 +00:00
|
|
|
of psCanonical, psJson:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append(',' & newline)
|
|
|
|
t.append(repeat(' ', indentation))
|
2016-04-02 15:48:22 +00:00
|
|
|
of psBlockOnly: discard # can never happen
|
|
|
|
except:
|
|
|
|
var e = newException(YamlPresenterOutputError, "")
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2016-01-28 20:59:26 +00:00
|
|
|
|
2020-11-06 20:39:50 +00:00
|
|
|
proc writeTagAndAnchor(c: Context, props: Properties) {.raises: [YamlPresenterOutputError].} =
|
|
|
|
let t = c.target
|
2016-04-02 15:48:22 +00:00
|
|
|
try:
|
2020-11-03 21:08:21 +00:00
|
|
|
if props.tag notin [yTagQuestionMark, yTagExclamationMark]:
|
2020-11-10 12:55:22 +00:00
|
|
|
let tagUri = $props.tag
|
2020-11-06 20:39:50 +00:00
|
|
|
let (handle, length) = c.searchHandle(tagUri)
|
2016-10-10 18:16:54 +00:00
|
|
|
if length > 0:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append(handle)
|
|
|
|
t.append(tagUri[length..tagUri.high])
|
|
|
|
t.append(' ')
|
2016-04-02 15:48:22 +00:00
|
|
|
else:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append("!<")
|
|
|
|
t.append(tagUri)
|
|
|
|
t.append("> ")
|
2020-11-03 21:08:21 +00:00
|
|
|
if props.anchor != yAnchorNone:
|
2020-11-06 20:39:50 +00:00
|
|
|
t.append("&")
|
|
|
|
t.append($props.anchor)
|
|
|
|
t.append(' ')
|
2016-04-02 15:48:22 +00:00
|
|
|
except:
|
|
|
|
var e = newException(YamlPresenterOutputError, "")
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2015-12-27 15:36:32 +00:00
|
|
|
|
2019-01-23 07:32:59 +00:00
|
|
|
proc nextItem(c: var Deque, s: var YamlStream):
|
2020-11-03 21:08:21 +00:00
|
|
|
Event {.raises: [YamlStreamError].} =
|
2016-06-26 10:37:15 +00:00
|
|
|
if c.len > 0:
|
2019-01-23 07:32:59 +00:00
|
|
|
try: result = c.popFirst
|
2020-11-03 21:08:21 +00:00
|
|
|
except IndexDefect: internalError("Unexpected IndexError")
|
2016-06-26 10:37:15 +00:00
|
|
|
else:
|
|
|
|
result = s.next()
|
|
|
|
|
2020-11-06 20:39:50 +00:00
|
|
|
proc doPresent(c: var Context, s: var YamlStream) =
|
2016-04-02 15:48:22 +00:00
|
|
|
var
|
|
|
|
indentation = 0
|
2020-11-03 21:08:21 +00:00
|
|
|
cached = initDeQue[Event]()
|
2020-11-06 20:39:50 +00:00
|
|
|
let newline = if c.options.newlines == nlLF: "\l"
|
|
|
|
elif c.options.newlines == nlCRLF: "\c\l" else: "\n"
|
2020-11-03 21:08:21 +00:00
|
|
|
var firstDoc = true
|
|
|
|
while true:
|
2016-06-26 10:37:15 +00:00
|
|
|
let item = nextItem(cached, s)
|
2016-04-02 15:48:22 +00:00
|
|
|
case item.kind
|
2020-11-03 21:08:21 +00:00
|
|
|
of yamlStartStream: discard
|
|
|
|
of yamlEndStream: break
|
2016-04-02 15:48:22 +00:00
|
|
|
of yamlStartDoc:
|
2020-11-06 20:39:50 +00:00
|
|
|
resetHandles(c.handles)
|
|
|
|
for v in item.handles:
|
|
|
|
discard registerHandle(c.handles, v.handle, v.uriPrefix)
|
2020-11-03 21:08:21 +00:00
|
|
|
if not firstDoc:
|
2020-11-06 20:39:50 +00:00
|
|
|
if c.options.style == psJson:
|
2020-11-03 21:08:21 +00:00
|
|
|
raise newException(YamlPresenterJsonError,
|
|
|
|
"Cannot output more than one document in JSON style")
|
2020-11-06 20:39:50 +00:00
|
|
|
c.safeWrite("..." & newline)
|
2020-11-03 21:08:21 +00:00
|
|
|
|
2020-11-06 20:39:50 +00:00
|
|
|
if c.options.style != psJson:
|
2016-04-02 15:48:22 +00:00
|
|
|
try:
|
2020-11-06 20:39:50 +00:00
|
|
|
case c.options.outputVersion
|
|
|
|
of ov1_2: c.target.append("%YAML 1.2" & newline)
|
|
|
|
of ov1_1: c.target.append("%YAML 1.1" & newLine)
|
2016-04-02 15:48:22 +00:00
|
|
|
of ovNone: discard
|
2020-11-06 20:39:50 +00:00
|
|
|
for v in c.handles:
|
|
|
|
if v.handle == "!":
|
|
|
|
if v.uriPrefix != "!":
|
|
|
|
c.target.append("%TAG ! " & v.uriPrefix & newline)
|
|
|
|
elif v.handle == "!!":
|
|
|
|
if v.uriPrefix != yamlTagRepositoryPrefix:
|
|
|
|
c.target.append("%TAG !! " & v.uriPrefix & newline)
|
2016-10-10 18:16:54 +00:00
|
|
|
else:
|
2020-11-06 20:39:50 +00:00
|
|
|
c.target.append("%TAG " & v.handle & ' ' & v.uriPrefix & newline)
|
|
|
|
c.target.append("--- ")
|
2016-04-02 15:48:22 +00:00
|
|
|
except:
|
|
|
|
var e = newException(YamlPresenterOutputError, "")
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
|
|
|
of yamlScalar:
|
2020-11-06 20:39:50 +00:00
|
|
|
if c.levels.len == 0:
|
|
|
|
if c.options.style != psJson: c.safeWrite(newline)
|
2016-04-02 15:48:22 +00:00
|
|
|
else:
|
2020-11-06 20:39:50 +00:00
|
|
|
c.startItem(indentation, false, newline)
|
|
|
|
if c.options.style != psJson:
|
|
|
|
c.writeTagAndAnchor(item.scalarProperties)
|
2016-08-30 20:15:29 +00:00
|
|
|
|
2020-11-06 20:39:50 +00:00
|
|
|
if c.options.style == psJson:
|
2016-04-02 15:48:22 +00:00
|
|
|
let hint = guessType(item.scalarContent)
|
2020-11-03 21:08:21 +00:00
|
|
|
let tag = item.scalarProperties.tag
|
|
|
|
if tag in [yTagQuestionMark, yTagBoolean] and
|
2016-04-02 15:48:22 +00:00
|
|
|
hint in {yTypeBoolTrue, yTypeBoolFalse}:
|
2020-11-06 20:39:50 +00:00
|
|
|
c.safeWrite(if hint == yTypeBoolTrue: "true" else: "false")
|
2020-11-03 21:08:21 +00:00
|
|
|
elif tag in [yTagQuestionMark, yTagNull] and
|
2016-04-02 15:48:22 +00:00
|
|
|
hint == yTypeNull:
|
2020-11-06 20:39:50 +00:00
|
|
|
c.safeWrite("null")
|
2020-11-03 21:08:21 +00:00
|
|
|
elif tag in [yTagQuestionMark, yTagInteger,
|
2016-04-02 15:48:22 +00:00
|
|
|
yTagNimInt8, yTagNimInt16, yTagNimInt32, yTagNimInt64,
|
|
|
|
yTagNimUInt8, yTagNimUInt16, yTagNimUInt32, yTagNimUInt64] and
|
|
|
|
hint == yTypeInteger:
|
2020-11-06 20:39:50 +00:00
|
|
|
c.safeWrite(item.scalarContent)
|
2020-11-03 21:08:21 +00:00
|
|
|
elif tag in [yTagQuestionMark, yTagFloat, yTagNimFloat32,
|
2016-04-02 15:48:22 +00:00
|
|
|
yTagNimFloat64] and hint in {yTypeFloatInf, yTypeFloatNaN}:
|
|
|
|
raise newException(YamlPresenterJsonError,
|
|
|
|
"Infinity and not-a-number values cannot be presented as JSON!")
|
2020-11-03 21:08:21 +00:00
|
|
|
elif tag in [yTagQuestionMark, yTagFloat] and
|
2016-04-02 15:48:22 +00:00
|
|
|
hint == yTypeFloat:
|
2020-11-06 20:39:50 +00:00
|
|
|
c.safeWrite(item.scalarContent)
|
|
|
|
else: c.writeDoubleQuotedJson(item.scalarContent)
|
|
|
|
elif c.options.style == psCanonical:
|
|
|
|
c.writeDoubleQuoted(item.scalarContent,
|
2022-09-07 13:11:59 +00:00
|
|
|
indentation + c.options.indentationStep, newline,
|
|
|
|
c.options.maxLineLength)
|
2016-04-02 15:48:22 +00:00
|
|
|
else:
|
|
|
|
var words, lines = newSeq[tuple[start, finish: int]]()
|
|
|
|
case item.scalarContent.inspect(
|
2022-09-07 13:11:59 +00:00
|
|
|
indentation + c.options.indentationStep, words, lines,
|
|
|
|
c.options.maxLineLength)
|
2020-11-06 20:39:50 +00:00
|
|
|
of sLiteral: c.writeLiteral(item.scalarContent, indentation,
|
|
|
|
c.options.indentationStep, lines, newline)
|
|
|
|
of sFolded: c.writeFolded(item.scalarContent, indentation,
|
|
|
|
c.options.indentationStep, words, newline)
|
|
|
|
of sPlain: c.safeWrite(item.scalarContent)
|
|
|
|
of sDoubleQuoted: c.writeDoubleQuoted(item.scalarContent,
|
2022-09-07 13:11:59 +00:00
|
|
|
indentation + c.options.indentationStep, newline,
|
|
|
|
c.options.maxLineLength)
|
2016-04-02 15:48:22 +00:00
|
|
|
of yamlAlias:
|
2020-11-06 20:39:50 +00:00
|
|
|
if c.options.style == psJson:
|
2016-04-02 15:48:22 +00:00
|
|
|
raise newException(YamlPresenterJsonError,
|
|
|
|
"Alias not allowed in JSON output")
|
2020-11-06 20:39:50 +00:00
|
|
|
yAssert c.levels.len > 0
|
|
|
|
c.startItem(indentation, false, newline)
|
2016-04-02 15:48:22 +00:00
|
|
|
try:
|
2020-11-06 20:39:50 +00:00
|
|
|
c.target.append('*')
|
|
|
|
c.target.append($item.aliasTarget)
|
2016-04-02 15:48:22 +00:00
|
|
|
except:
|
|
|
|
var e = newException(YamlPresenterOutputError, "")
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
|
|
|
of yamlStartSeq:
|
|
|
|
var nextState: DumperState
|
2020-11-06 20:39:50 +00:00
|
|
|
case c.options.style
|
2016-04-02 15:48:22 +00:00
|
|
|
of psDefault:
|
|
|
|
var length = 0
|
|
|
|
while true:
|
|
|
|
let next = s.next()
|
2019-01-23 07:32:59 +00:00
|
|
|
cached.addLast(next)
|
2016-04-02 15:48:22 +00:00
|
|
|
case next.kind
|
|
|
|
of yamlScalar: length += 2 + next.scalarContent.len
|
|
|
|
of yamlAlias: length += 6
|
|
|
|
of yamlEndSeq: break
|
|
|
|
else:
|
|
|
|
length = high(int)
|
|
|
|
break
|
|
|
|
nextState = if length <= 60: dFlowSequenceStart else: dBlockSequenceItem
|
|
|
|
of psJson:
|
2020-11-06 20:39:50 +00:00
|
|
|
if c.levels.len > 0 and c.state in [dFlowMapStart, dFlowMapValue]:
|
2019-01-24 02:44:21 +00:00
|
|
|
raise newException(YamlPresenterJsonError, "Cannot have sequence as map key in JSON output!")
|
2016-04-02 15:48:22 +00:00
|
|
|
nextState = dFlowSequenceStart
|
|
|
|
of psMinimal, psCanonical: nextState = dFlowSequenceStart
|
2017-01-10 10:45:55 +00:00
|
|
|
of psBlockOnly:
|
|
|
|
let next = s.peek()
|
|
|
|
if next.kind == yamlEndSeq: nextState = dFlowSequenceStart
|
|
|
|
else: nextState = dBlockSequenceItem
|
2016-08-30 20:15:29 +00:00
|
|
|
|
2020-11-06 20:39:50 +00:00
|
|
|
if c.levels.len == 0:
|
2016-06-08 18:01:46 +00:00
|
|
|
case nextState
|
|
|
|
of dBlockSequenceItem:
|
2020-11-06 20:39:50 +00:00
|
|
|
if c.options.style != psJson:
|
|
|
|
c.writeTagAndAnchor(item.seqProperties)
|
2016-06-08 18:01:46 +00:00
|
|
|
of dFlowSequenceStart:
|
2020-11-06 20:39:50 +00:00
|
|
|
c.safeWrite(newline)
|
|
|
|
if c.options.style != psJson:
|
|
|
|
c.writeTagAndAnchor(item.seqProperties)
|
|
|
|
indentation += c.options.indentationStep
|
2016-08-09 18:47:22 +00:00
|
|
|
else: internalError("Invalid nextState: " & $nextState)
|
2016-04-02 15:48:22 +00:00
|
|
|
else:
|
2020-11-06 20:39:50 +00:00
|
|
|
c.startItem(indentation, true, newline)
|
|
|
|
if c.options.style != psJson:
|
|
|
|
c.writeTagAndAnchor(item.seqProperties)
|
|
|
|
indentation += c.options.indentationStep
|
|
|
|
|
|
|
|
if nextState == dFlowSequenceStart: c.safeWrite('[')
|
|
|
|
if c.levels.len > 0 and c.options.style in [psJson, psCanonical] and
|
|
|
|
c.state in [dBlockExplicitMapKey, dBlockMapValue,
|
|
|
|
dBlockImplicitMapKey, dBlockSequenceItem]:
|
|
|
|
indentation += c.options.indentationStep
|
|
|
|
c.levels.add(nextState)
|
2016-04-02 15:48:22 +00:00
|
|
|
of yamlStartMap:
|
|
|
|
var nextState: DumperState
|
2020-11-06 20:39:50 +00:00
|
|
|
case c.options.style
|
2016-04-02 15:48:22 +00:00
|
|
|
of psDefault:
|
|
|
|
type MapParseState = enum
|
|
|
|
mpInitial, mpKey, mpValue, mpNeedBlock
|
|
|
|
var mps: MapParseState = mpInitial
|
|
|
|
while mps != mpNeedBlock:
|
|
|
|
case s.peek().kind
|
|
|
|
of yamlScalar, yamlAlias:
|
|
|
|
case mps
|
|
|
|
of mpInitial: mps = mpKey
|
|
|
|
of mpKey: mps = mpValue
|
|
|
|
else: mps = mpNeedBlock
|
|
|
|
of yamlEndMap: break
|
|
|
|
else: mps = mpNeedBlock
|
|
|
|
nextState = if mps == mpNeedBlock: dBlockMapValue else: dBlockInlineMap
|
|
|
|
of psMinimal: nextState = dFlowMapStart
|
|
|
|
of psCanonical: nextState = dFlowMapStart
|
|
|
|
of psJson:
|
2020-11-06 20:39:50 +00:00
|
|
|
if c.levels.len > 0 and c.state in [dFlowMapStart, dFlowMapValue]:
|
2016-04-02 15:48:22 +00:00
|
|
|
raise newException(YamlPresenterJsonError,
|
|
|
|
"Cannot have map as map key in JSON output!")
|
|
|
|
nextState = dFlowMapStart
|
2017-01-10 10:45:55 +00:00
|
|
|
of psBlockOnly:
|
|
|
|
let next = s.peek()
|
|
|
|
if next.kind == yamlEndMap: nextState = dFlowMapStart
|
|
|
|
else: nextState = dBlockMapValue
|
2020-11-06 20:39:50 +00:00
|
|
|
if c.levels.len == 0:
|
2016-06-08 18:01:46 +00:00
|
|
|
case nextState
|
|
|
|
of dBlockMapValue:
|
2020-11-06 20:39:50 +00:00
|
|
|
if c.options.style != psJson:
|
|
|
|
c.writeTagAndAnchor(item.mapProperties)
|
2016-04-02 15:48:22 +00:00
|
|
|
else:
|
2020-11-06 20:39:50 +00:00
|
|
|
if c.options.style != psJson:
|
|
|
|
c.safeWrite(newline)
|
|
|
|
c.writeTagAndAnchor(item.mapProperties)
|
|
|
|
indentation += c.options.indentationStep
|
2016-06-08 18:01:46 +00:00
|
|
|
of dFlowMapStart:
|
2020-11-06 20:39:50 +00:00
|
|
|
c.safeWrite(newline)
|
|
|
|
if c.options.style != psJson:
|
|
|
|
c.writeTagAndAnchor(item.mapProperties)
|
|
|
|
indentation += c.options.indentationStep
|
2016-06-08 18:01:46 +00:00
|
|
|
of dBlockInlineMap: discard
|
2016-08-09 18:47:22 +00:00
|
|
|
else: internalError("Invalid nextState: " & $nextState)
|
2016-04-02 15:48:22 +00:00
|
|
|
else:
|
|
|
|
if nextState in [dBlockMapValue, dBlockImplicitMapKey]:
|
2020-11-06 20:39:50 +00:00
|
|
|
c.startItem(indentation, true, newline)
|
|
|
|
if c.options.style != psJson:
|
|
|
|
c.writeTagAndAnchor(item.mapProperties)
|
2016-04-02 15:48:22 +00:00
|
|
|
else:
|
2020-11-06 20:39:50 +00:00
|
|
|
c.startItem(indentation, true, newline)
|
|
|
|
if c.options.style != psJson:
|
|
|
|
c.writeTagAndAnchor(item.mapProperties)
|
|
|
|
indentation += c.options.indentationStep
|
|
|
|
|
|
|
|
if nextState == dFlowMapStart: c.safeWrite('{')
|
|
|
|
if c.levels.len > 0 and c.options.style in [psJson, psCanonical] and
|
|
|
|
c.state in [dBlockExplicitMapKey, dBlockMapValue,
|
|
|
|
dBlockImplicitMapKey, dBlockImplicitMapKey,
|
|
|
|
dBlockSequenceItem]:
|
|
|
|
indentation += c.options.indentationStep
|
|
|
|
c.levels.add(nextState)
|
2016-08-30 20:15:29 +00:00
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
of yamlEndSeq:
|
2020-11-06 20:39:50 +00:00
|
|
|
yAssert c.levels.len > 0
|
|
|
|
case c.levels.pop()
|
2016-04-02 15:48:22 +00:00
|
|
|
of dFlowSequenceItem:
|
2020-11-06 20:39:50 +00:00
|
|
|
case c.options.style
|
|
|
|
of psDefault, psMinimal, psBlockOnly: c.safeWrite(']')
|
2016-04-02 15:48:22 +00:00
|
|
|
of psJson, psCanonical:
|
2020-11-06 20:39:50 +00:00
|
|
|
indentation -= c.options.indentationStep
|
2016-04-02 15:48:22 +00:00
|
|
|
try:
|
2020-11-06 20:39:50 +00:00
|
|
|
c.target.append(newline)
|
|
|
|
c.target.append(repeat(' ', indentation))
|
|
|
|
c.target.append(']')
|
2016-04-02 15:48:22 +00:00
|
|
|
except:
|
|
|
|
var e = newException(YamlPresenterOutputError, "")
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2020-11-06 20:39:50 +00:00
|
|
|
if c.levels.len == 0 or c.state notin
|
2016-04-02 15:48:22 +00:00
|
|
|
[dBlockExplicitMapKey, dBlockMapValue,
|
|
|
|
dBlockImplicitMapKey, dBlockSequenceItem]:
|
|
|
|
continue
|
|
|
|
of dFlowSequenceStart:
|
2020-11-06 20:39:50 +00:00
|
|
|
if c.levels.len > 0 and c.options.style in [psJson, psCanonical] and
|
|
|
|
c.state in [dBlockExplicitMapKey, dBlockMapValue,
|
|
|
|
dBlockImplicitMapKey, dBlockSequenceItem]:
|
|
|
|
indentation -= c.options.indentationStep
|
|
|
|
c.safeWrite(']')
|
2016-04-02 15:48:22 +00:00
|
|
|
of dBlockSequenceItem: discard
|
2016-08-09 18:47:22 +00:00
|
|
|
else: internalError("Invalid popped level")
|
2020-11-06 20:39:50 +00:00
|
|
|
indentation -= c.options.indentationStep
|
2016-04-02 15:48:22 +00:00
|
|
|
of yamlEndMap:
|
2020-11-06 20:39:50 +00:00
|
|
|
yAssert c.levels.len > 0
|
|
|
|
let level = c.levels.pop()
|
2016-04-02 15:48:22 +00:00
|
|
|
case level
|
|
|
|
of dFlowMapValue:
|
2020-11-06 20:39:50 +00:00
|
|
|
case c.options.style
|
|
|
|
of psDefault, psMinimal, psBlockOnly: c.safeWrite('}')
|
2016-04-02 15:48:22 +00:00
|
|
|
of psJson, psCanonical:
|
2020-11-06 20:39:50 +00:00
|
|
|
indentation -= c.options.indentationStep
|
2016-04-02 15:48:22 +00:00
|
|
|
try:
|
2020-11-06 20:39:50 +00:00
|
|
|
c.target.append(newline)
|
|
|
|
c.target.append(repeat(' ', indentation))
|
|
|
|
c.target.append('}')
|
2016-04-02 15:48:22 +00:00
|
|
|
except:
|
|
|
|
var e = newException(YamlPresenterOutputError, "")
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2020-11-06 20:39:50 +00:00
|
|
|
if c.levels.len == 0 or c.state notin
|
2016-04-02 15:48:22 +00:00
|
|
|
[dBlockExplicitMapKey, dBlockMapValue,
|
|
|
|
dBlockImplicitMapKey, dBlockSequenceItem]:
|
|
|
|
continue
|
|
|
|
of dFlowMapStart:
|
2020-11-06 20:39:50 +00:00
|
|
|
if c.levels.len > 0 and c.options.style in [psJson, psCanonical] and
|
|
|
|
c.state in [dBlockExplicitMapKey, dBlockMapValue,
|
|
|
|
dBlockImplicitMapKey, dBlockSequenceItem]:
|
|
|
|
indentation -= c.options.indentationStep
|
|
|
|
c.safeWrite('}')
|
2016-04-02 15:48:22 +00:00
|
|
|
of dBlockMapValue, dBlockInlineMap: discard
|
2016-08-09 18:47:22 +00:00
|
|
|
else: internalError("Invalid level: " & $level)
|
2020-11-06 20:39:50 +00:00
|
|
|
indentation -= c.options.indentationStep
|
2016-04-02 15:48:22 +00:00
|
|
|
of yamlEndDoc:
|
2020-11-03 21:08:21 +00:00
|
|
|
firstDoc = false
|
2022-09-07 13:11:59 +00:00
|
|
|
c.safeWrite(newline)
|
2015-12-27 15:36:32 +00:00
|
|
|
|
2016-09-19 17:33:29 +00:00
|
|
|
proc present*(s: var YamlStream, target: Stream,
|
2016-09-20 19:53:38 +00:00
|
|
|
options: PresentationOptions = defaultPresentationOptions)
|
|
|
|
{.raises: [YamlPresenterJsonError, YamlPresenterOutputError,
|
|
|
|
YamlStreamError].} =
|
|
|
|
## Convert ``s`` to a YAML character stream and write it to ``target``.
|
2021-03-23 17:51:05 +00:00
|
|
|
var c = Context(target: target, options: options)
|
2020-11-06 20:39:50 +00:00
|
|
|
doPresent(c, s)
|
2016-09-19 17:33:29 +00:00
|
|
|
|
2021-03-23 17:51:05 +00:00
|
|
|
proc present*(s: var YamlStream,
|
2016-09-19 17:33:29 +00:00
|
|
|
options: PresentationOptions = defaultPresentationOptions):
|
2016-09-20 19:53:38 +00:00
|
|
|
string {.raises: [YamlPresenterJsonError, YamlPresenterOutputError,
|
|
|
|
YamlStreamError].} =
|
|
|
|
## Convert ``s`` to a YAML character stream and return it as string.
|
2016-09-19 17:33:29 +00:00
|
|
|
|
2020-11-06 20:39:50 +00:00
|
|
|
var
|
|
|
|
ss = newStringStream()
|
2021-03-23 17:51:05 +00:00
|
|
|
c = Context(target: ss, options: options)
|
2020-11-06 20:39:50 +00:00
|
|
|
doPresent(c, s)
|
|
|
|
return ss.data
|
|
|
|
|
|
|
|
proc doTransform(c: var Context, input: Stream,
|
|
|
|
resolveToCoreYamlTags: bool) =
|
2021-03-23 17:51:05 +00:00
|
|
|
var parser: YamlParser
|
|
|
|
parser.init()
|
2020-11-04 15:40:37 +00:00
|
|
|
var events = parser.parse(input)
|
2016-04-02 15:48:22 +00:00
|
|
|
try:
|
2020-11-06 20:39:50 +00:00
|
|
|
if c.options.style == psCanonical:
|
2016-09-01 19:08:27 +00:00
|
|
|
var bys: YamlStream = newBufferYamlStream()
|
|
|
|
for e in events:
|
2016-12-05 17:27:32 +00:00
|
|
|
if resolveToCoreYamlTags:
|
|
|
|
var event = e
|
|
|
|
case event.kind
|
2020-11-06 20:39:50 +00:00
|
|
|
of yamlStartStream, yamlEndStream, yamlStartDoc, yamlEndDoc, yamlEndMap, yamlAlias, yamlEndSeq:
|
2016-12-05 17:27:32 +00:00
|
|
|
discard
|
|
|
|
of yamlStartMap:
|
2020-11-06 20:39:50 +00:00
|
|
|
if event.mapProperties.tag in [yTagQuestionMark, yTagExclamationMark]:
|
|
|
|
event.mapProperties.tag = yTagMapping
|
2016-12-05 17:27:32 +00:00
|
|
|
of yamlStartSeq:
|
2020-11-06 20:39:50 +00:00
|
|
|
if event.seqProperties.tag in [yTagQuestionMark, yTagExclamationMark]:
|
|
|
|
event.seqProperties.tag = yTagSequence
|
2016-12-05 17:27:32 +00:00
|
|
|
of yamlScalar:
|
2020-11-06 20:39:50 +00:00
|
|
|
if event.scalarProperties.tag == yTagQuestionMark:
|
2016-12-05 17:27:32 +00:00
|
|
|
case guessType(event.scalarContent)
|
2020-11-06 20:39:50 +00:00
|
|
|
of yTypeInteger: event.scalarProperties.tag = yTagInteger
|
2016-12-05 17:27:32 +00:00
|
|
|
of yTypeFloat, yTypeFloatInf, yTypeFloatNaN:
|
2020-11-06 20:39:50 +00:00
|
|
|
event.scalarProperties.tag = yTagFloat
|
|
|
|
of yTypeBoolTrue, yTypeBoolFalse: event.scalarProperties.tag = yTagBoolean
|
|
|
|
of yTypeNull: event.scalarProperties.tag = yTagNull
|
|
|
|
of yTypeTimestamp: event.scalarProperties.tag = yTagTimestamp
|
|
|
|
of yTypeUnknown: event.scalarProperties.tag = yTagString
|
|
|
|
elif event.scalarProperties.tag == yTagExclamationMark:
|
|
|
|
event.scalarProperties.tag = yTagString
|
2016-12-05 17:27:32 +00:00
|
|
|
BufferYamlStream(bys).put(event)
|
|
|
|
else: BufferYamlStream(bys).put(e)
|
2020-11-06 20:39:50 +00:00
|
|
|
doPresent(c, bys)
|
2016-12-13 20:22:36 +00:00
|
|
|
else:
|
2020-11-06 20:39:50 +00:00
|
|
|
doPresent(c, events)
|
2016-04-02 15:48:22 +00:00
|
|
|
except YamlStreamError:
|
|
|
|
var e = getCurrentException()
|
|
|
|
while e.parent of YamlStreamError: e = e.parent
|
|
|
|
if e.parent of IOError: raise (ref IOError)(e.parent)
|
2020-11-10 20:28:56 +00:00
|
|
|
elif e.parent of OSError: raise (ref OSError)(e.parent)
|
2016-04-02 15:48:22 +00:00
|
|
|
elif e.parent of YamlParserError: raise (ref YamlParserError)(e.parent)
|
2016-08-09 18:47:22 +00:00
|
|
|
else: internalError("Unexpected exception: " & e.parent.repr)
|
2016-09-19 17:33:29 +00:00
|
|
|
|
2020-11-06 20:39:50 +00:00
|
|
|
proc genInput(input: Stream): Stream = input
|
|
|
|
proc genInput(input: string): Stream = newStringStream(input)
|
|
|
|
|
2016-09-19 17:33:29 +00:00
|
|
|
proc transform*(input: Stream | string, output: Stream,
|
2016-12-05 17:27:32 +00:00
|
|
|
options: PresentationOptions = defaultPresentationOptions,
|
|
|
|
resolveToCoreYamlTags: bool = false)
|
2020-11-10 20:28:56 +00:00
|
|
|
{.raises: [IOError, OSError, YamlParserError, YamlPresenterJsonError,
|
2016-09-20 19:53:38 +00:00
|
|
|
YamlPresenterOutputError].} =
|
|
|
|
## Parser ``input`` as YAML character stream and then dump it to ``output``
|
|
|
|
## while resolving non-specific tags to the ones in the YAML core tag
|
2016-12-05 17:27:32 +00:00
|
|
|
## library. If ``resolveToCoreYamlTags`` is ``true``, non-specific tags will
|
|
|
|
## be replaced by specific tags according to the YAML core schema.
|
2021-05-17 17:50:10 +00:00
|
|
|
var c = Context(target: output, options: options)
|
|
|
|
doTransform(c, genInput(input), resolveToCoreYamlTags)
|
2016-09-19 17:33:29 +00:00
|
|
|
|
|
|
|
proc transform*(input: Stream | string,
|
2016-12-05 17:27:32 +00:00
|
|
|
options: PresentationOptions = defaultPresentationOptions,
|
|
|
|
resolveToCoreYamlTags: bool = false):
|
2020-11-10 20:28:56 +00:00
|
|
|
string {.raises: [IOError, OSError, YamlParserError, YamlPresenterJsonError,
|
2016-09-20 19:53:38 +00:00
|
|
|
YamlPresenterOutputError].} =
|
|
|
|
## Parser ``input`` as YAML character stream, resolves non-specific tags to
|
|
|
|
## the ones in the YAML core tag library, and then returns a serialized
|
2016-12-05 17:27:32 +00:00
|
|
|
## 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.
|
2021-05-17 17:50:10 +00:00
|
|
|
var
|
|
|
|
ss = newStringStream()
|
|
|
|
c = Context(target: ss, options: options)
|
|
|
|
doTransform(c, genInput(input), resolveToCoreYamlTags)
|
2022-01-22 00:18:11 +00:00
|
|
|
return ss.data
|