SectionNode.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 ErrorOr<String> path() const override;
  29. virtual PageNode const* document() const override { return nullptr; }
  30. virtual bool is_open() const override { return m_open; }
  31. void set_open(bool open);
  32. static ErrorOr<NonnullRefPtr<SectionNode>> try_create_from_number(StringView section_number);
  33. protected:
  34. // In this class, the section is a number, but in lower sections it might be the same as the name.
  35. String m_section;
  36. String m_name;
  37. private:
  38. ErrorOr<void> reify_if_needed() const;
  39. mutable NonnullRefPtrVector<Node const> m_children;
  40. mutable bool m_reified { false };
  41. bool m_open { false };
  42. };
  43. constexpr size_t number_of_sections = 8;
  44. extern Array<NonnullRefPtr<SectionNode>, number_of_sections> const sections;
  45. constexpr Array<StringView, number_of_sections> const section_numbers = {
  46. "1"sv,
  47. "2"sv,
  48. "3"sv,
  49. "4"sv,
  50. "5"sv,
  51. "6"sv,
  52. "7"sv,
  53. "8"sv,
  54. };
  55. }