/* * Copyright (c) 2019-2020, Sergey Bugaev * * SPDX-License-Identifier: BSD-2-Clause */ #include "SectionNode.h" #include "PageNode.h" #include "Path.h" #include #include #include namespace Manual { ErrorOr> SectionNode::try_create_from_number(StringView section) { auto maybe_section_number = section.to_uint(); if (!maybe_section_number.has_value()) return Error::from_string_literal("Section is not a number"); auto section_number = maybe_section_number.release_value(); if (section_number > number_of_sections) return Error::from_string_literal("Section number too large"); return sections[section_number - 1]; } ErrorOr SectionNode::path() const { return String::formatted("{}/{}{}", manual_base_path, top_level_section_prefix, m_section); } ErrorOr SectionNode::name() const { return String::formatted("{}. {}", m_section, m_name); } ErrorOr SectionNode::reify_if_needed() const { if (m_reified) return {}; m_reified = true; Core::DirIterator dir_iter { TRY(path()).to_deprecated_string(), Core::DirIterator::Flags::SkipDots }; Vector 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()))); } quick_sort(page_names); for (auto& page_name : page_names) m_children.append(TRY(try_make_ref_counted(*this, move(page_name)))); return {}; } void SectionNode::set_open(bool open) { if (m_open == open) return; m_open = open; } Array, number_of_sections> const sections = { { make_ref_counted("1"sv, "User Programs"sv), make_ref_counted("2"sv, "System Calls"sv), make_ref_counted("3"sv, "Library Functions"sv), make_ref_counted("4"sv, "Special Files"sv), make_ref_counted("5"sv, "File Formats"sv), make_ref_counted("6"sv, "Games"sv), make_ref_counted("7"sv, "Miscellanea"sv), make_ref_counted("8"sv, "Sysadmin Tools"sv), } }; }