Skip to content

Commit

Permalink
introduce figcaption support for vanilla image syntax
Browse files Browse the repository at this point in the history
also fixes a bug with $if
  • Loading branch information
kristoff-it committed Aug 30, 2024
1 parent a037f44 commit 9d4289b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
6 changes: 3 additions & 3 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
.version = "0.0.0",
.dependencies = .{
.supermd = .{
.url = "git+https://github.com/kristoff-it/supermd#8d6390cc3fe84135050b485e582affc579eaa549",
.hash = "12203eaf39f0cad5f151817ab4ab117f5e6d6e2e6317a6dc0da229996375acbfdbe7",
.url = "git+https://github.com/kristoff-it/supermd#50a76de16fa2942e9d1d00100661d17b4ce3cc5a",
.hash = "1220929ddb502ff2616b1621ad7ff93a36c772a0961507437726186c33582233923c",
},
.scripty = .{
.url = "git+https://github.com/kristoff-it/scripty#df8c11380f9e9bec34809f2242fb116d27cf39d6",
.hash = "122014e78d7c69d93595993b3231f3141368e22634b332b0b91a2fb73a8570f147a5",
},
.superhtml = .{
.url = "git+https://github.com/kristoff-it/superhtml#d57b196b69f93844f5bdfe63ae735b60b71b0e19",
.url = "git+https://github.com/kristoff-it/superhtml#15e2efb525338d15aab57dc5fda7a3badbd3ed5b",
.hash = "12205fb978583b0c0d6915a2763b01b7f9eaa7ffbea4afa1a68e80191622be09ea15",
},
.ziggy = .{
Expand Down
9 changes: 6 additions & 3 deletions src/context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,22 @@ pub const Value = union(enum) {
Map.ZiggyMap => .{ .map = .{ .value = v } },
Map.KV => .{ .map_kv = v },
*const context.Optional => .{ .optional = v },
?*const context.Optional => if (v) |opt| .{ .optional = opt } else context.Optional.Null,
?*const context.Optional => if (v) |opt|
opt.value
else
.{ .err = "$if is not set" },
?[]const u8 => if (v) |opt|
try context.Optional.init(gpa, opt)
else
context.Optional.Null,
Value => v,
?Value => if (v) |opt|
try context.Optional.init(gpa, opt)
else
context.Optional.Null,
Value => v,
?*context.Iterator => if (v) |opt| .{
.iterator = opt,
} else context.Optional.Null,
} else .{ .err = "$loop is not set" },
*context.Iterator => .{ .iterator = v },
[]const []const u8 => .{
.iterator = try context.Iterator.init(gpa, .{
Expand Down
2 changes: 1 addition & 1 deletion src/context/Map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub const Builtins = struct {
};

if (map.value.fields.get(path)) |value| {
return Value.fromZiggy(gpa, value);
return Optional.init(gpa, value);
}

return Optional.Null;
Expand Down
2 changes: 1 addition & 1 deletion src/context/Page.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ aliases: []const []const u8 = &.{},
alternatives: []const Alternative = &.{},
skip_subdirs: bool = false,
translation_key: ?[]const u8 = null,
custom: ziggy.dynamic.Value = .null,
custom: ziggy.dynamic.Value = .{ .kv = .{} },

_meta: struct {
site: *const context.Site = undefined,
Expand Down
36 changes: 28 additions & 8 deletions src/render/html.zig
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ fn renderDirective(
.section, .block, .heading => {},
.image => |img| switch (ev.dir) {
.enter => {
if (img.caption != null) try w.print("<figure>", .{});
const caption = node.firstChild();
if (caption != null) try w.print("<figure>", .{});
try w.print("<img", .{});
if (directive.id) |id| try w.print(" id=\"{s}\"", .{id});
if (directive.attrs) |attrs| {
Expand All @@ -333,15 +334,19 @@ fn renderDirective(
try w.print(" src=\"{s}\"", .{img.src.?.url});
if (img.alt) |alt| try w.print(" alt=\"{s}\"", .{alt});
try w.print(">", .{});
if (img.caption) |caption| try w.print(
"\n<figcaption>{s}</figcaption>\n</figure>",
.{caption},
);
if (caption != null) try w.print("\n<figcaption>", .{});
},
.exit => {
const caption = node.firstChild();
if (caption != null) {
try w.print("</figcaption></figure>", .{});
}
},
.exit => {},
},
.video => |vid| switch (ev.dir) {
.enter => {
const caption = node.firstChild();
if (caption != null) try w.print("<figure>", .{});
try w.print("<video", .{});
if (directive.id) |id| try w.print(" id=\"{s}\"", .{id});
if (directive.attrs) |attrs| {
Expand All @@ -358,8 +363,14 @@ fn renderDirective(
};
const src = vid.src.?.url;
try w.print(">\n<source src=\"{s}\">\n</video>", .{src});
if (caption != null) try w.print("\n<figcaption>", .{});
},
.exit => {
const caption = node.firstChild();
if (caption != null) {
try w.print("</figcaption></figure>", .{});
}
},
.exit => {},
},
.link => |lnk| switch (ev.dir) {
.enter => {
Expand All @@ -382,6 +393,8 @@ fn renderDirective(
},
.code => |code| switch (ev.dir) {
.enter => {
const caption = node.firstChild();
if (caption != null) try w.print("<figure>", .{});
try w.print("<pre", .{});
if (directive.id) |id| try w.print(" id=\"{s}\"", .{id});
if (directive.attrs) |attrs| {
Expand All @@ -393,8 +406,15 @@ fn renderDirective(

// In this case src.url contains the prerendered source code
try w.writeAll(code.src.?.url);
try w.print("</code></pre>", .{});
if (caption != null) try w.print("\n<figcaption>", .{});
},
.exit => {
const caption = node.firstChild();
if (caption != null) {
try w.print("</figcaption></figure>", .{});
}
},
.exit => try w.print("</code></pre>", .{}),
},
}
}
Expand Down

0 comments on commit 9d4289b

Please sign in to comment.