diff options
author | Grzegorz Adam Hankiewicz <gradha@imap.cc> | 2013-12-02 15:13:40 -0800 |
---|---|---|
committer | Grzegorz Adam Hankiewicz <gradha@imap.cc> | 2013-12-02 15:13:40 -0800 |
commit | 6e451d4f333c61ec7735760640a8903a22f6ef0c (patch) | |
tree | c9b9b89d5a7e4ccc62345bf079e29d21aa792a5b | |
parent | b5ac23477157ed8bcdac854d3ac000ace7aa8942 (diff) | |
parent | f91a34c9e6ab91b57627803631cf099cd11e32ab (diff) | |
download | Nim-6e451d4f333c61ec7735760640a8903a22f6ef0c.tar.gz |
Merge pull request #695 from gradha/pr_adds_htmlparser_example
Adds an example to htmlparser showing how to save changes.
-rw-r--r-- | lib/pure/htmlparser.nim | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/lib/pure/htmlparser.nim b/lib/pure/htmlparser.nim index 7bcc501a7..060f0e386 100644 --- a/lib/pure/htmlparser.nim +++ b/lib/pure/htmlparser.nim @@ -17,11 +17,37 @@ ## ## echo loadHtml("mydirty.html") ## -## ## Every tag in the resulting tree is in lower case. ## ## **Note:** The resulting ``PXmlNode`` already uses the ``clientData`` field, ## so it cannot be used by clients of this library. +## +## Example: Transforming hyperlinks +## ================================ +## +## This code demonstrates how you can iterate over all the tags in an HTML file +## and write back the modified version. In this case we look for hyperlinks +## ending with the extension ``.rst`` and convert them to ``.html``. +## +## .. code-block:: nimrod +## +## import htmlparser +## import xmltree # To use '$' for PXmlNode +## import strtabs # To access PXmlAttributes +## import os # To use splitFile +## import strutils # To use cmpIgnoreCase +## +## proc transformHyperlinks() = +## let html = loadHTML("input.html") +## +## for a in html.findAll("a"): +## let href = a.attrs["href"] +## if not href.isNil: +## let (dir, filename, ext) = splitFile(href) +## if cmpIgnoreCase(ext, ".rst") == 0: +## a.attrs["href"] = dir / filename & ".html" +## +## writeFile("output.html", $html) import strutils, streams, parsexml, xmltree, unicode, strtabs |