From f3fceb169684e8c7dd06bf64f1d9fb75396d8c00 Mon Sep 17 00:00:00 2001 From: Maxim Dikun Date: Sat, 8 Aug 2015 12:37:49 +0300 Subject: [PATCH] Add linter to build. Fix linter errors. --- README.md | 16 +++--- lib/{src => }/definitions.dart | 36 +++++++------- lib/{src => }/entities.dart | 0 lib/{src => }/html_writer.dart | 4 +- lib/{src => }/markdown_parser.dart | 73 +++++++++++++--------------- lib/{src => }/markdown_writer.dart | 28 +++++------ lib/md_proc.dart | 20 ++++---- lib/options.dart | 27 +++++++++++ lib/src/options.dart | 31 ------------ pubspec.yaml | 6 ++- test/individual_parsers.dart | 2 +- test/library_test.dart | 10 ++-- test/parser.dart | 26 +++++----- test/reference_resolver.dart | 4 +- test/service.dart | 78 +++++++++++++++--------------- tool/travis.sh | 7 ++- 16 files changed, 180 insertions(+), 188 deletions(-) rename lib/{src => }/definitions.dart (91%) rename lib/{src => }/entities.dart (100%) rename lib/{src => }/html_writer.dart (98%) rename lib/{src => }/markdown_parser.dart (96%) rename lib/{src => }/markdown_writer.dart (96%) create mode 100644 lib/options.dart delete mode 100644 lib/src/options.dart diff --git a/README.md b/README.md index f16d4f5..b9ce811 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Parsing import "package:md_proc/md_proc.dart"; void main() { - Document doc = CommonMarkParser.DEFAULT.parse('Hello world!\n==='); + Document doc = CommonMarkParser.defaults.parse('Hello world!\n==='); print(doc); // Document [SetextHeader 1 [Str "Hello", Space, Str "world", Str "!"]] } ``` @@ -37,8 +37,8 @@ Writing html import "package:md_proc/md_proc.dart"; void main() { - Document doc = CommonMarkParser.DEFAULT.parse('Hello world!\n==='); - String res = HtmlWriter.DEFAULT.write(doc); + Document doc = CommonMarkParser.defaults.parse('Hello world!\n==='); + String res = HtmlWriter.defaults.write(doc); print(res); //

Hello world!

} ``` @@ -50,8 +50,8 @@ Writing markdown import "package:md_proc/md_proc.dart"; void main() { - Document doc = CommonMarkParser.DEFAULT.parse('Hello world!\n==='); - String res = MarkdownWriter.DEFAULT.write(doc); + Document doc = CommonMarkParser.defaults.parse('Hello world!\n==='); + String res = MarkdownWriter.defaults.write(doc); print(res); // Hello world! // ============ } @@ -69,8 +69,8 @@ By default smart punctuation is enabled. To disable it use STRICT version of par import "package:md_proc/md_proc.dart"; void main() { - Document doc = CommonMarkParser.STRICT.parse('...'); // STRICT here - String res = HtmlWriter.STRICT.write(doc); // and here + Document doc = CommonMarkParser.strict.parse('...'); // STRICT here + String res = HtmlWriter.strict.write(doc); // and here print(res); //

...

} ``` @@ -104,7 +104,7 @@ Target linkResolver(String normalizedReference, String reference) { CommonMarkParser parser = new CommonMarkParser(new Options(linkResolver: linkResolver)); Document doc = parser.parse('Hello world!\n==='); -String res = HtmlWriter.DEFAULT.write(doc); +String res = HtmlWriter.defaults.write(doc); ``` High-level plan for development diff --git a/lib/src/definitions.dart b/lib/definitions.dart similarity index 91% rename from lib/src/definitions.dart rename to lib/definitions.dart index e0495d5..2f9942d 100644 --- a/lib/src/definitions.dart +++ b/lib/definitions.dart @@ -62,7 +62,7 @@ class Target { Target(this.link, this.title); - String toString() => 'Target "${link}" ${title == null ? "null" : "\"${title}\""}'; + String toString() => 'Target "$link" ${title == null ? "null" : "\"$title\""}'; bool operator== (obj) => obj is Target && link == obj.link && title == obj.title; @@ -131,8 +131,8 @@ class SetextHeader extends Header { class FenceType { - static const FenceType BacktickFence = const FenceType._(0, "BacktickFence"); - static const FenceType TildeFence = const FenceType._(1, "TildeFence"); + static const FenceType backtick = const FenceType._(0, "backtick"); + static const FenceType tilde = const FenceType._(1, "tilde"); final int value; final String name; @@ -167,7 +167,7 @@ class IndentedCodeBlock extends CodeBlock { class FencedCodeBlock extends CodeBlock { FenceType fenceType; int fenceSize; - FencedCodeBlock(String contents, {this.fenceType: FenceType.BacktickFence, this.fenceSize: 3, Attr attributes}) + FencedCodeBlock(String contents, {this.fenceType: FenceType.backtick, this.fenceSize: 3, Attr attributes}) : super(contents, attributes == null ? new EmptyAttr() : attributes); String toString() => "FencedCodeBlock $attributes $contents"; @@ -222,9 +222,9 @@ class ListItem { class BulletType { - static const BulletType MinusBullet = const BulletType._(0, "MinusBullet", "-"); - static const BulletType PlusBullet = const BulletType._(1, "PlusBullet", "+"); - static const BulletType StarBullet = const BulletType._(2, "StarBullet", "*"); + static const BulletType minus = const BulletType._(0, "minus", "-"); + static const BulletType plus = const BulletType._(1, "plus", "+"); + static const BulletType star = const BulletType._(2, "star", "*"); final int value; final String name; @@ -236,20 +236,20 @@ class BulletType { BulletType type; switch(markerChar) { case '+': - type = BulletType.PlusBullet; + type = BulletType.plus; break; case '-': - type = BulletType.MinusBullet; + type = BulletType.minus; break; case '*': - type = BulletType.StarBullet; + type = BulletType.star; break; default: assert(false); - type = BulletType.PlusBullet; + type = BulletType.plus; } return type; @@ -263,8 +263,8 @@ class BulletType { class IndexSeparator { - static const IndexSeparator DotSeparator = const IndexSeparator._(0, "DotSeparator", "."); - static const IndexSeparator ParenthesisSeparator = const IndexSeparator._(1, "ParenthesisSeparator", ")"); + static const IndexSeparator dot = const IndexSeparator._(0, "dot", "."); + static const IndexSeparator parenthesis = const IndexSeparator._(1, "parenthesis", ")"); final int value; final String name; @@ -276,16 +276,16 @@ class IndexSeparator { IndexSeparator separator; switch(indexSeparator) { case '.': - separator = IndexSeparator.DotSeparator; + separator = IndexSeparator.dot; break; case ')': - separator = IndexSeparator.ParenthesisSeparator; + separator = IndexSeparator.parenthesis; break; default: assert(false); - separator = IndexSeparator.DotSeparator; + separator = IndexSeparator.dot; } return separator; @@ -310,7 +310,7 @@ abstract class ListBlock extends Block { class UnorderedList extends ListBlock { BulletType bulletType; - UnorderedList(Iterable items, {this.bulletType: BulletType.MinusBullet, bool tight: false}) + UnorderedList(Iterable items, {this.bulletType: BulletType.minus, bool tight: false}) : super(items, tight); String toString() => "UnorderedList $bulletType $items"; @@ -327,7 +327,7 @@ class OrderedList extends ListBlock { int startIndex; OrderedList(Iterable items, {bool tight: false, - this.indexSeparator: IndexSeparator.DotSeparator, this.startIndex: 1}) : super(items, tight); + this.indexSeparator: IndexSeparator.dot, this.startIndex: 1}) : super(items, tight); String toString() => "OrderedList start=$startIndex $indexSeparator $items"; diff --git a/lib/src/entities.dart b/lib/entities.dart similarity index 100% rename from lib/src/entities.dart rename to lib/entities.dart diff --git a/lib/src/html_writer.dart b/lib/html_writer.dart similarity index 98% rename from lib/src/html_writer.dart rename to lib/html_writer.dart index 77812e1..0921e0a 100644 --- a/lib/src/html_writer.dart +++ b/lib/html_writer.dart @@ -335,6 +335,6 @@ class HtmlWriter { return builder.toString(); } - static HtmlWriter STRICT = new HtmlWriter(Options.STRICT); - static HtmlWriter DEFAULT = new HtmlWriter(Options.DEFAULT); + static HtmlWriter strict = new HtmlWriter(Options.strict); + static HtmlWriter defaults = new HtmlWriter(Options.defaults); } diff --git a/lib/src/markdown_parser.dart b/lib/markdown_parser.dart similarity index 96% rename from lib/src/markdown_parser.dart rename to lib/markdown_parser.dart index f3ddd2c..17e73b0 100644 --- a/lib/src/markdown_parser.dart +++ b/lib/markdown_parser.dart @@ -8,8 +8,6 @@ import 'entities.dart'; import 'options.dart'; -const TAB_STOP = 4; - class _UnparsedInlines extends Inlines { String raw; @@ -64,7 +62,7 @@ class _EmphasisStackItem { // CommonMark parser class CommonMarkParser { - static const int TAB_STOP = 4; + static const int tabStop = 4; Options _options; @@ -81,7 +79,7 @@ class CommonMarkParser { if (!s.endsWith("\n")) { s += "\n"; } - var doc = document.parse(s, tabStop: TAB_STOP); + var doc = document.parse(s, tabStop: tabStop); _inlinesInDocument(doc); return doc; @@ -153,7 +151,7 @@ class CommonMarkParser { Inlines _parseInlines(String raw) { - return inlines.parse(raw, tabStop: TAB_STOP); + return inlines.parse(raw, tabStop: tabStop); } @@ -225,9 +223,9 @@ class CommonMarkParser { } var newPos; if (offset < len && s[offset] == '\n') { - newPos = new Position(offset + 1, pos.line + 1, 1, tabStop: TAB_STOP); + newPos = new Position(offset + 1, pos.line + 1, 1, tabStop: tabStop); } else { - newPos = new Position(offset, pos.line, pos.character + result.length, tabStop: TAB_STOP); + newPos = new Position(offset, pos.line, pos.character + result.length, tabStop: tabStop); } return new ParseResult(s, new Expectations.empty(newPos), newPos, true, false, result); }); @@ -243,12 +241,12 @@ class CommonMarkParser { // that were actually skipped. // TODO test all parsers that use skipNonindentSpaces, skipListNonindentSpaces, indentSpaces, atMostSpaces // TODO rename indentSpaces => indent, atMostSpaces => atMostIndent - static final Parser skipNonindentChars = atMostIndent(TAB_STOP - 1).notFollowedBy(whitespaceChar); + static final Parser skipNonindentChars = atMostIndent(tabStop - 1).notFollowedBy(whitespaceChar); static final Parser skipNonindentCharsFromAnyPosition = - atMostIndent(TAB_STOP - 1, fromLineStart: false).notFollowedBy(whitespaceChar); - static Parser skipListIndentChars(int max) => (atMostIndent(max - 1) | atMostIndent(TAB_STOP - 1, fromLineStart: false)).notFollowedBy(whitespaceChar); + atMostIndent(tabStop - 1, fromLineStart: false).notFollowedBy(whitespaceChar); + static Parser skipListIndentChars(int max) => (atMostIndent(max - 1) | atMostIndent(tabStop - 1, fromLineStart: false)).notFollowedBy(whitespaceChar); static Parser spnl = (skipSpaces > newline); - static Parser get indent => waitForIndent(TAB_STOP) % "indentation"; + static Parser get indent => waitForIndent(tabStop) % "indentation"; static Parser atMostIndent(int indent, {bool fromLineStart: true}) => new Parser((String s, Position pos) { if (fromLineStart && pos.character != 1) { @@ -737,7 +735,7 @@ class CommonMarkParser { if (isLink && labelRes.value.contains(new RegExp(r"^\s*$"))) { return fail.run(s, pos); } - Inlines linkInlines = inlines.parse(labelRes.value, tabStop: TAB_STOP); + Inlines linkInlines = inlines.parse(labelRes.value, tabStop: tabStop); if (isLink && _isContainsLink(linkInlines)) { List resValue = [new Str('[')]; resValue.addAll(linkInlines); @@ -1108,9 +1106,9 @@ class CommonMarkParser { int fenceSize = openFenceRes.value[2]; String infoString = openFenceRes.value[3]; - FenceType fenceType = FenceType.BacktickFence; + FenceType fenceType = FenceType.backtick; if (fenceChar == '~') { - fenceType = FenceType.TildeFence; + fenceType = FenceType.tilde; } Parser lineParser = anyLine; @@ -1358,7 +1356,7 @@ class CommonMarkParser { void buildBuffer() { String s = buffer.map((l) => l + "\n").join(); - List innerRes = (block.manyUntil(eof) ^ (res) => processParsedBlocks(res)).parse(s, tabStop: TAB_STOP); + List innerRes = (block.manyUntil(eof) ^ (res) => processParsedBlocks(res)).parse(s, tabStop: tabStop); if (!closeParagraph && innerRes.length > 0 && innerRes.first is Para) { var first = innerRes.first; if (_acceptLazy(blocks, first.contents.raw)) { @@ -1385,7 +1383,7 @@ class CommonMarkParser { } else { if (buffer.length > 0) { buildBuffer(); - List lineBlock = block.parse(line + "\n", tabStop: TAB_STOP); + List lineBlock = block.parse(line + "\n", tabStop: tabStop); // TODO fix condition if (!closeParagraph && lineBlock.length == 1 && lineBlock[0] is Para) { var block = lineBlock[0] as Para; @@ -1412,11 +1410,8 @@ class CommonMarkParser { // Lists // - // TODO tabs after marker - // 1.\tSomething - // -\t\tCode - static const _LIST_TYPE_ORDERED = 0; - static const _LIST_TYPE_UNORDERED = 1; + static const _listTypeOrdered = 0; + static const _listTypeUnordered = 1; static ParserAccumulator3 orderedListMarkerTest(int indent) => skipListIndentChars(indent) + countBetween(1, 9, digit) + // 1-9 digits @@ -1426,8 +1421,8 @@ class CommonMarkParser { oneOf('-+*'); static Parser listMarkerTest(int indent) => ( ( - (orderedListMarkerTest(indent) ^ (sp, d, c) => [_LIST_TYPE_ORDERED, sp, d, c]) | - (unorderedListMarkerTest(indent) ^ (sp, c) => [_LIST_TYPE_UNORDERED, sp, c]) + (orderedListMarkerTest(indent) ^ (sp, d, c) => [_listTypeOrdered, sp, d, c]) | + (unorderedListMarkerTest(indent) ^ (sp, c) => [_listTypeUnordered, sp, c]) ) + ( char("\n") | @@ -1472,7 +1467,7 @@ class CommonMarkParser { } if (!getTight()) { - innerBlocks = (block.manyUntil(eof) ^ (res) => processParsedBlocks(res)).parse(s, tabStop: TAB_STOP); + innerBlocks = (block.manyUntil(eof) ^ (res) => processParsedBlocks(res)).parse(s, tabStop: tabStop); } if (!afterEmptyLine && innerBlocks.length > 0 && innerBlocks.first is Para && _acceptLazy(blocks, ((innerBlocks.first as Para).contents as _UnparsedInlines).raw)) { @@ -1500,10 +1495,10 @@ class CommonMarkParser { return false; } ListBlock block = stack.last.block; - if (type == _LIST_TYPE_ORDERED && block is OrderedList && block.indexSeparator == indexSeparator) { + if (type == _listTypeOrdered && block is OrderedList && block.indexSeparator == indexSeparator) { success = true; } - if (type == _LIST_TYPE_UNORDERED && block is UnorderedList && block.bulletType == bulletType) { + if (type == _listTypeUnordered && block is UnorderedList && block.bulletType == bulletType) { success = true; } if (success) { @@ -1531,7 +1526,7 @@ class CommonMarkParser { } else { int diff = res.value[1].length - 1; return new Position(res.position.offset - diff, res.position.line, res.position.character - diff, - tabStop: TAB_STOP); + tabStop: tabStop); } } @@ -1581,7 +1576,7 @@ class CommonMarkParser { // TODO Speedup by checking impossible starts ParseResult lineRes = anyLine.run(s, position); assert(lineRes.isSuccess); - List lineBlock = block.parse(lineRes.value.trimLeft() + "\n", tabStop: TAB_STOP); + List lineBlock = block.parse(lineRes.value.trimLeft() + "\n", tabStop: tabStop); if ( lineBlock.length == 1 && lineBlock[0] is Para && @@ -1615,12 +1610,12 @@ class CommonMarkParser { // Trying to find new list item - ParseResult markerRes = listMarkerTest(getIndent() + TAB_STOP).run(s, position); + ParseResult markerRes = listMarkerTest(getIndent() + tabStop).run(s, position); if (markerRes.isSuccess) { int type = markerRes.value[0][0]; - IndexSeparator indexSeparator = (type == _LIST_TYPE_ORDERED ? IndexSeparator.fromChar(markerRes.value[0][3]) : null); - int startIndex = type == _LIST_TYPE_ORDERED ? int.parse(markerRes.value[0][2].join(), onError: (_) => 1) : 1; - BulletType bulletType = (type == _LIST_TYPE_UNORDERED ? BulletType.fromChar(markerRes.value[0][2]) : null); + IndexSeparator indexSeparator = (type == _listTypeOrdered ? IndexSeparator.fromChar(markerRes.value[0][3]) : null); + int startIndex = type == _listTypeOrdered ? int.parse(markerRes.value[0][2].join(), onError: (_) => 1) : 1; + BulletType bulletType = (type == _listTypeUnordered ? BulletType.fromChar(markerRes.value[0][2]) : null); // It's a new list item on same level if (!nextLevel) { @@ -1636,7 +1631,7 @@ class CommonMarkParser { int subIndent = markerRes.position.character - 1; if (markerRes.value[1] == "\n") { subIndent = position.character + markerRes.value[0][1] + 1; // marker + space after marker - char - if (type == _LIST_TYPE_ORDERED) { + if (type == _listTypeOrdered) { subIndent += markerRes.value[0][2].length; } } @@ -1664,11 +1659,11 @@ class CommonMarkParser { int subIndent = markerRes.position.character - 1; if (markerRes.value[1] == "\n") { subIndent = position.character + markerRes.value[0][1] + 1; // marker + space after marker - char - if (type == _LIST_TYPE_ORDERED) { + if (type == _listTypeOrdered) { subIndent += markerRes.value[0][2].length; } } - if (type == _LIST_TYPE_ORDERED) { + if (type == _listTypeOrdered) { newListBlock = new OrderedList([new ListItem([])], tight: true, indexSeparator: indexSeparator, startIndex: startIndex); //subIndent += markerRes.value[0][2].length; @@ -1712,9 +1707,9 @@ class CommonMarkParser { int fenceSize = openFenceRes.value[2]; String infoString = openFenceRes.value[3]; - FenceType fenceType = FenceType.BacktickFence; + FenceType fenceType = FenceType.backtick; if (fenceChar == '~') { - fenceType = FenceType.TildeFence; + fenceType = FenceType.tilde; } position = openFenceRes.position; @@ -1798,6 +1793,6 @@ class CommonMarkParser { Parser get document => (block.manyUntil(eof) ^ (res) => new Document(processParsedBlocks(res))) % "document"; - static CommonMarkParser DEFAULT = new CommonMarkParser(Options.DEFAULT); - static CommonMarkParser STRICT = new CommonMarkParser(Options.STRICT); + static CommonMarkParser defaults = new CommonMarkParser(Options.defaults); + static CommonMarkParser strict = new CommonMarkParser(Options.strict); } diff --git a/lib/src/markdown_writer.dart b/lib/markdown_writer.dart similarity index 96% rename from lib/src/markdown_writer.dart rename to lib/markdown_writer.dart index 30bb22c..dc671d0 100644 --- a/lib/src/markdown_writer.dart +++ b/lib/markdown_writer.dart @@ -57,7 +57,7 @@ class _EscapeContext { ); } - static const _EscapeContext EMPTY = const _EscapeContext(); + static const _EscapeContext empty = const _EscapeContext(); } @@ -69,7 +69,7 @@ class _NotCheckedPart extends _InlinePart { Options _options; CommonMarkParser _parser; - _NotCheckedPart(String content, this._options, [this.context = _EscapeContext.EMPTY]) : super(content) { + _NotCheckedPart(String content, this._options, [this.context = _EscapeContext.empty]) : super(content) { _parser = new CommonMarkParser(_options, {}); } @@ -233,9 +233,7 @@ class _InlineRenderer { _InlineRenderer(this._references, this._options) : parts = <_InlinePart>[]; - /** - * If context == null then token doesn't require escaping - */ + /// If context == null then token doesn't require escaping void write(String str, [_EscapeContext context]) { if (parts.length == 0) { parts.add(context == null ? new _CheckedPart(str) : new _NotCheckedPart(str, _options, context)); @@ -255,7 +253,7 @@ class _InlineRenderer { } - void writeInlines(Iterable inlines, {String prevEmph, _EscapeContext context: _EscapeContext.EMPTY}) { + void writeInlines(Iterable inlines, {String prevEmph, _EscapeContext context: _EscapeContext.empty}) { if (inlines.length == 1 && prevEmph != null) { if (inlines.first is Emph) { writeEmph(inlines.first, delimiter: prevEmph == "*" ? "_" : "*", context: context); @@ -329,7 +327,7 @@ class _InlineRenderer { write(fence); } - void writeEmph(Emph emph, {String delimiter: "*", _EscapeContext context: _EscapeContext.EMPTY}) { + void writeEmph(Emph emph, {String delimiter: "*", _EscapeContext context: _EscapeContext.empty}) { if (delimiter == "*" && !context.escapeStar) { context = context.copy(escapeStar: true); } else if (delimiter == "_" && !context.escapeUnderscore) { @@ -341,7 +339,7 @@ class _InlineRenderer { write(delimiter); } - void writeStrong(Strong strong, {String delimiter: "*", _EscapeContext context: _EscapeContext.EMPTY}) { + void writeStrong(Strong strong, {String delimiter: "*", _EscapeContext context: _EscapeContext.empty}) { if (delimiter == "*" && !context.escapeStar) { context = context.copy(escapeStar: true); } else if (delimiter == "_" && !context.escapeUnderscore) { @@ -355,7 +353,7 @@ class _InlineRenderer { } - void writeSmartQuote(SmartQuote quote, {_EscapeContext context: _EscapeContext.EMPTY}) { + void writeSmartQuote(SmartQuote quote, {_EscapeContext context: _EscapeContext.empty}) { if (quote.open) { write(quote.single ? "'" : '"'); } @@ -573,7 +571,7 @@ class _MarkdownBuilder extends StringBuffer { void writeCodeBlock(CodeBlock codeBlock) { if (codeBlock is FencedCodeBlock) { - String fence = (codeBlock.fenceType == FenceType.BacktickFence ? '`' : '~') * codeBlock.fenceSize; + String fence = (codeBlock.fenceType == FenceType.backtick ? '`' : '~') * codeBlock.fenceSize; write(fence); if (codeBlock.attributes is InfoString) { var attributes = codeBlock.attributes; @@ -674,7 +672,7 @@ class _MarkdownBuilder extends StringBuffer { } } - void writeInlines(Iterable inlines, {_EscapeContext context: _EscapeContext.EMPTY}) { + void writeInlines(Iterable inlines, {_EscapeContext context: _EscapeContext.empty}) { _InlineRenderer renderer = new _InlineRenderer(_references, _options); renderer.writeInlines(inlines, context: context); write(renderer.toString()); @@ -699,9 +697,9 @@ class _MarkdownBuilder extends StringBuffer { class MarkdownWriter { - Options _options; + final Options _options; - MarkdownWriter(this._options); + const MarkdownWriter(this._options); String write(Document document) { _MarkdownBuilder builder = new _MarkdownBuilder({}, _options); @@ -710,6 +708,6 @@ class MarkdownWriter { return builder.toString(); } - static MarkdownWriter STRICT = new MarkdownWriter(Options.STRICT); - static MarkdownWriter DEFAULT = new MarkdownWriter(Options.DEFAULT); + static const MarkdownWriter strict = const MarkdownWriter(Options.strict); + static const MarkdownWriter defaults = const MarkdownWriter(Options.defaults); } diff --git a/lib/md_proc.dart b/lib/md_proc.dart index 00c9c9e..6c3a292 100644 --- a/lib/md_proc.dart +++ b/lib/md_proc.dart @@ -1,17 +1,15 @@ library md_proc; -import 'src/html_writer.dart'; -import 'src/markdown_parser.dart'; +import 'html_writer.dart'; +import 'markdown_parser.dart'; -export 'src/definitions.dart'; -export 'src/html_writer.dart'; -export 'src/markdown_parser.dart'; -export 'src/markdown_writer.dart'; -export 'src/options.dart'; +export 'definitions.dart'; +export 'html_writer.dart'; +export 'markdown_parser.dart'; +export 'markdown_writer.dart'; +export 'options.dart'; -/** - * Converts markdown string to html string. - */ +/// Converts markdown string to html string. String markdownToHtml(String markdown) { - return HtmlWriter.DEFAULT.write(CommonMarkParser.DEFAULT.parse(markdown)); + return HtmlWriter.defaults.write(CommonMarkParser.defaults.parse(markdown)); } diff --git a/lib/options.dart b/lib/options.dart new file mode 100644 index 0000000..14a7741 --- /dev/null +++ b/lib/options.dart @@ -0,0 +1,27 @@ +library md_proc.options; + +import 'definitions.dart'; + +/// Link resolver accepts two references and should return [Target] with correspondent link. +/// If link doesn't exists link resolver should return `null`. +/// +/// CommonMark defines reference as case insensitive. Use [normalizedReference] when you need reference +/// normalized according to CommonMark rules, or just [reference] if you want to get reference as it +/// written in document. +typedef Target LinkResolver(String normalizedReference, String reference); + +/// Default resolver doesn't return any link, so be default parser parses only explicitly written references. +Target defaultLinkResolver(String normalizedReference, String reference) => null; + +class Options { + final bool smartPunctuation; + final LinkResolver linkResolver; + + const Options({ + this.smartPunctuation: false, + this.linkResolver: defaultLinkResolver + }); + + static const Options defaults = const Options(smartPunctuation: true); + static const Options strict = const Options(); +} diff --git a/lib/src/options.dart b/lib/src/options.dart deleted file mode 100644 index 075731e..0000000 --- a/lib/src/options.dart +++ /dev/null @@ -1,31 +0,0 @@ -library md_proc.options; - -import 'definitions.dart'; - -/** - * Link resolver accepts two references and should return [Target] with correspondent link. - * If link doesn't exists link resolver should return `null`. - * - * CommonMark defines reference as case insensitive. Use [normalizedReference] when you need reference - * normalized according to CommonMark rules, or just [reference] if you want to get reference as it - * written in document. - */ -typedef Target LinkResolver(String normalizedReference, String reference); - -/** - * Default resolver doesn't return any link, so be default parser parses only explicitly written references. - */ -Target DEFAULT_LINK_RESOLVER(String normalizedReference, String reference) => null; - -class Options { - bool smartPunctuation; - LinkResolver linkResolver; - - Options({ - this.smartPunctuation: false, - this.linkResolver: DEFAULT_LINK_RESOLVER - }); - - static Options DEFAULT = new Options(smartPunctuation: true); - static Options STRICT = new Options(); -} diff --git a/pubspec.yaml b/pubspec.yaml index f3eaaa8..399ad72 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,14 +1,16 @@ name: md_proc -version: 0.5.1 +version: 0.6.0 description: CommonMark-compliant markdown parser author: Maxim Dikun homepage: https://github.com/dikmax/md_proc environment: - sdk: ">=0.3.4" + sdk: ">=1.11.3" dependencies: collection: '>=1.0.0 <2.0.0' unicode_helper: '>=0.2.0 <0.3.0' parsers: '>=0.14.4 <0.15.0' dev_dependencies: test: any + linter: any + dart_coveralls: any diff --git a/test/individual_parsers.dart b/test/individual_parsers.dart index 0138bd5..bdeee82 100644 --- a/test/individual_parsers.dart +++ b/test/individual_parsers.dart @@ -1,4 +1,4 @@ -library individualParsersTest; +library md_proc.test.individual_parsers; import 'package:test/test.dart'; import 'package:parsers/parsers.dart'; diff --git a/test/library_test.dart b/test/library_test.dart index 49bf2af..d0c1777 100644 --- a/test/library_test.dart +++ b/test/library_test.dart @@ -1,4 +1,4 @@ -import 'package:md_proc/src/options.dart'; +import 'package:md_proc/options.dart'; import 'individual_parsers.dart'; import 'parser.dart'; @@ -15,14 +15,14 @@ void main() { //fileTest("CommonMark", "spec.txt", mdToHtmlTest(Options.STRICT, (t, num) => t == TestType.HTML && (num == 129))); //fileTest("CommonMark", "spec.txt", mdToHtmlTest(Options.STRICT, (t, num) => t == TestType.HTML && (num >= 103 && num < 144))); //fileTest("CommonMark", "spec.txt", mdToHtmlTest(Options.STRICT, (t, num) => t == TestType.HTML)); - fileTest("CommonMark", "spec.txt", mdToHtmlTest(Options.STRICT)); + fileTest("CommonMark", "spec.txt", mdToHtmlTest(Options.strict)); // Additional tests //fileTest("Additional", "additionalMarkdownToHtml.txt", mdToHtmlTest(Options.STRICT, (t, num) => t == TestType.HTML && num == 19)); - fileTest("Additional", "additionalMarkdownToHtml.txt", mdToHtmlTest(Options.STRICT)); + fileTest("Additional", "additionalMarkdownToHtml.txt", mdToHtmlTest(Options.strict)); // Additional tests - fileTest("SmartPunct", "smart_punct.txt", mdToHtmlTest(Options.DEFAULT)); + fileTest("SmartPunct", "smart_punct.txt", mdToHtmlTest(Options.defaults)); // Markdown to markdown tests - fileTest("md2md", "markdownToMarkdown.txt", mdToMdTest(Options.STRICT)); + fileTest("md2md", "markdownToMarkdown.txt", mdToMdTest(Options.strict)); // Custom resolver referenceResolverTests(); } diff --git a/test/parser.dart b/test/parser.dart index 31daebc..3ec6f1f 100644 --- a/test/parser.dart +++ b/test/parser.dart @@ -1,16 +1,16 @@ -library parserTest; +library md_proc.test.parser; import 'dart:io'; import 'package:test/test.dart' as t; import 'package:md_proc/md_proc.dart'; -import 'package:md_proc/src/markdown_writer.dart'; +import 'package:md_proc/markdown_writer.dart'; -const int STATE_WAIT = 0; -const int STATE_MARKDOWN = 1; -const int STATE_HTML = 2; +const int stateWait = 0; +const int stateMarkdown = 1; +const int stateHtml = 2; enum TestType { - HTML, MARKDOWN + html, markdown } typedef bool FilterFunc(TestType type, int num); @@ -21,7 +21,7 @@ Map readFile(fileName) { Map result = {}; File file = new File(fileName); - int state = STATE_WAIT; + int state = stateWait; List destination = []; List source = []; List lines = file.readAsLinesSync(); @@ -30,13 +30,13 @@ Map readFile(fileName) { state++; if (state == 3) { result[source.map((line) => line + "\n").join()] = destination.map((line) => line + "\n").join(); - state = STATE_WAIT; + state = stateWait; destination = []; source = []; } - } else if (state == STATE_MARKDOWN) { + } else if (state == stateMarkdown) { source.add(line); - } else if (state == STATE_HTML) { + } else if (state == stateHtml) { destination.add(line); } } @@ -142,13 +142,13 @@ TestFunc mdToHtmlTest(Options options, [FilterFunc filter = emptyFilter]) => (in String md = mdOrig.replaceAll("→", "\t").replaceAll("␣", " "); html = html.replaceAll("→", "\t").replaceAll("␣", " "); - if (filter(TestType.HTML, num)) { + if (filter(TestType.html, num)) { t.test('html $num', () { Document doc = parser.parse(md); t.expect(tidy(writer.write(doc)), new ExampleDescription(t.equals(tidy(html)), mdOrig)); }); } - if (filter(TestType.MARKDOWN, num)) { + if (filter(TestType.markdown, num)) { t.test('markdown $num', () { var generatedMarkdown = mdWriter.write(parser.parse(md)); Document doc = parser.parse(generatedMarkdown); @@ -161,7 +161,7 @@ TestFunc mdToMdTest(Options options, [FilterFunc filter = emptyFilter]) => (int CommonMarkParser parser = new CommonMarkParser(options); MarkdownWriter writer = new MarkdownWriter(options); - if (filter(TestType.MARKDOWN, num)) { + if (filter(TestType.markdown, num)) { t.test(num.toString(), () { var generatedMarkdown = writer.write(parser.parse(md)); t.expect(generatedMarkdown, new ExampleDescription(t.equals(destMd), md)); diff --git a/test/reference_resolver.dart b/test/reference_resolver.dart index a450267..524bf8f 100644 --- a/test/reference_resolver.dart +++ b/test/reference_resolver.dart @@ -1,4 +1,4 @@ -library referenceResolverTest; +library md_proc.test.reference_resolver; import 'package:test/test.dart' as t; @@ -45,7 +45,7 @@ This is a [link]. void referenceResolverTests() { CommonMarkParser parser = new CommonMarkParser(new Options(linkResolver: linkResolver)); - CommonMarkParser defaultParser = CommonMarkParser.DEFAULT; + CommonMarkParser defaultParser = CommonMarkParser.defaults; t.group("Custom reference resolver test", () { t.test("Should leave defined links as is", () { diff --git a/test/service.dart b/test/service.dart index cae136d..08ef521 100644 --- a/test/service.dart +++ b/test/service.dart @@ -1,4 +1,4 @@ -library serviceTest; +library md_proc.test.service; import 'package:test/test.dart' as t; @@ -11,25 +11,25 @@ void serviceTests() { }); t.test('Parsing', () { - t.expect(CommonMarkParser.DEFAULT.parse('Hello world!\n===').toString(), + t.expect(CommonMarkParser.defaults.parse('Hello world!\n===').toString(), t.equals('Document [SetextHeader 1 [Str "Hello", Space, Str "world", Str "!"]]')); }); t.test('Writing html', () { - Document doc = CommonMarkParser.DEFAULT.parse('Hello world!\n==='); - String res = HtmlWriter.DEFAULT.write(doc); + Document doc = CommonMarkParser.defaults.parse('Hello world!\n==='); + String res = HtmlWriter.defaults.write(doc); t.expect(res, t.equals('

Hello world!

\n')); }); t.test('Writing markdown', () { - Document doc = CommonMarkParser.DEFAULT.parse('Hello world!\n==='); - String res = MarkdownWriter.DEFAULT.write(doc); + Document doc = CommonMarkParser.defaults.parse('Hello world!\n==='); + String res = MarkdownWriter.defaults.write(doc); t.expect(res, t.equals('Hello world!\n============\n')); }); t.test('Smart punctuation', () { - Document doc = CommonMarkParser.STRICT.parse('...'); - String res = HtmlWriter.STRICT.write(doc); + Document doc = CommonMarkParser.strict.parse('...'); + String res = HtmlWriter.strict.write(doc); t.expect(res, t.equals('

...

\n')); }); }); @@ -151,15 +151,15 @@ void serviceTests() { t.group('FenceType', () { t.test('toString', () { - t.expect(FenceType.BacktickFence.toString(), t.equals('BacktickFence')); - t.expect(FenceType.TildeFence.toString(), t.equals('TildeFence')); + t.expect(FenceType.backtick.toString(), t.equals('backtick')); + t.expect(FenceType.tilde.toString(), t.equals('tilde')); }); t.test('==', () { - t.expect(FenceType.BacktickFence, t.equals(FenceType.BacktickFence)); - t.expect(FenceType.TildeFence, t.equals(FenceType.TildeFence)); + t.expect(FenceType.backtick, t.equals(FenceType.backtick)); + t.expect(FenceType.tilde, t.equals(FenceType.tilde)); }); t.test('!=', () { - t.expect(FenceType.BacktickFence, t.isNot(t.equals(FenceType.TildeFence))); + t.expect(FenceType.backtick, t.isNot(t.equals(FenceType.tilde))); }); }); @@ -184,11 +184,11 @@ void serviceTests() { }); t.test('==', () { t.expect(code, t.equals(new FencedCodeBlock("Code", - fenceType: FenceType.BacktickFence, fenceSize: 3, attributes: new EmptyAttr()))); + fenceType: FenceType.backtick, fenceSize: 3, attributes: new EmptyAttr()))); }); t.test('!=', () { t.expect(code, t.isNot(t.equals(new FencedCodeBlock("Code1")))); - t.expect(code, t.isNot(t.equals(new FencedCodeBlock("Code", fenceType: FenceType.TildeFence)))); + t.expect(code, t.isNot(t.equals(new FencedCodeBlock("Code", fenceType: FenceType.tilde)))); t.expect(code, t.isNot(t.equals(new FencedCodeBlock("Code", fenceSize: 5)))); t.expect(code, t.isNot(t.equals(new FencedCodeBlock("Code", attributes: new InfoString('dart'))))); t.expect(code, t.isNot(t.equals(new IndentedCodeBlock("Code")))); @@ -239,57 +239,57 @@ void serviceTests() { t.group('BulletType', () { t.test('toString', () { - t.expect(BulletType.MinusBullet.toString(), t.equals('MinusBullet')); - t.expect(BulletType.PlusBullet.toString(), t.equals('PlusBullet')); - t.expect(BulletType.StarBullet.toString(), t.equals('StarBullet')); + t.expect(BulletType.minus.toString(), t.equals('minus')); + t.expect(BulletType.plus.toString(), t.equals('plus')); + t.expect(BulletType.star.toString(), t.equals('star')); }); t.test('fromChar', () { - t.expect(BulletType.fromChar('-'), t.equals(BulletType.MinusBullet)); - t.expect(BulletType.fromChar('+'), t.equals(BulletType.PlusBullet)); - t.expect(BulletType.fromChar('*'), t.equals(BulletType.StarBullet)); + t.expect(BulletType.fromChar('-'), t.equals(BulletType.minus)); + t.expect(BulletType.fromChar('+'), t.equals(BulletType.plus)); + t.expect(BulletType.fromChar('*'), t.equals(BulletType.star)); }); t.test('==', () { - t.expect(BulletType.MinusBullet, t.equals(BulletType.MinusBullet)); - t.expect(BulletType.PlusBullet, t.equals(BulletType.PlusBullet)); - t.expect(BulletType.StarBullet, t.equals(BulletType.StarBullet)); + t.expect(BulletType.minus, t.equals(BulletType.minus)); + t.expect(BulletType.plus, t.equals(BulletType.plus)); + t.expect(BulletType.star, t.equals(BulletType.star)); }); t.test('!=', () { - t.expect(BulletType.MinusBullet, t.isNot(t.equals(BulletType.PlusBullet))); - t.expect(BulletType.MinusBullet, t.isNot(t.equals(BulletType.StarBullet))); + t.expect(BulletType.minus, t.isNot(t.equals(BulletType.plus))); + t.expect(BulletType.minus, t.isNot(t.equals(BulletType.star))); }); }); t.group('IndexSeparator', () { t.test('toString', () { - t.expect(IndexSeparator.DotSeparator.toString(), t.equals('DotSeparator')); - t.expect(IndexSeparator.ParenthesisSeparator.toString(), t.equals('ParenthesisSeparator')); + t.expect(IndexSeparator.dot.toString(), t.equals('dot')); + t.expect(IndexSeparator.parenthesis.toString(), t.equals('parenthesis')); }); t.test('fromChar', () { - t.expect(IndexSeparator.fromChar('.').toString(), t.equals('DotSeparator')); - t.expect(IndexSeparator.fromChar(')').toString(), t.equals('ParenthesisSeparator')); + t.expect(IndexSeparator.fromChar('.').toString(), t.equals('dot')); + t.expect(IndexSeparator.fromChar(')').toString(), t.equals('parenthesis')); }); t.test('==', () { - t.expect(IndexSeparator.DotSeparator, t.equals(IndexSeparator.DotSeparator)); - t.expect(IndexSeparator.ParenthesisSeparator, t.equals(IndexSeparator.ParenthesisSeparator)); + t.expect(IndexSeparator.dot, t.equals(IndexSeparator.dot)); + t.expect(IndexSeparator.parenthesis, t.equals(IndexSeparator.parenthesis)); }); t.test('!=', () { - t.expect(IndexSeparator.ParenthesisSeparator, t.isNot(t.equals(IndexSeparator.DotSeparator))); + t.expect(IndexSeparator.parenthesis, t.isNot(t.equals(IndexSeparator.dot))); }); }); t.group('UnorderedList', () { var list = new UnorderedList([new ListItem([new HorizontalRule()])]); t.test('toString', () { - t.expect(list.toString(), t.equals('UnorderedList MinusBullet [ListItem [HorizontalRule]]')); + t.expect(list.toString(), t.equals('UnorderedList minus [ListItem [HorizontalRule]]')); }); t.test('==', () { t.expect(list, t.equals(new UnorderedList([new ListItem([new HorizontalRule()])], - tight: false, bulletType: BulletType.MinusBullet))); + tight: false, bulletType: BulletType.minus))); }); t.test('!=', () { t.expect(list, t.isNot(new UnorderedList([]))); t.expect(list, t.isNot(new UnorderedList([new ListItem([new HorizontalRule()])], tight: true))); - t.expect(list, t.isNot(new UnorderedList([new ListItem([new HorizontalRule()])], bulletType: BulletType.PlusBullet))); + t.expect(list, t.isNot(new UnorderedList([new ListItem([new HorizontalRule()])], bulletType: BulletType.plus))); t.expect(list, t.isNot(new OrderedList([new ListItem([new HorizontalRule()])]))); }); }); @@ -297,16 +297,16 @@ void serviceTests() { t.group('OrderedList', () { var list = new OrderedList([new ListItem([new HorizontalRule()])]); t.test('toString', () { - t.expect(list.toString(), t.equals('OrderedList start=1 DotSeparator [ListItem [HorizontalRule]]')); + t.expect(list.toString(), t.equals('OrderedList start=1 dot [ListItem [HorizontalRule]]')); }); t.test('==', () { t.expect(list, t.equals(new OrderedList([new ListItem([new HorizontalRule()])], - tight: false, indexSeparator: IndexSeparator.DotSeparator, startIndex: 1))); + tight: false, indexSeparator: IndexSeparator.dot, startIndex: 1))); }); t.test('!=', () { t.expect(list, t.isNot(new OrderedList([]))); t.expect(list, t.isNot(new OrderedList([new ListItem([new HorizontalRule()])], tight: true))); - t.expect(list, t.isNot(new OrderedList([new ListItem([new HorizontalRule()])], indexSeparator: IndexSeparator.ParenthesisSeparator))); + t.expect(list, t.isNot(new OrderedList([new ListItem([new HorizontalRule()])], indexSeparator: IndexSeparator.parenthesis))); t.expect(list, t.isNot(new OrderedList([new ListItem([new HorizontalRule()])], startIndex: 0))); t.expect(list, t.isNot(new UnorderedList([new ListItem([new HorizontalRule()])]))); }); diff --git a/tool/travis.sh b/tool/travis.sh index 8f9c878..a696a36 100755 --- a/tool/travis.sh +++ b/tool/travis.sh @@ -8,6 +8,10 @@ dartanalyzer --fatal-warnings \ lib/md_proc.dart \ test/library_test.dart +# Linter +pub run linter lib +pub run linter test + # Run the tests. pub run test @@ -15,8 +19,7 @@ pub run test # Install dart_coveralls # Rerun tests with coverage and send to coveralls if [ "$COVERALLS_TOKEN" ]; then - pub global activate dart_coveralls - pub global run dart_coveralls report \ + pub run dart_coveralls report \ --token $COVERALLS_TOKEN \ --retry 2 \ --exclude-test-files \