Node.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <LibCore/DeprecatedFile.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. if (path_from_query.is_absolute()
  30. && path_from_query.is_child_of(manual_base_path)
  31. && path_from_query.extension() == "md"sv) {
  32. auto section_directory = path_from_query.parent();
  33. auto man_string_location = section_directory.basename().find("man"sv);
  34. if (!man_string_location.has_value())
  35. return Error::from_string_literal("Page is inside invalid section");
  36. auto section_name = section_directory.basename().substring_view(man_string_location.value() + 3);
  37. auto section = TRY(SectionNode::try_create_from_number(section_name));
  38. return try_make_ref_counted<PageNode>(section, TRY(String::from_utf8(path_from_query.title())));
  39. }
  40. // [page] (in any section)
  41. Optional<NonnullRefPtr<PageNode>> maybe_page;
  42. for (auto const& section : sections) {
  43. auto const page = TRY(try_make_ref_counted<PageNode>(section, TRY(String::from_utf8(first_query_parameter))));
  44. if (Core::DeprecatedFile::exists(TRY(page->path()))) {
  45. maybe_page = page;
  46. break;
  47. }
  48. }
  49. if (maybe_page.has_value())
  50. return maybe_page.release_value();
  51. return Error::from_string_literal("Page not found");
  52. }
  53. // [section] [name]
  54. auto second_query_parameter = *query_parameter_iterator;
  55. auto section = TRY(SectionNode::try_create_from_number(first_query_parameter));
  56. auto const page = TRY(try_make_ref_counted<PageNode>(section, TRY(String::from_utf8(second_query_parameter))));
  57. if (Core::DeprecatedFile::exists(TRY(page->path())))
  58. return page;
  59. return Error::from_string_literal("Page doesn't exist in section");
  60. }
  61. ErrorOr<NonnullRefPtr<Node const>> Node::try_find_from_help_url(URL const& url)
  62. {
  63. if (url.host() != "man")
  64. return Error::from_string_view("Bad help operation"sv);
  65. if (url.paths().size() < 2)
  66. return Error::from_string_view("Bad help page URL"sv);
  67. auto paths = url.paths();
  68. auto const section = paths.take_first();
  69. auto maybe_section_number = section.to_uint();
  70. if (!maybe_section_number.has_value())
  71. return Error::from_string_view("Bad section number"sv);
  72. auto section_number = maybe_section_number.value();
  73. if (section_number > number_of_sections)
  74. return Error::from_string_view("Section number out of bounds"sv);
  75. NonnullRefPtr<Node const> current_node = sections[section_number - 1];
  76. while (!paths.is_empty()) {
  77. auto next_path_segment = TRY(String::from_deprecated_string(paths.take_first()));
  78. auto children = TRY(current_node->children());
  79. for (auto const& child : children) {
  80. if (TRY(child->name()) == next_path_segment) {
  81. current_node = child;
  82. break;
  83. }
  84. }
  85. }
  86. return current_node;
  87. }
  88. }