80 lines
2.6 KiB
Nim
80 lines
2.6 KiB
Nim
import std/htmlparser
|
|
import std/xmltree
|
|
import std/strtabs
|
|
import std/strutils
|
|
|
|
type
|
|
Descriptor* = object
|
|
name* : string
|
|
html_tag* : string
|
|
html_context_tag* : string
|
|
html_context_attrs* : string
|
|
html_context_key* : string
|
|
contains_string* : string
|
|
attrs* : string
|
|
content* : seq[string]
|
|
type
|
|
Entry* = object
|
|
desc* : seq[Descriptor]
|
|
|
|
proc getEntryFromHtml*(entry :var Entry, node : XmlNode) : Entry =
|
|
var
|
|
context : seq[XmlNode]
|
|
tmpContext : seq[XmlNode]
|
|
for i,desc in entry.desc:
|
|
context = @[] #clear sequences
|
|
tmpContext = @[]
|
|
echo("descriptor ",i," content: ",entry.desc[i])
|
|
#filter context
|
|
if entry.desc[i].html_context_attrs == "":
|
|
context = node.findAll(entry.desc[i].html_context_tag)
|
|
else:
|
|
tmpContext = node.findAll(entry.desc[i].html_context_tag)
|
|
#filter by attributes
|
|
for i3,c in tmpContext:
|
|
echo("c: ",c.attrs, "i3: ", i3, "i: ", i, "len tmpContext: ",tmpContext.len())
|
|
if c.attrs.hasKey(entry.desc[i].html_context_attrs):
|
|
echo("HAS-Context_KEY: ",c.attrs)
|
|
if entry.desc[i].html_context_key != "":
|
|
if c.attrs[entry.desc[i].html_context_attrs] == entry.desc[i].html_context_key:
|
|
context.add(c)
|
|
echo("key_MATCH")
|
|
else:
|
|
context.add(c)
|
|
echo("out of for loop")
|
|
#end filter context
|
|
for a in context:
|
|
let subContext = a.findAll(entry.desc[0].html_tag)
|
|
for b in subContext:
|
|
if entry.desc[i].attrs != "":
|
|
if b.attrs.hasKey(entry.desc[i].attrs):
|
|
echo("found key [",entry.desc[i].attrs,"] in : ",b)
|
|
echo("value: ",b.attrs[entry.desc[i].attrs])
|
|
if entry.desc[i].contains_string != "":
|
|
var content_txt :string = b.innerText()
|
|
echo(" and string(innerText): ",content_txt)
|
|
if content_txt.contains(entry.desc[i].contains_string):
|
|
echo("PATTERN MATCH")
|
|
entry.desc[i].content.add(content_txt)
|
|
else:
|
|
entry.desc[i].content.add(b.attrs[entry.desc[i].attrs])
|
|
elif entry.desc[i].contains_string != "":
|
|
var content_txt = b.innerText()
|
|
echo(" string(innerText): ",content_txt)
|
|
if content_txt.contains(entry.desc[i].contains_string):
|
|
echo("PATTERN MATCH")
|
|
entry.desc[i].content.add(content_txt)
|
|
return entry
|
|
|
|
proc test() =
|
|
var htmlnode : XmlNode
|
|
var str_html : string
|
|
var entry: Entry
|
|
entry.desc[0] = Descriptor(html_tag : "p", contains_string : "test_container")
|
|
echo entry
|
|
|
|
htmlnode = parseHtml(str_html)
|
|
discard entry.getEntryFromHtml(htmlnode)
|
|
|
|
#test()
|