Skip to content

Commit 4b216d6

Browse files
jdkorenjcollins-g
authored andcommitted
Create renderer for Category (#2076)
1 parent 3399c3f commit 4b216d6

File tree

5 files changed

+78
-17
lines changed

5 files changed

+78
-17
lines changed

lib/src/model/category.dart

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:analyzer/dart/element/element.dart';
88
import 'package:dartdoc/src/dartdoc_options.dart';
99
import 'package:dartdoc/src/model/model.dart';
1010
import 'package:dartdoc/src/package_meta.dart';
11+
import 'package:dartdoc/src/render/category_renderer.dart';
1112
import 'package:dartdoc/src/warnings.dart';
1213

1314
/// A category is a subcategory of a package, containing libraries tagged
@@ -123,28 +124,24 @@ class Category extends Nameable
123124
String get href =>
124125
isCanonical ? '${package.baseHref}topics/${name}-topic.html' : null;
125126

127+
String get categorization {
128+
return CategoryRendererHtml().renderCategoryLabel(this);
129+
}
130+
126131
String get linkedName {
127-
String unbrokenCategoryName = name.replaceAll(' ', ' ');
128-
if (isDocumented) {
129-
return '<a href="$href">$unbrokenCategoryName</a>';
130-
} else {
131-
return unbrokenCategoryName;
132-
}
132+
return CategoryRendererHtml().renderLinkedName(this);
133133
}
134134

135-
String _categoryNumberClass;
135+
int _categoryIndex;
136136

137137
/// The position in the container order for this category.
138-
String get categoryNumberClass {
139-
if (_categoryNumberClass == null) {
140-
_categoryNumberClass = "cp-${package.categories.indexOf(this)}";
138+
int get categoryIndex {
139+
if (_categoryIndex == null) {
140+
_categoryIndex = package.categories.indexOf(this);
141141
}
142-
return _categoryNumberClass;
142+
return _categoryIndex;
143143
}
144144

145-
/// Category name used in template as part of the class.
146-
String get spanClass => name.split(' ').join('-').toLowerCase();
147-
148145
CategoryDefinition get categoryDefinition =>
149146
config.categories.categoryDefinitions[sortKey];
150147

lib/src/render/category_renderer.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:dartdoc/src/model/category.dart';
6+
7+
abstract class CategoryRenderer {
8+
String renderCategoryLabel(Category category);
9+
String renderLinkedName(Category category);
10+
}
11+
12+
class CategoryRendererHtml extends CategoryRenderer {
13+
static final CategoryRendererHtml _instance = CategoryRendererHtml._();
14+
15+
factory CategoryRendererHtml() {
16+
return _instance;
17+
}
18+
19+
CategoryRendererHtml._();
20+
21+
@override
22+
String renderCategoryLabel(Category category) {
23+
List<String> spanClasses = [];
24+
spanClasses.add('category');
25+
spanClasses.add(category.name.split(' ').join('-').toLowerCase());
26+
spanClasses.add('cp-${category.categoryIndex}');
27+
if (category.isDocumented) {
28+
spanClasses.add('linked');
29+
}
30+
var spanTitle = "This is part of the ${category.name} ${category.kind}.";
31+
32+
StringBuffer buf = StringBuffer();
33+
buf.write('<span class="${spanClasses.join(' ')}" title="$spanTitle">');
34+
buf.write(category.linkedName);
35+
buf.write('</span>');
36+
return buf.toString();
37+
}
38+
39+
@override
40+
String renderLinkedName(Category category) {
41+
String unbrokenName = category.name.replaceAll(' ', '&nbsp;');
42+
if (category.isDocumented) {
43+
return '<a href="${category.href}">$unbrokenName</a>';
44+
} else {
45+
return unbrokenName;
46+
}
47+
}
48+
}

lib/templates/_categorization.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{#hasCategoryNames}}
22
{{#displayedCategories}}
3-
<span class="category {{spanClass}} {{categoryNumberClass}} {{#isDocumented}}linked{{/isDocumented}}" title="This is part of the {{name}} {{kind}}.">{{{linkedName}}}</span>
3+
{{{categorization}}}
44
{{/displayedCategories}}
55
{{/hasCategoryNames}}

test/model_special_cases_test.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,7 @@ void main() {
256256
expect(IAmAClassWithCategoriesReexport.displayedCategories, isNotEmpty);
257257
Category category =
258258
IAmAClassWithCategoriesReexport.displayedCategories.first;
259-
expect(category.spanClass, equals('superb'));
260-
expect(category.categoryNumberClass, equals('cp-0'));
259+
expect(category.categoryIndex, equals(0));
261260
expect(category.isDocumented, isTrue);
262261
});
263262

test/model_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'dart:io';
99
import 'package:dartdoc/dartdoc.dart';
1010
import 'package:dartdoc/src/model/model.dart';
1111
import 'package:dartdoc/src/model_utils.dart';
12+
import 'package:dartdoc/src/render/category_renderer.dart';
1213
import 'package:dartdoc/src/warnings.dart';
1314
import 'package:test/test.dart';
1415

@@ -361,6 +362,22 @@ void main() {
361362
// Only 5 libraries have categories, the rest belong in default.
362363
equals(utils.kTestPackagePublicLibraries - 5));
363364
});
365+
366+
test('CategoryRendererHtml renders category label', () {
367+
Category category = packageGraph.publicPackages.first.categories.first;
368+
CategoryRendererHtml renderer = CategoryRendererHtml();
369+
expect(
370+
renderer.renderCategoryLabel(category),
371+
'<span class="category superb cp-0 linked" title="This is part of the Superb Topic.">'
372+
'<a href="topics/Superb-topic.html">Superb</a></span>');
373+
});
374+
375+
test('CategoryRendererHtml renders linkedName', () {
376+
Category category = packageGraph.publicPackages.first.categories.first;
377+
CategoryRendererHtml renderer = CategoryRendererHtml();
378+
expect(renderer.renderLinkedName(category),
379+
'<a href="topics/Superb-topic.html">Superb</a>');
380+
});
364381
});
365382

366383
group('LibraryContainer', () {

0 commit comments

Comments
 (0)