Fixed and improved presenter.transform

* Added option to resolve non-specific tags
 * Actually resolve non-specific tags
 * Made it compile again after hints enhancement
This commit is contained in:
Felix Krause 2016-12-05 18:27:32 +01:00
parent 36bdf5ba79
commit 4b529c1fd6
2 changed files with 38 additions and 31 deletions

View File

@ -47,7 +47,7 @@ routes:
var var
output = newStringStream() output = newStringStream()
highlighted = "" highlighted = ""
transform(newStringStream(@"input"), output, defineOptions(style)) transform(newStringStream(@"input"), output, defineOptions(style), true)
# syntax highlighting (stolen and modified from stlib's rstgen) # syntax highlighting (stolen and modified from stlib's rstgen)
var g: GeneralTokenizer var g: GeneralTokenizer

View File

@ -453,7 +453,6 @@ proc doPresent(s: var YamlStream, target: PresenterTarget,
case item.kind case item.kind
of yamlStartDoc: of yamlStartDoc:
if options.style != psJson: if options.style != psJson:
# TODO: tag directives
try: try:
case options.outputVersion case options.outputVersion
of ov1_2: target.append("%YAML 1.2" & newline) of ov1_2: target.append("%YAML 1.2" & newline)
@ -728,7 +727,7 @@ proc present*(s: var YamlStream, tagLib: TagLibrary,
doPresent(s, addr result, tagLib, options) doPresent(s, addr result, tagLib, options)
proc doTransform(input: Stream | string, output: PresenterTarget, proc doTransform(input: Stream | string, output: PresenterTarget,
options: PresentationOptions = defaultPresentationOptions) = options: PresentationOptions, resolveToCoreYamlTags: bool) =
var var
taglib = initExtendedTagLibrary() taglib = initExtendedTagLibrary()
parser = newYamlParser(tagLib) parser = newYamlParser(tagLib)
@ -737,6 +736,7 @@ proc doTransform(input: Stream | string, output: PresenterTarget,
if options.style == psCanonical: if options.style == psCanonical:
var bys: YamlStream = newBufferYamlStream() var bys: YamlStream = newBufferYamlStream()
for e in events: for e in events:
if resolveToCoreYamlTags:
var event = e var event = e
case event.kind case event.kind
of yamlStartDoc, yamlEndDoc, yamlEndMap, yamlAlias, yamlEndSeq: of yamlStartDoc, yamlEndDoc, yamlEndMap, yamlAlias, yamlEndSeq:
@ -755,10 +755,12 @@ proc doTransform(input: Stream | string, output: PresenterTarget,
event.scalarTag = yTagFloat event.scalarTag = yTagFloat
of yTypeBoolTrue, yTypeBoolFalse: event.scalarTag = yTagBoolean of yTypeBoolTrue, yTypeBoolFalse: event.scalarTag = yTagBoolean
of yTypeNull: event.scalarTag = yTagNull of yTypeNull: event.scalarTag = yTagNull
of yTypeTimestamp: event.scalarTag = yTagTimestamp
of yTypeUnknown: event.scalarTag = yTagString of yTypeUnknown: event.scalarTag = yTagString
elif event.scalarTag == yTagExclamationMark: elif event.scalarTag == yTagExclamationMark:
event.scalarTag = yTagString event.scalarTag = yTagString
BufferYamlStream(bys).put(e) BufferYamlStream(bys).put(event)
else: BufferYamlStream(bys).put(e)
present(bys, output, tagLib, options) present(bys, output, tagLib, options)
else: present(events, output, tagLib, options) else: present(events, output, tagLib, options)
except YamlStreamError: except YamlStreamError:
@ -769,20 +771,25 @@ proc doTransform(input: Stream | string, output: PresenterTarget,
else: internalError("Unexpected exception: " & e.parent.repr) else: internalError("Unexpected exception: " & e.parent.repr)
proc transform*(input: Stream | string, output: Stream, proc transform*(input: Stream | string, output: Stream,
options: PresentationOptions = defaultPresentationOptions) options: PresentationOptions = defaultPresentationOptions,
resolveToCoreYamlTags: bool = false)
{.raises: [IOError, YamlParserError, YamlPresenterJsonError, {.raises: [IOError, YamlParserError, YamlPresenterJsonError,
YamlPresenterOutputError].} = YamlPresenterOutputError].} =
## Parser ``input`` as YAML character stream and then dump it to ``output`` ## 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 ## while resolving non-specific tags to the ones in the YAML core tag
## library. ## library. If ``resolveToCoreYamlTags`` is ``true``, non-specific tags will
doTransform(input, output, options) ## be replaced by specific tags according to the YAML core schema.
doTransform(input, output, options, resolveToCoreYamlTags)
proc transform*(input: Stream | string, proc transform*(input: Stream | string,
options: PresentationOptions = defaultPresentationOptions): options: PresentationOptions = defaultPresentationOptions,
resolveToCoreYamlTags: bool = false):
string {.raises: [IOError, YamlParserError, YamlPresenterJsonError, string {.raises: [IOError, YamlParserError, YamlPresenterJsonError,
YamlPresenterOutputError].} = YamlPresenterOutputError].} =
## Parser ``input`` as YAML character stream, resolves non-specific tags to ## Parser ``input`` as YAML character stream, resolves non-specific tags to
## the ones in the YAML core tag library, and then returns a serialized ## the ones in the YAML core tag library, and then returns a serialized
## YAML string that represents the stream. ## 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.
result = "" result = ""
doTransform(input, addr result, options) doTransform(input, addr result, options, resolveToCoreYamlTags)