Node.h 2.0 KB

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