Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(615)

Side by Side Diff: pkg/docgen/lib/src/mdn.dart

Issue 180243024: pkg/docgen: refactoring and cleanup (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: just refactoring Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 library docgen.mdn;
2
3 import 'dart:convert';
4 import 'dart:io';
5
6 import 'package:logging/logging.dart';
7 import 'package:path/path.dart' as p;
8
9 // TODO(janicejl): Make MDN content generic or pluggable.
10
11 /// Map of all the comments for dom elements from MDN.
12 Map<String, dynamic> _mdn;
13
14 /// Generates MDN comments from database.json.
15 String mdnComment(String root, Logger logger, String domName) {
16 //Check if MDN is loaded.
17 if (_mdn == null) {
18 // Reading in MDN related json file.
19 var mdnPath = p.join(root, 'utils/apidoc/mdn/database.json');
20 var mdnFile = new File(mdnPath);
21 if (mdnFile.existsSync()) {
22 _mdn = JSON.decode(mdnFile.readAsStringSync());
23 } else {
24 logger.warning("Cannot find MDN docs expected at $mdnPath");
25 _mdn = {};
26 }
27 }
28
29 var parts = domName.split('.');
30 if (parts.length == 2) return _mdnMemberComment(parts[0], parts[1]);
31 if (parts.length == 1) return _mdnTypeComment(parts[0]);
32
33 throw new StateError('More than two items is not supported: $parts');
34 }
35
36 /// Generates the MDN Comment for variables and method DOM elements.
37 String _mdnMemberComment(String type, String member) {
38 var mdnType = _mdn[type];
39 if (mdnType == null) return '';
40 var mdnMember = mdnType['members'].firstWhere((e) => e['name'] == member,
41 orElse: () => null);
42 if (mdnMember == null) return '';
43 if (mdnMember['help'] == null || mdnMember['help'] == '') return '';
44 if (mdnMember['url'] == null) return '';
45 return _htmlifyMdn(mdnMember['help'], mdnMember['url']);
46 }
47
48 /// Generates the MDN Comment for class DOM elements.
49 String _mdnTypeComment(String type) {
50 var mdnType = _mdn[type];
51 if (mdnType == null) return '';
52 if (mdnType['summary'] == null || mdnType['summary'] == "") return '';
53 if (mdnType['srcUrl'] == null) return '';
54 return _htmlifyMdn(mdnType['summary'], mdnType['srcUrl']);
55 }
56
57 /// Encloses the given content in an MDN div and the original source link.
58 String _htmlifyMdn(String content, String url) {
59 return '<div class="mdn">' + content.trim() + '<p class="mdn-note">'
60 '<a href="' + url.trim() + '">from Mdn</a></p></div>';
61 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698