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

Side by Side Diff: pkg/dartdoc/markdown.dart

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

Powered by Google App Engine
This is Rietveld 408576698