Node.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/File.h>
  15. #include <LibCore/Stream.h>
  16. #include <LibManual/Path.h>
  17. namespace Manual {
  18. ErrorOr<NonnullRefPtr<PageNode>> Node::try_create_from_query(Vector<StringView, 2> const& query_parameters)
  19. {
  20. if (query_parameters.size() > 2)
  21. return Error::from_string_literal("Queries longer than 2 strings are not supported yet");
  22. auto query_parameter_iterator = query_parameters.begin();
  23. if (query_parameter_iterator.is_end())
  24. return PageNode::help_index_page();
  25. auto first_query_parameter = *query_parameter_iterator;
  26. ++query_parameter_iterator;
  27. if (query_parameter_iterator.is_end()) {
  28. // [/path/to/docs.md]
  29. auto path_from_query = LexicalPath { first_query_parameter };
  30. if (path_from_query.is_absolute()
  31. && path_from_query.is_child_of(manual_base_path)
  32. && path_from_query.extension() == "md"sv) {
  33. auto section_directory = path_from_query.parent();
  34. auto man_string_location = section_directory.basename().find("man"sv);
  35. if (!man_string_location.has_value())
  36. return Error::from_string_literal("Page is inside invalid section");
  37. auto section_name = section_directory.basename().substring_view(man_string_location.value() + 3);
  38. auto section = TRY(SectionNode::try_create_from_number(section_name));
  39. return try_make_ref_counted<PageNode>(section, TRY(String::from_utf8(path_from_query.title())));
  40. }
  41. // [page] (in any section)
  42. Optional<NonnullRefPtr<PageNode>> maybe_page;
  43. for (auto const& section : sections) {
  44. auto const page = TRY(try_make_ref_counted<PageNode>(section, TRY(String::from_utf8(first_query_parameter))));
  45. if (Core::File::exists(TRY(page->path()))) {
  46. maybe_page = page;
  47. break;
  48. }
  49. }
  50. if (maybe_page.has_value())
  51. return maybe_page.release_value();
  52. return Error::from_string_literal("Page not found");
  53. }
  54. // [section] [name]
  55. auto second_query_parameter = *query_parameter_iterator;
  56. auto section = TRY(SectionNode::try_create_from_number(first_query_parameter));
  57. auto const page = TRY(try_make_ref_counted<PageNode>(section, TRY(String::from_utf8(second_query_parameter))));
  58. if (Core::File::exists(TRY(page->path())))
  59. return page;
  60. return Error::from_string_literal("Page doesn't exist in section");
  61. }
  62. ErrorOr<NonnullRefPtr<Node>> Node::try_find_from_help_url(URL const& url)
  63. {
  64. if (url.host() != "man")
  65. return Error::from_string_view("Bad help operation"sv);
  66. if (url.paths().size() < 2)
  67. return Error::from_string_view("Bad help page URL"sv);
  68. auto paths = url.paths();
  69. auto const section = paths.take_first();
  70. auto maybe_section_number = section.to_uint();
  71. if (!maybe_section_number.has_value())
  72. return Error::from_string_view("Bad section number"sv);
  73. auto section_number = maybe_section_number.value();
  74. if (section_number > number_of_sections)
  75. return Error::from_string_view("Section number out of bounds"sv);
  76. NonnullRefPtr<Node> current_node = sections[section_number - 1];
  77. while (!paths.is_empty()) {
  78. auto next_path_segment = TRY(String::from_deprecated_string(paths.take_first()));
  79. auto children = TRY(current_node->children());
  80. for (auto const& child : children) {
  81. if (TRY(child->name()) == next_path_segment) {
  82. current_node = child;
  83. break;
  84. }
  85. }
  86. }
  87. return current_node;
  88. }
  89. }