srctree

Gregory Mullen parent 80e83a56 e2bca9d5
enable building recursive templates

src/endpoints/repos/commits.zig added: 92, removed: 43, total 49
@@ -125,7 +125,7 @@ fn commitHtml(ctx: *Context, sha: []const u8, repo_name: []const u8, repo: Git.R
},
};
 
var comments: []Template.Structs.Comments = &[0]Template.Structs.Comments{};
var thread: []Template.Structs.Thread = &[0]Template.Structs.Thread{};
if (CommitMap.open(ctx.alloc, repo_name, sha) catch null) |map| {
var dlt = map.delta(ctx.alloc) catch |err| n: {
std.debug.print("error generating delta {}\n", .{err});
@@ -134,14 +134,15 @@ fn commitHtml(ctx: *Context, sha: []const u8, repo_name: []const u8, repo: Git.R
if (dlt) |*delta| {
_ = delta.loadThread(ctx.alloc) catch unreachable;
if (delta.getComments(ctx.alloc)) |thread_comments| {
comments = try ctx.alloc.alloc(Template.Structs.Comments, thread_comments.len);
for (thread_comments, comments) |thr_cmt, *pg_comment| {
pg_comment.* = .{ .comment = .{
thread = try ctx.alloc.alloc(Template.Structs.Thread, thread_comments.len);
for (thread_comments, thread) |thr_cmt, *pg_comment| {
pg_comment.* = .{
.author = try Bleach.sanitizeAlloc(ctx.alloc, thr_cmt.author, .{}),
.date = try allocPrint(ctx.alloc, "{}", .{Humanize.unix(thr_cmt.updated)}),
.message = try Bleach.sanitizeAlloc(ctx.alloc, thr_cmt.message, .{}),
.direct_reply = null,
} };
.sub_thread = null,
};
}
} else |err| {
std.debug.print("Unable to load comments for thread {} {}\n", .{ map.attach.delta, err });
@@ -160,7 +161,7 @@ fn commitHtml(ctx: *Context, sha: []const u8, repo_name: []const u8, repo: Git.R
.nav_auth = undefined,
} },
.commit = try commitCtx(ctx.alloc, current, repo_name),
.comments = comments,
.comments = .{ .thread = thread },
.patch = Diffs.patchStruct(ctx.alloc, &patch, !inline_html) catch return error.Unknown,
});
 
 
src/endpoints/repos/diffs.zig added: 92, removed: 43, total 49
@@ -539,16 +539,17 @@ fn view(ctx: *Context) Error!void {
std.debug.print("Unable to load patch {} {s}\n", .{ err, patch_filename });
}
 
var comments_: []Template.Structs.Comments = &[0]Template.Structs.Comments{};
var root_thread: []S.Thread = &[0]S.Thread{};
if (delta.getComments(ctx.alloc)) |comments| {
comments_ = try ctx.alloc.alloc(Template.Structs.Comments, comments.len);
for (comments, comments_) |comment, *c_ctx| {
c_ctx.* = .{ .comment = .{
root_thread = try ctx.alloc.alloc(S.Thread, comments.len);
for (comments, root_thread) |comment, *c_ctx| {
c_ctx.* = .{
.author = try Bleach.sanitizeAlloc(ctx.alloc, comment.author, .{}),
.date = try allocPrint(ctx.alloc, "{}", .{Humanize.unix(comment.updated)}),
.message = translateComment(ctx.alloc, comment.message, patch) catch unreachable,
.direct_reply = .{ .uri = try allocPrint(ctx.alloc, "{}/direct_reply/{x}", .{ index, fmtSliceHexLower(comment.hash[0..]) }) },
} };
.sub_thread = null,
};
}
} else |err| {
std.debug.print("Unable to load comments for thread {} {}\n", .{ index, err });
@@ -574,7 +575,7 @@ fn view(ctx: *Context) Error!void {
.header = patch_header,
.patch = .{ .files = &[0]Template.Structs.Files{} },
},
.comments = comments_,
.comments = .{ .thread = root_thread },
.delta_id = delta_id,
.patch_does_not_apply = if (patch_applies) null else .{},
.current_username = username,
 
src/template-compiler.zig added: 92, removed: 43, total 49
@@ -1,7 +1,8 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const eql = std.mem.eql;
const compiled = @import("templates-compiled");
const Template = @import("template.zig");
const Allocator = std.mem.Allocator;
 
const AbstTree = struct {
pub const Member = struct {
@@ -15,14 +16,16 @@ const AbstTree = struct {
}
};
 
parent: ?*AbstTree,
alloc: Allocator,
name: []u8,
children: []Member,
child_cap: usize = 0,
 
pub fn init(a: Allocator, name: []const u8) !*AbstTree {
pub fn init(a: Allocator, name: []const u8, parent: ?*AbstTree) !*AbstTree {
const self = try a.create(AbstTree);
self.* = .{
.parent = parent,
.alloc = a,
.name = try a.dupe(u8, name),
.children = try a.alloc(Member, 50),
@@ -32,6 +35,13 @@ const AbstTree = struct {
return self;
}
 
pub fn exists(self: *AbstTree, name: []const u8) bool {
for (self.children) |child| {
if (eql(u8, child.name, name)) return true;
}
return false;
}
 
pub fn append(self: *AbstTree, name: []const u8, kind: []const u8) !void {
if (self.children.len >= self.child_cap) @panic("large structs not implemented");
 
@@ -99,7 +109,7 @@ pub fn main() !void {
defer a.free(fdata);
 
const name = makeStructName(tplt.path);
const this = try AbstTree.init(a, name);
const this = try AbstTree.init(a, name, null);
const gop = try tree.getOrPut(this.name);
if (!gop.found_existing) {
gop.value_ptr.* = this;
@@ -145,7 +155,8 @@ fn emitVars(a: Allocator, fdata: []const u8, current: *AbstTree) !void {
else => |verb| {
data = data[drct.end..];
const name = makeStructName(drct.noun);
var this = try AbstTree.init(a, name);
const field = makeFieldName(drct.noun);
var this = try AbstTree.init(a, name, current);
const gop = try tree.getOrPut(this.name);
if (!gop.found_existing) {
gop.value_ptr.* = this;
@@ -158,22 +169,28 @@ fn emitVars(a: Allocator, fdata: []const u8, current: *AbstTree) !void {
.foreach => {
var buffer: [0xFF]u8 = undefined;
const kind = try std.fmt.bufPrint(&buffer, ": []const {s},\n", .{name});
try current.append(makeFieldName(drct.noun), kind);
try current.append(field, kind);
try emitVars(a, drct.otherwise.blob.trimmed, this);
},
.forrow => {
var buffer: [0xFF]u8 = undefined;
const kind = try std.fmt.bufPrint(&buffer, ": []const []const u8,\n", .{});
try current.append(makeFieldName(drct.noun), kind);
try current.append(field, kind);
try emitVars(a, drct.otherwise.blob.trimmed, this);
},
.with => {
var buffer: [0xFF]u8 = undefined;
const kind = try std.fmt.bufPrint(&buffer, ": ?{s},\n", .{name});
try current.append(makeFieldName(drct.noun), kind);
try current.append(field, kind);
try emitVars(a, drct.otherwise.blob.trimmed, this);
},
.build => unreachable,
.build => {
var buffer: [0xFF]u8 = undefined;
const tmpl_name = makeStructName(drct.otherwise.template.name);
const kind = try std.fmt.bufPrint(&buffer, ": {s},\n", .{tmpl_name});
try current.append(field, kind);
//try emitVars(a, drct.otherwise.template.blob, this);
},
}
},
} else if (std.mem.indexOfPos(u8, data, 1, "<")) |next| {
 
src/template.zig added: 92, removed: 43, total 49
@@ -269,7 +269,7 @@ pub const Directive = struct {
.otherwise = .{ .template = bi },
.end = verb.len + 1 + noun.len,
};
} else unreachable;
} else return null;
} else return null;
 
// TODO convert to while
 
src/template/page.zig added: 92, removed: 43, total 49
@@ -4,7 +4,6 @@ const eql = std.mem.eql;
 
const Templates = @import("../template.zig");
const Template = Templates.Template;
//const TemplateRuntime = Templates.TemplateRuntime;
const DataMap = Templates.DataMap;
const Directive = Templates.Directive;
 
@@ -132,7 +131,7 @@ pub fn PageRuntime(comptime PageDataType: type) type {
}
},
.Struct => {
if (std.mem.eql(u8, field.name, noun.vari)) {
if (std.mem.eql(u8, field.name, noun)) {
const subdata = @field(self.data, field.name);
var subpage = subt.pageOf(@TypeOf(subdata), subdata);
try subpage.format(fmts, .{}, out);
 
filename was Deleted added: 92, removed: 43, total 49
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<title>srctree</title>
<_meta_head.html>
</head>
<body>
<_body_header.html>
<content class="diff">
<context>
<With Patch>
<With Header>
<context><Title></context>
<p><Message></p>
</With>
<diff>
<_patch.html>
</diff>
</With>
</context>
 
<comments>
<Build Comments comment_thread.html />
<form class="pretty" action="add-comment" method="POST">
<context><CurrentUsername></context>
<textarea name="comment"></textarea>
<button type="submit" name="comment">Comment</button>
<With PatchDoesNotApply><button type="submit" name="no-apply" class="red">Patch Does Not Apply</button></With>
<input type="hidden" name="did" value="<DeltaId>" />
</form>
</comments>
</content>
</body>
</html>
 
templates/_comment.html added: 92, removed: 43, total 49
@@ -1,3 +1,4 @@
<For Thread>
<comment>
<context>
<Author>
@@ -9,4 +10,6 @@
<a class="muted reply" href="<Uri>">Reply</a>
</With>
</message>
<With SubThread><Build Comments _comment_thread.html /></With>
</comment>
</For>
 
templates/commit.html added: 92, removed: 43, total 49
@@ -23,9 +23,7 @@
</content>
 
<content class="diff">
<For Comments>
<_comment.html>
</For>
<Build Comments _comment_thread.html />
<diff>
<_patch.html>
</diff>
 
templates/delta-diff.html added: 92, removed: 43, total 49
@@ -20,9 +20,7 @@
</context>
 
<comments>
<For Comments>
<_comment.html>
</For>
<Build Comments _comment_thread.html />
<form class="pretty" action="add-comment" method="POST">
<context><CurrentUsername></context>
<textarea name="comment"></textarea>
 
templates/delta-issue.html added: 92, removed: 43, total 49
@@ -13,11 +13,9 @@
<desc><Desc></desc>
</issue>
<comments>
<For Comments>
<_comment.html>
</For>
<Build Comments _comment_thread.html />
<form class="pretty" action="add-comment" method="POST">
<context><Current_username ORNULL></context>
<context><CurrentUsername ORNULL></context>
<textarea name="comment"></textarea>
<button type="submit" name="comment">Comment</button>
<input type="hidden" name="did" value="<Delta_id>" />