DOM, HTML & Markdown

This document describes how to handle dom parsing, markdown etc.
Contents
HTML parsing
Using the HtmlDocument class and the HtmlParserSettings class we can parse html like below:
auto htmlDoc = parseDom!HtmlDocument(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Hello World</title>
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html>`, new HtmlParserSettings);
auto helloWorld = htmlDoc.body.querySelectorAll("h1");
auto text = helloWorld.text;
XHTML/XML parsing
Using the XHtmlDocument/XmlDocument class and the XHtmlParserSettings/XmlParserSettings class we can parse xhtml or xml like below:
auto xmlDoc = parseDom!XmlDocument(`<?xml version="1.0" encoding="UTF-8"?>
<message>Hello, world!</message>`, new XmlParserSettings);
auto helloWorld = xmlDoc.root;
auto text = helloWorld.text;
Markdown support
Markdown parsing is supported too in DVN.
You can either parse it to full html or parse it to parts.
string parseToHtml(string markdown, string delegate(MarkdownPart) customParser = null);
MarkdownPart[] parse(string markdown);
Use cases
The use-case for dom parsing is simply to support dynamic content and/or translation tools that often output xml or html like data.
For markdown the use-case is simply debugging or creating documentation views where the documentation is written in markdown.
Custom DOM integration
You can implement your own DOM Parser settings by inheriting the DomParserSettings class.
final class MyDomParserSettings : DomParserSettings
{
public:
final:
this() @safe
{
// My dom is strict, has flexible tags (<flex></flex>), allows no self-closing tags, has no standard tags and cannot be repaired.
super(true, ["flex"], false, null, null, null, null);
}
}
You must also implement the DomDocument class.
final class MyDomDocument : DomDocument
{
private:
DomNode _root;
public:
final:
this(DomParserSettings parserSettings) @safe
{
super(parserSettings);
}
override void parseElements(DomNode[] elements) @safe
{
if (!elements || elements.length != 1)
{
throw new Exception("No root element found.");
}
_root = elements[0];
}
@property
{
DomNode root() @safe { return _root; }
void root(DomNode newRoot) @safe
{
_root = newRoot;
}
}
override void repairDocument() @safe
{
throw new Exception("Cannot repair my dom documents.");
}
}
Usage:
auto myDoc = parseDom!MyDomDocument(`<translations>
<translation>
<from>Hello</from>
<to>Bonjour</to>
<flex>function convert(text) { return text; }</flex>
</translation>
</translations>`, new MyDomParserSettings);
auto translations = myDoc.root.querySelectorAll("translation");
foreach (translation; translations)
{
auto from = translation.querySelector("from").text;
auto to = translation.querySelector("to").text;
auto flex = translation.querySelector("flex").text;
// handle ...
}