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,28 +736,31 @@ 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:
var event = e if resolveToCoreYamlTags:
case event.kind var event = e
of yamlStartDoc, yamlEndDoc, yamlEndMap, yamlAlias, yamlEndSeq: case event.kind
discard of yamlStartDoc, yamlEndDoc, yamlEndMap, yamlAlias, yamlEndSeq:
of yamlStartMap: discard
if event.mapTag in [yTagQuestionMark, yTagExclamationMark]: of yamlStartMap:
event.mapTag = yTagMapping if event.mapTag in [yTagQuestionMark, yTagExclamationMark]:
of yamlStartSeq: event.mapTag = yTagMapping
if event.seqTag in [yTagQuestionMark, yTagExclamationMark]: of yamlStartSeq:
event.seqTag = yTagSequence if event.seqTag in [yTagQuestionMark, yTagExclamationMark]:
of yamlScalar: event.seqTag = yTagSequence
if event.scalarTag == yTagQuestionMark: of yamlScalar:
case guessType(event.scalarContent) if event.scalarTag == yTagQuestionMark:
of yTypeInteger: event.scalarTag = yTagInteger case guessType(event.scalarContent)
of yTypeFloat, yTypeFloatInf, yTypeFloatNaN: of yTypeInteger: event.scalarTag = yTagInteger
event.scalarTag = yTagFloat of yTypeFloat, yTypeFloatInf, yTypeFloatNaN:
of yTypeBoolTrue, yTypeBoolFalse: event.scalarTag = yTagBoolean event.scalarTag = yTagFloat
of yTypeNull: event.scalarTag = yTagNull of yTypeBoolTrue, yTypeBoolFalse: event.scalarTag = yTagBoolean
of yTypeUnknown: event.scalarTag = yTagString of yTypeNull: event.scalarTag = yTagNull
elif event.scalarTag == yTagExclamationMark: of yTypeTimestamp: event.scalarTag = yTagTimestamp
event.scalarTag = yTagString of yTypeUnknown: event.scalarTag = yTagString
BufferYamlStream(bys).put(e) elif event.scalarTag == yTagExclamationMark:
event.scalarTag = yTagString
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)