Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(564)

Side by Side Diff: utils/markdown/lib.dart

Issue 8953042: Move markdown library. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add markdown tests to dartdoc. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « utils/markdown/inline_parser.dart ('k') | utils/markdown/markdown.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
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.
4
5 /// Parses text in markdown format. Use this entrypoint if you want to parse
6 /// markdown from your own Dart code. To parse markdown by running the script
7 /// directly from the command line, see markdown.dart.
8 #library('markdown');
9
10 #source('ast.dart');
11 #source('block_parser.dart');
12 #source('html_renderer.dart');
13 #source('inline_parser.dart');
14
15 /// Converts the given string of markdown to HTML.
16 String markdownToHtml(String markdown) {
17 final document = new Document();
18
19 final lines = markdown.split('\n');
20 document.parseRefLinks(lines);
21 final blocks = document.parseLines(lines);
22 return renderToHtml(blocks);
23 }
24
25 /// Replaces `<`, `&`, and `>`, with their HTML entity equivalents.
26 String escapeHtml(String html) {
27 return html.replaceAll('&', '&amp;')
28 .replaceAll('<', '&lt;')
29 .replaceAll('>', '&gt;');
30 }
31
32 var _implicitLinkResolver;
33
34 Node setImplicitLinkResolver(Node resolver(String text)) {
35 _implicitLinkResolver = resolver;
36 }
37
38 /// Maintains the context needed to parse a markdown document.
39 class Document {
40 final Map<String, Link> refLinks;
41
42 Document()
43 : refLinks = <Link>{};
44
45 parseRefLinks(List<String> lines) {
46 // This is a hideous regex. It matches:
47 // [id]: http:foo.com "some title"
48 // Where there may whitespace in there, and where the title may be in
49 // single quotes, double quotes, or parentheses.
50 final indent = @'^[ ]{0,3}'; // Leading indentation.
51 final id = @'\[([^\]]+)\]'; // Reference id in [brackets].
52 final quote = @'"[^"]+"'; // Title in "double quotes".
53 final apos = @"'[^']+'"; // Title in 'single quotes'.
54 final paren = @"\([^)]+\)"; // Title in (parentheses).
55 final pattern = new RegExp(
56 '$indent$id:\\s+(\\S+)\\s*($quote|$apos|$paren|)\\s*\$');
57
58 for (int i = 0; i < lines.length; i++) {
59 final match = pattern.firstMatch(lines[i]);
60 if (match != null) {
61 // Parse the link.
62 final id = match[1];
63 final url = match[2];
64 var title = match[3];
65
66 if (title == '') {
67 // No title.
68 title = null;
69 } else {
70 // Remove "", '', or ().
71 title = title.substring(1, title.length - 1);
72 }
73
74 refLinks[id] = new Link(id, url, title);
75
76 // Remove it from the output. We replace it with a blank line which will
77 // get consumed by later processing.
78 lines[i] = '';
79 }
80 }
81 }
82
83 /// Parse the given [lines] of markdown to a series of AST nodes.
84 List<Node> parseLines(List<String> lines) {
85 final parser = new BlockParser(lines, this);
86
87 final blocks = [];
88 while (!parser.isDone) {
89 for (final syntax in BlockSyntax.syntaxes) {
90 if (syntax.canParse(parser)) {
91 final block = syntax.parse(parser);
92 if (block != null) blocks.add(block);
93 break;
94 }
95 }
96 }
97
98 return blocks;
99 }
100
101 /// Takes a string of raw text and processes all inline markdown tags,
102 /// returning a list of AST nodes. For example, given ``"*this **is** a*
103 /// `markdown`"``, returns:
104 /// `<em>this <strong>is</strong> a</em> <code>markdown</code>`.
105 List<Node> parseInline(String text) => new InlineParser(text, this).parse();
106 }
107
108 class Link {
109 final String id;
110 final String url;
111 final String title;
112 Link(this.id, this.url, this.title);
113 }
OLDNEW
« no previous file with comments | « utils/markdown/inline_parser.dart ('k') | utils/markdown/markdown.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698