SectionNode.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "SectionNode.h"
  7. #include "PageNode.h"
  8. #include "Path.h"
  9. #include <AK/LexicalPath.h>
  10. #include <AK/QuickSort.h>
  11. #include <LibCore/DirIterator.h>
  12. namespace Manual {
  13. ErrorOr<String> SectionNode::path() const
  14. {
  15. return String::formatted("{}/{}{}", manual_base_path, top_level_section_prefix, m_section);
  16. }
  17. ErrorOr<String> SectionNode::name() const
  18. {
  19. return String::formatted("{}. {}", m_section, m_name);
  20. }
  21. ErrorOr<void> SectionNode::reify_if_needed() const
  22. {
  23. if (m_reified)
  24. return {};
  25. m_reified = true;
  26. Core::DirIterator dir_iter { TRY(path()).to_deprecated_string(), Core::DirIterator::Flags::SkipDots };
  27. Vector<String> page_names;
  28. while (dir_iter.has_next()) {
  29. LexicalPath lexical_path(dir_iter.next_path());
  30. if (lexical_path.extension() != "md")
  31. continue;
  32. page_names.append(TRY(String::from_utf8(lexical_path.title())));
  33. }
  34. quick_sort(page_names);
  35. for (auto& page_name : page_names)
  36. m_children.append(TRY(try_make_ref_counted<PageNode>(*this, move(page_name))));
  37. return {};
  38. }
  39. void SectionNode::set_open(bool open)
  40. {
  41. if (m_open == open)
  42. return;
  43. m_open = open;
  44. }
  45. Array<NonnullRefPtr<SectionNode>, number_of_sections> const sections = { {
  46. make_ref_counted<SectionNode>("1"sv, "User Programs"sv),
  47. make_ref_counted<SectionNode>("2"sv, "System Calls"sv),
  48. make_ref_counted<SectionNode>("3"sv, "Library Functions"sv),
  49. make_ref_counted<SectionNode>("4"sv, "Special Files"sv),
  50. make_ref_counted<SectionNode>("5"sv, "File Formats"sv),
  51. make_ref_counted<SectionNode>("6"sv, "Games"sv),
  52. make_ref_counted<SectionNode>("7"sv, "Miscellanea"sv),
  53. make_ref_counted<SectionNode>("8"sv, "Sysadmin Tools"sv),
  54. } };
  55. }