OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library markdown.inline_parser; | 5 library markdown.inline_parser; |
6 | 6 |
7 import 'ast.dart'; | 7 import 'ast.dart'; |
8 import 'document.dart'; | 8 import 'document.dart'; |
9 import 'util.dart'; | 9 import 'util.dart'; |
10 | 10 |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 | 273 |
274 Node createNode(InlineParser parser, Match match, TagState state) { | 274 Node createNode(InlineParser parser, Match match, TagState state) { |
275 // If we didn't match refLink or inlineLink, then it means there was | 275 // If we didn't match refLink or inlineLink, then it means there was |
276 // nothing after the first square bracket, so it isn't a normal markdown | 276 // nothing after the first square bracket, so it isn't a normal markdown |
277 // link at all. Instead, we allow users of the library to specify a special | 277 // link at all. Instead, we allow users of the library to specify a special |
278 // resolver function ([linkResolver]) that may choose to handle | 278 // resolver function ([linkResolver]) that may choose to handle |
279 // this. Otherwise, it's just treated as plain text. | 279 // this. Otherwise, it's just treated as plain text. |
280 if (isNullOrEmpty(match[1])) { | 280 if (isNullOrEmpty(match[1])) { |
281 if (linkResolver == null) return null; | 281 if (linkResolver == null) return null; |
282 | 282 |
283 // Only allow implicit links if the content is just text. | 283 // Treat the contents as unparsed text even if they happen to match. This |
284 // TODO(rnystrom): Do we want to relax this? | 284 // way, we can handle things like [LINK_WITH_UNDERSCORES] as a link and |
285 if (state.children.any((child) => child is! Text)) return null; | 285 // not get confused by the emphasis. |
286 // If there are multiple children, but they are all text, send the | 286 var textToResolve = parser.source.substring(state.endPos, parser.pos); |
287 // combined text to linkResolver. | |
288 var textToResolve = | |
289 state.children.fold('', (oldVal, child) => oldVal + child.text); | |
290 | 287 |
291 // See if we have a resolver that will generate a link for us. | 288 // See if we have a resolver that will generate a link for us. |
292 resolved = true; | 289 resolved = true; |
293 return linkResolver(textToResolve); | 290 return linkResolver(textToResolve); |
294 } else { | 291 } else { |
295 Link link = getLink(parser, match, state); | 292 Link link = getLink(parser, match, state); |
296 if (link == null) return null; | 293 if (link == null) return null; |
297 | 294 |
298 final Element node = new Element('a', state.children) | 295 final Element node = new Element('a', state.children) |
299 ..attributes["href"] = escapeHtml(link.url) | 296 ..attributes["href"] = escapeHtml(link.url) |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 parser.consume(endMatch[0].length); | 438 parser.consume(endMatch[0].length); |
442 } else { | 439 } else { |
443 // Didn't close correctly so revert to text. | 440 // Didn't close correctly so revert to text. |
444 parser.start = startPos; | 441 parser.start = startPos; |
445 parser.advanceBy(endMatch[0].length); | 442 parser.advanceBy(endMatch[0].length); |
446 } | 443 } |
447 | 444 |
448 return null; | 445 return null; |
449 } | 446 } |
450 } | 447 } |
OLD | NEW |