Merge pull request dart-lang/markdown#46 from srawlins/add-binary

Add a simple binary Dart script
diff --git a/pkgs/markdown/CHANGELOG.md b/pkgs/markdown/CHANGELOG.md
index fdbb0d6..d7d580c 100644
--- a/pkgs/markdown/CHANGELOG.md
+++ b/pkgs/markdown/CHANGELOG.md
@@ -1,7 +1,10 @@
 ## 0.8.0
 
+* **Breaking:** Remove (probably unused) fields: `LinkSyntax.resolved`,
+  `InlineParser.currentSource`.
 * Switch tests to use [test][] instead of [unittest][].
-* Remove (probably unused) `resolved` field from `LinkSyntax`.
+* Fix a few bugs in inline code syntax.
+* Ignore underscores inside words (#41).
 
 [test]: https://pub.dartlang.org/packages/test
 [unittest]: https://pub.dartlang.org/packages/unittest
diff --git a/pkgs/markdown/benchmark/output.html b/pkgs/markdown/benchmark/output.html
index 71e5771..3330a63 100644
--- a/pkgs/markdown/benchmark/output.html
+++ b/pkgs/markdown/benchmark/output.html
@@ -364,4 +364,4 @@
 "pub serve" is compiling test/my_app_test.dart...
 "pub serve" is compiling test/utils_test.dart...
 00:00 +42: All tests passed!
-</code></pre>
\ No newline at end of file
+</code></pre>
diff --git a/pkgs/markdown/lib/src/inline_parser.dart b/pkgs/markdown/lib/src/inline_parser.dart
index fe7c4dc..9f0db08 100644
--- a/pkgs/markdown/lib/src/inline_parser.dart
+++ b/pkgs/markdown/lib/src/inline_parser.dart
@@ -43,17 +43,12 @@
     // Parse "**strong**" tags.
     new TagSyntax(r'\*\*', tag: 'strong'),
     // Parse "__strong__" tags.
-    new TagSyntax(r'__', tag: 'strong'),
+    new TagSyntax(r'\b__', tag: 'strong', end: r'__\b'),
     // Parse "*emphasis*" tags.
     new TagSyntax(r'\*', tag: 'em'),
     // Parse "_emphasis_" tags.
-    // TODO(rnystrom): Underscores in the middle of a word should not be
-    // parsed as emphasis like_in_this.
-    new TagSyntax(r'_', tag: 'em'),
-    // Parse inline code within double backticks: "``code``".
-    new CodeSyntax(r'``\s?((?:.|\n)*?)\s?``'),
-    // Parse inline code within backticks: "`code`".
-    new CodeSyntax(r'`([^`]*)`')
+    new TagSyntax(r'\b_', tag: 'em', end: r'_\b'),
+    new CodeSyntax(),
     // We will add the LinkSyntax once we know about the specific link resolver.
   ];
 
@@ -149,11 +144,6 @@
     _stack.last.children.add(node);
   }
 
