Add parseHtml overload for string (#7198)

This commit is contained in:
Oscar Nihlgård 2018-02-10 22:47:20 +01:00 • committed by Andreas Rumpf
commit 7847cd8759

View file

@ -425,19 +425,19 @@ proc toHtmlTag(s: string): HtmlTag =
proc htmlTag*(n: XmlNode): HtmlTag = proc htmlTag*(n: XmlNode): HtmlTag =
## gets `n`'s tag as a ``HtmlTag``. ## Gets `n`'s tag as a ``HtmlTag``.
if n.clientData == 0: if n.clientData == 0:
n.clientData = toHtmlTag(n.tag).ord n.clientData = toHtmlTag(n.tag).ord
result = HtmlTag(n.clientData) result = HtmlTag(n.clientData)
proc htmlTag*(s: string): HtmlTag = proc htmlTag*(s: string): HtmlTag =
## converts `s` to a ``HtmlTag``. If `s` is no HTML tag, ``tagUnknown`` is ## Converts `s` to a ``HtmlTag``. If `s` is no HTML tag, ``tagUnknown`` is
## returned. ## returned.
let s = if allLower(s): s else: toLowerAscii(s) let s = if allLower(s): s else: toLowerAscii(s)
result = toHtmlTag(s) result = toHtmlTag(s)
proc entityToUtf8*(entity: string): string = proc entityToUtf8*(entity: string): string =
## converts an HTML entity name like ``Ü`` to its UTF-8 equivalent. ## Converts an HTML entity name like ``Ü`` to its UTF-8 equivalent.
## "" is returned if the entity name is unknown. The HTML parser ## "" is returned if the entity name is unknown. The HTML parser
## already converts entities to UTF-8. ## already converts entities to UTF-8.
for name, val in items(Entities): for name, val in items(Entities):
@ -565,7 +565,7 @@ proc parse(x: var XmlParser, errors: var seq[string]): XmlNode =
proc parseHtml*(s: Stream, filename: string, proc parseHtml*(s: Stream, filename: string,
errors: var seq[string]): XmlNode = errors: var seq[string]): XmlNode =
## parses the XML from stream `s` and returns a ``PXmlNode``. 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, reportWhitespace}) open(x, s, filename, {reportComments, reportWhitespace})
@ -588,14 +588,19 @@ proc parseHtml*(s: Stream, filename: string,
result = result[0] result = result[0]
proc parseHtml*(s: Stream): XmlNode = proc parseHtml*(s: Stream): XmlNode =
## parses the XTML from stream `s` and returns a ``PXmlNode``. All parsing ## Parses the HTML from stream `s` and returns a ``XmlNode``. All parsing
## errors are ignored. ## errors are ignored.
var errors: seq[string] = @[] var errors: seq[string] = @[]
result = parseHtml(s, "unknown_html_doc", errors) result = parseHtml(s, "unknown_html_doc", errors)
proc parseHtml*(html: string): XmlNode =
## Parses the HTML from string ``html`` and returns a ``XmlNode``. All parsing
## errors are ignored.
parseHtml(newStringStream(html))
proc loadHtml*(path: string, errors: var seq[string]): XmlNode = proc loadHtml*(path: string, errors: var seq[string]): XmlNode =
## Loads and parses HTML from file specified by ``path``, and returns ## Loads and parses HTML from file specified by ``path``, and returns
## a ``PXmlNode``. Every occurred parsing error is added to ## a ``XmlNode``. Every occurred parsing error is added to
## the `errors` sequence. ## the `errors` 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)
@ -603,7 +608,7 @@ proc loadHtml*(path: string, errors: var seq[string]): XmlNode =
proc loadHtml*(path: string): XmlNode = proc loadHtml*(path: string): XmlNode =
## Loads and parses HTML from file specified by ``path``, and returns ## Loads and parses HTML from file specified by ``path``, and returns
## a ``PXmlNode``. All parsing errors are ignored. ## a ``XmlNode``. All parsing errors are ignored.
var errors: seq[string] = @[] var errors: seq[string] = @[]
result = loadHtml(path, errors) result = loadHtml(path, errors)