LibManual: Allow directly obtaining any page's main section number

This has previously been incorrectly handled in a variety of
applications, e.g. subsections were not accounted for.
This commit is contained in:
kleines Filmröllchen 2023-07-02 13:25:53 +02:00 committed by Linus Groh
parent b1eacf8801
commit a9053618a8
Notes: sideshowbarker 2024-07-17 07:25:39 +09:00
6 changed files with 13 additions and 4 deletions

View file

@ -26,6 +26,7 @@ public:
virtual bool is_open() const { return false; }
virtual ErrorOr<String> path() const = 0;
virtual PageNode const* document() const = 0;
virtual unsigned section_number() const = 0;
// Backend for the command-line argument format that Help and man accept. Handles:
// [/path/to/documentation.md] (no second argument)

View file

@ -27,6 +27,11 @@ ErrorOr<String> PageNode::path() const
return TRY(String::formatted("{}/{}.md", TRY(m_section->path()), m_page));
}
unsigned PageNode::section_number() const
{
return m_section->section_number();
}
ErrorOr<NonnullRefPtr<PageNode>> PageNode::help_index_page()
{
static NonnullRefPtr<PageNode> const help_index_page = TRY(try_make_ref_counted<PageNode>(sections[7 - 1], TRY("Help-index"_string)));

View file

@ -28,6 +28,7 @@ public:
virtual ErrorOr<String> name() const override { return m_page; };
virtual bool is_page() const override { return true; }
virtual PageNode const* document() const override { return this; };
virtual unsigned section_number() const override;
virtual ErrorOr<String> path() const override;

View file

@ -32,6 +32,7 @@ public:
virtual Node const* parent() const override { return nullptr; }
virtual ErrorOr<String> name() const override;
String const& section_name() const { return m_section; }
virtual unsigned section_number() const override { return m_section.to_number<unsigned>().value_or(0); }
virtual ErrorOr<String> path() const override;
virtual PageNode const* document() const override { return nullptr; }

View file

@ -21,6 +21,7 @@ public:
virtual ErrorOr<String> path() const override;
virtual ErrorOr<String> name() const override;
virtual PageNode const* document() const override;
virtual unsigned section_number() const override { return m_parent->section_number(); }
protected:
NonnullRefPtr<SectionNode const> m_parent;

View file

@ -80,12 +80,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto page = TRY(Manual::Node::try_create_from_query(query_parameters));
auto page_name = TRY(page->name());
auto const* section = static_cast<Manual::SectionNode const*>(page->parent());
auto section_number = TRY(String::number(page->section_number()));
if (pager.is_empty())
pager = TRY(String::formatted("less -P 'Manual Page {}({}) line %l?e (END):.'",
TRY(page_name.replace("'"sv, "'\\''"sv, ReplaceMode::FirstOnly)),
TRY(section->section_name().replace("'"sv, "'\\''"sv, ReplaceMode::FirstOnly))));
section_number));
pid_t pager_pid = TRY(pipe_to_pager(pager));
auto file = TRY(Core::File::open(TRY(page->path()), Core::File::OpenMode::Read));
@ -97,8 +97,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto const title = TRY("SerenityOS manual"_string);
int spaces = max(view_width / 2 - page_name.code_points().length() - section->section_name().code_points().length() - title.code_points().length() / 2 - 4, 0);
outln("{}({}){}{}", page_name, section->section_name(), String::repeated(' ', spaces), title);
int spaces = max(view_width / 2 - page_name.code_points().length() - section_number.code_points().length() - title.code_points().length() / 2 - 4, 0);
outln("{}({}){}{}", page_name, section_number, String::repeated(' ', spaces), title);
auto document = Markdown::Document::parse(buffer);
VERIFY(document);