Something went wrong. Try again.
Markup language that is simple to read, to write and to parse.
zig parser html language markup typst
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123const std = @import("std");const Allocator = std.mem.Allocator;const HTML = Element.HTML;const Element = @import("Element.zig");const Node = Element.Node;
pub const Block = Element.Simple("p");
pub const Bold = Element.Simple("b");pub const Italic = Element.Simple("em");pub const Code = Element.Simple("code");
pub const Link = struct { link: []const u8, content: Element, target: ?[]const u8 = null, node: Node = .{ .ptr = undefined, .vtable = .{ .element = fromNode }, },
const Self = @This();
pub fn init(alloc: Allocator, content: Element, link: []const u8) !*Self { const v = try alloc.create(Self); v.* = .{ .content = content, .link = link }; v.node.ptr = v; return v; }
pub fn element(self: *Self) Element { return Element.Wrapper(Self, html, text).init(self); }
fn fromNode(context: *anyopaque) Element { const self: *Self = @ptrCast(@alignCast(context)); return self.element(); }
fn html(self: *Self, alloc: Allocator) HTML.Error!HTML { var el = try HTML.Content.init(alloc, "a"); el.content = try self.content.html(alloc); try el.base.setAttribute("href", self.link); if (self.target) |target| try el.base.setAttribute("target", target); return el.element(); }
fn text(self: *Self, alloc: Allocator) Allocator.Error![]u8 { var content = std.ArrayList(u8).empty; const ct = try self.content.renderText(alloc); defer alloc.free(ct); if (ct.len < 0) { defer content.deinit(alloc); return try alloc.dupe(u8, self.link); } try content.appendSlice(alloc, ct); try content.appendSlice(alloc, " ("); try content.appendSlice(alloc, self.link); try content.append(alloc, ')'); return try content.toOwnedSlice(alloc); }};
fn doTest(alloc: Allocator, el: Element, exp: []const u8) !void { const got = try el.renderHTML(alloc); defer alloc.free(got); std.testing.expect(std.mem.eql(u8, got, exp)) catch |err| { std.debug.print("{s}\n", .{got}); return err; };}
test "html" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator();
const lit = (try Element.Literal.init(alloc, "hello world")).element(); try doTest(alloc, lit, "hello world");
var p = try Block.init(alloc); var root = try Element.Root.init(alloc); p.content = root.element(); root.append(lit); try doTest(alloc, p.element(), "<p>hello world</p>");
const link = (try Link.init(alloc, (try Element.Literal.init(alloc, "foo")).element(), "example.org")).element(); try doTest(alloc, link, "<a href=\"example.org\">foo</a>");
root.append(link); try doTest(alloc, p.element(), "<p>hello world<a href=\"example.org\">foo</a></p>");}
fn doTestText(alloc: Allocator, el: Element, exp: []const u8) !void { const got = try el.renderText(alloc); defer alloc.free(got); std.testing.expect(std.mem.eql(u8, got, exp)) catch |err| { std.debug.print("{s}\n", .{got}); return err; };}
test "text" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator();
const lit = (try Element.Literal.init(alloc, "hello world")).element(); try doTestText(alloc, lit, "hello world");
var p = try Block.init(alloc); var root = try Element.Root.init(alloc); p.content = root.element(); root.append(lit); try doTestText(alloc, p.element(), "hello world");
const link = (try Link.init(alloc, (try Element.Literal.init(alloc, "foo")).element(), "example.org")).element(); try doTestText(alloc, link, "foo (example.org)");
root.append(link); try doTestText(alloc, p.element(), "hello worldfoo (example.org)");}