Node.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // +1 to trim the leading '/' from the start.
  50. auto page_name = section_directory.substring_view(section_name_end_index.value() + 1, page_name_end_index - 1);
  51. return try_make_ref_counted<PageNode>(section, TRY(String::from_utf8(page_name)));
  52. }
  53. // [page] (in any section)
  54. Optional<NonnullRefPtr<PageNode>> maybe_page;
  55. for (auto const& section : sections) {
  56. auto const page = TRY(try_make_ref_counted<PageNode>(section, TRY(String::from_utf8(first_query_parameter))));
  57. if (FileSystem::exists(TRY(page->path()))) {
  58. maybe_page = page;
  59. break;
  60. }
  61. }
  62. if (maybe_page.has_value())
  63. return maybe_page.release_value();
  64. return Error::from_string_literal("Page not found");
  65. }
  66. // [section] [name]
  67. auto second_query_parameter = *query_parameter_iterator;
  68. auto section = TRY(SectionNode::try_create_from_number(first_query_parameter));
  69. auto const page = TRY(try_make_ref_counted<PageNode>(section, TRY(String::from_utf8(second_query_parameter))));
  70. if (FileSystem::exists(TRY(page->path())))
  71. return page;
  72. return Error::from_string_literal("Page doesn't exist in section");
  73. }
  74. ErrorOr<NonnullRefPtr<Node const>> Node::try_find_from_help_url(URL const& url)
  75. {
  76. if (url.host() != "man"_short_string)
  77. return Error::from_string_view("Bad help operation"sv);
  78. if (url.path_segment_count() < 2)
  79. return Error::from_string_view("Bad help page URL"sv);
  80. auto const section = url.path_segment_at_index(0);
  81. auto maybe_section_number = section.to_uint();
  82. if (!maybe_section_number.has_value())
  83. return Error::from_string_view("Bad section number"sv);
  84. auto section_number = maybe_section_number.value();
  85. if (section_number > number_of_sections)
  86. return Error::from_string_view("Section number out of bounds"sv);
  87. NonnullRefPtr<Node const> current_node = sections[section_number - 1];
  88. bool child_node_found;
  89. for (size_t i = 1; i < url.path_segment_count(); i++) {
  90. child_node_found = false;
  91. auto children = TRY(current_node->children());
  92. for (auto const& child : children) {
  93. if (TRY(child->name()) == url.path_segment_at_index(i).view()) {
  94. child_node_found = true;
  95. current_node = child;
  96. break;
  97. }
  98. }
  99. if (!child_node_found)
  100. break;
  101. }
  102. if (!child_node_found)
  103. return Error::from_string_view("Page not found"sv);
  104. return current_node;
  105. }
  106. }