Skip to content

Commit b6365b4

Browse files
authored
Allow custom_categories to define a regex or a name for each category child (#1418)
A custom category can now specify a regex for each of its children by using an object with a regex key instead of just a string to find all of the declaration matching that regex. For example: ``` custom_categories: - name: some_category_name children: - some_exact_name - regex: some_regex ``` Will create a category containing: - the declaration "some_exact_name" (if it's found) - all the declaration that match the "some_regex" regex. (e.g. "some_regex", "some_regex1", ...) Add custom_categories documentation in readme
1 parent 0956e7e commit b6365b4

4 files changed

Lines changed: 52 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
##### Enhancements
88

9-
* None.
9+
* Allow `custom_categories` to specify a regex for a child.
10+
[Enrico Zannini](https://github.com/Enricoza)
11+
[#688](https://github.com/realm/jazzy/issues/688)
1012

1113
##### Bug Fixes
1214

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,19 @@ like _Classes_ and _Structures_ corresponding to programming-language concepts,
453453
as well as _Guides_ if `--documentation` is set.
454454
455455
You can customize the categories and their contents using `custom_categories` in
456-
the config file — see the ReSwift [docs](https://reswift.github.io/ReSwift/) and
456+
the config file. `custom_categories` is an array of objects. Each category must contain two properties:
457+
- `name`: A string with the name you want to give to this category
458+
- `children`: An array used to specify the root level declarations that you want to add to
459+
this category.
460+
461+
Each child, then, can be one of the following:
462+
- A string, containing either the exact name of one root level declaration you want to
463+
match, or the fully qualified name (`ModuleName.DeclarationName`)
464+
- An object, containing the property:
465+
- `regex`: a string which will be used to match multiple root level declarations from
466+
all of the modules.
467+
468+
See the ReSwift [docs](https://reswift.github.io/ReSwift/) and
457469
[config file](https://github.com/ReSwift/ReSwift/blob/e94737282850fa038b625b4e351d1608a3d02cee/.jazzy.json)
458470
for an example.
459471

lib/jazzy/doc_index.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ def lookup(parts)
6464
children[parts.first]&.lookup(parts[1...])
6565
end
6666

67+
# Look up of a regex matching all children for current level only.
68+
def lookup_regex(regex)
69+
children.select { |name, _| name.match(regex) }
70+
.map { |_, scope| scope.decl }.compact
71+
end
72+
6773
# Get an array of scopes matching the name parts.
6874
def lookup_path(parts)
6975
[self] +
@@ -90,6 +96,11 @@ def lookup(name, context = nil)
9096
lookup_context(lookup_name, context)
9197
end
9298

99+
# Look up a regex and return all matching top level SourceDeclaration.
100+
def lookup_regex(regex)
101+
root_scope.children.map { |_, scope| scope.lookup_regex(regex) }.flatten
102+
end
103+
93104
private
94105

95106
# Look up a fully-qualified name, ie. it starts with the module name.

lib/jazzy/grouper.rb

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,39 @@ def self.group_docs_by_module(docs, type_category_prefix)
4949

5050
def self.group_custom_categories(docs, doc_index)
5151
group = config.custom_categories.map do |category|
52-
children = category['children'].map do |name|
53-
unless doc = doc_index.lookup(name)
54-
warn 'WARNING: No documented top-level declarations match ' \
55-
"name \"#{name}\" specified in categories file"
56-
next nil
52+
children = category['children'].map do |selector|
53+
selected = select_docs(doc_index, selector)
54+
selected.map do |doc|
55+
unless doc.parent_in_code.nil?
56+
warn "WARNING: Declaration \"#{doc.fully_qualified_module_name}\" " \
57+
'specified in categories file exists but is not top-level and ' \
58+
'cannot be included here'
59+
next nil
60+
end
61+
docs.delete(doc)
5762
end
58-
59-
unless doc.parent_in_code.nil?
60-
warn "WARNING: Declaration \"#{doc.fully_qualified_module_name}\" " \
61-
'specified in categories file exists but is not top-level and ' \
62-
'cannot be included here'
63-
next nil
64-
end
65-
66-
docs.delete(doc)
67-
end.compact
63+
end.flatten.compact
6864
# Category config overrides alphabetization
6965
children.each.with_index { |child, i| child.nav_order = i }
7066
make_group(children, category['name'], '')
7167
end
7268
[group.compact, docs]
7369
end
7470

71+
def self.select_docs(doc_index, selector)
72+
if selector.is_a?(String)
73+
unless single_doc = doc_index.lookup(selector)
74+
warn 'WARNING: No documented top-level declarations match ' \
75+
"name \"#{selector}\" specified in categories file"
76+
return []
77+
end
78+
[single_doc]
79+
else
80+
doc_index.lookup_regex(selector['regex'])
81+
.sort_by(&:name)
82+
end
83+
end
84+
7585
def self.group_guides(docs, prefix)
7686
guides, others = docs.partition { |doc| doc.type.markdown? }
7787
return [[], others] unless guides.any?

0 commit comments

Comments
 (0)