Node.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. if (query_parameters.size() == 1 && query_parameters[0].starts_with("help://"sv)) {
  22. auto help_url = URL::create_with_url_or_path(query_parameters[0].trim("/"sv, TrimMode::Right));
  23. auto node_from_url = TRY(Manual::Node::try_find_from_help_url(help_url));
  24. return *node_from_url->document();
  25. }
  26. auto query_parameter_iterator = query_parameters.begin();
  27. if (query_parameter_iterator.is_end())
  28. return PageNode::help_index_page();
  29. auto first_query_parameter = *query_parameter_iterator;
  30. ++query_parameter_iterator;
  31. if (query_parameter_iterator.is_end()) {
  32. // [/path/to/docs.md]
  33. auto path_from_query = LexicalPath { first_query_parameter };
  34. constexpr auto MARKDOWN_FILE_EXTENSION = "md"sv;
  35. if (path_from_query.is_absolute()
  36. && path_from_query.is_child_of(manual_base_path)
  37. && path_from_query.extension() == MARKDOWN_FILE_EXTENSION) {
  38. // Parse the section number and page name from a directory string of the form:
  39. // /usr/share/man/man[section_number]/[page_name].md
  40. // The page_name includes any subsections.
  41. auto const& section_directory = path_from_query.string();
  42. auto section_name_start_index = manual_base_path.string().length() + 4;
  43. auto section_name_end_index = section_directory.find('/', section_name_start_index);
  44. if (!section_name_end_index.has_value())
  45. return Error::from_string_literal("Page is inside invalid section");
  46. auto section_name = section_directory.substring_view(section_name_start_index, section_name_end_index.value() - section_name_start_index);
  47. auto section = TRY(SectionNode::try_create_from_number(section_name));
  48. auto page_name_end_index = section_directory.length() - section_name_end_index.value() - MARKDOWN_FILE_EXTENSION.length() - 1;
  49. auto page_name = section_directory.substring_view(section_name_end_index.value(), page_name_end_index);
  50. return try_make_ref_counted<PageNode>(section, TRY(String::from_utf8(page_name)));
  51. }
  52. // [page] (in any section)
  53. Optional<NonnullRefPtr<PageNode>> maybe_page;
  54. for (auto const& section : sections) {
  55. auto const page = TRY(try_make_ref_counted<PageNode>(section, TRY(String::from_utf8(first_query_parameter))));
  56. if (FileSystem::exists(TRY(page->path()))) {
  57. maybe_page = page;
  58. break;
  59. }
  60. }
  61. if (maybe_page.has_value())
  62. return maybe_page.release_value();
  63. return Error::from_string_literal("Page not found");
  64. }
  65. // [section] [name]
  66. auto second_query_parameter = *query_parameter_iterator;
  67. auto section = TRY(SectionNode::try_create_from_number(first_query_parameter));
  68. auto const page = TRY(try_make_ref_counted<PageNode>(section, TRY(String::from_utf8(second_query_parameter))));
  69. if (FileSystem::exists(TRY(page->path())))
  70. return page;
  71. return Error::from_string_literal("Page doesn't exist in section");
  72. }
  73. ErrorOr<NonnullRefPtr<Node const>> Node::try_find_from_help_url(URL const& url)
  74. {
  75. if (url.host() != "man")
  76. return Error::from_string_view("Bad help operation"sv);
  77. if (url.path_segment_count() < 2)
  78. return Error::from_string_view("Bad help page URL"sv);
  79. auto const section = url.path_segment_at_index(0);
  80. auto maybe_section_number = section.to_uint();
  81. if (!maybe_section_number.has_value())
  82. return Error::from_string_view("Bad section number"sv);
  83. auto section_number = maybe_section_number.value();
  84. if (section_number > number_of_sections)
  85. return Error::from_string_view("Section number out of bounds"sv);
  86. NonnullRefPtr<Node const> current_node = sections[section_number - 1];
  87. bool child_node_found;
  88. for (size_t i = 1; i < url.path_segment_count(); i++) {
  89. child_node_found = false;
  90. auto children = TRY(current_node->children());
  91. for (auto const& child : children) {
  92. if (TRY(child->name()) == url.path_segment_at_index(i).view()) {
  93. child_node_found = true;
  94. current_node = child;
  95. break;
  96. }
  97. }
  98. if (!child_node_found)
  99. break;
  100. }
  101. if (!child_node_found)
  102. return Error::from_string_view("Page not found"sv);
  103. return current_node;
  104. }
  105. }