code formatting
diff --git a/pkgs/markdown/lib/src/ast.dart b/pkgs/markdown/lib/src/ast.dart index befd75e..3290efb 100644 --- a/pkgs/markdown/lib/src/ast.dart +++ b/pkgs/markdown/lib/src/ast.dart
@@ -18,20 +18,19 @@ final List<Node> children; final Map<String, String> attributes; - Element(this.tag, this.children) - : attributes = <String, String>{}; + Element(this.tag, this.children) : attributes = <String, String>{}; Element.empty(this.tag) - : children = null, - attributes = <String, String>{}; + : children = null, + attributes = <String, String>{}; Element.withTag(this.tag) - : children = [], - attributes = <String, String>{}; + : children = [], + attributes = <String, String>{}; Element.text(this.tag, String text) - : children = [new Text(text)], - attributes = <String, String>{}; + : children = [new Text(text)], + attributes = <String, String>{}; bool get isEmpty => children == null;
diff --git a/pkgs/markdown/lib/src/block_parser.dart b/pkgs/markdown/lib/src/block_parser.dart index 52ada6e..ce92867 100644 --- a/pkgs/markdown/lib/src/block_parser.dart +++ b/pkgs/markdown/lib/src/block_parser.dart
@@ -30,8 +30,8 @@ /// a line like `----` is valid as both HR and SETEXT. In case of a tie, /// SETEXT should win. final _RE_HR = new RegExp(r'^[ ]{0,3}((-+[ ]{0,2}){3,}|' - r'(_+[ ]{0,2}){3,}|' - r'(\*+[ ]{0,2}){3,})$'); + r'(_+[ ]{0,2}){3,}|' + r'(\*+[ ]{0,2}){3,})$'); /// Really hacky way to detect block-level embedded HTML. Just looks for /// "<somename". @@ -57,8 +57,7 @@ /// Index of the current line. int _pos; - BlockParser(this.lines, this.document) - : _pos = 0; + BlockParser(this.lines, this.document) : _pos = 0; /// Gets the current line. String get current => lines[_pos]; @@ -92,7 +91,7 @@ abstract class BlockSyntax { /// Gets the collection of built-in block parsers. To turn a series of lines /// into blocks, each of these will be tried in turn. Order matters here. - static const List<BlockSyntax> syntaxes = const[ + static const List<BlockSyntax> syntaxes = const [ const EmptyBlockSyntax(), const BlockHtmlSyntax(), const SetextHeaderSyntax(), @@ -155,7 +154,6 @@ /// Parses setext-style headers. class SetextHeaderSyntax extends BlockSyntax { - const SetextHeaderSyntax(); bool canParse(BlockParser parser) { @@ -224,8 +222,8 @@ } else { // If there's a codeblock, then a newline, then a codeblock, keep the // code blocks together. - var nextMatch = parser.next != null ? - pattern.firstMatch(parser.next) : null; + var nextMatch = + parser.next != null ? pattern.firstMatch(parser.next) : null; if (parser.current.trim() == '' && nextMatch != null) { childLines.add(''); childLines.add(nextMatch[1]); @@ -260,7 +258,7 @@ const FencedCodeBlockSyntax(); List<String> parseChildLines(BlockParser parser, [String endBlock]) { - if(endBlock == null) endBlock = ''; + if (endBlock == null) endBlock = ''; final childLines = <String>[]; parser.advance();
diff --git a/pkgs/markdown/lib/src/document.dart b/pkgs/markdown/lib/src/document.dart index 4724208..79f7c31 100644 --- a/pkgs/markdown/lib/src/document.dart +++ b/pkgs/markdown/lib/src/document.dart
@@ -12,7 +12,7 @@ Resolver imageLinkResolver; Document({this.inlineSyntaxes, this.linkResolver, this.imageLinkResolver}) - : refLinks = <String, Link>{}; + : refLinks = <String, Link>{}; parseRefLinks(List<String> lines) { // This is a hideous regex. It matches: @@ -20,10 +20,10 @@ // Where there may whitespace in there, and where the title may be in // single quotes, double quotes, or parentheses. final indent = r'^[ ]{0,3}'; // Leading indentation. - final id = r'\[([^\]]+)\]'; // Reference id in [brackets]. - final quote = r'"[^"]+"'; // Title in "double quotes". - final apos = r"'[^']+'"; // Title in 'single quotes'. - final paren = r"\([^)]+\)"; // Title in (parentheses). + final id = r'\[([^\]]+)\]'; // Reference id in [brackets]. + final quote = r'"[^"]+"'; // Title in "double quotes". + final apos = r"'[^']+'"; // Title in 'single quotes'. + final paren = r"\([^)]+\)"; // Title in (parentheses). final pattern = new RegExp( '$indent$id:\\s+(\\S+)\\s*($quote|$apos|$paren|)\\s*\$');
diff --git a/pkgs/markdown/lib/src/html_renderer.dart b/pkgs/markdown/lib/src/html_renderer.dart index 7dd1f9b..b2d96e7 100644 --- a/pkgs/markdown/lib/src/html_renderer.dart +++ b/pkgs/markdown/lib/src/html_renderer.dart
@@ -10,15 +10,18 @@ /// Converts the given string of markdown to HTML. String markdownToHtml(String markdown, {List<InlineSyntax> inlineSyntaxes, - Resolver linkResolver, Resolver imageLinkResolver, bool inlineOnly: false}) { - var document = new Document(inlineSyntaxes: inlineSyntaxes, - imageLinkResolver: imageLinkResolver, linkResolver: linkResolver); + Resolver linkResolver, Resolver imageLinkResolver, + bool inlineOnly: false}) { + var document = new Document( + inlineSyntaxes: inlineSyntaxes, + imageLinkResolver: imageLinkResolver, + linkResolver: linkResolver); if (inlineOnly) { return renderToHtml(document.parseInline(markdown)); } else { // Replace windows line endings with unix line endings, and split. - var lines = markdown.replaceAll('\r\n','\n').split('\n'); + var lines = markdown.replaceAll('\r\n', '\n').split('\n'); document.parseRefLinks(lines); var blocks = document.parseLines(lines); return renderToHtml(blocks); @@ -50,8 +53,7 @@ bool visitElementBefore(Element element) { // Hackish. Separate block-level elements with newlines. - if (!buffer.isEmpty && - _BLOCK_TAGS.firstMatch(element.tag) != null) { + if (!buffer.isEmpty && _BLOCK_TAGS.firstMatch(element.tag) != null) { buffer.write('\n'); }
diff --git a/pkgs/markdown/lib/src/inline_parser.dart b/pkgs/markdown/lib/src/inline_parser.dart index c127ede..42850ec 100644 --- a/pkgs/markdown/lib/src/inline_parser.dart +++ b/pkgs/markdown/lib/src/inline_parser.dart
@@ -74,8 +74,7 @@ final List<TagState> _stack; - InlineParser(this.source, this.document) - : _stack = <TagState>[] { + InlineParser(this.source, this.document) : _stack = <TagState>[] { /// User specified syntaxes will be the first syntaxes to be evaluated. if (document.inlineSyntaxes != null) { syntaxes.addAll(document.inlineSyntaxes); @@ -166,8 +165,7 @@ abstract class InlineSyntax { final RegExp pattern; - InlineSyntax(String pattern) - : pattern = new RegExp(pattern, multiLine: true); + InlineSyntax(String pattern) : pattern = new RegExp(pattern, multiLine: true); bool tryMatch(InlineParser parser) { final startMatch = pattern.firstMatch(parser.currentSource); @@ -190,8 +188,8 @@ class TextSyntax extends InlineSyntax { final String substitute; TextSyntax(String pattern, {String sub}) - : super(pattern), - substitute = sub; + : super(pattern), + substitute = sub; bool onMatch(InlineParser parser, Match match) { if (substitute == null) { @@ -208,8 +206,7 @@ /// Matches autolinks like `<http://foo.com>`. class AutolinkSyntax extends InlineSyntax { - AutolinkSyntax() - : super(r'<((http|https|ftp)://[^>]*)>'); + AutolinkSyntax() : super(r'<((http|https|ftp)://[^>]*)>'); // TODO(rnystrom): Make case insensitive. bool onMatch(InlineParser parser, Match match) { @@ -230,14 +227,14 @@ final String tag; TagSyntax(String pattern, {String tag, String end}) - : super(pattern), - endPattern = new RegExp((end != null) ? end : pattern, multiLine: true), - tag = tag; - // TODO(rnystrom): Doing this.field doesn't seem to work with named args. + : super(pattern), + endPattern = new RegExp((end != null) ? end : pattern, multiLine: true), + tag = tag; + // TODO(rnystrom): Doing this.field doesn't seem to work with named args. bool onMatch(InlineParser parser, Match match) { - parser._stack.add(new TagState(parser.pos, - parser.pos + match[0].length, this)); + parser._stack.add( + new TagState(parser.pos, parser.pos + match[0].length, this)); return true; } @@ -258,8 +255,8 @@ /// inline styles as well as optional titles for inline links. To make that /// a bit more palatable, this breaks it into pieces. static get linkPattern { - final refLink = r'\s?\[([^\]]*)\]'; // "[id]" reflink id. - final title = r'(?:[ ]*"([^"]+)"|)'; // Optional title in quotes. + final refLink = r'\s?\[([^\]]*)\]'; // "[id]" reflink id. + final title = r'(?:[ ]*"([^"]+)"|)'; // Optional title in quotes. final inlineLink = '\\s?\\(([^ )]+)$title\\)'; // "(url "title")" link. return '\](?:($refLink|$inlineLink)|)'; @@ -272,7 +269,7 @@ } LinkSyntax({this.linkResolver, String pattern: r'\['}) - : super(pattern, end: linkPattern); + : super(pattern, end: linkPattern); Node createNode(InlineParser parser, Match match, TagState state) { // If we didn't match refLink or inlineLink, then it means there was @@ -288,8 +285,8 @@ if (state.children.any((child) => child is! Text)) return null; // If there are multiple children, but they are all text, send the // combined text to linkResolver. - var textToResolve = state.children.fold('', - (oldVal, child) => oldVal + child.text); + var textToResolve = + state.children.fold('', (oldVal, child) => oldVal + child.text); // See if we have a resolver that will generate a link for us. resolved = true; @@ -323,10 +320,9 @@ var id; // Reference link like [foo] [bar]. if (match[2] == '') - // The id is empty ("[]") so infer it from the contents. - id = parser.source.substring(state.startPos + 1, parser.pos); - else - id = match[2]; + // The id is empty ("[]") so infer it from the contents. + id = parser.source.substring(state.startPos + 1, parser.pos); + else id = match[2]; // References are case-insensitive. id = id.toLowerCase(); @@ -346,8 +342,7 @@ /// `![alternate text][url reference]`. class ImageLinkSyntax extends LinkSyntax { final Resolver linkResolver; - ImageLinkSyntax({this.linkResolver}) - : super(pattern: r'!\['); + ImageLinkSyntax({this.linkResolver}) : super(pattern: r'!\['); Node createNode(InlineParser parser, Match match, TagState state) { var node = super.createNode(parser, match, state); @@ -358,8 +353,8 @@ ..attributes["src"] = node.attributes["href"] ..attributes["title"] = node.attributes["title"] ..attributes["alt"] = node.children - .map((e) => isNullOrEmpty(e) || e is! Text ? '' : e.text) - .join(' '); + .map((e) => isNullOrEmpty(e) || e is! Text ? '' : e.text) + .join(' '); cleanMap(imageElement.attributes); @@ -371,11 +366,9 @@ } } - /// Matches backtick-enclosed inline code blocks. class CodeSyntax extends InlineSyntax { - CodeSyntax(String pattern) - : super(pattern); + CodeSyntax(String pattern) : super(pattern); bool onMatch(InlineParser parser, Match match) { parser.addNode(new Element.text('code', escapeHtml(match[1]))); @@ -398,8 +391,7 @@ /// The children of this node. Will be `null` for text nodes. final List<Node> children; - TagState(this.startPos, this.endPos, this.syntax) - : children = <Node>[]; + TagState(this.startPos, this.endPos, this.syntax) : children = <Node>[]; /// Attempts to close this tag by matching the current text against its end /// pattern.
diff --git a/pkgs/markdown/lib/src/util.dart b/pkgs/markdown/lib/src/util.dart index 2da80da..4b5f6d5 100644 --- a/pkgs/markdown/lib/src/util.dart +++ b/pkgs/markdown/lib/src/util.dart
@@ -3,17 +3,15 @@ /// Replaces `<`, `&`, and `>`, with their HTML entity equivalents. String escapeHtml(String html) { if (html == '' || html == null) return null; - return html.replaceAll('&', '&') - .replaceAll('<', '<') - .replaceAll('>', '>'); + return html + .replaceAll('&', '&') + .replaceAll('<', '<') + .replaceAll('>', '>'); } /// Removes null or empty values from [map]. void cleanMap(Map map) { - map.keys - .where((e) => isNullOrEmpty(map[e])) - .toList() - .forEach(map.remove); + map.keys.where((e) => isNullOrEmpty(map[e])).toList().forEach(map.remove); } /// Returns true if an object is null or an empty string.
diff --git a/pkgs/markdown/test/markdown_test.dart b/pkgs/markdown/test/markdown_test.dart index bb1a7cc..3f7372c 100644 --- a/pkgs/markdown/test/markdown_test.dart +++ b/pkgs/markdown/test/markdown_test.dart
@@ -174,7 +174,6 @@ ''', ''' <h1>header</h1> '''); - }); group('Unordered lists', () { @@ -863,9 +862,9 @@ }); group('Inline Images', () { - validate('image',''' + validate('image', '''  - ''',''' + ''', ''' <p> <a href="http://foo.com/foo.png"> <img src="http://foo.com/foo.png"></img> @@ -873,9 +872,9 @@ </p> '''); - validate('alternate text',''' + validate('alternate text', '''  - ''',''' + ''', ''' <p> <a href="http://foo.com/foo.png"> <img alt="alternate text" src="http://foo.com/foo.png"></img> @@ -883,18 +882,18 @@ </p> '''); - validate('title',''' + validate('title', '''  - ''',''' + ''', ''' <p> <a href="http://foo.com/foo.png" title="optional title"> <img src="http://foo.com/foo.png" title="optional title"></img> </a> </p> '''); - validate('invalid alt text',''' + validate('invalid alt text', '''  - ''',''' + ''', ''' <p> <a href="http://foo.com/foo.png"> <img src="http://foo.com/foo.png"></img> @@ -904,10 +903,10 @@ }); group('Reference Images', () { - validate('image',''' + validate('image', ''' ![][foo] [foo]: http://foo.com/foo.png - ''',''' + ''', ''' <p> <a href="http://foo.com/foo.png"> <img src="http://foo.com/foo.png"></img> @@ -915,10 +914,10 @@ </p> '''); - validate('alternate text',''' + validate('alternate text', ''' ![alternate text][foo] [foo]: http://foo.com/foo.png - ''',''' + ''', ''' <p> <a href="http://foo.com/foo.png"> <img alt="alternate text" src="http://foo.com/foo.png"></img> @@ -926,10 +925,10 @@ </p> '''); - validate('title',''' + validate('title', ''' ![][foo] [foo]: http://foo.com/foo.png "optional title" - ''',''' + ''', ''' <p> <a href="http://foo.com/foo.png" title="optional title"> <img src="http://foo.com/foo.png" title="optional title"></img> @@ -937,17 +936,16 @@ </p> '''); - validate('invalid alt text',''' + validate('invalid alt text', ''' ![`alt`][foo] [foo]: http://foo.com/foo.png "optional title" - ''',''' + ''', ''' <p> <a href="http://foo.com/foo.png" title="optional title"> <img src="http://foo.com/foo.png" title="optional title"></img> </a> </p> '''); - }); group('Resolver', () { @@ -965,8 +963,9 @@ }); group('Custom inline syntax', () { - List<InlineSyntax> nyanSyntax = - [new TextSyntax('nyan', sub: '~=[,,_,,]:3')]; + List<InlineSyntax> nyanSyntax = [ + new TextSyntax('nyan', sub: '~=[,,_,,]:3') + ]; validate('simple inline syntax', ''' nyan ''', ''' @@ -974,9 +973,9 @@ ''', inlineSyntaxes: nyanSyntax); validate('dart custom links', 'links [are<foo>] awesome', - '<p>links <a>are<foo></a> awesome</p>', - linkResolver: (text) => new Element.text('a', text.replaceAll('<', - '<'))); + '<p>links <a>are<foo></a> awesome</p>', + linkResolver: (text) => new Element.text( + 'a', text.replaceAll('<', '<'))); // TODO(amouravski): need more tests here for custom syntaxes, as some // things are not quite working properly. The regexps are sometime a little @@ -1053,14 +1052,17 @@ } void validate(String description, String markdown, String html, - {bool verbose: false, List<InlineSyntax> inlineSyntaxes, - Resolver linkResolver, Resolver imageLinkResolver, bool inlineOnly: false}) { + {bool verbose: false, List<InlineSyntax> inlineSyntaxes, + Resolver linkResolver, Resolver imageLinkResolver, + bool inlineOnly: false}) { test(description, () { markdown = cleanUpLiteral(markdown); html = cleanUpLiteral(html); - var result = markdownToHtml(markdown, inlineSyntaxes: inlineSyntaxes, - linkResolver: linkResolver, imageLinkResolver: imageLinkResolver, + var result = markdownToHtml(markdown, + inlineSyntaxes: inlineSyntaxes, + linkResolver: linkResolver, + imageLinkResolver: imageLinkResolver, inlineOnly: inlineOnly); var passed = compareOutput(html, result);