fix #14056, incorporate review comments
This commit is contained in:
kemifl 2020-11-23 19:53:57 +01:00 • committed by GitHub
commit a764a5b9bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 12 deletions

View file

@ -99,11 +99,11 @@ proc parse(x: var XmlParser, errors: var seq[string]): XmlNode =
of xmlEof: discard of xmlEof: discard
proc parseXml*(s: Stream, filename: string, proc parseXml*(s: Stream, filename: string,
errors: var seq[string]): XmlNode = errors: var seq[string], options: set[XmlParseOption] = {reportComments}): XmlNode =
## Parses the XML from stream ``s`` and returns a ``XmlNode``. Every ## Parses the XML from stream ``s`` and returns a ``XmlNode``. Every
## occurred parsing error is added to the ``errors`` sequence. ## occurred parsing error is added to the ``errors`` sequence.
var x: XmlParser var x: XmlParser
open(x, s, filename, {reportComments}) open(x, s, filename, options)
while true: while true:
x.next() x.next()
case x.kind case x.kind
@ -118,32 +118,32 @@ proc parseXml*(s: Stream, filename: string,
break break
close(x) close(x)
proc parseXml*(s: Stream): XmlNode = proc parseXml*(s: Stream, options: set[XmlParseOption] = {reportComments}): XmlNode =
## Parses the XML from stream ``s`` and returns a ``XmlNode``. All parsing ## Parses the XML from stream ``s`` and returns a ``XmlNode``. All parsing
## errors are turned into an ``XmlError`` exception. ## errors are turned into an ``XmlError`` exception.
var errors: seq[string] = @[] var errors: seq[string] = @[]
result = parseXml(s, "unknown_xml_doc", errors) result = parseXml(s, "unknown_xml_doc", errors, options)
if errors.len > 0: raiseInvalidXml(errors) if errors.len > 0: raiseInvalidXml(errors)
proc parseXml*(str: string): XmlNode = proc parseXml*(str: string, options: set[XmlParseOption] = {reportComments}): XmlNode =
## Parses the XML from string ``str`` and returns a ``XmlNode``. All parsing ## Parses the XML from string ``str`` and returns a ``XmlNode``. All parsing
## errors are turned into an ``XmlError`` exception. ## errors are turned into an ``XmlError`` exception.
parseXml(newStringStream(str)) parseXml(newStringStream(str), options)
proc loadXml*(path: string, errors: var seq[string]): XmlNode = proc loadXml*(path: string, errors: var seq[string], options: set[XmlParseOption] = {reportComments}): XmlNode =
## Loads and parses XML from file specified by ``path``, and returns ## Loads and parses XML from file specified by ``path``, and returns
## a ``XmlNode``. Every occurred parsing error is added to the ``errors`` ## a ``XmlNode``. Every occurred parsing error is added to the ``errors``
## sequence. ## sequence.
var s = newFileStream(path, fmRead) var s = newFileStream(path, fmRead)
if s == nil: raise newException(IOError, "Unable to read file: " & path) if s == nil: raise newException(IOError, "Unable to read file: " & path)
result = parseXml(s, path, errors) result = parseXml(s, path, errors, options)
proc loadXml*(path: string): XmlNode = proc loadXml*(path: string, options: set[XmlParseOption] = {reportComments}): XmlNode =
## Loads and parses XML from file specified by ``path``, and returns ## Loads and parses XML from file specified by ``path``, and returns
## a ``XmlNode``. All parsing errors are turned into an ``XmlError`` ## a ``XmlNode``. All parsing errors are turned into an ``XmlError``
## exception. ## exception.
var errors: seq[string] = @[] var errors: seq[string] = @[]
result = loadXml(path, errors) result = loadXml(path, errors, options)
if errors.len > 0: raiseInvalidXml(errors) if errors.len > 0: raiseInvalidXml(errors)
when isMainModule: when isMainModule:

View file

@ -16,7 +16,18 @@ fpqeew
<Students> <Students>
<Student Name="Aprilfoo" /> <Student Name="Aprilfoo" />
<Student Name="bar" /> <Student Name="bar" />
</Students>''' </Students>
<chapter>
<title>This is a Docbook title</title>
<para>
This is a Docbook paragraph containing <emphasis>emphasized</emphasis>,
<literal>literal</literal> and <replaceable>replaceable</replaceable>
text. Sometimes scrunched together like this:
<literal>literal</literal><replaceable>replaceable</replaceable>
and sometimes not:
<literal>literal</literal> <replaceable>replaceable</replaceable>
</para>
</chapter>'''
""" """
block: block:
@ -123,7 +134,7 @@ block:
x.num += 10 x.num += 10
echo j echo j
import xmltree, xmlparser, streams, strtabs import xmltree, xmlparser, parsexml, streams, strtabs
block: block:
var d = parseXml(newStringStream """<Students> var d = parseXml(newStringStream """<Students>
@ -134,3 +145,17 @@ block:
x = <>Student(Name=x.attrs["Name"] & "foo") x = <>Student(Name=x.attrs["Name"] & "foo")
d[1].attrs["Name"] = "bar" d[1].attrs["Name"] = "bar"
echo d echo d
block:
var d = parseXml(newStringStream """<chapter>
<title>This is a Docbook title</title>
<para>
This is a Docbook paragraph containing <emphasis>emphasized</emphasis>,
<literal>literal</literal> and <replaceable>replaceable</replaceable>
text. Sometimes scrunched together like this:
<literal>literal</literal><replaceable>replaceable</replaceable>
and sometimes not:
<literal>literal</literal> <replaceable>replaceable</replaceable>
</para>
</chapter>""",{reportComments, reportWhitespace})
echo d