-
Notifications
You must be signed in to change notification settings - Fork 12
/
example.zig
39 lines (31 loc) · 1.55 KB
/
example.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (C) 2021-2024 Chadwain Holness
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const rem = @import("rem");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer std.debug.assert(gpa.deinit() == .ok);
const allocator = gpa.allocator();
// This is the text that will be read by the parser.
// Since the parser accepts Unicode codepoints, the text must be decoded before it can be used.
const input = "<!doctype html><html><h1 style=bold>Your text goes here!</h1>";
const decoded_input = &rem.util.utf8DecodeStringComptime(input);
// Create the DOM in which the parsed Document will be created.
var dom = rem.Dom{ .allocator = allocator };
defer dom.deinit();
// Create the HTML parser.
var parser = try rem.Parser.init(&dom, decoded_input, allocator, .report, false);
defer parser.deinit();
// This causes the parser to read the input and produce a Document.
try parser.run();
// `errors` returns the list of parse errors that were encountered while parsing.
// Since we know that our input was well-formed HTML, we expect there to be 0 parse errors.
const errors = parser.errors();
std.debug.assert(errors.len == 0);
// We can now print the resulting Document to the console.
const stdout = std.io.getStdOut().writer();
const document = parser.getDocument();
try rem.util.printDocument(stdout, document, &dom, allocator);
}