OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, 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 library docgen.mdn; | |
6 | |
7 import 'dart:convert'; | |
8 import 'dart:io'; | |
9 | |
10 import 'package:logging/logging.dart'; | |
11 import 'package:path/path.dart' as p; | |
12 | |
13 // TODO(janicejl): Make MDN content generic or pluggable. | |
14 | |
15 /// Map of all the comments for dom elements from MDN. | |
16 Map<String, dynamic> _mdn; | |
17 | |
18 /// Generates MDN comments from database.json. | |
19 String mdnComment(String root, Logger logger, String domName) { | |
20 //Check if MDN is loaded. | |
21 if (_mdn == null) { | |
22 // Reading in MDN related json file. | |
23 var mdnPath = p.join(root, 'utils/apidoc/mdn/database.json'); | |
24 var mdnFile = new File(mdnPath); | |
25 if (mdnFile.existsSync()) { | |
26 _mdn = JSON.decode(mdnFile.readAsStringSync()); | |
27 } else { | |
28 logger.warning("Cannot find MDN docs expected at $mdnPath"); | |
29 _mdn = {}; | |
30 } | |
31 } | |
32 | |
33 var parts = domName.split('.'); | |
34 if (parts.length == 2) return _mdnMemberComment(parts[0], parts[1]); | |
35 if (parts.length == 1) return _mdnTypeComment(parts[0]); | |
36 | |
37 throw new StateError('More than two items is not supported: $parts'); | |
38 } | |
39 | |
40 /// Generates the MDN Comment for variables and method DOM elements. | |
41 String _mdnMemberComment(String type, String member) { | |
42 var mdnType = _mdn[type]; | |
43 if (mdnType == null) return ''; | |
44 var mdnMember = mdnType['members'].firstWhere((e) => e['name'] == member, | |
45 orElse: () => null); | |
46 if (mdnMember == null) return ''; | |
47 if (mdnMember['help'] == null || mdnMember['help'] == '') return ''; | |
48 if (mdnMember['url'] == null) return ''; | |
49 return _htmlifyMdn(mdnMember['help'], mdnMember['url']); | |
50 } | |
51 | |
52 /// Generates the MDN Comment for class DOM elements. | |
53 String _mdnTypeComment(String type) { | |
54 var mdnType = _mdn[type]; | |
55 if (mdnType == null) return ''; | |
56 if (mdnType['summary'] == null || mdnType['summary'] == "") return ''; | |
57 if (mdnType['srcUrl'] == null) return ''; | |
58 return _htmlifyMdn(mdnType['summary'], mdnType['srcUrl']); | |
59 } | |
60 | |
61 /// Encloses the given content in an MDN div and the original source link. | |
62 String _htmlifyMdn(String content, String url) { | |
63 return '<div class="mdn">' + content.trim() + '<p class="mdn-note">' | |
64 '<a href="' + url.trim() + '">from Mdn</a></p></div>'; | |
65 } | |
OLD | NEW |