summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkleines Filmröllchen <filmroellchen@serenityos.org>2022-12-14 17:19:40 +0100
committerAndrew Kaster <andrewdkaster@gmail.com>2023-01-02 06:15:13 -0700
commit3e67f14e584f9c700d8ee04b757286edf80b0c56 (patch)
treea7785504965342ac7801f0ab8b3afbeba9e28a22
parenta9fe80550d095db69c16c76e36b34d80cd0c0e99 (diff)
downloadserenity-3e67f14e584f9c700d8ee04b757286edf80b0c56.zip
LibManual: Create subsections for subdirectories automatically
-rw-r--r--Userland/Libraries/LibManual/SectionNode.cpp32
1 files changed, 24 insertions, 8 deletions
diff --git a/Userland/Libraries/LibManual/SectionNode.cpp b/Userland/Libraries/LibManual/SectionNode.cpp
index 1d18e77613..1dc0e447ff 100644
--- a/Userland/Libraries/LibManual/SectionNode.cpp
+++ b/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 {};
}