Skip to content

Commit 3d0928a

Browse files
committed
add a --with_base option to fetch
with_base option adds a <base> tag to the dump for better offline preview.
1 parent ea1bca0 commit 3d0928a

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/browser/dump.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const Walker = @import("dom/walker.zig").WalkerChildren;
2323

2424
pub const Opts = struct {
2525
exclude_scripts: bool = false,
26+
include_base: ?[]const u8 = null,
2627
};
2728

2829
// writer must be a std.io.Writer
@@ -91,6 +92,14 @@ pub fn writeNode(node: *parser.Node, opts: Opts, writer: anytype) anyerror!void
9192
// void elements can't have any content.
9293
if (try isVoid(parser.nodeToElement(node))) return;
9394

95+
// If we wrote the <header> and we want to include a <base>, add it
96+
// now.
97+
if (opts.include_base != null and tag_type == .head) {
98+
try writer.writeAll("<base href=\"");
99+
try writer.writeAll(opts.include_base.?);
100+
try writer.writeAll("\">");
101+
}
102+
94103
if (tag_type == .script) {
95104
try writer.writeAll(try parser.nodeTextContent(node) orelse "");
96105
} else {

src/main.zig

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ fn run(alloc: Allocator) !void {
134134

135135
// dump
136136
if (opts.dump) {
137-
try page.dump(.{ .exclude_scripts = opts.noscript }, std.io.getStdOut());
137+
try page.dump(.{
138+
.exclude_scripts = opts.noscript,
139+
.include_base = if (opts.withbase) page.url.raw else null,
140+
}, std.io.getStdOut());
138141
}
139142
},
140143
else => unreachable,
@@ -213,6 +216,7 @@ const Command = struct {
213216
dump: bool = false,
214217
common: Common,
215218
noscript: bool = false,
219+
withbase: bool = false,
216220
};
217221

218222
const Common = struct {
@@ -277,6 +281,7 @@ const Command = struct {
277281
\\--dump Dumps document to stdout.
278282
\\ Defaults to false.
279283
\\--noscript Exclude <script> tags in dump. Defaults to false.
284+
\\--with_base Add a <base> tag in dump. Defaults to false.
280285
\\
281286
++ common_options ++
282287
\\
@@ -351,13 +356,19 @@ fn inferMode(opt: []const u8) ?App.RunMode {
351356
return .serve;
352357
}
353358

359+
if (std.mem.startsWith(u8, opt, "--") == false) {
360+
return .fetch;
361+
}
362+
354363
if (std.mem.eql(u8, opt, "--dump")) {
355364
return .fetch;
356365
}
366+
357367
if (std.mem.eql(u8, opt, "--noscript")) {
358368
return .fetch;
359369
}
360-
if (std.mem.startsWith(u8, opt, "--") == false) {
370+
371+
if (std.mem.eql(u8, opt, "--with_base")) {
361372
return .fetch;
362373
}
363374

@@ -443,6 +454,7 @@ fn parseFetchArgs(
443454
) !Command.Fetch {
444455
var dump: bool = false;
445456
var noscript: bool = false;
457+
var withbase: bool = false;
446458
var url: ?[]const u8 = null;
447459
var common: Command.Common = .{};
448460

@@ -457,6 +469,11 @@ fn parseFetchArgs(
457469
continue;
458470
}
459471

472+
if (std.mem.eql(u8, "--with_base", opt)) {
473+
withbase = true;
474+
continue;
475+
}
476+
460477
if (try parseCommonArg(allocator, opt, args, &common)) {
461478
continue;
462479
}
@@ -483,6 +500,7 @@ fn parseFetchArgs(
483500
.dump = dump,
484501
.common = common,
485502
.noscript = noscript,
503+
.withbase = withbase,
486504
};
487505
}
488506

0 commit comments

Comments
 (0)