Description

Lightweight and forgiving HTML parser

Package Information

Version0.2.16 (2016-Dec-01)
Repository https://github.com/eBookingServices/htmld
LicenseMIT License
CopyrightCopyright © 2015, Marcio Martins
AuthorsMárcio Martins
Registered byMárcio Martins
Dependencies none

Installation

To use this package, put the following dependency into your project's dependencies section:

dub.json
dub.sdl

Readme

htmld Build Status

Lightweight and forgiving HTML parser and DOM.

The parser was inspired by htmlparse2 by fb55

HTML Entity parsing and decoding are both optional. The current parser interface is based on callbacks.

Creating the DOM from source:

auto doc = createDocument(`<html><body>&nbsp;</body></html>`);
writeln(doc.root.outerHTML);

Creating/mutating DOM manually:

auto doc = createDocument();
doc.root.html = `<body>&nbsp;</body>`;

auto container = doc.createElement("div", doc.root.firstChild);
container.attr("class", "container");
container.html = "<p>moo!</p>";

auto app = appender!string;
doc.root.outerHTML(app);

QuerySelector interface:

if (auto p = doc.querySelector("p:nth-of-type(2)"))
    p.text = "mooo";

foreach(p; doc.querySelectorAll("p")) {
    p.text = "mooo";
}

Example parser usage:

auto builder = DOMBuilder();
parseHTML(`<html><body>&nbsp;</body></html>`, builder);

Example handler:

struct DOMBuilder {
    void onText(const(char)[] data) {}
    void onSelfClosing() {}
    void onOpenStart(const(char)[] data) {}
    void onOpenEnd(const(char)[] data) {}
    void onClose(const(char)[] data) {}
    void onAttrName(const(char)[] data) {}
    void onAttrEnd() {}
    void onAttrValue(const(char)[] data) {}
    void onComment(const(char)[] data) {}
    void onDeclaration(const(char)[] data) {}
    void onProcessingInstruction(const(char)[] data) {}
    void onCDATA(const(char)[] data) {}

    // the following are required if ParseEntities is set
    void onNamedEntity(const(char)[] data) {}
    void onNumericEntity(const(char)[] data) {}
    void onHexEntity(const(char)[] data) {}
    
    // required only if DecodeEntities is set
    void onEntity(const(char)[] data, const(char)[] decoded) {}
    
    void onDocumentEnd() {}
}

todo

  • implement range-based interface for parser

Available versions

0.2.16 0.2.15 0.2.14 0.2.13 0.2.12 0.2.11 0.2.10 0.2.9 0.2.8 0.2.7 0.2.6 0.2.5 0.2.4 0.2.3 0.2.2 0.2.1 0.2.0 0.1.1 0.1.0 ~master