Node.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Node.h"
  7. #include "PageNode.h"
  8. #include "SectionNode.h"
  9. #include <AK/Assertions.h>
  10. #include <AK/LexicalPath.h>
  11. #include <AK/Optional.h>
  12. #include <AK/StringView.h>
  13. #include <AK/URL.h>
  14. #include <LibFileSystem/FileSystem.h>
  15. #include <LibManual/Path.h>
  16. namespace Manual {
  17. ErrorOr<NonnullRefPtr<PageNode const>> Node::try_create_from_query(Vector<StringView, 2> const& query_parameters)
  18. {
  19. if (query_parameters.size() > 2)
  20. return Error::from_string_literal("Queries longer than 2 strings are not supported yet");
  21. auto query_parameter_iterator = query_parameters.begin();
  22. if (query_parameter_iterator.is_end())
  23. return PageNode::help_index_page();
  24. auto first_query_parameter = *query_parameter_iterator;
  25. ++query_parameter_iterator;
  26. if (query_parameter_iterator.is_end()) {
  27. // [/path/to/docs.md]
  28. auto path_from_query = LexicalPath { first_query_parameter };
  29. constexpr auto MARKDOWN_FILE_EXTENSION = "md"sv;
  30. if (path_from_query.is_absolute()
  31. && path_from_query.is_child_of(manual_base_path)
  32. && path_from_query.extension() == MARKDOWN_FILE_EXTENSION) {
  33. // Parse the section number and page name from a directory string of the form:
  34. // /usr/share/man/man[section_number]/[page_name].md
  35. // The page_name includes any subsections.
  36. auto const& section_directory = path_from_query.string();
  37. auto section_name_start_index = manual_base_path.string().length() + 4;
  38. auto section_name_end_index = section_directory.find('/', section_name_start_index);
  39. if (!section_name_end_index.has_value())
  40. return Error::from_string_literal("Page is inside invalid section");
  41. auto section_name = section_directory.substring_view(section_name_start_index, section_name_end_index.value() - section_name_start_index);
  42. auto section = TRY(SectionNode::try_create_from_number(section_name));
  43. auto page_name_end_index = section_directory.length() - section_name_end_index.value() - MARKDOWN_FILE_EXTENSION.length() - 1;
  44. auto page_name = section_directory.substring_view(section_name_end_index.value(), page_name_end_index);
  45. return try_make_ref_counted<PageNode>(section, TRY(String::from_utf8(page_name)));
  46. }
  47. // [page] (in any section)
  48. Optional<NonnullRefPtr<PageNode>> maybe_page;
  49. for (auto const& section : sections) {
  50. auto const page = TRY(try_make_ref_counted<PageNode>(section, TRY(String::from_utf8(first_query_parameter))));
  51. if (FileSystem::exists(TRY(page->path()))) {
  52. maybe_page = page;
  53. break;
  54. }
  55. }
  56. if (maybe_page.has_value())
  57. return maybe_page.release_value();
  58. return Error::from_string_literal("Page not found");
  59. }
  60. // [section] [name]
  61. auto second_query_parameter = *query_parameter_iterator;
  62. auto section = TRY(SectionNode::try_create_from_number(first_query_parameter));
  63. auto const page = TRY(try_make_ref_counted<PageNode>(section, TRY(String::from_utf8(second_query_parameter))));
  64. if (FileSystem::exists(TRY(page->path())))
  65. return page;
  66. return Error::from_string_literal("Page doesn't exist in section");
  67. }
  68. ErrorOr<NonnullRefPtr<Node const>> Node::try_find_from_help_url(URL const& url)
  69. {
  70. if (url.host() != "man")
  71. return Error::from_string_view("Bad help operation"sv);
  72. if (url.path_segment_count() < 2)
  73. return Error::from_string_view("Bad help page URL"sv);
  74. auto const section = url.path_segment_at_index(0);
  75. auto maybe_section_number = section.to_uint();
  76. if (!maybe_section_number.has_value())
  77. return Error::from_string_view("Bad section number"sv);
  78. auto section_number = maybe_section_number.value();
  79. if (section_number > number_of_sections)
  80. return Error::from_string_view("Section number out of bounds"sv);
  81. NonnullRefPtr<Node const> current_node = sections[section_number - 1];
  82. for (size_t i = 1; i < url.path_segment_count(); i++) {
  83. auto children = TRY(current_node->children());
  84. for (auto const& child : children) {
  85. if (TRY(child->name()) == url.path_segment_at_index(i).view()) {
  86. current_node = child;
  87. break;
  88. }
  89. }
  90. }
  91. return current_node;
  92. }
  93. }