Node.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/RefCounted.h>
  9. #include <AK/String.h>
  10. #include <AK/StringView.h>
  11. namespace Manual {
  12. class PageNode;
  13. class Node : public RefCounted<Node> {
  14. public:
  15. virtual ~Node() = default;
  16. virtual ErrorOr<Span<NonnullRefPtr<Node const>>> children() const = 0;
  17. virtual Node const* parent() const = 0;
  18. virtual ErrorOr<String> name() const = 0;
  19. virtual bool is_page() const { return false; }
  20. virtual bool is_open() const { return false; }
  21. virtual ErrorOr<String> path() const = 0;
  22. virtual PageNode const* document() const = 0;
  23. virtual unsigned section_number() const = 0;
  24. // Backend for the command-line argument format that Help and man accept. Handles:
  25. // [/path/to/documentation.md] (no second argument)
  26. // [page] (no second argument) - will find first section with that page
  27. // [section] [page]
  28. // Help can also (externally) handle search queries, which is not possible (yet) in man.
  29. static ErrorOr<NonnullRefPtr<PageNode const>> try_create_from_query(Vector<StringView, 2> const& query_parameters);
  30. // Finds a page via the help://man/<number>/<subsections...>/page URLs.
  31. // This will automatically start discovering pages by inspecting the filesystem.
  32. static ErrorOr<NonnullRefPtr<Node const>> try_find_from_help_url(URL const&);
  33. bool operator==(Node const& other) const
  34. {
  35. if (auto this_path = this->path(), other_path = other.path();
  36. !this_path.is_error() && !other_path.is_error()) {
  37. return this_path.release_value() == other_path.release_value();
  38. }
  39. return false;
  40. }
  41. };
  42. }
  43. namespace AK {
  44. template<typename T>
  45. requires(IsBaseOf<Manual::Node, T>) struct Traits<T> : public DefaultTraits<T> {
  46. static unsigned hash(T p) { return Traits::hash(p.path()); }
  47. };
  48. }