-  // TODO(rnystrom): Only need this because RegExp doesn't let you start
-  // searching from a given offset.
-  @deprecated
-  String get currentSource => source.substring(pos, source.length);
-
   bool get isDone => pos == source.length;
 
   void advanceBy(int length) {
@@ -172,6 +162,9 @@
 
   InlineSyntax(String pattern) : pattern = new RegExp(pattern, multiLine: true);
 
+  /// Try to match at the parser's current position.
+  ///
+  /// Returns whether or not the pattern successfully matched.
   bool tryMatch(InlineParser parser) {
     var startMatch = pattern.matchAsPrefix(parser.source, parser.pos);
     if (startMatch != null) {
@@ -375,10 +368,41 @@
 
 /// Matches backtick-enclosed inline code blocks.
 class CodeSyntax extends InlineSyntax {
-  CodeSyntax(String pattern) : super(pattern);
+  // This pattern matches:
+  //
+  // * a string of backticks (not followed by any more), followed by
+  // * a non-greedy string of anying, including newlines, ending with anything
+  //   except a backtick, followed by
+  // * a string of backticks the same length as the first, not followed by any
+  //   more.
+  //
+  // This conforms to the delimiters of inline code, both in Markdown.pl, and
+  // CommonMark.
+  static String _pattern = r'(`+(?!`))((?:.|\n)*?[^`])\1(?!`)';
+
+  CodeSyntax() : super(_pattern);
+
+  bool tryMatch(InlineParser parser) {
+    if (parser.pos > 0 && parser.source[parser.pos-1] == '`') {
+      // Not really a match! We can't just sneak past one backtick to try the
+      // next character. An example of this situation would be:
+      //
+      //     before ``` and `` after.
+      //             ^--parser.pos
+      return false;
+    }
+
+    var match = pattern.matchAsPrefix(parser.source, parser.pos);
+    if (match == null) {
+      return false;
+    }
+    parser.writeText();
+    if (onMatch(parser, match)) parser.consume(match[0].length);
+    return true;
+  }
 
   bool onMatch(InlineParser parser, Match match) {
-    parser.addNode(new Element.text('code', escapeHtml(match[1])));
+    parser.addNode(new Element.text('code', escapeHtml(match[2].trim())));
     return true;
   }
 }
diff --git a/pkgs/markdown/pubspec.yaml b/pkgs/markdown/pubspec.yaml
index 0c2eedf..e459456 100644
--- a/pkgs/markdown/pubspec.yaml
+++ b/pkgs/markdown/pubspec.yaml
@@ -1,5 +1,5 @@
 name: markdown
-version: 0.8.0-dev
+version: 0.8.0
 author: Dart Team <misc@dartlang.org>
 description: A library for converting markdown to HTML.
 homepage: https://github.com/dart-lang/markdown
diff --git a/pkgs/markdown/test/original/emphasis_and_strong.unit b/pkgs/markdown/test/original/emphasis_and_strong.unit
index 5e67fc0..90aea56 100644
--- a/pkgs/markdown/test/original/emphasis_and_strong.unit
+++ b/pkgs/markdown/test/original/emphasis_and_strong.unit
@@ -70,3 +70,19 @@
 
 <<<
 <p><em>a _b </em>c<em> d_ e</em></p>
+>>> in the middle of a word
+a_b_c a__b__c a*b*c a**b**c
+<<<
+<p>a_b_c a__b__c a<em>b</em>c a<strong>b</strong>c</p>
+>>> prefixing a word
+_a_b __a__b *a*b **a**b
+<<<
+<p>_a_b __a__b <em>a</em>b <strong>a</strong>b</p>
+>>> suffixing a word
+a_b_ a__b__ a*b* a**b**
+<<<
+<p>a_b_ a__b__ a<em>b</em> a<strong>b</strong></p>
+>>> spanning words
+_a_b c_d_ __a__b c__d__ *a*b c*d* **a**b c**d**
+<<<
+<p><em>a_b c_d</em> <strong>a__b c__d</strong> <em>a</em>b c<em>d</em> <strong>a</strong>b c<strong>d</strong></p>
\ No newline at end of file
diff --git a/pkgs/markdown/test/original/inline_code.unit b/pkgs/markdown/test/original/inline_code.unit
index 30fd05c..05c31f9 100644
--- a/pkgs/markdown/test/original/inline_code.unit
+++ b/pkgs/markdown/test/original/inline_code.unit
@@ -3,6 +3,11 @@
 
 <<<
 <p>before <code>source</code> after</p>
+>>> single characters
+before `x` and `_` after
+
+<<<
+<p>before <code>x</code> and <code>_</code> after</p>
 >>> unmatched backtick
 before ` after
 
@@ -25,6 +30,11 @@
 
 <<<
 <p>before <code>source</code> after</p>
+>>> even more backticks
+before ````source with ``` and```` after
+
+<<<
+<p>before <code>source with ``` and</code> after</p>
 >>> double backticks
 before ``can `contain` backticks`` after
 
@@ -35,6 +45,13 @@
 
 <<<
 <p>before <code>`tick`</code> after</p>
+>>> multiline single backticks with spaces
+before `in tick
+another` after
+
+<<<
+<p>before <code>in tick
+another</code> after</p>
 >>> multiline double backticks with spaces
 before ``in `tick`
 another`` after
@@ -57,3 +74,13 @@
 
 <<<
 <p>'*' <code>&lt;em&gt;</code></p>
+>>> leave unmatched backticks when first are too long
+before ``` tick `` after
+
+<<<
+<p>before ``` tick `` after</p>
+>>> leave unmatched backticks when first are too short
+before `` tick ``` after
+
+<<<
+<p>before `` tick ``` after</p>