11// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2+
23// for details. All rights reserved. Use of this source code is governed by a
34// BSD-style license that can be found in the LICENSE file.
45
@@ -24,122 +25,14 @@ import 'package:markdown/markdown.dart' as md;
2425
2526import 'model.dart' ;
2627
27- final List <md. InlineSyntax > _markdown_syntaxes = [ new _InlineCodeSyntax ()] ;
28+ const bool _emitWarning = false ;
2829
2930// We don't emit warnings currently: #572.
30- const bool _emitWarning = false ;
31+ const List < String > _oneLinerSkipTags = const [ "code" , "pre" ] ;
3132
32- String _linkDocReference (String reference, ModelElement element,
33- NodeList <CommentReference > commentRefs) {
34- ModelElement linkedElement;
35- // support for [new Constructor] and [new Class.namedCtr]
36- var refs = reference.split (' ' );
37- if (refs.length == 2 && refs.first == 'new' ) {
38- linkedElement = _getMatchingLinkElement (refs[1 ], element, commentRefs,
39- isConstructor: true );
40- } else {
41- linkedElement = _getMatchingLinkElement (reference, element, commentRefs);
42- }
43- if (linkedElement != null ) {
44- var classContent = '' ;
45- if (linkedElement.isDeprecated) {
46- classContent = 'class="deprecated" ' ;
47- }
48- // this would be linkedElement.linkedName, but link bodies are slightly
49- // different for doc references. sigh.
50- return '<a ${classContent }href="${linkedElement .href }">$reference </a>' ;
51- } else {
52- if (_emitWarning) {
53- print (" warning: unresolved doc reference '$reference ' (in $element )" );
54- }
55- return '<code>$reference </code>' ;
56- }
57- }
33+ final List <md.InlineSyntax > _markdown_syntaxes = [new _InlineCodeSyntax ()];
5834
5935// TODO: this is in the wrong place
60- class Documentation {
61- final String raw;
62- final String asHtml;
63- final String asOneLiner;
64- final bool hasMoreThanOneLineDocs;
65-
66- factory Documentation (String markdown) {
67- String tempHtml = _renderMarkdownToHtml (markdown);
68- return new Documentation ._internal (markdown, tempHtml);
69- }
70-
71- factory Documentation .forElement (ModelElement element) {
72- String tempHtml = _renderMarkdownToHtml (element.documentation, element);
73- return new Documentation ._internal (element.documentation, tempHtml);
74- }
75-
76- factory Documentation ._internal (String markdown, String rawHtml) {
77- var asHtmlDocument = parse (rawHtml);
78- for (var s in asHtmlDocument.querySelectorAll ('script' )) {
79- s.remove ();
80- }
81- for (var e in asHtmlDocument.querySelectorAll ('pre' )) {
82- if (e.children.length != 1 && e.children.first.localName != 'code' ) {
83- continue ;
84- }
85-
86- // TODO(kevmoo): This should be applied to <code>, not <pre>
87- // Waiting on pkg/markdown v0.10
88- // See https://github.com/dart-lang/markdown/commit/a7bf3dd
89- e.classes.add ('prettyprint' );
90-
91- // only "assume" the user intended dart if there are no other classes
92- // present
93- // TODO(kevmoo): This should be `language-dart`.
94- // Waiting on pkg/markdown v0.10
95- // See https://github.com/dart-lang/markdown/commit/a7bf3dd
96- if (e.classes.length == 1 ) {
97- e.classes.add ('lang-dart' );
98- }
99- }
100- var asHtml = asHtmlDocument.body.innerHtml;
101-
102- // Fixes issue with line ending differences between mac and windows, affecting tests
103- if (asHtml != null ) asHtml = asHtml.trim ();
104-
105- var asOneLiner = '' ;
106- var moreThanOneLineDoc = asHtmlDocument.body.children.length > 1 ;
107-
108- if (asHtmlDocument.body.children.isNotEmpty) {
109- asOneLiner = asHtmlDocument.body.children.first.innerHtml;
110- }
111-
112- return new Documentation ._(
113- markdown, asHtml, moreThanOneLineDoc, asOneLiner);
114- }
115-
116- Documentation ._(
117- this .raw, this .asHtml, this .hasMoreThanOneLineDocs, this .asOneLiner);
118- }
119-
120- String _renderMarkdownToHtml (String text, [ModelElement element]) {
121- md.Node _linkResolver (String name) {
122- NodeList <CommentReference > commentRefs = _getCommentRefs (element);
123- return new md.Text (_linkDocReference (name, element, commentRefs));
124- }
125-
126- return md.markdownToHtml (text,
127- inlineSyntaxes: _markdown_syntaxes, linkResolver: _linkResolver);
128- }
129-
130- class _InlineCodeSyntax extends md.InlineSyntax {
131- _InlineCodeSyntax () : super (r'\[:\s?((?:.|\n)*?)\s?:\]' );
132-
133- @override
134- bool onMatch (md.InlineParser parser, Match match) {
135- var element = new md.Element .text ('code' , HTML_ESCAPE .convert (match[1 ]));
136- parser.addNode (element);
137- return true ;
138- }
139- }
140-
141- const List <String > _oneLinerSkipTags = const ["code" , "pre" ];
142-
14336NodeList <CommentReference > _getCommentRefs (ModelElement modelElement) {
14437 if (modelElement == null ) return null ;
14538 if (modelElement.documentation == null && modelElement.canOverride ()) {
@@ -231,3 +124,113 @@ ModelElement _getMatchingLinkElement(
231124 }
232125 return null ;
233126}
127+
128+ String _linkDocReference (String reference, ModelElement element,
129+ NodeList <CommentReference > commentRefs) {
130+ ModelElement linkedElement;
131+ // support for [new Constructor] and [new Class.namedCtr]
132+ var refs = reference.split (' ' );
133+ if (refs.length == 2 && refs.first == 'new' ) {
134+ linkedElement = _getMatchingLinkElement (refs[1 ], element, commentRefs,
135+ isConstructor: true );
136+ } else {
137+ linkedElement = _getMatchingLinkElement (reference, element, commentRefs);
138+ }
139+ if (linkedElement != null ) {
140+ var classContent = '' ;
141+ if (linkedElement.isDeprecated) {
142+ classContent = 'class="deprecated" ' ;
143+ }
144+ // this would be linkedElement.linkedName, but link bodies are slightly
145+ // different for doc references. sigh.
146+ return '<a ${classContent }href="${linkedElement .href }">$reference </a>' ;
147+ } else {
148+ if (_emitWarning) {
149+ print (" warning: unresolved doc reference '$reference ' (in $element )" );
150+ }
151+ return '<code>$reference </code>' ;
152+ }
153+ }
154+
155+ String _renderMarkdownToHtml (String text, [ModelElement element]) {
156+ md.Node _linkResolver (String name) {
157+ NodeList <CommentReference > commentRefs = _getCommentRefs (element);
158+ return new md.Text (_linkDocReference (name, element, commentRefs));
159+ }
160+
161+ return md.markdownToHtml (text,
162+ inlineSyntaxes: _markdown_syntaxes, linkResolver: _linkResolver);
163+ }
164+
165+ class Documentation {
166+ final String raw;
167+ final String asHtml;
168+ final String asOneLiner;
169+ final bool hasMoreThanOneLineDocs;
170+
171+ factory Documentation (String markdown) {
172+ String tempHtml = _renderMarkdownToHtml (markdown);
173+ return new Documentation ._internal (markdown, tempHtml);
174+ }
175+
176+ factory Documentation .forElement (ModelElement element) {
177+ String tempHtml = _renderMarkdownToHtml (element.documentation, element);
178+ return new Documentation ._internal (element.documentation, tempHtml);
179+ }
180+
181+ Documentation ._(
182+ this .raw, this .asHtml, this .hasMoreThanOneLineDocs, this .asOneLiner);
183+
184+ factory Documentation ._internal (String markdown, String rawHtml) {
185+ var asHtmlDocument = parse (rawHtml);
186+ for (var s in asHtmlDocument.querySelectorAll ('script' )) {
187+ s.remove ();
188+ }
189+ for (var e in asHtmlDocument.querySelectorAll ('pre' )) {
190+ if (e.children.isNotEmpty &&
191+ e.children.length != 1 &&
192+ e.children.first.localName != 'code' ) {
193+ continue ;
194+ }
195+
196+ // TODO(kevmoo): This should be applied to <code>, not <pre>
197+ // Waiting on pkg/markdown v0.10
198+ // See https://github.com/dart-lang/markdown/commit/a7bf3dd
199+ e.classes.add ('prettyprint' );
200+
201+ // only "assume" the user intended dart if there are no other classes
202+ // present
203+ // TODO(kevmoo): This should be `language-dart`.
204+ // Waiting on pkg/markdown v0.10
205+ // See https://github.com/dart-lang/markdown/commit/a7bf3dd
206+ if (e.classes.length == 1 ) {
207+ e.classes.add ('lang-dart' );
208+ }
209+ }
210+ var asHtml = asHtmlDocument.body.innerHtml;
211+
212+ // Fixes issue with line ending differences between mac and windows, affecting tests
213+ if (asHtml != null ) asHtml = asHtml.trim ();
214+
215+ var asOneLiner = '' ;
216+ var moreThanOneLineDoc = asHtmlDocument.body.children.length > 1 ;
217+
218+ if (asHtmlDocument.body.children.isNotEmpty) {
219+ asOneLiner = asHtmlDocument.body.children.first.innerHtml;
220+ }
221+
222+ return new Documentation ._(
223+ markdown, asHtml, moreThanOneLineDoc, asOneLiner);
224+ }
225+ }
226+
227+ class _InlineCodeSyntax extends md.InlineSyntax {
228+ _InlineCodeSyntax () : super (r'\[:\s?((?:.|\n)*?)\s?:\]' );
229+
230+ @override
231+ bool onMatch (md.InlineParser parser, Match match) {
232+ var element = new md.Element .text ('code' , HTML_ESCAPE .convert (match[1 ]));
233+ parser.addNode (element);
234+ return true ;
235+ }
236+ }
0 commit comments