SectionNode.cpp 1.8 KB

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