/zhtml

HTML parser built in Zig

Primary LanguageZigMIT LicenseMIT

Z-HTML

This is a work in progress, spec compliant, HTML parser built with Zig. Currently lots of things are broken. You can check the status of tests that are not passing by checking out the ignored_tests in test/tokenizer-html5lib.zig.

Roadmap

  • Tokenizer (missing a few edge cases)
  • Parser (in progress)
  • JavaScript DOM API support

See the CHANGELOG.md for detailed information on past changes.

Tokenizer

The Tokenizer struct provides a (mostly) fully featured HTML tokenizer built according to the WHATGW HTML Spec. It is a streaming tokenizer which takes as input a full document, processes the document character by character, and emits both Tokens and ParseErrors. An example usage of it by itself could look like this:

const std = @import("std");
const Tokenizer = @import("zhtml/tokenizer.zig").Tokenizer;

pub fn main() void {
    var allocator = std.heap.page_allocator;
    var tokenizer = try Tokenizer.initWithFile(alloc, "./test.html");
    while (true) {
        var token = self.tokenizer.nextToken() catch |err| {
            std.debug.warn("{} (line: {}, column: {})\n", .{ err, tokenizer.line, tokenizer.column });
            continue;
        };

        if (token) |tok| {
            switch (tok) {
                Token.EndOfFile => break,
                else => std.debug.warn("{}\n", .{ tok });
            }
        }
    }
}

though the Tokenizer is meant to be used in conjunction with the Parser.

Parser

Work in progress. Check back later.

License

Copyright 2022 Chris Watson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.