Skip to content

Commit

Permalink
Make markdown error-free in strong mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
munificent committed Sep 1, 2015
1 parent 079ea42 commit 2b8c4db
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 38 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.7.3
## 0.8.0

* Switch tests to use [test][] instead of [unittest][].
* Remove (probably unused) `resolved` field from `LinkSyntax`.

[test]: https://pub.dartlang.org/packages/test
[unittest]: https://pub.dartlang.org/packages/unittest
Expand Down
67 changes: 35 additions & 32 deletions lib/src/inline_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ class InlineParser {

// If the previous node is text too, just append.
if (nodes.length > 0 && nodes.last is Text) {
nodes[nodes.length - 1] = new Text('${nodes.last.text}$text');
var textNode = nodes.last as Text;
nodes[nodes.length - 1] = new Text('${textNode.text}$text');
} else {
nodes.add(new Text(text));
}
Expand Down Expand Up @@ -192,8 +193,8 @@ class TextSyntax extends InlineSyntax {
final String substitute;

TextSyntax(String pattern, {String sub})
: super(pattern),
substitute = sub;
: substitute = sub,
super(pattern);

bool onMatch(InlineParser parser, Match match) {
if (substitute == null) {
Expand Down Expand Up @@ -230,8 +231,8 @@ class TagSyntax extends InlineSyntax {
final String tag;

TagSyntax(String pattern, {this.tag, String end})
: super(pattern),
endPattern = new RegExp((end != null) ? end : pattern, multiLine: true);
: endPattern = new RegExp((end != null) ? end : pattern, multiLine: true),
super(pattern);

bool onMatch(InlineParser parser, Match match) {
parser._stack
Expand All @@ -249,9 +250,6 @@ class TagSyntax extends InlineSyntax {
class LinkSyntax extends TagSyntax {
final Resolver linkResolver;

/// Weather or not this link was resolved by a [Resolver]
bool resolved = false;

/// The regex for the end of a link needs to handle both reference style and
/// inline styles as well as optional titles for inline links. To make that
/// a bit more palatable, this breaks it into pieces.
Expand Down Expand Up @@ -287,19 +285,26 @@ class LinkSyntax extends TagSyntax {
var textToResolve = parser.source.substring(state.endPos, parser.pos);

// See if we have a resolver that will generate a link for us.
resolved = true;
return linkResolver(textToResolve);
} else {
var link = getLink(parser, match, state);
if (link == null) return null;
return _createElement(parser, match, state);
}
}

var node = new Element('a', state.children);
/// Given that [match] has matched both a title and URL, creates an `<a>`
/// [Element] for it.
Element _createElement(InlineParser parser, Match match, TagState state) {
var link = getLink(parser, match, state);
if (link == null) return null;

node.attributes["href"] = escapeHtml(link.url);
if (link.title != null) node.attributes['title'] = escapeHtml(link.title);
var element = new Element('a', state.children);

return node;
element.attributes["href"] = escapeHtml(link.url);
if (link.title != null) {
element.attributes['title'] = escapeHtml(link.title);
}

return element;
}

Link getLink(InlineParser parser, Match match, TagState state) {
Expand Down Expand Up @@ -342,31 +347,29 @@ class LinkSyntax extends TagSyntax {
/// Matches images like `![alternate text](url "optional title")` and
/// `![alternate text][url reference]`.
class ImageLinkSyntax extends LinkSyntax {
final Resolver linkResolver;

ImageLinkSyntax({this.linkResolver}) : super(pattern: r'!\[');

Node createNode(InlineParser parser, Match match, TagState state) {
var node = super.createNode(parser, match, state);
ImageLinkSyntax({Resolver linkResolver})
: super(linkResolver: linkResolver, pattern: r'!\[');

if (resolved) return node;
if (node == null) return null;
/// Creates an <a> element from the given complete [match].
Element _createElement(InlineParser parser, Match match, TagState state) {
var element = super._createElement(parser, match, state);
if (element == null) return null;

var imageElement = new Element.withTag("img");
imageElement.attributes["src"] = node.attributes["href"];
var image = new Element.withTag("img");
image.attributes["src"] = element.attributes["href"];

if (node.attributes.containsKey("title")) {
imageElement.attributes["title"] = node.attributes["title"];
if (element.attributes.containsKey("title")) {
image.attributes["title"] = element.attributes["title"];
}

var alt = node.children.map((e) => e is! Text ? '' : e.text).join(" ");
if (alt != "") imageElement.attributes["alt"] = alt;
var alt = element.children.map((e) => e is! Text ? "" : e.text).join(" ");
if (alt != "") image.attributes["alt"] = alt;

node.children
element.children
..clear()
..add(imageElement);
..add(image);

return node;
return element;
}
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: markdown
version: 0.7.3-dev
version: 0.8.0-dev
author: Dart Team <[email protected]>
description: A library for converting markdown to HTML.
homepage: https://github.com/dart-lang/markdown
Expand Down
6 changes: 2 additions & 4 deletions test/markdown_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ void main() {
});

group('Resolver', () {
nyanResolver(text) => new Text('~=[,,_${text}_,,]:3');
Node nyanResolver(String text) => new Text('~=[,,_${text}_,,]:3');

validate(
'simple link resolver',
Expand Down Expand Up @@ -1315,8 +1315,6 @@ void main() {
});

group('Custom inline syntax', () {
var nyanSyntax = [new TextSyntax('nyan', sub: '~=[,,_,,]:3')];

validate(
'simple inline syntax',
'''
Expand All @@ -1325,7 +1323,7 @@ void main() {
'''
<p>~=[,,_,,]:3</p>
''',
inlineSyntaxes: nyanSyntax);
inlineSyntaxes: [new TextSyntax('nyan', sub: '~=[,,_,,]:3')]);

validate('dart custom links', 'links [are<foo>] awesome',
'<p>links <a>are&lt;foo></a> awesome</p>',
Expand Down

0 comments on commit 2b8c4db

Please sign in to comment.