mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 18:02:05 -05:00
LibManual: Create subsections for subdirectories automatically
This commit is contained in:
parent
a9fe80550d
commit
3e67f14e58
1 changed files with 24 additions and 8 deletions
|
@ -7,9 +7,11 @@
|
||||||
#include "SectionNode.h"
|
#include "SectionNode.h"
|
||||||
#include "PageNode.h"
|
#include "PageNode.h"
|
||||||
#include "Path.h"
|
#include "Path.h"
|
||||||
|
#include "SubsectionNode.h"
|
||||||
#include <AK/LexicalPath.h>
|
#include <AK/LexicalPath.h>
|
||||||
#include <AK/QuickSort.h>
|
#include <AK/QuickSort.h>
|
||||||
#include <LibCore/DirIterator.h>
|
#include <LibCore/DirIterator.h>
|
||||||
|
#include <LibCore/File.h>
|
||||||
|
|
||||||
namespace Manual {
|
namespace Manual {
|
||||||
|
|
||||||
|
@ -40,20 +42,34 @@ ErrorOr<void> SectionNode::reify_if_needed() const
|
||||||
return {};
|
return {};
|
||||||
m_reified = true;
|
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()) {
|
while (dir_iter.has_next()) {
|
||||||
LexicalPath lexical_path(dir_iter.next_path());
|
LexicalPath lexical_path(dir_iter.next_path());
|
||||||
if (lexical_path.extension() != "md")
|
if (lexical_path.extension() != "md") {
|
||||||
continue;
|
if (Core::File::is_directory(LexicalPath::absolute_path(own_path.to_deprecated_string(), lexical_path.string()))) {
|
||||||
page_names.append(TRY(String::from_utf8(lexical_path.title())));
|
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.ensure_capacity(children.size());
|
||||||
m_children.append(TRY(try_make_ref_counted<PageNode>(*this, move(page_name))));
|
for (auto child : children)
|
||||||
|
m_children.unchecked_append(move(child.node));
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue