SectionNode.cpp 1.7 KB

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