| Index: pkg/docgen/lib/src/mdn.dart
|
| diff --git a/pkg/docgen/lib/src/mdn.dart b/pkg/docgen/lib/src/mdn.dart
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..77d24f1d06f36a6a02c183f8151a6e96fa378b73
|
| --- /dev/null
|
| +++ b/pkg/docgen/lib/src/mdn.dart
|
| @@ -0,0 +1,61 @@
|
| +library docgen.mdn;
|
| +
|
| +import 'dart:convert';
|
| +import 'dart:io';
|
| +
|
| +import 'package:logging/logging.dart';
|
| +import 'package:path/path.dart' as p;
|
| +
|
| +// TODO(janicejl): Make MDN content generic or pluggable.
|
| +
|
| +/// Map of all the comments for dom elements from MDN.
|
| +Map<String, dynamic> _mdn;
|
| +
|
| +/// Generates MDN comments from database.json.
|
| +String mdnComment(String root, Logger logger, String domName) {
|
| + //Check if MDN is loaded.
|
| + if (_mdn == null) {
|
| + // Reading in MDN related json file.
|
| + var mdnPath = p.join(root, 'utils/apidoc/mdn/database.json');
|
| + var mdnFile = new File(mdnPath);
|
| + if (mdnFile.existsSync()) {
|
| + _mdn = JSON.decode(mdnFile.readAsStringSync());
|
| + } else {
|
| + logger.warning("Cannot find MDN docs expected at $mdnPath");
|
| + _mdn = {};
|
| + }
|
| + }
|
| +
|
| + var parts = domName.split('.');
|
| + if (parts.length == 2) return _mdnMemberComment(parts[0], parts[1]);
|
| + if (parts.length == 1) return _mdnTypeComment(parts[0]);
|
| +
|
| + throw new StateError('More than two items is not supported: $parts');
|
| +}
|
| +
|
| +/// Generates the MDN Comment for variables and method DOM elements.
|
| +String _mdnMemberComment(String type, String member) {
|
| + var mdnType = _mdn[type];
|
| + if (mdnType == null) return '';
|
| + var mdnMember = mdnType['members'].firstWhere((e) => e['name'] == member,
|
| + orElse: () => null);
|
| + if (mdnMember == null) return '';
|
| + if (mdnMember['help'] == null || mdnMember['help'] == '') return '';
|
| + if (mdnMember['url'] == null) return '';
|
| + return _htmlifyMdn(mdnMember['help'], mdnMember['url']);
|
| +}
|
| +
|
| +/// Generates the MDN Comment for class DOM elements.
|
| +String _mdnTypeComment(String type) {
|
| + var mdnType = _mdn[type];
|
| + if (mdnType == null) return '';
|
| + if (mdnType['summary'] == null || mdnType['summary'] == "") return '';
|
| + if (mdnType['srcUrl'] == null) return '';
|
| + return _htmlifyMdn(mdnType['summary'], mdnType['srcUrl']);
|
| +}
|
| +
|
| +/// Encloses the given content in an MDN div and the original source link.
|
| +String _htmlifyMdn(String content, String url) {
|
| + return '<div class="mdn">' + content.trim() + '<p class="mdn-note">'
|
| + '<a href="' + url.trim() + '">from Mdn</a></p></div>';
|
| +}
|
|
|