SectionNode.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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/Error.h>
  9. #include <AK/String.h>
  10. #include <LibManual/Node.h>
  11. namespace Manual {
  12. class SectionNode : public Node {
  13. public:
  14. virtual ~SectionNode() override = default;
  15. SectionNode(StringView section, StringView name)
  16. : m_section(MUST(String::from_utf8(section)))
  17. , m_name(MUST(String::from_utf8(name)))
  18. {
  19. }
  20. virtual ErrorOr<Span<NonnullRefPtr<Node const>>> children() const override
  21. {
  22. TRY(reify_if_needed());
  23. return m_children.span();
  24. }
  25. virtual Node const* parent() const override { return nullptr; }
  26. virtual ErrorOr<String> name() const override;
  27. String const& section_name() const { return m_section; }
  28. virtual unsigned section_number() const override { return m_section.to_number<unsigned>().value_or(0); }
  29. virtual ErrorOr<String> path() const override;
  30. virtual PageNode const* document() const override { return nullptr; }
  31. virtual bool is_open() const override { return m_open; }
  32. void set_open(bool open);
  33. static ErrorOr<NonnullRefPtr<SectionNode>> try_create_from_number(StringView section_number);
  34. protected:
  35. // In this class, the section is a number, but in lower sections it might be the same as the name.
  36. String m_section;
  37. String m_name;
  38. private:
  39. ErrorOr<void> reify_if_needed() const;
  40. mutable Vector<NonnullRefPtr<Node const>> m_children;
  41. mutable bool m_reified { false };
  42. bool m_open { false };
  43. };
  44. constexpr size_t number_of_sections = 8;
  45. extern Array<NonnullRefPtr<SectionNode>, number_of_sections> const sections;
  46. constexpr Array<StringView, number_of_sections> const section_numbers = {
  47. "1"sv,
  48. "2"sv,
  49. "3"sv,
  50. "4"sv,
  51. "5"sv,
  52. "6"sv,
  53. "7"sv,
  54. "8"sv,
  55. };
  56. }