Forráskód Böngészése

LibManual: Create subsections for subdirectories automatically

kleines Filmröllchen 2 éve
szülő
commit
3e67f14e58
1 módosított fájl, 24 hozzáadás és 8 törlés
  1. 24 8
      Userland/Libraries/LibManual/SectionNode.cpp

+ 24 - 8
Userland/Libraries/LibManual/SectionNode.cpp

@@ -7,9 +7,11 @@
 #include "SectionNode.h"
 #include "PageNode.h"
 #include "Path.h"
+#include "SubsectionNode.h"
 #include <AK/LexicalPath.h>
 #include <AK/QuickSort.h>
 #include <LibCore/DirIterator.h>
+#include <LibCore/File.h>
 
 namespace Manual {
 
@@ -40,20 +42,34 @@ ErrorOr<void> SectionNode::reify_if_needed() const
         return {};
     m_reified = true;
 
-    Core::DirIterator dir_iter { TRY(path()).to_deprecated_string(), Core::DirIterator::Flags::SkipDots };
+    auto own_path = TRY(path());
+    Core::DirIterator dir_iter { own_path.to_deprecated_string(), Core::DirIterator::Flags::SkipDots };
+
+    struct Child {
+        NonnullRefPtr<Node> node;
+        String name_for_sorting;
+    };
+    Vector<Child> children;
 
-    Vector<String> page_names;
     while (dir_iter.has_next()) {
         LexicalPath lexical_path(dir_iter.next_path());
-        if (lexical_path.extension() != "md")
-            continue;
-        page_names.append(TRY(String::from_utf8(lexical_path.title())));
+        if (lexical_path.extension() != "md") {
+            if (Core::File::is_directory(LexicalPath::absolute_path(own_path.to_deprecated_string(), lexical_path.string()))) {
+                dbgln("Found subsection {}", lexical_path);
+                children.append({ .node = TRY(try_make_ref_counted<SubsectionNode>(*this, lexical_path.title())),
+                    .name_for_sorting = TRY(String::from_utf8(lexical_path.title())) });
+            }
+        } else {
+            children.append({ .node = TRY(try_make_ref_counted<PageNode>(*this, TRY(String::from_utf8(lexical_path.title())))),
+                .name_for_sorting = TRY(String::from_utf8(lexical_path.title())) });
+        }
     }
 
-    quick_sort(page_names);
+    quick_sort(children, [](auto const& a, auto const& b) { return a.name_for_sorting < b.name_for_sorting; });
 
-    for (auto& page_name : page_names)
-        m_children.append(TRY(try_make_ref_counted<PageNode>(*this, move(page_name))));
+    m_children.ensure_capacity(children.size());
+    for (auto child : children)
+        m_children.unchecked_append(move(child.node));
 
     return {};
 }