Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kristoff-it committed Aug 29, 2024
1 parent 8311dfb commit b1cb3aa
Show file tree
Hide file tree
Showing 16 changed files with 736 additions and 349 deletions.
4 changes: 2 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ pub fn scriptyReferenceDocs(
);

const reference_md = run_docgen.addOutputFileArg(
"shtml_scripty_reference.md",
"shtml_scripty_reference.smd",
);

const wf = project.addWriteFiles();
Expand All @@ -372,7 +372,7 @@ pub fn scriptyReferenceDocs(
);

const reference_md = run_docgen.addOutputFileArg(
"smd_scripty_reference.md",
"smd_scripty_reference.smd",
);

const wf = project.addWriteFiles();
Expand Down
32 changes: 16 additions & 16 deletions build/content.zig
Original file line number Diff line number Diff line change
Expand Up @@ -437,26 +437,26 @@ pub fn scanVariant(
}
}

if (dir_entry.dir.openFile("index.md", .{})) |file| blk: {
if (dir_entry.dir.openFile("index.smd", .{})) |file| blk: {
defer file.close();

var buf_reader = std.io.bufferedReader(file.reader());
const r = buf_reader.reader();
const result = FrontParser.parse(project.allocator, r, "index.md") catch @panic("TODO: report frontmatter parser error");
const result = FrontParser.parse(project.allocator, r, "index.smd") catch @panic("TODO: report frontmatter parser error");

const permalink = project.pathJoin(&.{ "/", url_path_prefix, dir_entry.path, "/" });

const fm = switch (result) {
.success => |s| s.header,
.empty => {
std.debug.print("WARNING: ignoring empty file '{s}{s}'\n", .{
permalink, "index.md",
permalink, "index.smd",
});
break :blk;
},
.framing_error => |line| {
std.debug.print("ERROR: bad frontmatter framing in '{s}{s}' (line {})\n", .{
permalink, "index.md", line,
permalink, "index.smd", line,
});
std.process.exit(1);
},
Expand All @@ -475,14 +475,14 @@ pub fn scanVariant(
current_section.* = .{ .content_sub_path = content_sub_path };
parent_section.pages.append(project.allocator, .{
.content_sub_path = content_sub_path,
.md_name = "index.md",
.md_name = "index.smd",
.fm = fm,
.subpages = current_section,
}) catch unreachable;
} else {
root_index = .{
.content_sub_path = project.dupe(dir_entry.path),
.md_name = "index.md",
.md_name = "index.smd",
.fm = fm,

.subpages = root_section,
Expand All @@ -493,7 +493,7 @@ pub fn scanVariant(
} else |index_md_err| {
if (index_md_err != error.FileNotFound) {
std.debug.print(
"Unable to access `index.md` in {s}\n",
"Unable to access `index.smd` in {s}\n",
.{content_dir_path},
);
std.process.exit(1);
Expand All @@ -504,8 +504,8 @@ pub fn scanVariant(
while (it.next() catch unreachable) |entry| {
switch (entry.kind) {
else => continue,
.file => if (std.mem.endsWith(u8, entry.name, ".md")) {
if (std.mem.eql(u8, entry.name, "index.md")) continue;
.file => if (std.mem.endsWith(u8, entry.name, ".smd")) {
if (std.mem.eql(u8, entry.name, "index.smd")) continue;
const file = dir_entry.dir.openFile(entry.name, .{}) catch {
std.debug.print(
"Error while reading {s} in /{s}\n",
Expand All @@ -522,20 +522,20 @@ pub fn scanVariant(
"/",
url_path_prefix,
dir_entry.path,
entry.name[0 .. entry.name.len - 3],
entry.name[0 .. entry.name.len - ".smd".len],
});

const result = FrontParser.parse(project.allocator, r, entry.name) catch @panic("TODO: report frontmatter parse error");
const fm = switch (result) {
.success => |s| s.header,
.empty => {
std.debug.print("WARNING: ignoring empty file '{s}.md'\n", .{
std.debug.print("WARNING: ignoring empty file '{s}.smd'\n", .{
permalink,
});
continue;
},
.framing_error => |line| {
std.debug.print("ERROR: bad frontmatter framing in '{s}.md' (line {})\n", .{
std.debug.print("ERROR: bad frontmatter framing in '{s}.smd' (line {})\n", .{
permalink, line,
});
std.process.exit(1);
Expand Down Expand Up @@ -637,7 +637,7 @@ pub fn addAllSteps(
layouts_dir_path,
assets_dir_path,
"",
"index.md",
"index.smd",
"index.html",
idx.fm.layout,
idx.fm.aliases,
Expand Down Expand Up @@ -683,7 +683,7 @@ pub fn addAllSteps(
var section_it = sections.constIterator(0);
while (section_it.next()) |s| {
for (s.pages.items, 0..) |p, idx| {
const out_basename = p.md_name[0 .. p.md_name.len - 3];
const out_basename = p.md_name[0 .. p.md_name.len - ".smd".len];
const out_path = if (std.mem.eql(u8, out_basename, "index"))
project.pathJoin(&.{ p.content_sub_path, "index.html" })
else
Expand Down Expand Up @@ -761,7 +761,7 @@ fn addMarkdownRenderStep(
permalink: []const u8,
) RenderResult {
const in_path = project.pathJoin(&.{ content_dir_path, content_sub_path, md_basename });
const out_basename = md_basename[0 .. md_basename.len - 3];
const out_basename = md_basename[0 .. md_basename.len - ".smd".len];

const render_step = project.addRunArtifact(renderer);
// assets_in_dir_path
Expand Down Expand Up @@ -1041,7 +1041,7 @@ const Section = struct {
if (!gop.found_existing) {
var hash = std.hash.Wyhash.init(1990);
if (locale_code) |lc| hash.update(lc);
if (std.mem.eql(u8, p.md_name, "index.md")) {
if (std.mem.eql(u8, p.md_name, "index.smd")) {
hash.update(std.fs.path.dirname(p.content_sub_path) orelse "");
} else {
hash.update(p.content_sub_path);
Expand Down
2 changes: 1 addition & 1 deletion frontmatter.ziggy-schema
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct Frontmatter {
alternatives: ?[Alternative],
///Ignore other markdown files in this directory and
///any sub-directory. Can only be meaningfully set to
///true for 'index.md' pages.
///true for 'index.smd' pages.
skip_subdirs: ?bool,
///User-defined properties that you can then reference
///in templates.
Expand Down
2 changes: 1 addition & 1 deletion src/context/Bool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub const Builtins = struct {
\\If the boolean is `true`, returns the first argument.
\\Otherwise, returns the second argument.
\\
\\Omitting the second argument defaults to an empty string.
\\The second argument defaults to an empty string.
\\
;
pub const examples =
Expand Down
41 changes: 35 additions & 6 deletions src/context/Map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,11 @@ pub const Builtins = struct {

pub const iterate = struct {
pub const signature: Signature = .{
.params = &.{.{ .Opt = .String }},
.params = &.{},
.ret = .{ .Many = .KV },
};
pub const description =
\\Iterates over key-value pairs of a Ziggy map.
\\
\\You can optionally pass a string that will be used to filter key names.
;
pub const examples =
\\$page.custom.iterate()
Expand All @@ -180,10 +178,41 @@ pub const Builtins = struct {
gpa: Allocator,
args: []const Value,
) !Value {
const bad_arg = .{ .err = "expected 0 or 1 string argument" };
if (args.len > 1) return bad_arg;
const bad_arg = .{ .err = "expected 0 arguments" };
if (args.len != 0) return bad_arg;

return .{
.iterator = try context.Iterator.init(gpa, .{
.map_it = context.Iterator.MapIterator.init(
map.value.fields.iterator(),
null,
),
}),
};
}
};

pub const iterPattern = struct {
pub const signature: Signature = .{
.params = &.{.String},
.ret = .{ .Many = .KV },
};
pub const description =
\\Iterates over key-value pairs of a Ziggy map where the key
\\matches the given pattern.
;
pub const examples =
\\$page.custom.iterPattern("user-")
;
pub fn call(
map: Map,
gpa: Allocator,
args: []const Value,
) !Value {
const bad_arg = .{ .err = "expected 1 string argument" };
if (args.len != 1) return bad_arg;

const filter: ?[]const u8 = if (args.len == 0) null else switch (args[0]) {
const filter: []const u8 = switch (args[0]) {
.string => |s| s.value,
else => return bad_arg,
};
Expand Down
50 changes: 29 additions & 21 deletions src/context/Page.zig
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ pub const Fields = struct {
\\Skips any other potential content present in the subdir of the page,
\\as set in the SuperMD frontmatter.
\\
\\Can only be set to true on section pages (i.e. `index.md` pages).
\\Can only be set to true on section pages (i.e. `index.smd` pages).
;
pub const translation_key =
\\Translation key used to map this page with corresponding localized variants,
Expand Down Expand Up @@ -227,12 +227,12 @@ pub const Builtins = struct {
\\Assets for a non-section page must be placed under a subdirectory
\\that shares the same name with the corresponding markdown file.
\\
\\(as a reminder sections are defined by pages named `index.md`)
\\(as a reminder sections are defined by pages named `index.smd`)
\\
\\| section? | page path | asset directory |
\\|----------|---------------------|-----------------|
\\| yes | blog/foo/index.md | blog/foo/ |
\\| no | blog/bar.md | blog/bar/ |
\\| section? | page path | asset directory |
\\|----------|--------------------|-----------------|
\\| yes | blog/foo/index.smd | blog/foo/ |
\\| no | blog/bar.smd | blog/bar/ |
;
pub const examples =
\\<img src="$page.asset('foo.png').link(false)">
Expand Down Expand Up @@ -432,7 +432,7 @@ pub const Builtins = struct {
pub const signature: Signature = .{ .ret = .Bool };
pub const description =
\\Returns true if the current page defines a section (i.e. if
\\the current page is an 'index.md' page).
\\the current page is an 'index.smd' page).
\\
;
pub const examples =
Expand All @@ -455,7 +455,7 @@ pub const Builtins = struct {
\\Returns a list of all the pages in this section. If the page is
\\not a section, returns an empty list.
\\
\\Sections are defined by `index.md` files, see the content
\\Sections are defined by `index.smd` files, see the content
\\structure section in the official docs for more info.
;
pub const examples =
Expand Down Expand Up @@ -602,8 +602,8 @@ pub const Builtins = struct {
if (args.len != 0) return .{ .err = "expected 0 arguments" };
const p = self._meta.md_rel_path;
const path = switch (self._meta.is_section) {
true => p[0 .. p.len - "index.md".len],
false => p[0 .. p.len - ".md".len],
true => p[0 .. p.len - "index.smd".len],
false => p[0 .. p.len - ".smd".len],
};

// TODO: support host url overrides
Expand Down Expand Up @@ -656,20 +656,17 @@ pub const Builtins = struct {
return String.init(try buf.toOwnedSlice());
}
};
pub const block = struct {
pub const contentSection = struct {
pub const signature: Signature = .{
.params = &.{.String},
.ret = .String,
};
pub const description =
\\Renders the specified content block of a page.
\\
\\Example:
\\ `# [Title]($block.id('section-id'))`
\\Renders the specified [content section]($link.page('docs/supermd/scripty').ref('Section')) of a page.
;
pub const examples =
\\<div html="$page.block('section-id')"></div>
\\<div html="$page.block('other-section')"></div>
\\<div html="$page.contentSection('section-id')"></div>
\\<div html="$page.contentSection('other-section')"></div>
;
pub fn call(
p: *const Page,
Expand All @@ -681,7 +678,7 @@ pub const Builtins = struct {
};
if (args.len != 1) return bad_arg;

const block_id = switch (args[0]) {
const section_id = switch (args[0]) {
.string => |s| s.value,
else => return bad_arg,
};
Expand All @@ -691,14 +688,25 @@ pub const Builtins = struct {
};
var buf = std.ArrayList(u8).init(gpa);

const node = ast.blocks.get(block_id) orelse {
const node = ast.ids.get(section_id) orelse {
return Value.errFmt(
gpa,
"content section '{s}' doesn't exist, available sections are: {s}",
.{ block_id, ast.blocks.keys() },
"content section '{s}' doesn't exist",
.{section_id},
);
};

switch (node.getDirective().?.kind) {
.section => {},
else => {
return Value.errFmt(
gpa,
"id '{s}' exists but is not a section",
.{section_id},
);
},
}

try render.html(gpa, ast, node, "", buf.writer());
return String.init(try buf.toOwnedSlice());
}
Expand Down
15 changes: 10 additions & 5 deletions src/context/Site.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const Value = context.Value;
const Bool = context.Bool;
const String = context.String;

const log = std.log.scoped(.scripty);

host_url: []const u8,
title: []const u8,
_meta: struct {
Expand Down Expand Up @@ -181,8 +183,8 @@ pub const Builtins = struct {
\\
\\For example, the value 'foo/bar' will be automatically
\\matched by Zine with either:
\\ - content/foo/bar.md
\\ - content/foo/bar/index.md
\\ - content/foo/bar.smd
\\ - content/foo/bar/index.smd
;
pub const examples =
\\<a href="$site.page('downloads').link()">Downloads</a>
Expand All @@ -204,18 +206,21 @@ pub const Builtins = struct {
else => return bad_arg,
};

return context.pageFind(.{
const res = try context.pageFind(.{
.ref = .{
.path = ref,
.site = site,
},
});

log.debug("res = {*}", .{res.page});
return res;
}
};
pub const pages = struct {
pub const signature: Signature = .{
.params = &.{.{ .Many = .String }},
.ret = .Page,
.params = &.{ .String, .{ .Many = .String } },
.ret = .{ .Many = .Page },
};
pub const description =
\\Same as `page`, but accepts a variable number of page references and
Expand Down
Loading

0 comments on commit b1cb3aa

Please sign in to comment.