make 'koch web' work again
This commit is contained in:
parent
320582a55c
commit
b53531ee31
2 changed files with 149 additions and 26 deletions
|
|
@ -17,12 +17,37 @@
|
||||||
##
|
##
|
||||||
## .. include:: ../../doc/mytest.cfg
|
## .. include:: ../../doc/mytest.cfg
|
||||||
## :literal:
|
## :literal:
|
||||||
## The file ``examples/parsecfgex.nim`` demonstrates how to use the
|
|
||||||
## configuration file parser:
|
|
||||||
##
|
|
||||||
## .. code-block:: nim
|
|
||||||
## :file: ../../examples/parsecfgex.nim
|
|
||||||
##
|
##
|
||||||
|
|
||||||
|
##[ Here is an example of how to use the configuration file parser:
|
||||||
|
|
||||||
|
.. code-block:: nim
|
||||||
|
|
||||||
|
import
|
||||||
|
os, parsecfg, strutils, streams
|
||||||
|
|
||||||
|
var f = newFileStream(paramStr(1), fmRead)
|
||||||
|
if f != nil:
|
||||||
|
var p: CfgParser
|
||||||
|
open(p, f, paramStr(1))
|
||||||
|
while true:
|
||||||
|
var e = next(p)
|
||||||
|
case e.kind
|
||||||
|
of cfgEof: break
|
||||||
|
of cfgSectionStart: ## a ``[section]`` has been parsed
|
||||||
|
echo("new section: " & e.section)
|
||||||
|
of cfgKeyValuePair:
|
||||||
|
echo("key-value-pair: " & e.key & ": " & e.value)
|
||||||
|
of cfgOption:
|
||||||
|
echo("command: " & e.key & ": " & e.value)
|
||||||
|
of cfgError:
|
||||||
|
echo(e.msg)
|
||||||
|
close(p)
|
||||||
|
else:
|
||||||
|
echo("cannot open: " & paramStr(1))
|
||||||
|
|
||||||
|
]##
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
## --------
|
## --------
|
||||||
##
|
##
|
||||||
|
|
|
||||||
|
|
@ -26,27 +26,125 @@
|
||||||
## creates.
|
## creates.
|
||||||
##
|
##
|
||||||
##
|
##
|
||||||
## Example 1: Retrieve HTML title
|
|
||||||
## ==============================
|
##[
|
||||||
##
|
|
||||||
## The file ``examples/htmltitle.nim`` demonstrates how to use the
|
Example 1: Retrieve HTML title
|
||||||
## XML parser to accomplish a simple task: To determine the title of an HTML
|
==============================
|
||||||
## document.
|
|
||||||
##
|
The file ``examples/htmltitle.nim`` demonstrates how to use the
|
||||||
## .. code-block:: nim
|
XML parser to accomplish a simple task: To determine the title of an HTML
|
||||||
## :file: ../../examples/htmltitle.nim
|
document.
|
||||||
##
|
|
||||||
##
|
.. code-block:: nim
|
||||||
## Example 2: Retrieve all HTML links
|
|
||||||
## ==================================
|
# Example program to show the parsexml module
|
||||||
##
|
# This program reads an HTML file and writes its title to stdout.
|
||||||
## The file ``examples/htmlrefs.nim`` demonstrates how to use the
|
# Errors and whitespace are ignored.
|
||||||
## XML parser to accomplish another simple task: To determine all the links
|
|
||||||
## an HTML document contains.
|
import os, streams, parsexml, strutils
|
||||||
##
|
|
||||||
## .. code-block:: nim
|
if paramCount() < 1:
|
||||||
## :file: ../../examples/htmlrefs.nim
|
quit("Usage: htmltitle filename[.html]")
|
||||||
##
|
|
||||||
|
var filename = addFileExt(paramStr(1), "html")
|
||||||
|
var s = newFileStream(filename, fmRead)
|
||||||
|
if s == nil: quit("cannot open the file " & filename)
|
||||||
|
var x: XmlParser
|
||||||
|
open(x, s, filename)
|
||||||
|
while true:
|
||||||
|
x.next()
|
||||||
|
case x.kind
|
||||||
|
of xmlElementStart:
|
||||||
|
if cmpIgnoreCase(x.elementName, "title") == 0:
|
||||||
|
var title = ""
|
||||||
|
x.next() # skip "<title>"
|
||||||
|
while x.kind == xmlCharData:
|
||||||
|
title.add(x.charData)
|
||||||
|
x.next()
|
||||||
|
if x.kind == xmlElementEnd and cmpIgnoreCase(x.elementName, "title") == 0:
|
||||||
|
echo("Title: " & title)
|
||||||
|
quit(0) # Success!
|
||||||
|
else:
|
||||||
|
echo(x.errorMsgExpected("/title"))
|
||||||
|
|
||||||
|
of xmlEof: break # end of file reached
|
||||||
|
else: discard # ignore other events
|
||||||
|
|
||||||
|
x.close()
|
||||||
|
quit("Could not determine title!")
|
||||||
|
|
||||||
|
]##
|
||||||
|
|
||||||
|
##[
|
||||||
|
|
||||||
|
Example 2: Retrieve all HTML links
|
||||||
|
==================================
|
||||||
|
|
||||||
|
The file ``examples/htmlrefs.nim`` demonstrates how to use the
|
||||||
|
XML parser to accomplish another simple task: To determine all the links
|
||||||
|
an HTML document contains.
|
||||||
|
|
||||||
|
.. code-block:: nim
|
||||||
|
|
||||||
|
# Example program to show the new parsexml module
|
||||||
|
# This program reads an HTML file and writes all its used links to stdout.
|
||||||
|
# Errors and whitespace are ignored.
|
||||||
|
|
||||||
|
import os, streams, parsexml, strutils
|
||||||
|
|
||||||
|
proc `=?=` (a, b: string): bool =
|
||||||
|
# little trick: define our own comparator that ignores case
|
||||||
|
return cmpIgnoreCase(a, b) == 0
|
||||||
|
|
||||||
|
if paramCount() < 1:
|
||||||
|
quit("Usage: htmlrefs filename[.html]")
|
||||||
|
|
||||||
|
var links = 0 # count the number of links
|
||||||
|
var filename = addFileExt(paramStr(1), "html")
|
||||||
|
var s = newFileStream(filename, fmRead)
|
||||||
|
if s == nil: quit("cannot open the file " & filename)
|
||||||
|
var x: XmlParser
|
||||||
|
open(x, s, filename)
|
||||||
|
next(x) # get first event
|
||||||
|
block mainLoop:
|
||||||
|
while true:
|
||||||
|
case x.kind
|
||||||
|
of xmlElementOpen:
|
||||||
|
# the <a href = "xyz"> tag we are interested in always has an attribute,
|
||||||
|
# thus we search for ``xmlElementOpen`` and not for ``xmlElementStart``
|
||||||
|
if x.elementName =?= "a":
|
||||||
|
x.next()
|
||||||
|
if x.kind == xmlAttribute:
|
||||||
|
if x.attrKey =?= "href":
|
||||||
|
var link = x.attrValue
|
||||||
|
inc(links)
|
||||||
|
# skip until we have an ``xmlElementClose`` event
|
||||||
|
while true:
|
||||||
|
x.next()
|
||||||
|
case x.kind
|
||||||
|
of xmlEof: break mainLoop
|
||||||
|
of xmlElementClose: break
|
||||||
|
else: discard
|
||||||
|
x.next() # skip ``xmlElementClose``
|
||||||
|
# now we have the description for the ``a`` element
|
||||||
|
var desc = ""
|
||||||
|
while x.kind == xmlCharData:
|
||||||
|
desc.add(x.charData)
|
||||||
|
x.next()
|
||||||
|
echo(desc & ": " & link)
|
||||||
|
else:
|
||||||
|
x.next()
|
||||||
|
of xmlEof: break # end of file reached
|
||||||
|
of xmlError:
|
||||||
|
echo(errorMsg(x))
|
||||||
|
x.next()
|
||||||
|
else: x.next() # skip other events
|
||||||
|
|
||||||
|
echo($links & " link(s) found!")
|
||||||
|
x.close()
|
||||||
|
|
||||||
|
]##
|
||||||
|
|
||||||
import
|
import
|
||||||
hashes, strutils, lexbase, streams, unicode
|
hashes, strutils, lexbase, streams, unicode
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue