-
Notifications
You must be signed in to change notification settings - Fork 409
[WIP] Allow nested categories with recursive navigation. #982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
55d469f
0c899ac
14b567b
c9c597e
d8b2f27
baf5989
ab9b537
6cbe723
7c400eb
e263604
4c6c76c
bee9a9d
75e4721
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,18 +34,24 @@ def self.doc_structure_for_docs(docs) | |
children = doc.children | ||
.sort_by { |c| [c.nav_order, c.name, c.usr || ''] } | ||
.flat_map do |child| | ||
# FIXME: include arbitrarily nested extensible types | ||
[{ name: child.name, url: child.url }] + | ||
Array(child.children.select do |sub_child| | ||
sub_child.type.swift_extensible? || sub_child.type.extension? | ||
end).map do |sub_child| | ||
{ name: "– #{sub_child.name}", url: sub_child.url } | ||
end | ||
if child.type == SourceDeclaration::Type.category | ||
doc_structure_for_docs([child]) | ||
else | ||
# FIXME: include arbitrarily nested extensible types | ||
[{ name: child.name, url: child.url, children: nil}] + | ||
Array(child.children.select do |sub_child| | ||
sub_child.type.swift_extensible? || sub_child.type.extension? | ||
end).map do |sub_child| | ||
{ name: "– #{sub_child.name}", url: sub_child.url, children: nil } | ||
end | ||
end | ||
end | ||
|
||
{ | ||
section: doc.name, | ||
url: doc.url, | ||
children: children, | ||
level: doc.level, | ||
} | ||
end | ||
end | ||
|
@@ -105,6 +111,13 @@ def self.each_doc(output_dir, docs, &block) | |
doc.children, | ||
&block | ||
) | ||
if doc.subsections != nil | ||
each_doc( | ||
output_dir, | ||
doc.subsections, | ||
&block | ||
) | ||
end | ||
end | ||
end | ||
|
||
|
@@ -383,6 +396,26 @@ def self.render_tasks(source_module, children) | |
end | ||
end | ||
|
||
def self.render_subsections(subsections, source_module) | ||
subsections.map do |subsection| | ||
overview = (subsection.abstract || '') + (subsection.discussion || '') | ||
alternative_abstract = subsection.alternative_abstract | ||
if alternative_abstract | ||
overview = render(subsection, alternative_abstract) + overview | ||
end | ||
|
||
tasks = render_tasks(source_module, subsection.children.select { |c| c.type != SourceDeclaration::Type.category }) | ||
|
||
{ | ||
name: subsection.name, | ||
overview: overview, | ||
uid: URI.encode(subsection.name), | ||
url: subsection.url, | ||
level: subsection.level, | ||
tasks: tasks, | ||
} | ||
end | ||
end | ||
|
||
# rubocop:disable Metrics/MethodLength | ||
# Build Mustache document from single parsed doc | ||
# @param [Config] options Build options | ||
|
@@ -412,7 +445,9 @@ def self.document(source_module, doc_model, path_to_root) | |
doc[:declaration] = doc_model.display_declaration | ||
doc[:overview] = overview | ||
doc[:structure] = source_module.doc_structure | ||
doc[:tasks] = render_tasks(source_module, doc_model.children) | ||
categories, children = doc_model.children.partition { |c| c.type == SourceDeclaration::Type.category } | ||
doc[:tasks] = render_tasks(source_module, children) | ||
doc[:subsections] = render_subsections(categories, source_module) | ||
doc[:module_name] = source_module.name | ||
doc[:author_name] = source_module.author_name | ||
doc[:github_url] = source_module.github_url | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
require 'jazzy/source_declaration' | ||
require 'jazzy/config' | ||
require 'jazzy/source_mark' | ||
require 'jazzy/jazzy_markdown' | ||
|
||
module Jazzy | ||
# Category (group, contents) pages generated by jazzy | ||
class SourceCategory < SourceDeclaration | ||
extend Config::Mixin | ||
|
||
def initialize(group, name, abstract, url_name, level = 1) | ||
super() | ||
self.type = SourceDeclaration::Type.category | ||
|
||
self.name = name | ||
self.url_name = url_name | ||
self.abstract = Markdown.render(abstract) | ||
self.children = group | ||
self.parameters = [] | ||
self.mark = SourceMark.new | ||
|
||
self.level = level | ||
|
||
end | ||
|
||
# Group root-level docs into custom categories or by type | ||
def self.group_docs(docs) | ||
custom_categories, docs = | ||
group_custom_categories(docs, config.custom_categories) | ||
type_categories, uncategorized = group_type_categories( | ||
docs, custom_categories.any? ? 'Other ' : '' | ||
) | ||
custom_categories + type_categories + uncategorized | ||
end | ||
|
||
def self.group_custom_categories(docs, categories, level = 1) | ||
group = categories.map do |category| | ||
children = category['children'].flat_map do |child| | ||
if child.is_a?(Hash) | ||
# Nested category, recurse | ||
docs_with_name, docs = group_custom_categories(docs, [child], level + 1) | ||
else | ||
# Doc name, find it | ||
docs_with_name, docs = docs.partition { |doc| doc.name == child } | ||
if docs_with_name.empty? | ||
STDERR.puts( | ||
'WARNING: No documented top-level declarations match ' \ | ||
"name \"#{child}\" specified in categories file", | ||
) | ||
end | ||
end | ||
docs_with_name | ||
end | ||
# Category config overrides alphabetization | ||
children.each.with_index { |child, i| child.nav_order = i } | ||
make_group(children, category['name'], '', nil, level) | ||
end | ||
[group.compact, docs] | ||
end | ||
|
||
def self.group_type_categories(docs, type_category_prefix) | ||
group = SourceDeclaration::Type.all.map do |type| | ||
children, docs = docs.partition { |doc| doc.type == type } | ||
make_group( | ||
children, | ||
type_category_prefix + type.plural_name, | ||
"The following #{type.plural_name.downcase} are available globally.", | ||
type_category_prefix + type.plural_url_name, | ||
) | ||
end | ||
[group.compact, docs] | ||
end | ||
|
||
def self.make_group(group, name, abstract, url_name = nil, level = 1) | ||
group.reject! { |doc| doc.name.empty? } | ||
unless group.empty? | ||
SourceCategory.new(group, name, abstract, url_name, level) | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,8 @@ def display_other_language_declaration | |
attr_accessor :end_line | ||
attr_accessor :nav_order | ||
attr_accessor :url_name | ||
attr_accessor :level | ||
attr_accessor :subsections | ||
|
||
|
||
def alternative_abstract | ||
if file = alternative_abstract_file | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
require 'jazzy/source_declaration' | ||
require 'jazzy/source_mark' | ||
require 'jazzy/stats' | ||
require 'jazzy/source_category' | ||
|
||
ELIDED_AUTOLINK_TOKEN = '36f8f5912051ae747ef441d6511ca4cb'.freeze | ||
|
||
|
@@ -59,58 +60,6 @@ def self.undocumented_abstract | |
).freeze | ||
end | ||
|
||
# Group root-level docs by custom categories (if any) and type | ||
def self.group_docs(docs) | ||
custom_categories, docs = group_custom_categories(docs) | ||
type_categories, uncategorized = group_type_categories( | ||
docs, custom_categories.any? ? 'Other ' : '' | ||
) | ||
custom_categories + type_categories + uncategorized | ||
end | ||
|
||
def self.group_custom_categories(docs) | ||
group = Config.instance.custom_categories.map do |category| | ||
children = category['children'].flat_map do |name| | ||
docs_with_name, docs = docs.partition { |doc| doc.name == name } | ||
if docs_with_name.empty? | ||
STDERR.puts 'WARNING: No documented top-level declarations match ' \ | ||
"name \"#{name}\" specified in categories file" | ||
end | ||
docs_with_name | ||
end | ||
# Category config overrides alphabetization | ||
children.each.with_index { |child, i| child.nav_order = i } | ||
make_group(children, category['name'], '') | ||
end | ||
[group.compact, docs] | ||
end | ||
|
||
def self.group_type_categories(docs, type_category_prefix) | ||
group = SourceDeclaration::Type.all.map do |type| | ||
children, docs = docs.partition { |doc| doc.type == type } | ||
make_group( | ||
children, | ||
type_category_prefix + type.plural_name, | ||
"The following #{type.plural_name.downcase} are available globally.", | ||
type_category_prefix + type.plural_url_name, | ||
) | ||
end | ||
[group.compact, docs] | ||
end | ||
|
||
def self.make_group(group, name, abstract, url_name = nil) | ||
group.reject! { |doc| doc.name.empty? } | ||
unless group.empty? | ||
SourceDeclaration.new.tap do |sd| | ||
sd.type = SourceDeclaration::Type.overview | ||
sd.name = name | ||
sd.url_name = url_name | ||
sd.abstract = Markdown.render(abstract) | ||
sd.children = group | ||
end | ||
end | ||
end | ||
|
||
def self.sanitize_filename(doc) | ||
unsafe_filename = doc.url_name || doc.name | ||
sanitzation_enabled = Config.instance.use_safe_filenames | ||
|
@@ -132,7 +81,9 @@ def self.make_doc_urls(docs) | |
subdir_for_doc(doc) + | ||
[sanitize_filename(doc) + '.html'] | ||
).join('/') | ||
doc.children = make_doc_urls(doc.children) | ||
if doc.children.count > 0 | ||
doc.children = make_doc_urls(doc.children) | ||
end | ||
|
||
else | ||
# Don't create HTML page for this doc if it doesn't have children | ||
# Instead, make its link a hash-link on its parent's page | ||
|
@@ -806,7 +757,7 @@ def self.parse(sourcekitten_output, min_acl, skip_undocumented, inject_docs) | |
docs = docs.reject { |doc| doc.type.swift_enum_element? } | ||
end | ||
ungrouped_docs = docs | ||
docs = group_docs(docs) | ||
docs = SourceCategory.group_docs(docs) | ||
make_doc_urls(docs) | ||
autolink(docs, ungrouped_docs) | ||
[docs, @stats] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,7 @@ | |
</section> | ||
|
||
{{> tasks}} | ||
|
||
|
||
</article> | ||
</div> | ||
{{> footer}} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,7 @@ | ||
<nav class="navigation"> | ||
<ul class="nav-groups"> | ||
{{#structure}} | ||
<li class="nav-group-name"> | ||
<a class="nav-group-name-link" href="{{path_to_root}}{{url}}">{{section}}</a> | ||
<ul class="nav-group-tasks"> | ||
{{#children}} | ||
<li class="nav-group-task"> | ||
<a class="nav-group-task-link" href="{{path_to_root}}{{url}}">{{name}}</a> | ||
</li> | ||
{{/children}} | ||
</ul> | ||
</li> | ||
{{> nav_section}} | ||
{{/structure}} | ||
</ul> | ||
</nav> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change not required,
subsections
unused.