Переглянути джерело

Help+LibManual: Move URL handling to LibManual

kleines Filmröllchen 2 роки тому
батько
коміт
2aa374eba1

+ 8 - 28
Userland/Applications/Help/MainWidget.cpp

@@ -116,35 +116,15 @@ MainWidget::MainWidget()
                 return;
                 return;
             open_page(string_path.value());
             open_page(string_path.value());
         } else if (url.scheme() == "help") {
         } else if (url.scheme() == "help") {
-            if (url.host() == "man") {
-                if (url.paths().size() != 2) {
-                    dbgln("Bad help page URL '{}'", url);
-                    return;
-                }
-                auto const section = url.paths()[0];
-                auto maybe_section_number = section.to_uint();
-                if (!maybe_section_number.has_value()) {
-                    dbgln("Bad section number '{}'", section);
-                    return;
-                }
-                auto section_number = maybe_section_number.value();
-                auto page = String::from_utf8(url.paths()[1]);
-                if (page.is_error())
-                    return;
-
-                auto const page_object = try_make_ref_counted<Manual::PageNode>(Manual::sections[section_number - 1], page.release_value());
-                if (page_object.is_error())
-                    return;
-                auto const maybe_path = page_object.value()->path();
-                if (maybe_path.is_error())
-                    return;
-
-                auto path = maybe_path.value().to_deprecated_string();
-                m_history.push(path);
-                open_url(URL::create_with_file_scheme(path, url.fragment()));
-            } else {
-                dbgln("Bad help operation '{}' in URL '{}'", url.host(), url);
+            auto maybe_page = Manual::Node::try_find_from_help_url(url);
+            if (maybe_page.is_error()) {
+                dbgln("Error opening page: {}", maybe_page.error());
+                return;
             }
             }
+            auto maybe_path = maybe_page.value()->path();
+            if (!maybe_path.is_error())
+                return;
+            open_page(maybe_path.release_value());
         } else {
         } else {
             open_external(url);
             open_external(url);
         }
         }

+ 32 - 0
Userland/Libraries/LibManual/Node.cpp

@@ -11,6 +11,7 @@
 #include <AK/LexicalPath.h>
 #include <AK/LexicalPath.h>
 #include <AK/Optional.h>
 #include <AK/Optional.h>
 #include <AK/StringView.h>
 #include <AK/StringView.h>
+#include <AK/URL.h>
 #include <LibCore/File.h>
 #include <LibCore/File.h>
 #include <LibCore/Stream.h>
 #include <LibCore/Stream.h>
 #include <LibManual/Path.h>
 #include <LibManual/Path.h>
@@ -66,4 +67,35 @@ ErrorOr<NonnullRefPtr<PageNode>> Node::try_create_from_query(Vector<StringView,
     return Error::from_string_literal("Page doesn't exist in section");
     return Error::from_string_literal("Page doesn't exist in section");
 }
 }
 
 
+ErrorOr<NonnullRefPtr<Node>> Node::try_find_from_help_url(URL const& url)
+{
+    if (url.host() != "man")
+        return Error::from_string_view("Bad help operation"sv);
+    if (url.paths().size() < 2)
+        return Error::from_string_view("Bad help page URL"sv);
+
+    auto paths = url.paths();
+    auto const section = paths.take_first();
+    auto maybe_section_number = section.to_uint();
+    if (!maybe_section_number.has_value())
+        return Error::from_string_view("Bad section number"sv);
+    auto section_number = maybe_section_number.value();
+    if (section_number > number_of_sections)
+        return Error::from_string_view("Section number out of bounds"sv);
+
+    NonnullRefPtr<Node> current_node = sections[section_number - 1];
+
+    while (!paths.is_empty()) {
+        auto next_path_segment = TRY(String::from_deprecated_string(paths.take_first()));
+        auto children = TRY(current_node->children());
+        for (auto const& child : children) {
+            if (TRY(child->name()) == next_path_segment) {
+                current_node = child;
+                break;
+            }
+        }
+    }
+    return current_node;
+}
+
 }
 }

+ 4 - 0
Userland/Libraries/LibManual/Node.h

@@ -33,6 +33,10 @@ public:
     // [section] [page]
     // [section] [page]
     // Help can also (externally) handle search queries, which is not possible (yet) in man.
     // Help can also (externally) handle search queries, which is not possible (yet) in man.
     static ErrorOr<NonnullRefPtr<PageNode>> try_create_from_query(Vector<StringView, 2> const& query_parameters);
     static ErrorOr<NonnullRefPtr<PageNode>> try_create_from_query(Vector<StringView, 2> const& query_parameters);
+
+    // Finds a page via the help://man/<number>/<subsections...>/page URLs.
+    // This will automatically start discovering pages by inspecting the filesystem.
+    static ErrorOr<NonnullRefPtr<Node>> try_find_from_help_url(URL const&);
 };
 };
 
 
 }
 }