summary refs log tree commit diff stats
path: root/tools/cmerge.nim
Commit message (Expand)AuthorAgeFilesLines
* minor style changesAraq2019-07-101-2/+2
* tools: Trim .nim files trailing whitespaceAdam Strzelecki2015-09-041-1/+1
* renamed writeln to writeLine in toolspatrick dw2015-06-191-2/+2
* posix.nim compiles againAraq2014-08-291-4/+4
* case consistency part 7Araq2013-12-281-2/+2
* Removes executable bit for text files.Grzegorz Adam Hankiewicz2013-03-161-0/+0
* bugfix: optimization of complex constant string concatenationsAraq2012-02-171-9/+18
* clean exit codes for all the toolsAraq2011-08-011-1/+1
* fixed pango/pangoutils new wrappersAndreas Rumpf2010-02-261-0/+0
* continued work on html/xmlparserrumpf_a@web.de2010-02-141-0/+0
* atomic is now a keywordAndreas Rumpf2010-01-211-0/+31
e> 2009-05-08 16:36:06 +0200 version 0.7.8' href='/ahoang/Nim/commit/examples/htmlrefs.nim?h=devel&id=db4f617afcd095db087dcb52e3ea603cca111da7'>db4f617af ^
11b695875 ^
08bc9ac03 ^




5d80548cc ^
08bc9ac03 ^

5d80548cc ^
08bc9ac03 ^
5d80548cc ^
08bc9ac03 ^



5d80548cc ^
08bc9ac03 ^



ce5a49492 ^
08bc9ac03 ^


5d80548cc ^
08bc9ac03 ^

1a3b730bf ^
08bc9ac03 ^
5d80548cc ^
08bc9ac03 ^
5d80548cc ^
1a3b730bf ^
08bc9ac03 ^





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57


                                                                          


                                      
                                 


                                                             
                    


                                         
                                              
                                       
                                                     
                




                         
                      

                                                                              
                               
                
                                  



                                                             
                       



                                       
                           


                                                               
                                        

                                  
                                    
           
                
                                          
                
                       





                                      
# 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()