Code Monkey home page Code Monkey logo

htmld's Introduction

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. It can also output compact HTML, and can do basic validation.

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);

Compacting HTML:

auto doc = createDocument!(DOMCreateOptions.none)("<html>\n<body attr="a" attr="a b c">    abc    </body>\n</html>");
writeln(doc.root.compactHTML);

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
  • implement a basic validation mode

htmld's People

Contributors

archipel avatar devebookingservices avatar forbjok avatar marcioapm avatar martinnowak avatar sirnickolas avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.