mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-22 20:54:43 +00:00
Unify dump flags into --dump <format>
This commit is contained in:
@@ -163,10 +163,14 @@ pub const Serve = struct {
|
|||||||
common: Common = .{},
|
common: Common = .{},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub const DumpFormat = enum {
|
||||||
|
html,
|
||||||
|
markdown,
|
||||||
|
};
|
||||||
|
|
||||||
pub const Fetch = struct {
|
pub const Fetch = struct {
|
||||||
url: [:0]const u8,
|
url: [:0]const u8,
|
||||||
html: bool = false,
|
dump_mode: ?DumpFormat = null,
|
||||||
markdown: bool = false,
|
|
||||||
common: Common = .{},
|
common: Common = .{},
|
||||||
withbase: bool = false,
|
withbase: bool = false,
|
||||||
strip: dump.Opts.Strip = .{},
|
strip: dump.Opts.Strip = .{},
|
||||||
@@ -303,16 +307,12 @@ pub fn printUsageAndExit(self: *const Config, success: bool) void {
|
|||||||
\\
|
\\
|
||||||
\\fetch command
|
\\fetch command
|
||||||
\\Fetches the specified URL
|
\\Fetches the specified URL
|
||||||
\\Example: {s} fetch --html https://lightpanda.io/
|
\\Example: {s} fetch --dump html https://lightpanda.io/
|
||||||
\\
|
\\
|
||||||
\\Options:
|
\\Options:
|
||||||
\\--html Dumps document to stdout as HTML.
|
\\--dump Dumps document to stdout.
|
||||||
\\ Defaults to false.
|
\\ Argument must be 'html' or 'markdown'.
|
||||||
\\
|
\\ Defaults to no dump.
|
||||||
\\--dump Alias for --html (deprecated).
|
|
||||||
\\
|
|
||||||
\\--markdown Dumps document to stdout as Markdown.
|
|
||||||
\\ Defaults to false.
|
|
||||||
\\
|
\\
|
||||||
\\--strip_mode Comma separated list of tag groups to remove from dump
|
\\--strip_mode Comma separated list of tag groups to remove from dump
|
||||||
\\ the dump. e.g. --strip_mode js,css
|
\\ the dump. e.g. --strip_mode js,css
|
||||||
@@ -412,18 +412,10 @@ fn inferMode(opt: []const u8) ?RunMode {
|
|||||||
return .fetch;
|
return .fetch;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (std.mem.eql(u8, opt, "--html")) {
|
|
||||||
return .fetch;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (std.mem.eql(u8, opt, "--dump")) {
|
if (std.mem.eql(u8, opt, "--dump")) {
|
||||||
return .fetch;
|
return .fetch;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (std.mem.eql(u8, opt, "--markdown")) {
|
|
||||||
return .fetch;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (std.mem.eql(u8, opt, "--noscript")) {
|
if (std.mem.eql(u8, opt, "--noscript")) {
|
||||||
return .fetch;
|
return .fetch;
|
||||||
}
|
}
|
||||||
@@ -560,21 +552,23 @@ fn parseFetchArgs(
|
|||||||
allocator: Allocator,
|
allocator: Allocator,
|
||||||
args: *std.process.ArgIterator,
|
args: *std.process.ArgIterator,
|
||||||
) !Fetch {
|
) !Fetch {
|
||||||
var fetch_html: bool = false;
|
var dump_mode: ?DumpFormat = null;
|
||||||
var fetch_markdown: bool = false;
|
|
||||||
var withbase: bool = false;
|
var withbase: bool = false;
|
||||||
var url: ?[:0]const u8 = null;
|
var url: ?[:0]const u8 = null;
|
||||||
var common: Common = .{};
|
var common: Common = .{};
|
||||||
var strip: dump.Opts.Strip = .{};
|
var strip: dump.Opts.Strip = .{};
|
||||||
|
|
||||||
while (args.next()) |opt| {
|
while (args.next()) |opt| {
|
||||||
if (std.mem.eql(u8, "--html", opt) or std.mem.eql(u8, "--dump", opt)) {
|
if (std.mem.eql(u8, "--dump", opt)) {
|
||||||
fetch_html = true;
|
const str = args.next() orelse {
|
||||||
continue;
|
log.fatal(.app, "missing argument value", .{ .arg = "--dump" });
|
||||||
}
|
return error.InvalidArgument;
|
||||||
|
};
|
||||||
|
|
||||||
if (std.mem.eql(u8, "--markdown", opt)) {
|
dump_mode = std.meta.stringToEnum(DumpFormat, str) orelse {
|
||||||
fetch_markdown = true;
|
log.fatal(.app, "invalid option choice", .{ .arg = "--dump", .value = str });
|
||||||
|
return error.InvalidArgument;
|
||||||
|
};
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -641,8 +635,7 @@ fn parseFetchArgs(
|
|||||||
|
|
||||||
return .{
|
return .{
|
||||||
.url = url.?,
|
.url = url.?,
|
||||||
.html = fetch_html,
|
.dump_mode = dump_mode,
|
||||||
.markdown = fetch_markdown,
|
|
||||||
.strip = strip,
|
.strip = strip,
|
||||||
.common = common,
|
.common = common,
|
||||||
.withbase = withbase,
|
.withbase = withbase,
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ const IS_DEBUG = @import("builtin").mode == .Debug;
|
|||||||
pub const FetchOpts = struct {
|
pub const FetchOpts = struct {
|
||||||
wait_ms: u32 = 5000,
|
wait_ms: u32 = 5000,
|
||||||
dump: dump.RootOpts,
|
dump: dump.RootOpts,
|
||||||
markdown: bool = false,
|
dump_mode: ?Config.DumpFormat = null,
|
||||||
writer: ?*std.Io.Writer = null,
|
writer: ?*std.Io.Writer = null,
|
||||||
};
|
};
|
||||||
pub fn fetch(app: *App, url: [:0]const u8, opts: FetchOpts) !void {
|
pub fn fetch(app: *App, url: [:0]const u8, opts: FetchOpts) !void {
|
||||||
@@ -96,10 +96,11 @@ pub fn fetch(app: *App, url: [:0]const u8, opts: FetchOpts) !void {
|
|||||||
_ = session.wait(opts.wait_ms);
|
_ = session.wait(opts.wait_ms);
|
||||||
|
|
||||||
const writer = opts.writer orelse return;
|
const writer = opts.writer orelse return;
|
||||||
if (opts.markdown) {
|
if (opts.dump_mode) |mode| {
|
||||||
try markdown.dump(page.window._document.asNode(), .{}, writer, page);
|
switch (mode) {
|
||||||
} else {
|
.html => try dump.root(page.window._document, opts.dump, writer, page),
|
||||||
try dump.root(page.window._document, opts.dump, writer, page);
|
.markdown => try markdown.dump(page.window._document.asNode(), .{}, writer, page),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
try writer.flush();
|
try writer.flush();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,11 +107,11 @@ fn run(allocator: Allocator, main_arena: Allocator) !void {
|
|||||||
},
|
},
|
||||||
.fetch => |opts| {
|
.fetch => |opts| {
|
||||||
const url = opts.url;
|
const url = opts.url;
|
||||||
log.debug(.app, "startup", .{ .mode = "fetch", .html = opts.html, .url = url, .snapshot = app.snapshot.fromEmbedded() });
|
log.debug(.app, "startup", .{ .mode = "fetch", .dump_mode = opts.dump_mode, .url = url, .snapshot = app.snapshot.fromEmbedded() });
|
||||||
|
|
||||||
var fetch_opts = lp.FetchOpts{
|
var fetch_opts = lp.FetchOpts{
|
||||||
.wait_ms = 5000,
|
.wait_ms = 5000,
|
||||||
.markdown = opts.markdown,
|
.dump_mode = opts.dump_mode,
|
||||||
.dump = .{
|
.dump = .{
|
||||||
.strip = opts.strip,
|
.strip = opts.strip,
|
||||||
.with_base = opts.withbase,
|
.with_base = opts.withbase,
|
||||||
@@ -120,7 +120,7 @@ fn run(allocator: Allocator, main_arena: Allocator) !void {
|
|||||||
|
|
||||||
var stdout = std.fs.File.stdout();
|
var stdout = std.fs.File.stdout();
|
||||||
var writer = stdout.writer(&.{});
|
var writer = stdout.writer(&.{});
|
||||||
if (opts.html or opts.markdown) {
|
if (opts.dump_mode != null) {
|
||||||
fetch_opts.writer = &writer.interface;
|
fetch_opts.writer = &writer.interface;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user