ucotta/brillianthtml5parser
This package was designed to parse HTML 5 files for Brilliant Template. However, it can also be used independently.
Create an HTML5 from scratch
let parser = ParserHTML5()
let html = TagHTML(tagName: "html")
let head = TagHTML(tagName: "head")
let body = TagHTML(tagName: "body")
let div = TagHTML(tagName: "div")
div["dupl"] = "3"
html["lang"] = "en"
head.addNode(node: TagHTML(tagName: "title", content: "Welcome title!!"))
body.addNode(node: TagHTML(tagName: "h1", content: "Welcome!"))
body.addNode(node: div)
// Add nodes
html.addNode(node: head)
html.addNode(node: body)
parser.root.addNode(node: html)
// Now print result.
print(parser.toHTML)You will get this (but you won't see it tabbed)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome title!!</title>
</head>
<body>
<h1>Welcome!</h1>
<div dupl="3"></div>
</body>
</html>
If you'd like to have a tabbed result, please feel free to request this feature in ISSUES.
Parsing an HTML5 and commenting a div
let parser = ParserHTML5(html: "somehtmlhere with a div with attribute dupl")
if let div = parser.root.getNextNodeWithAtt(att: "dupl") {
// first comment the div with att dupl=3
div.parentNode?.addNode(node: CommentHTML(comment: div.toHTML))
// Delete the node.
div.parentNode = nil
print(parser.toHTML)
} else {
print("The HTML doesnt contains a div with an attribute named *dupl*")
}
More examples in testing unit:
Package Metadata
Repository: ucotta/brillianthtml5parser
Default branch: master
README: README.md