ladybird/Userland/Libraries/LibManual/Node.h
Shannon Booth e800605ad3 AK+LibURL: Move AK::URL into a new URL library
This URL library ends up being a relatively fundamental base library of
the system, as LibCore depends on LibURL.

This change has two main benefits:
 * Moving AK back more towards being an agnostic library that can
   be used between the kernel and userspace. URL has never really fit
   that description - and is not used in the kernel.
 * URL _should_ depend on LibUnicode, as it needs punnycode support.
   However, it's not really possible to do this inside of AK as it can't
   depend on any external library. This change brings us a little closer
   to being able to do that, but unfortunately we aren't there quite
   yet, as the code generators depend on LibCore.
2024-03-18 14:06:28 -04:00

63 lines
2.0 KiB
C++

/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/RefCounted.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <LibURL/Forward.h>
namespace Manual {
class PageNode;
class Node : public RefCounted<Node> {
public:
virtual ~Node() = default;
virtual ErrorOr<Span<NonnullRefPtr<Node const>>> children() const = 0;
virtual Node const* parent() const = 0;
virtual ErrorOr<String> name() const = 0;
virtual bool is_page() const { return false; }
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)
// [page] (no second argument) - will find first section with that page
// [section] [page]
// Help can also (externally) handle search queries, which is not possible (yet) in man.
static ErrorOr<NonnullRefPtr<PageNode const>> try_create_from_query(Vector<StringView, 2> const& query_parameters);
// Finds a page via the help://man/<number>/<subsections...>/page URLs.
// This will automatically start discovering pages by inspecting the filesystem.
static ErrorOr<NonnullRefPtr<Node const>> try_find_from_help_url(URL::URL const&);
bool operator==(Node const& other) const
{
if (auto this_path = this->path(), other_path = other.path();
!this_path.is_error() && !other_path.is_error()) {
return this_path.release_value() == other_path.release_value();
}
return false;
}
};
}
namespace AK {
template<typename T>
requires(IsBaseOf<Manual::Node, T>) struct Traits<T> : public DefaultTraits<T> {
static unsigned hash(T p) { return Traits::hash(p.path()); }
};
}