diff options
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/fsmonitor.nim | 10 | ||||
-rw-r--r-- | lib/pure/htmlparser.nim | 30 |
2 files changed, 33 insertions, 7 deletions
diff --git a/lib/pure/fsmonitor.nim b/lib/pure/fsmonitor.nim index a554cf963..d6584c1a0 100644 --- a/lib/pure/fsmonitor.nim +++ b/lib/pure/fsmonitor.nim @@ -64,10 +64,10 @@ const proc newMonitor*(): PFSMonitor = ## Creates a new file system monitor. new(result) - result.fd = inotifyInit() result.targets = initTable[cint, string]() + result.fd = inotifyInit() if result.fd < 0: - OSError() + OSError(OSLastError()) proc add*(monitor: PFSMonitor, target: string, filters = {MonitorAll}): cint {.discardable.} = @@ -93,7 +93,7 @@ proc add*(monitor: PFSMonitor, target: string, result = inotifyAddWatch(monitor.fd, target, INFilter.uint32) if result < 0: - OSError() + OSError(OSLastError()) monitor.targets.add(result, target) proc del*(monitor: PFSMonitor, wd: cint) = @@ -101,7 +101,7 @@ proc del*(monitor: PFSMonitor, wd: cint) = ## ## If ``wd`` is not a part of ``monitor`` an EOS error is raised. if inotifyRmWatch(monitor.fd, wd) < 0: - OSError() + OSError(OSLastError()) proc getEvent(m: PFSMonitor, fd: cint): seq[TMonitorEvent] = result = @[] @@ -184,7 +184,7 @@ proc FSMonitorRead(h: PObject) = proc toDelegate(m: PFSMonitor): PDelegate = result = newDelegate() result.deleVal = m - result.fd = m.fd + result.fd = (type(result.fd))(m.fd) result.mode = fmRead result.handleRead = FSMonitorRead result.open = true diff --git a/lib/pure/htmlparser.nim b/lib/pure/htmlparser.nim index d60d2e583..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 @@ -528,7 +554,7 @@ proc parseHtml*(s: PStream, filename: string, ## parses the XML from stream `s` and returns a ``PXmlNode``. Every ## occured parsing error is added to the `errors` sequence. var x: TXmlParser - open(x, s, filename, {reportComments}) + open(x, s, filename, {reportComments, reportWhitespace}) next(x) # skip the DOCTYPE: if x.kind == xmlSpecial: next(x) |