| 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 /// Maintains the internal state needed to parse inline span elements in | 5 /// Maintains the internal state needed to parse inline span elements in |
| 6 /// markdown. | 6 /// markdown. |
| 7 class InlineParser { | 7 class InlineParser { |
| 8 static List<InlineSyntax> get syntaxes { | 8 static List<InlineSyntax> get syntaxes { |
| 9 // Lazy initialize. | 9 // Lazy initialize. |
| 10 if (_syntaxes == null) { | 10 if (_syntaxes == null) { |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 pos += length; | 141 pos += length; |
| 142 } | 142 } |
| 143 | 143 |
| 144 void consume(int length) { | 144 void consume(int length) { |
| 145 pos += length; | 145 pos += length; |
| 146 start = pos; | 146 start = pos; |
| 147 } | 147 } |
| 148 } | 148 } |
| 149 | 149 |
| 150 /// Represents one kind of markdown tag that can be parsed. | 150 /// Represents one kind of markdown tag that can be parsed. |
| 151 class InlineSyntax { | 151 abstract class InlineSyntax { |
| 152 final RegExp pattern; | 152 final RegExp pattern; |
| 153 | 153 |
| 154 InlineSyntax(String pattern) | 154 InlineSyntax(String pattern) |
| 155 : pattern = new RegExp(pattern, multiLine: true); | 155 : pattern = new RegExp(pattern, multiLine: true); |
| 156 | 156 |
| 157 bool tryMatch(InlineParser parser) { | 157 bool tryMatch(InlineParser parser) { |
| 158 final startMatch = pattern.firstMatch(parser.currentSource); | 158 final startMatch = pattern.firstMatch(parser.currentSource); |
| 159 if ((startMatch != null) && (startMatch.start == 0)) { | 159 if ((startMatch != null) && (startMatch.start == 0)) { |
| 160 // Write any existing plain text up to this point. | 160 // Write any existing plain text up to this point. |
| 161 parser.writeText(); | 161 parser.writeText(); |
| 162 | 162 |
| 163 if (onMatch(parser, startMatch)) { | 163 if (onMatch(parser, startMatch)) { |
| 164 parser.consume(startMatch[0].length); | 164 parser.consume(startMatch[0].length); |
| 165 } | 165 } |
| 166 return true; | 166 return true; |
| 167 } | 167 } |
| 168 return false; | 168 return false; |
| 169 } | 169 } |
| 170 | 170 |
| 171 abstract bool onMatch(InlineParser parser, Match match); | 171 bool onMatch(InlineParser parser, Match match); |
| 172 } | 172 } |
| 173 | 173 |
| 174 /// Matches stuff that should just be passed through as straight text. | 174 /// Matches stuff that should just be passed through as straight text. |
| 175 class TextSyntax extends InlineSyntax { | 175 class TextSyntax extends InlineSyntax { |
| 176 String substitute; | 176 String substitute; |
| 177 TextSyntax(String pattern, {String sub}) | 177 TextSyntax(String pattern, {String sub}) |
| 178 : super(pattern), | 178 : super(pattern), |
| 179 substitute = sub; | 179 substitute = sub; |
| 180 | 180 |
| 181 bool onMatch(InlineParser parser, Match match) { | 181 bool onMatch(InlineParser parser, Match match) { |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 parser.consume(endMatch[0].length); | 399 parser.consume(endMatch[0].length); |
| 400 } else { | 400 } else { |
| 401 // Didn't close correctly so revert to text. | 401 // Didn't close correctly so revert to text. |
| 402 parser.start = startPos; | 402 parser.start = startPos; |
| 403 parser.advanceBy(endMatch[0].length); | 403 parser.advanceBy(endMatch[0].length); |
| 404 } | 404 } |
| 405 | 405 |
| 406 return null; | 406 return null; |
| 407 } | 407 } |
| 408 } | 408 } |
| OLD | NEW |