2015-12-28 22:24:05 +01:00
|
|
|
# NimYAML - YAML implementation in Nim
|
2023-08-29 23:23:15 +02:00
|
|
|
# (c) Copyright 2015-2023 Felix Krause
|
2015-12-28 22:24:05 +01:00
|
|
|
#
|
|
|
|
# See the file "copying.txt", included in this
|
|
|
|
# distribution, for details about the copyright.
|
|
|
|
|
2016-09-21 21:20:57 +02:00
|
|
|
## =====================
|
2021-05-18 00:31:47 +02:00
|
|
|
## Module yaml/presenter
|
2016-09-21 21:20:57 +02:00
|
|
|
## =====================
|
|
|
|
##
|
|
|
|
## This is the presenter API, used for generating YAML character streams.
|
|
|
|
|
2022-09-07 15:11:59 +02:00
|
|
|
import std / [streams, deques, strutils, options]
|
2021-10-22 17:46:05 +02:00
|
|
|
import data, taglib, stream, private/internal, hints, parser
|
2016-09-20 21:53:38 +02:00
|
|
|
|
2015-12-27 16:36:32 +01:00
|
|
|
type
|
2016-09-20 21:53:38 +02:00
|
|
|
|
2023-07-31 19:16:24 +02:00
|
|
|
ContainerStyle* = enum
|
|
|
|
## How to serialize containers nodes.
|
|
|
|
##
|
|
|
|
## - ``cBlock`` writes all container nodes in block style,
|
|
|
|
## i.e. indentation-based.
|
|
|
|
## - ``cFlow`` writes all container nodes in flow style,
|
|
|
|
## i.e. JSON-like.
|
|
|
|
## - ``cMixed`` writes container nodes that only contain alias nodes
|
|
|
|
## and short scalar nodes in flow style, all other container nodes
|
|
|
|
## in block style.
|
|
|
|
cBlock, cFlow, cMixed
|
|
|
|
|
2016-09-20 21:53:38 +02:00
|
|
|
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).
|
2023-07-31 19:16:24 +02:00
|
|
|
## - ``nlNone``: Don't use newlines, write everything in one line.
|
|
|
|
## forces ContainerStyle cFlow.
|
|
|
|
nlLF, nlCRLF, nlOSDefault, nlNone
|
2016-09-20 21:53:38 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-07-31 19:16:24 +02:00
|
|
|
ScalarQuotingStyle* = enum
|
|
|
|
## Specifies whether scalars should forcibly be double-quoted.
|
|
|
|
## - ``sqUnset``: Quote where necessary
|
|
|
|
## - ``sqDouble``: Force double-quoted style for every scalar
|
|
|
|
## - ``sqJson``: Force JSON-compatible double-quoted style for every scalar
|
|
|
|
## except for scalars of other JSON types (bool, int, double)
|
|
|
|
sqUnset, sqDouble, sqJson
|
|
|
|
|
|
|
|
DirectivesEndStyle* = enum
|
|
|
|
## Whether to write a directives end marker '---'
|
|
|
|
## - ``deAlways``: Always write it.
|
|
|
|
## - ``deIfNecessary``: Write it if any directive has been written,
|
|
|
|
## or if the root node has an explicit tag
|
2023-08-29 20:46:26 +02:00
|
|
|
## - ``deNever``: Don't write it. Suppresses output of directives
|
2023-07-31 19:16:24 +02:00
|
|
|
deAlways, deIfNecessary, deNever
|
|
|
|
|
2016-09-20 21:53:38 +02:00
|
|
|
PresentationOptions* = object
|
|
|
|
## Options for generating a YAML character stream
|
2023-08-29 20:46:26 +02:00
|
|
|
containers* : ContainerStyle = cMixed ## how mappings and sequences are presented
|
|
|
|
indentationStep*: int = 2 ## how many spaces a new level should be indented
|
|
|
|
newlines* : NewLineStyle = nlOSDefault ## kind of newline sequence to use
|
|
|
|
outputVersion* : OutputYamlVersion = ovNone ## whether to write the %YAML tag
|
|
|
|
maxLineLength* : Option[int] = some(80) ## max length of a line, including indentation
|
|
|
|
directivesEnd* : DirectivesEndStyle = deIfNecessary ## whether to write '---' after tags
|
|
|
|
suppressAttrs* : bool = false ## whether to suppress all attributes on nodes
|
|
|
|
quoting* : ScalarQuotingStyle = sqUnset ## how scalars are quoted
|
|
|
|
condenseFlow* : bool = true ## whether non-nested flow containers use a single line
|
|
|
|
explicitKeys* : bool = false ## whether mapping keys should always use '?'
|
2016-09-20 21:53:38 +02:00
|
|
|
|
2020-11-03 22:08:21 +01:00
|
|
|
YamlPresenterJsonError* = object of ValueError
|
2016-09-20 21:53:38 +02: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 22:08:21 +01:00
|
|
|
YamlPresenterOutputError* = object of ValueError
|
2016-09-20 21:53:38 +02: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 17:48:22 +02:00
|
|
|
DumperState = enum
|
|
|
|
dBlockExplicitMapKey, dBlockImplicitMapKey, dBlockMapValue,
|
|
|
|
dBlockInlineMap, dBlockSequenceItem, dFlowImplicitMapKey, dFlowMapValue,
|
|
|
|
dFlowExplicitMapKey, dFlowSequenceItem, dFlowMapStart, dFlowSequenceStart
|
2023-07-31 19:16:24 +02:00
|
|
|
|
|
|
|
DumperLevel = tuple
|
|
|
|
state: DumperState
|
|
|
|
indentation: int
|
|
|
|
singleLine: bool
|
|
|
|
wroteAnything: bool
|
2016-08-30 22:15:29 +02:00
|
|
|
|
2016-04-02 17:48:22 +02:00
|
|
|
ScalarStyle = enum
|
|
|
|
sLiteral, sFolded, sPlain, sDoubleQuoted
|
2015-12-27 16:36:32 +01:00
|
|
|
|
2020-11-06 21:39:50 +01:00
|
|
|
Context = object
|
|
|
|
target: Stream
|
|
|
|
options: PresentationOptions
|
|
|
|
handles: seq[tuple[handle, uriPrefix: string]]
|
2023-07-31 19:16:24 +02:00
|
|
|
levels: seq[DumperLevel]
|
|
|
|
needsWhitespace: int
|
2016-09-19 19:33:29 +02:00
|
|
|
|
2023-07-31 19:16:24 +02:00
|
|
|
proc level(ctx: var Context): var DumperLevel = ctx.levels[^1]
|
|
|
|
|
|
|
|
proc level(ctx: Context): DumperLevel = ctx.levels[^1]
|
|
|
|
|
|
|
|
proc state(ctx: Context): DumperState = ctx.level.state
|
2020-11-06 21:39:50 +01:00
|
|
|
|
2023-07-31 19:16:24 +02:00
|
|
|
proc `state=`(ctx: var Context, v: DumperState) =
|
|
|
|
ctx.level.state = v
|
2020-11-06 21:39:50 +01:00
|
|
|
|
2023-07-31 19:16:24 +02:00
|
|
|
proc indentation(ctx: Context): int =
|
|
|
|
result = if ctx.levels.len == 0: 0 else: ctx.level.indentation
|
|
|
|
|
|
|
|
proc searchHandle(ctx: Context, tag: string):
|
2020-11-06 21:39:50 +01:00
|
|
|
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
|
2023-07-31 19:16:24 +02:00
|
|
|
for item in ctx.handles:
|
2020-11-06 21:39:50 +01:00
|
|
|
if item.uriPrefix.len > result.len:
|
|
|
|
if tag.startsWith(item.uriPrefix):
|
|
|
|
result.len = item.uriPrefix.len
|
|
|
|
result.handle = item.handle
|
|
|
|
|
2023-08-29 23:23:15 +02:00
|
|
|
proc inspect(
|
|
|
|
scalar : string,
|
|
|
|
indentation : int,
|
|
|
|
words, lines: var seq[tuple[start, finish: int]],
|
|
|
|
lineLength : Option[int],
|
|
|
|
): ScalarStyle {.raises: [].} =
|
2016-04-02 17:48:22 +02:00
|
|
|
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 21:04:44 +01:00
|
|
|
multipleSpaces = true
|
2016-04-02 17:48:22 +02:00
|
|
|
inWord = true
|
|
|
|
if not inLine:
|
|
|
|
inLine = true
|
|
|
|
curLine.start = i
|
|
|
|
# space at beginning of line will preserve previous and next
|
2023-07-31 19:16:24 +02:00
|
|
|
# line break. that is currently too complex to handle.
|
2016-04-02 17:48:22 +02:00
|
|
|
canUseFolded = false
|
|
|
|
of '\l':
|
2019-01-23 09:32:59 +02:00
|
|
|
canUsePlain = false # we don't use multiline plain scalars
|
2016-04-02 17:48:22 +02:00
|
|
|
curWord.finish = i - 1
|
2022-09-07 15:11:59 +02:00
|
|
|
if lineLength.isSome and
|
|
|
|
curWord.finish - curWord.start + 1 > lineLength.get() - indentation:
|
2016-04-02 17:48:22 +02: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 15:11:59 +02:00
|
|
|
if lineLength.isSome and
|
|
|
|
curLine.finish - curLine.start + 1 > lineLength.get() - indentation:
|
2016-04-02 17:48:22 +02: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 15:11:59 +02:00
|
|
|
if lineLength.isSome and
|
|
|
|
curWord.finish - curWord.start + 1 > lineLength.get() - indentation:
|
2016-02-25 21:04:44 +01:00
|
|
|
return if canUsePlain: sPlain else: sDoubleQuoted
|
2016-04-02 17:48:22 +02:00
|
|
|
words.add(curWord)
|
|
|
|
curWord.start = i
|
|
|
|
inWord = true
|
|
|
|
multipleSpaces = false
|
|
|
|
if inWord:
|
|
|
|
curWord.finish = scalar.len - 1
|
2022-09-07 15:11:59 +02:00
|
|
|
if lineLength.isSome and
|
|
|
|
curWord.finish - curWord.start + 1 > lineLength.get() - indentation:
|
2016-04-02 17:48:22 +02:00
|
|
|
return if canUsePlain: sPlain else: sDoubleQuoted
|
|
|
|
words.add(curWord)
|
|
|
|
if inLine:
|
|
|
|
curLine.finish = scalar.len - 1
|
2022-09-07 15:11:59 +02:00
|
|
|
if lineLength.isSome and
|
|
|
|
curLine.finish - curLine.start + 1 > lineLength.get() - indentation:
|
2016-04-02 17:48:22 +02:00
|
|
|
canUseLiteral = false
|
|
|
|
lines.add(curLine)
|
2022-09-07 15:11:59 +02:00
|
|
|
if lineLength.isNone or scalar.len <= lineLength.get() - indentation:
|
2016-04-02 17:48:22 +02: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 22:15:29 +02:00
|
|
|
|
2023-07-31 19:16:24 +02:00
|
|
|
proc append(ctx: var Context, val: string | char) {.inline.} =
|
|
|
|
if ctx.needsWhitespace > 0:
|
|
|
|
ctx.target.write(repeat(' ', ctx.needsWhitespace))
|
|
|
|
ctx.needsWhitespace = 0
|
|
|
|
ctx.target.write(val)
|
|
|
|
if ctx.levels.len > 0: ctx.level.wroteAnything = true
|
|
|
|
|
|
|
|
proc whitespace(ctx: var Context, single: bool = false) {.inline.} =
|
|
|
|
if single or ctx.options.indentationStep == 1: ctx.needsWhitespace = 1
|
|
|
|
else: ctx.needsWhitespace = ctx.options.indentationStep - 1
|
|
|
|
|
|
|
|
proc newline(ctx: var Context) {.inline.} =
|
|
|
|
case ctx.options.newlines
|
|
|
|
of nlCRLF: ctx.target.write("\c\l")
|
|
|
|
of nlLF: ctx.target.write("\l")
|
|
|
|
else: ctx.target.write("\n")
|
|
|
|
ctx.needsWhitespace = 0
|
|
|
|
if ctx.levels.len > 0: ctx.level.wroteAnything = true
|
|
|
|
|
2023-08-29 23:23:15 +02:00
|
|
|
proc writeDoubleQuoted(
|
|
|
|
ctx : var Context,
|
|
|
|
scalar: string,
|
|
|
|
) {.raises: [YamlPresenterOutputError].} =
|
2023-07-31 19:16:24 +02:00
|
|
|
let indentation = ctx.indentation + ctx.options.indentationStep
|
2016-04-02 17:48:22 +02:00
|
|
|
var curPos = indentation
|
2023-07-31 19:16:24 +02:00
|
|
|
let t = ctx.target
|
2016-04-02 17:48:22 +02:00
|
|
|
try:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.append('"')
|
2016-04-02 17:48:22 +02:00
|
|
|
curPos.inc()
|
|
|
|
for c in scalar:
|
2023-07-31 19:16:24 +02:00
|
|
|
if ctx.options.maxLineLength.isSome and
|
|
|
|
curPos == ctx.options.maxLineLength.get() - 1:
|
|
|
|
t.write('\\')
|
|
|
|
ctx.newline()
|
|
|
|
t.write(repeat(' ', indentation))
|
2016-04-02 17:48:22 +02:00
|
|
|
curPos = indentation
|
|
|
|
if c == ' ':
|
2023-07-31 19:16:24 +02:00
|
|
|
t.write('\\')
|
2016-04-02 17:48:22 +02:00
|
|
|
curPos.inc()
|
|
|
|
case c
|
|
|
|
of '"':
|
2023-07-31 19:16:24 +02:00
|
|
|
t.write("\\\"")
|
2016-04-02 17:48:22 +02:00
|
|
|
curPos.inc(2)
|
|
|
|
of '\l':
|
2023-07-31 19:16:24 +02:00
|
|
|
t.write("\\n")
|
2016-04-02 17:48:22 +02:00
|
|
|
curPos.inc(2)
|
|
|
|
of '\t':
|
2023-07-31 19:16:24 +02:00
|
|
|
t.write("\\t")
|
2016-04-02 17:48:22 +02:00
|
|
|
curPos.inc(2)
|
|
|
|
of '\\':
|
2023-07-31 19:16:24 +02:00
|
|
|
t.write("\\\\")
|
2016-04-02 17:48:22 +02:00
|
|
|
curPos.inc(2)
|
|
|
|
else:
|
|
|
|
if ord(c) < 32:
|
2023-07-31 19:16:24 +02:00
|
|
|
t.write("\\x" & toHex(ord(c), 2))
|
2016-04-02 17:48:22 +02:00
|
|
|
curPos.inc(4)
|
|
|
|
else:
|
2023-07-31 19:16:24 +02:00
|
|
|
t.write(c)
|
2016-04-02 17:48:22 +02:00
|
|
|
curPos.inc()
|
2023-07-31 19:16:24 +02:00
|
|
|
t.write('"')
|
2023-03-18 13:54:45 +01:00
|
|
|
except CatchableError as ce:
|
2016-04-02 17:48:22 +02:00
|
|
|
var e = newException(YamlPresenterOutputError,
|
|
|
|
"Error while writing to output stream")
|
2023-03-18 13:54:45 +01:00
|
|
|
e.parent = ce
|
2016-04-02 17:48:22 +02:00
|
|
|
raise e
|
2016-02-25 22:32:50 +01:00
|
|
|
|
2023-08-29 23:23:15 +02:00
|
|
|
proc writeDoubleQuotedJson(
|
|
|
|
ctx : var Context,
|
|
|
|
scalar: string,
|
|
|
|
) {.raises: [YamlPresenterOutputError].} =
|
2023-07-31 19:16:24 +02:00
|
|
|
let t = ctx.target
|
2016-04-02 17:48:22 +02:00
|
|
|
try:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.append('"')
|
2016-04-02 17:48:22 +02:00
|
|
|
for c in scalar:
|
|
|
|
case c
|
2023-07-31 19:16:24 +02:00
|
|
|
of '"': t.write("\\\"")
|
|
|
|
of '\\': t.write("\\\\")
|
|
|
|
of '\l': t.write("\\n")
|
|
|
|
of '\t': t.write("\\t")
|
|
|
|
of '\f': t.write("\\f")
|
|
|
|
of '\b': t.write("\\b")
|
2016-04-02 17:48:22 +02:00
|
|
|
else:
|
2023-07-31 19:16:24 +02:00
|
|
|
if ord(c) < 32: t.write("\\u" & toHex(ord(c), 4)) else: t.write(c)
|
|
|
|
t.write('"')
|
2016-04-02 17:48:22 +02:00
|
|
|
except:
|
|
|
|
var e = newException(YamlPresenterOutputError,
|
|
|
|
"Error while writing to output stream")
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2016-02-25 21:04:44 +01:00
|
|
|
|
2023-07-31 19:16:24 +02:00
|
|
|
proc writeLiteral(
|
2023-08-29 23:23:15 +02:00
|
|
|
ctx : var Context,
|
|
|
|
scalar: string,
|
|
|
|
lines : seq[tuple[start, finish: int]],
|
|
|
|
) {.raises: [YamlPresenterOutputError].} =
|
2023-07-31 19:16:24 +02:00
|
|
|
let indentation = ctx.indentation + ctx.options.indentationStep
|
|
|
|
let t = ctx.target
|
2016-04-02 17:48:22 +02:00
|
|
|
try:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.append('|')
|
|
|
|
if scalar[^1] != '\l': t.write('-')
|
|
|
|
if scalar[0] in [' ', '\t']: t.write($ctx.options.indentationStep)
|
2016-04-02 17:48:22 +02:00
|
|
|
for line in lines:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.newline()
|
|
|
|
t.write(repeat(' ', indentation))
|
2016-04-02 17:48:22 +02:00
|
|
|
if line.finish >= line.start:
|
2023-07-31 19:16:24 +02:00
|
|
|
t.write(scalar[line.start .. line.finish])
|
2023-03-18 13:54:45 +01:00
|
|
|
except CatchableError as ce:
|
2016-04-02 17:48:22 +02:00
|
|
|
var e = newException(YamlPresenterOutputError,
|
|
|
|
"Error while writing to output stream")
|
2023-03-18 13:54:45 +01:00
|
|
|
e.parent = ce
|
2016-04-02 17:48:22 +02:00
|
|
|
raise e
|
2016-02-25 21:04:44 +01:00
|
|
|
|
2023-08-29 23:23:15 +02:00
|
|
|
proc writeFolded(
|
|
|
|
ctx : var Context,
|
|
|
|
scalar: string,
|
|
|
|
words : seq[tuple[start, finish: int]],
|
|
|
|
) {.raises: [YamlPresenterOutputError].} =
|
2023-07-31 19:16:24 +02:00
|
|
|
let t = ctx.target
|
|
|
|
let indentation = ctx.indentation + ctx.options.indentationStep
|
2022-09-07 15:11:59 +02:00
|
|
|
let lineLength = (
|
2023-07-31 19:16:24 +02:00
|
|
|
if ctx.options.maxLineLength.isSome:
|
|
|
|
ctx.options.maxLineLength.get() else: 1024)
|
2016-04-02 17:48:22 +02:00
|
|
|
try:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.append('>')
|
|
|
|
if scalar[^1] != '\l': t.write('-')
|
|
|
|
if scalar[0] in [' ', '\t']: t.write($ctx.options.indentationStep)
|
2022-09-07 15:11:59 +02:00
|
|
|
var curPos = lineLength
|
2016-04-02 17:48:22 +02:00
|
|
|
for word in words:
|
|
|
|
if word.start > 0 and scalar[word.start - 1] == '\l':
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.newline()
|
|
|
|
ctx.newline()
|
|
|
|
t.write(repeat(' ', indentation))
|
|
|
|
curPos = indentation
|
2022-09-07 15:11:59 +02:00
|
|
|
elif curPos + (word.finish - word.start + 1) > lineLength:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.newline()
|
|
|
|
t.write(repeat(' ', indentation))
|
|
|
|
curPos = indentation
|
2016-04-02 17:48:22 +02:00
|
|
|
else:
|
2023-07-31 19:16:24 +02:00
|
|
|
t.write(' ')
|
2016-04-02 17:48:22 +02:00
|
|
|
curPos.inc()
|
2023-07-31 19:16:24 +02:00
|
|
|
t.write(scalar[word.start .. word.finish])
|
2016-04-02 17:48:22 +02:00
|
|
|
curPos += word.finish - word.start + 1
|
2023-03-18 13:54:45 +01:00
|
|
|
except CatchableError as ce:
|
2016-04-02 17:48:22 +02:00
|
|
|
var e = newException(YamlPresenterOutputError,
|
|
|
|
"Error while writing to output stream")
|
2023-03-18 13:54:45 +01:00
|
|
|
e.parent = ce
|
2016-04-02 17:48:22 +02:00
|
|
|
raise e
|
2016-01-05 19:58:46 +01:00
|
|
|
|
2023-07-31 19:16:24 +02:00
|
|
|
template safeWrite(ctx: var Context, s: string or char) =
|
|
|
|
try: ctx.append(s)
|
|
|
|
except CatchableError as ce:
|
|
|
|
var e = newException(YamlPresenterOutputError, "")
|
|
|
|
e.parent = ce
|
|
|
|
raise e
|
|
|
|
|
|
|
|
template safeNewline(c: var Context) =
|
|
|
|
try: ctx.newline()
|
2023-03-18 13:54:45 +01:00
|
|
|
except CatchableError as ce:
|
2016-04-02 17:48:22 +02:00
|
|
|
var e = newException(YamlPresenterOutputError, "")
|
2023-03-18 13:54:45 +01:00
|
|
|
e.parent = ce
|
2016-04-02 17:48:22 +02:00
|
|
|
raise e
|
2015-12-27 16:36:32 +01:00
|
|
|
|
2023-08-29 23:23:15 +02:00
|
|
|
proc startItem(
|
|
|
|
ctx : var Context,
|
|
|
|
isObject: bool,
|
|
|
|
) {.raises: [YamlPresenterOutputError].} =
|
2023-07-31 19:16:24 +02:00
|
|
|
let t = ctx.target
|
2016-04-02 17:48:22 +02:00
|
|
|
try:
|
2023-07-31 19:16:24 +02:00
|
|
|
case ctx.state
|
2016-04-02 17:48:22 +02:00
|
|
|
of dBlockMapValue:
|
2023-07-31 19:16:24 +02:00
|
|
|
if ctx.level.wroteAnything or ctx.options.indentationStep < 2:
|
|
|
|
ctx.newline()
|
|
|
|
t.write(repeat(' ', ctx.indentation))
|
|
|
|
else:
|
|
|
|
ctx.level.wroteAnything = true
|
|
|
|
if isObject or ctx.options.explicitKeys:
|
|
|
|
ctx.append('?')
|
|
|
|
ctx.whitespace()
|
|
|
|
ctx.state = dBlockExplicitMapKey
|
|
|
|
else: ctx.state = dBlockImplicitMapKey
|
|
|
|
of dBlockInlineMap: ctx.state = dBlockImplicitMapKey
|
2016-04-02 17:48:22 +02:00
|
|
|
of dBlockExplicitMapKey:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.newline()
|
|
|
|
t.write(repeat(' ', ctx.indentation))
|
|
|
|
t.write(':')
|
|
|
|
ctx.whitespace()
|
|
|
|
ctx.state = dBlockMapValue
|
2016-04-02 17:48:22 +02:00
|
|
|
of dBlockImplicitMapKey:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.append(':')
|
|
|
|
ctx.whitespace(true)
|
|
|
|
ctx.state = dBlockMapValue
|
2016-04-02 17:48:22 +02:00
|
|
|
of dFlowExplicitMapKey:
|
2023-07-31 19:16:24 +02:00
|
|
|
if ctx.options.newlines != nlNone:
|
|
|
|
ctx.newline()
|
|
|
|
t.write(repeat(' ', ctx.indentation))
|
|
|
|
ctx.append(':')
|
|
|
|
ctx.whitespace()
|
|
|
|
ctx.state = dFlowMapValue
|
2016-04-02 17:48:22 +02:00
|
|
|
of dFlowMapValue:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.append(',')
|
|
|
|
ctx.whitespace(true)
|
|
|
|
if not ctx.level.singleLine:
|
|
|
|
ctx.newline()
|
|
|
|
t.write(repeat(' ', ctx.indentation))
|
|
|
|
if not isObject and not ctx.options.explicitKeys:
|
|
|
|
ctx.state = dFlowImplicitMapKey
|
2016-04-02 17:48:22 +02:00
|
|
|
else:
|
2023-07-31 19:16:24 +02:00
|
|
|
t.write('?')
|
|
|
|
ctx.whitespace()
|
|
|
|
ctx.state = dFlowExplicitMapKey
|
2016-04-02 17:48:22 +02:00
|
|
|
of dFlowMapStart:
|
2023-07-31 19:16:24 +02:00
|
|
|
if not ctx.level.singleLine:
|
|
|
|
ctx.newline()
|
|
|
|
t.write(repeat(' ', ctx.indentation))
|
|
|
|
if not isObject and not ctx.options.explicitKeys:
|
|
|
|
ctx.state = dFlowImplicitMapKey
|
|
|
|
else:
|
|
|
|
ctx.append('?')
|
|
|
|
ctx.whitespace()
|
|
|
|
ctx.state = dFlowExplicitMapKey
|
2016-04-02 17:48:22 +02:00
|
|
|
of dFlowImplicitMapKey:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.append(':')
|
|
|
|
ctx.whitespace(true)
|
|
|
|
ctx.state = dFlowMapValue
|
2016-04-02 17:48:22 +02:00
|
|
|
of dBlockSequenceItem:
|
2023-07-31 19:16:24 +02:00
|
|
|
if ctx.level.wroteAnything or ctx.options.indentationStep < 2:
|
|
|
|
ctx.newline()
|
|
|
|
t.write(repeat(' ', ctx.indentation))
|
|
|
|
else:
|
|
|
|
ctx.level.wroteAnything = true
|
|
|
|
ctx.append('-')
|
|
|
|
ctx.whitespace()
|
2016-04-02 17:48:22 +02:00
|
|
|
of dFlowSequenceStart:
|
2023-07-31 19:16:24 +02:00
|
|
|
if not ctx.level.singleLine:
|
|
|
|
ctx.newline()
|
|
|
|
t.write(repeat(' ', ctx.indentation))
|
|
|
|
ctx.state = dFlowSequenceItem
|
2016-04-02 17:48:22 +02:00
|
|
|
of dFlowSequenceItem:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.append(',')
|
|
|
|
ctx.whitespace(true)
|
|
|
|
if not ctx.options.condenseFlow:
|
|
|
|
ctx.newline()
|
|
|
|
t.write(repeat(' ', ctx.indentation))
|
2023-03-18 13:54:45 +01:00
|
|
|
except CatchableError as ce:
|
2016-04-02 17:48:22 +02:00
|
|
|
var e = newException(YamlPresenterOutputError, "")
|
2023-03-18 13:54:45 +01:00
|
|
|
e.parent = ce
|
2016-04-02 17:48:22 +02:00
|
|
|
raise e
|
2016-01-28 21:59:26 +01:00
|
|
|
|
2023-08-29 23:23:15 +02:00
|
|
|
proc writeTagAndAnchor(
|
|
|
|
ctx : var Context,
|
|
|
|
props: Properties,
|
|
|
|
): bool {.raises: [YamlPresenterOutputError].} =
|
2023-07-31 19:16:24 +02:00
|
|
|
if ctx.options.suppressAttrs: return false
|
|
|
|
let t = ctx.target
|
|
|
|
result = false
|
2016-04-02 17:48:22 +02:00
|
|
|
try:
|
2020-11-03 22:08:21 +01:00
|
|
|
if props.tag notin [yTagQuestionMark, yTagExclamationMark]:
|
2020-11-10 13:55:22 +01:00
|
|
|
let tagUri = $props.tag
|
2023-07-31 19:16:24 +02:00
|
|
|
let (handle, length) = ctx.searchHandle(tagUri)
|
2016-10-10 20:16:54 +02:00
|
|
|
if length > 0:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.append(handle)
|
|
|
|
t.write(tagUri[length..tagUri.high])
|
|
|
|
ctx.whitespace(true)
|
2016-04-02 17:48:22 +02:00
|
|
|
else:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.append("!<")
|
|
|
|
t.write(tagUri)
|
|
|
|
t.write('>')
|
|
|
|
ctx.whitespace(true)
|
|
|
|
result = true
|
2020-11-03 22:08:21 +01:00
|
|
|
if props.anchor != yAnchorNone:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.append("&")
|
|
|
|
t.write($props.anchor)
|
|
|
|
ctx.whitespace(true)
|
|
|
|
result = true
|
2023-03-18 13:54:45 +01:00
|
|
|
except CatchableError as ce:
|
2016-04-02 17:48:22 +02:00
|
|
|
var e = newException(YamlPresenterOutputError, "")
|
2023-03-18 13:54:45 +01:00
|
|
|
e.parent = ce
|
2016-04-02 17:48:22 +02:00
|
|
|
raise e
|
2015-12-27 16:36:32 +01:00
|
|
|
|
2023-08-29 23:23:15 +02:00
|
|
|
proc nextItem(
|
|
|
|
c: var Deque,
|
|
|
|
s: YamlStream,
|
|
|
|
): Event {.raises: [YamlStreamError].} =
|
2016-06-26 12:37:15 +02:00
|
|
|
if c.len > 0:
|
2019-01-23 09:32:59 +02:00
|
|
|
try: result = c.popFirst
|
2020-11-03 22:08:21 +01:00
|
|
|
except IndexDefect: internalError("Unexpected IndexError")
|
2016-06-26 12:37:15 +02:00
|
|
|
else:
|
|
|
|
result = s.next()
|
|
|
|
|
2023-08-29 23:23:15 +02:00
|
|
|
proc doPresent(
|
|
|
|
ctx: var Context,
|
|
|
|
s : YamlStream,
|
|
|
|
) {.raises: [
|
|
|
|
YamlPresenterJsonError, YamlPresenterOutputError,
|
|
|
|
YamlStreamError
|
|
|
|
].} =
|
2016-04-02 17:48:22 +02:00
|
|
|
var
|
2020-11-03 22:08:21 +01:00
|
|
|
cached = initDeQue[Event]()
|
2023-07-31 19:16:24 +02:00
|
|
|
firstDoc = true
|
|
|
|
wroteDirectivesEnd = false
|
2020-11-03 22:08:21 +01:00
|
|
|
while true:
|
2016-06-26 12:37:15 +02:00
|
|
|
let item = nextItem(cached, s)
|
2016-04-02 17:48:22 +02:00
|
|
|
case item.kind
|
2020-11-03 22:08:21 +01:00
|
|
|
of yamlStartStream: discard
|
|
|
|
of yamlEndStream: break
|
2016-04-02 17:48:22 +02:00
|
|
|
of yamlStartDoc:
|
2020-11-03 22:08:21 +01:00
|
|
|
if not firstDoc:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.safeWrite("...")
|
|
|
|
ctx.safeNewline()
|
|
|
|
wroteDirectivesEnd =
|
2023-08-29 20:46:26 +02:00
|
|
|
ctx.options.directivesEnd == deAlways or not s.peek().emptyProperties()
|
2023-07-31 19:16:24 +02:00
|
|
|
|
|
|
|
if ctx.options.directivesEnd != deNever:
|
|
|
|
resetHandles(ctx.handles)
|
|
|
|
for v in item.handles:
|
|
|
|
discard registerHandle(ctx.handles, v.handle, v.uriPrefix)
|
|
|
|
|
2016-04-02 17:48:22 +02:00
|
|
|
try:
|
2023-07-31 19:16:24 +02:00
|
|
|
case ctx.options.outputVersion
|
|
|
|
of ov1_2:
|
|
|
|
ctx.target.write("%YAML 1.2")
|
|
|
|
ctx.newline()
|
|
|
|
wroteDirectivesEnd = true
|
|
|
|
of ov1_1:
|
|
|
|
ctx.target.write("%YAML 1.1")
|
|
|
|
ctx.newline()
|
|
|
|
wroteDirectivesEnd = true
|
|
|
|
echo "write --- because %YAML"
|
2016-04-02 17:48:22 +02:00
|
|
|
of ovNone: discard
|
2023-07-31 19:16:24 +02:00
|
|
|
for v in ctx.handles:
|
2020-11-06 21:39:50 +01:00
|
|
|
if v.handle == "!":
|
|
|
|
if v.uriPrefix != "!":
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.target.write("%TAG ! " & v.uriPrefix)
|
|
|
|
ctx.newline()
|
|
|
|
wroteDirectivesEnd = true
|
2020-11-06 21:39:50 +01:00
|
|
|
elif v.handle == "!!":
|
|
|
|
if v.uriPrefix != yamlTagRepositoryPrefix:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.target.write("%TAG !! " & v.uriPrefix)
|
|
|
|
ctx.newline()
|
|
|
|
wroteDirectivesEnd = true
|
2016-10-10 20:16:54 +02:00
|
|
|
else:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.target.write("%TAG " & v.handle & ' ' & v.uriPrefix)
|
|
|
|
ctx.newline()
|
|
|
|
wroteDirectivesEnd = true
|
2023-03-18 13:54:45 +01:00
|
|
|
except CatchableError as ce:
|
2016-04-02 17:48:22 +02:00
|
|
|
var e = newException(YamlPresenterOutputError, "")
|
2023-03-18 13:54:45 +01:00
|
|
|
e.parent = ce
|
2016-04-02 17:48:22 +02:00
|
|
|
raise e
|
2023-07-31 19:16:24 +02:00
|
|
|
if wroteDirectivesEnd:
|
|
|
|
ctx.safeWrite("---")
|
|
|
|
ctx.whitespace(true)
|
2016-04-02 17:48:22 +02:00
|
|
|
of yamlScalar:
|
2023-07-31 19:16:24 +02:00
|
|
|
if ctx.levels.len > 0:
|
|
|
|
ctx.startItem(false)
|
|
|
|
discard ctx.writeTagAndAnchor(item.scalarProperties)
|
|
|
|
if ctx.levels.len == 0:
|
|
|
|
if wroteDirectivesEnd: ctx.safeNewline()
|
|
|
|
case ctx.options.quoting
|
|
|
|
of sqJson:
|
|
|
|
var hint = yTypeUnknown
|
|
|
|
if ctx.state == dFlowMapValue: hint = guessType(item.scalarContent)
|
2020-11-03 22:08:21 +01:00
|
|
|
let tag = item.scalarProperties.tag
|
|
|
|
if tag in [yTagQuestionMark, yTagBoolean] and
|
2023-07-31 19:16:24 +02:00
|
|
|
hint in {yTypeBoolTrue, yTypeBoolFalse}:
|
|
|
|
ctx.safeWrite(if hint == yTypeBoolTrue: "true" else: "false")
|
2020-11-03 22:08:21 +01:00
|
|
|
elif tag in [yTagQuestionMark, yTagNull] and
|
2016-04-02 17:48:22 +02:00
|
|
|
hint == yTypeNull:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.safeWrite("null")
|
2020-11-03 22:08:21 +01:00
|
|
|
elif tag in [yTagQuestionMark, yTagInteger,
|
2016-04-02 17:48:22 +02:00
|
|
|
yTagNimInt8, yTagNimInt16, yTagNimInt32, yTagNimInt64,
|
|
|
|
yTagNimUInt8, yTagNimUInt16, yTagNimUInt32, yTagNimUInt64] and
|
|
|
|
hint == yTypeInteger:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.safeWrite(item.scalarContent)
|
2020-11-03 22:08:21 +01:00
|
|
|
elif tag in [yTagQuestionMark, yTagFloat, yTagNimFloat32,
|
2016-04-02 17:48:22 +02:00
|
|
|
yTagNimFloat64] and hint in {yTypeFloatInf, yTypeFloatNaN}:
|
|
|
|
raise newException(YamlPresenterJsonError,
|
|
|
|
"Infinity and not-a-number values cannot be presented as JSON!")
|
2020-11-03 22:08:21 +01:00
|
|
|
elif tag in [yTagQuestionMark, yTagFloat] and
|
2016-04-02 17:48:22 +02:00
|
|
|
hint == yTypeFloat:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.safeWrite(item.scalarContent)
|
|
|
|
else: ctx.writeDoubleQuotedJson(item.scalarContent)
|
|
|
|
of sqDouble:
|
|
|
|
ctx.writeDoubleQuoted(item.scalarContent)
|
2016-04-02 17:48:22 +02:00
|
|
|
else:
|
|
|
|
var words, lines = newSeq[tuple[start, finish: int]]()
|
|
|
|
case item.scalarContent.inspect(
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.indentation + ctx.options.indentationStep, words, lines,
|
|
|
|
ctx.options.maxLineLength)
|
|
|
|
of sLiteral: ctx.writeLiteral(item.scalarContent, lines)
|
|
|
|
of sFolded: ctx.writeFolded(item.scalarContent, words)
|
|
|
|
of sPlain: ctx.safeWrite(item.scalarContent)
|
|
|
|
of sDoubleQuoted: ctx.writeDoubleQuoted(item.scalarContent)
|
2016-04-02 17:48:22 +02:00
|
|
|
of yamlAlias:
|
2023-07-31 19:16:24 +02:00
|
|
|
if ctx.options.quoting == sqJson:
|
2016-04-02 17:48:22 +02:00
|
|
|
raise newException(YamlPresenterJsonError,
|
|
|
|
"Alias not allowed in JSON output")
|
2023-07-31 19:16:24 +02:00
|
|
|
yAssert ctx.levels.len > 0
|
|
|
|
ctx.startItem(false)
|
2016-04-02 17:48:22 +02:00
|
|
|
try:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.append('*')
|
|
|
|
ctx.target.write($item.aliasTarget)
|
2023-03-18 13:54:45 +01:00
|
|
|
except CatchableError as ce:
|
2016-04-02 17:48:22 +02:00
|
|
|
var e = newException(YamlPresenterOutputError, "")
|
2023-03-18 13:54:45 +01:00
|
|
|
e.parent = ce
|
2016-04-02 17:48:22 +02:00
|
|
|
raise e
|
|
|
|
of yamlStartSeq:
|
|
|
|
var nextState: DumperState
|
2023-07-31 19:16:24 +02:00
|
|
|
case ctx.options.containers
|
|
|
|
of cMixed:
|
2016-04-02 17:48:22 +02:00
|
|
|
var length = 0
|
|
|
|
while true:
|
|
|
|
let next = s.next()
|
2019-01-23 09:32:59 +02:00
|
|
|
cached.addLast(next)
|
2016-04-02 17:48:22 +02: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
|
2023-07-31 19:16:24 +02:00
|
|
|
of cFlow: nextState = dFlowSequenceStart
|
|
|
|
of cBlock:
|
2017-01-10 11:45:55 +01:00
|
|
|
let next = s.peek()
|
|
|
|
if next.kind == yamlEndSeq: nextState = dFlowSequenceStart
|
|
|
|
else: nextState = dBlockSequenceItem
|
2016-08-30 22:15:29 +02:00
|
|
|
|
2023-07-31 19:16:24 +02:00
|
|
|
var indentation = 0
|
|
|
|
var singleLine = ctx.options.condenseFlow or ctx.options.newlines == nlNone
|
|
|
|
|
|
|
|
var wroteAnything = false
|
|
|
|
if ctx.levels.len > 0: wroteAnything = ctx.state == dBlockImplicitMapKey
|
|
|
|
|
|
|
|
if ctx.levels.len == 0:
|
|
|
|
if nextState == dFlowSequenceStart:
|
|
|
|
indentation = ctx.options.indentationStep
|
2016-04-02 17:48:22 +02:00
|
|
|
else:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.startItem(true)
|
|
|
|
indentation = ctx.indentation + ctx.options.indentationStep
|
|
|
|
|
|
|
|
let wroteAttrs = ctx.writeTagAndAnchor(item.seqProperties)
|
|
|
|
if wroteAttrs or (wroteDirectivesEnd and ctx.levels.len == 0):
|
|
|
|
wroteAnything = true
|
|
|
|
|
|
|
|
if nextState == dFlowSequenceStart:
|
|
|
|
if ctx.levels.len == 0:
|
|
|
|
if wroteAttrs or wroteDirectivesEnd: ctx.safeNewline()
|
|
|
|
ctx.safeWrite('[')
|
|
|
|
|
|
|
|
if ctx.levels.len > 0 and not ctx.options.condenseFlow and
|
|
|
|
ctx.state in [dBlockExplicitMapKey, dBlockMapValue,
|
|
|
|
dBlockImplicitMapKey, dBlockSequenceItem]:
|
|
|
|
indentation += ctx.options.indentationStep
|
|
|
|
if ctx.options.newlines != nlNone: singleLine = false
|
|
|
|
ctx.levels.add (nextState, indentation, singleLine, wroteAnything)
|
2016-04-02 17:48:22 +02:00
|
|
|
of yamlStartMap:
|
|
|
|
var nextState: DumperState
|
2023-07-31 19:16:24 +02:00
|
|
|
case ctx.options.containers
|
|
|
|
of cMixed:
|
2016-04-02 17:48:22 +02:00
|
|
|
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
|
2023-07-31 19:16:24 +02:00
|
|
|
of cFlow: nextState = dFlowMapStart
|
|
|
|
of cBlock:
|
2017-01-10 11:45:55 +01:00
|
|
|
let next = s.peek()
|
|
|
|
if next.kind == yamlEndMap: nextState = dFlowMapStart
|
|
|
|
else: nextState = dBlockMapValue
|
2023-07-31 19:16:24 +02:00
|
|
|
|
|
|
|
var indentation = 0
|
|
|
|
var singleLine = ctx.options.condenseFlow or ctx.options.newlines == nlNone
|
|
|
|
|
|
|
|
var wroteAnything = false
|
|
|
|
if ctx.levels.len > 0: wroteAnything = ctx.state == dBlockImplicitMapKey
|
|
|
|
|
|
|
|
if ctx.levels.len == 0:
|
|
|
|
if nextState == dFlowMapStart:
|
|
|
|
indentation = ctx.options.indentationStep
|
2016-04-02 17:48:22 +02:00
|
|
|
else:
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.startItem(true)
|
|
|
|
indentation = ctx.indentation + ctx.options.indentationStep
|
|
|
|
|
|
|
|
let wroteAttrs = ctx.writeTagAndAnchor(item.properties)
|
|
|
|
if wroteAttrs or (wroteDirectivesEnd and ctx.levels.len == 0):
|
|
|
|
wroteAnything = true
|
|
|
|
|
|
|
|
if nextState == dFlowMapStart:
|
|
|
|
if ctx.levels.len == 0:
|
|
|
|
if wroteAttrs or wroteDirectivesEnd: ctx.safeNewline()
|
|
|
|
ctx.safeWrite('{')
|
|
|
|
|
|
|
|
if ctx.levels.len > 0 and not ctx.options.condenseFlow and
|
|
|
|
ctx.state in [dBlockExplicitMapKey, dBlockMapValue,
|
|
|
|
dBlockImplicitMapKey, dBlockSequenceItem]:
|
|
|
|
indentation += ctx.options.indentationStep
|
|
|
|
if ctx.options.newlines != nlNone: singleLine = false
|
|
|
|
ctx.levels.add (nextState, indentation, singleLine, wroteAnything)
|
2016-04-02 17:48:22 +02:00
|
|
|
of yamlEndSeq:
|
2023-07-31 19:16:24 +02:00
|
|
|
yAssert ctx.levels.len > 0
|
|
|
|
let level = ctx.levels.pop()
|
|
|
|
case level.state
|
2016-04-02 17:48:22 +02:00
|
|
|
of dFlowSequenceItem:
|
2023-07-31 19:16:24 +02:00
|
|
|
try:
|
|
|
|
if not level.singleLine:
|
|
|
|
ctx.newline()
|
|
|
|
ctx.target.write(repeat(' ', ctx.indentation))
|
|
|
|
ctx.target.write(']')
|
|
|
|
except CatchableError as ce:
|
|
|
|
var e = newException(YamlPresenterOutputError, "")
|
|
|
|
e.parent = ce
|
|
|
|
raise e
|
|
|
|
of dFlowSequenceStart: ctx.safeWrite(']')
|
2016-04-02 17:48:22 +02:00
|
|
|
of dBlockSequenceItem: discard
|
2016-08-09 20:47:22 +02:00
|
|
|
else: internalError("Invalid popped level")
|
2016-04-02 17:48:22 +02:00
|
|
|
of yamlEndMap:
|
2023-07-31 19:16:24 +02:00
|
|
|
yAssert ctx.levels.len > 0
|
|
|
|
let level = ctx.levels.pop()
|
|
|
|
case level.state
|
2016-04-02 17:48:22 +02:00
|
|
|
of dFlowMapValue:
|
2023-07-31 19:16:24 +02:00
|
|
|
try:
|
|
|
|
if not level.singleLine:
|
|
|
|
ctx.safeNewline()
|
|
|
|
ctx.target.write(repeat(' ', ctx.indentation))
|
|
|
|
ctx.append('}')
|
|
|
|
except CatchableError as ce:
|
|
|
|
var e = newException(YamlPresenterOutputError, "")
|
|
|
|
e.parent = ce
|
|
|
|
raise e
|
|
|
|
of dFlowMapStart: ctx.safeWrite('}')
|
2016-04-02 17:48:22 +02:00
|
|
|
of dBlockMapValue, dBlockInlineMap: discard
|
2016-08-09 20:47:22 +02:00
|
|
|
else: internalError("Invalid level: " & $level)
|
2016-04-02 17:48:22 +02:00
|
|
|
of yamlEndDoc:
|
2020-11-03 22:08:21 +01:00
|
|
|
firstDoc = false
|
2023-07-31 19:16:24 +02:00
|
|
|
ctx.safeNewline()
|
2015-12-27 16:36:32 +01:00
|
|
|
|
2023-08-29 20:46:26 +02:00
|
|
|
proc present*(
|
|
|
|
s : YamlStream,
|
|
|
|
target : Stream,
|
2023-08-29 23:23:15 +02:00
|
|
|
options: PresentationOptions = PresentationOptions(),
|
2023-08-29 20:46:26 +02:00
|
|
|
) {.raises: [
|
|
|
|
YamlPresenterJsonError, YamlPresenterOutputError,
|
|
|
|
YamlStreamError
|
|
|
|
].} =
|
2016-09-20 21:53:38 +02:00
|
|
|
## Convert ``s`` to a YAML character stream and write it to ``target``.
|
2021-03-23 18:51:05 +01:00
|
|
|
var c = Context(target: target, options: options)
|
2020-11-06 21:39:50 +01:00
|
|
|
doPresent(c, s)
|
2016-09-19 19:33:29 +02:00
|
|
|
|
2023-08-29 20:46:26 +02:00
|
|
|
proc present*(
|
|
|
|
s : YamlStream,
|
2023-08-29 23:23:15 +02:00
|
|
|
options: PresentationOptions = PresentationOptions(),
|
2023-08-29 20:46:26 +02:00
|
|
|
): string {.raises: [
|
|
|
|
YamlPresenterJsonError, YamlPresenterOutputError,
|
|
|
|
YamlStreamError
|
|
|
|
].} =
|
2016-09-20 21:53:38 +02:00
|
|
|
## Convert ``s`` to a YAML character stream and return it as string.
|
2016-09-19 19:33:29 +02:00
|
|
|
|
2020-11-06 21:39:50 +01:00
|
|
|
var
|
|
|
|
ss = newStringStream()
|
2021-03-23 18:51:05 +01:00
|
|
|
c = Context(target: ss, options: options)
|
2020-11-06 21:39:50 +01:00
|
|
|
doPresent(c, s)
|
|
|
|
return ss.data
|
|
|
|
|
2023-08-29 20:46:26 +02:00
|
|
|
proc doTransform(
|
|
|
|
ctx : var Context,
|
|
|
|
input: Stream,
|
2023-08-29 23:23:15 +02:00
|
|
|
resolveToCoreYamlTags: bool,
|
2023-08-29 20:46:26 +02:00
|
|
|
) =
|
2021-03-23 18:51:05 +01:00
|
|
|
var parser: YamlParser
|
|
|
|
parser.init()
|
2020-11-04 16:40:37 +01:00
|
|
|
var events = parser.parse(input)
|
2016-04-02 17:48:22 +02:00
|
|
|
try:
|
2023-07-31 19:16:24 +02:00
|
|
|
if ctx.options.explicitKeys:
|
2016-09-01 21:08:27 +02:00
|
|
|
var bys: YamlStream = newBufferYamlStream()
|
|
|
|
for e in events:
|
2016-12-05 18:27:32 +01:00
|
|
|
if resolveToCoreYamlTags:
|
|
|
|
var event = e
|
|
|
|
case event.kind
|
2020-11-06 21:39:50 +01:00
|
|
|
of yamlStartStream, yamlEndStream, yamlStartDoc, yamlEndDoc, yamlEndMap, yamlAlias, yamlEndSeq:
|
2016-12-05 18:27:32 +01:00
|
|
|
discard
|
|
|
|
of yamlStartMap:
|
2020-11-06 21:39:50 +01:00
|
|
|
if event.mapProperties.tag in [yTagQuestionMark, yTagExclamationMark]:
|
|
|
|
event.mapProperties.tag = yTagMapping
|
2016-12-05 18:27:32 +01:00
|
|
|
of yamlStartSeq:
|
2020-11-06 21:39:50 +01:00
|
|
|
if event.seqProperties.tag in [yTagQuestionMark, yTagExclamationMark]:
|
|
|
|
event.seqProperties.tag = yTagSequence
|
2016-12-05 18:27:32 +01:00
|
|
|
of yamlScalar:
|
2020-11-06 21:39:50 +01:00
|
|
|
if event.scalarProperties.tag == yTagQuestionMark:
|
2016-12-05 18:27:32 +01:00
|
|
|
case guessType(event.scalarContent)
|
2020-11-06 21:39:50 +01:00
|
|
|
of yTypeInteger: event.scalarProperties.tag = yTagInteger
|
2016-12-05 18:27:32 +01:00
|
|
|
of yTypeFloat, yTypeFloatInf, yTypeFloatNaN:
|
2020-11-06 21:39:50 +01: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 18:27:32 +01:00
|
|
|
BufferYamlStream(bys).put(event)
|
|
|
|
else: BufferYamlStream(bys).put(e)
|
2023-07-31 19:16:24 +02:00
|
|
|
doPresent(ctx, bys)
|
2016-12-13 21:22:36 +01:00
|
|
|
else:
|
2023-07-31 19:16:24 +02:00
|
|
|
doPresent(ctx, events)
|
2023-03-18 13:54:45 +01:00
|
|
|
except YamlStreamError as e:
|
|
|
|
var curE: ref Exception = e
|
|
|
|
while curE.parent of YamlStreamError: curE = curE.parent
|
|
|
|
if curE.parent of IOError: raise (ref IOError)(curE.parent)
|
|
|
|
elif curE.parent of OSError: raise (ref OSError)(curE.parent)
|
|
|
|
elif curE.parent of YamlParserError: raise (ref YamlParserError)(curE.parent)
|
|
|
|
else: internalError("Unexpected exception: " & curE.parent.repr)
|
2016-09-19 19:33:29 +02:00
|
|
|
|
2020-11-06 21:39:50 +01:00
|
|
|
proc genInput(input: Stream): Stream = input
|
|
|
|
proc genInput(input: string): Stream = newStringStream(input)
|
|
|
|
|
2023-08-29 20:46:26 +02:00
|
|
|
proc transform*(
|
|
|
|
input : Stream | string,
|
|
|
|
output : Stream,
|
|
|
|
options: PresentationOptions = PresentationOptions(),
|
2023-08-29 23:23:15 +02:00
|
|
|
resolveToCoreYamlTags: bool = false,
|
2023-08-29 20:46:26 +02:00
|
|
|
) {.raises: [
|
|
|
|
IOError, OSError, YamlParserError, YamlPresenterJsonError,
|
|
|
|
YamlPresenterOutputError
|
|
|
|
].} =
|
2016-09-20 21:53:38 +02:00
|
|
|
## 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 18:27:32 +01:00
|
|
|
## library. If ``resolveToCoreYamlTags`` is ``true``, non-specific tags will
|
|
|
|
## be replaced by specific tags according to the YAML core schema.
|
2021-05-17 19:50:10 +02:00
|
|
|
var c = Context(target: output, options: options)
|
|
|
|
doTransform(c, genInput(input), resolveToCoreYamlTags)
|
2016-09-19 19:33:29 +02:00
|
|
|
|
2023-08-29 20:46:26 +02:00
|
|
|
proc transform*(
|
|
|
|
input : Stream | string,
|
|
|
|
options: PresentationOptions = PresentationOptions(),
|
2023-08-29 23:23:15 +02:00
|
|
|
resolveToCoreYamlTags: bool = false,
|
2023-08-29 20:46:26 +02:00
|
|
|
): string {.raises: [
|
|
|
|
IOError, OSError, YamlParserError, YamlPresenterJsonError,
|
|
|
|
YamlPresenterOutputError
|
|
|
|
].} =
|
2016-09-20 21:53:38 +02:00
|
|
|
## 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 18:27:32 +01: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 19:50:10 +02:00
|
|
|
var
|
|
|
|
ss = newStringStream()
|
|
|
|
c = Context(target: ss, options: options)
|
|
|
|
doTransform(c, genInput(input), resolveToCoreYamlTags)
|
2022-01-22 01:18:11 +01:00
|
|
|
return ss.data
|