-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathexample.zig
More file actions
42 lines (34 loc) · 1.67 KB
/
example.zig
File metadata and controls
42 lines (34 loc) · 1.67 KB
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
40
41
42
// 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.
var buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&buffer);
const stdout = &stdout_writer.interface;
const document = parser.getDocument();
try rem.util.printDocument(stdout, document, &dom, allocator);
try stdout.flush();
}