| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.gitiles.doc; |
| 16 | |
| Shawn Pearce | 108599e | 2015-02-11 13:28:37 -0800 | [diff] [blame] | 17 | import static com.google.gitiles.doc.MarkdownUtil.getInnerText; |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 18 | |
| Shawn Pearce | c8fac64 | 2016-05-16 13:15:43 -0600 | [diff] [blame] | 19 | import com.google.common.annotations.VisibleForTesting; |
| Shawn Pearce | c10cc99 | 2015-02-09 00:22:33 -0800 | [diff] [blame] | 20 | import com.google.common.base.Strings; |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 21 | import com.google.gitiles.GitilesView; |
| Shawn Pearce | c10cc99 | 2015-02-09 00:22:33 -0800 | [diff] [blame] | 22 | import com.google.gitiles.ThreadSafePrettifyParser; |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 23 | import com.google.gitiles.doc.html.HtmlBuilder; |
| Shawn Pearce | 5c34e09 | 2017-06-29 21:18:30 -0700 | [diff] [blame] | 24 | import com.google.gitiles.doc.html.SoyHtmlBuilder; |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 25 | import com.google.template.soy.data.SanitizedContent; |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 26 | import com.google.template.soy.shared.restricted.EscapingConventions.FilterImageDataUri; |
| Shawn Pearce | b7e872d | 2015-07-10 15:21:47 -0700 | [diff] [blame] | 27 | import com.google.template.soy.shared.restricted.EscapingConventions.FilterNormalizeUri; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 28 | import java.util.List; |
| 29 | import javax.annotation.Nullable; |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 30 | import org.commonmark.ext.gfm.strikethrough.Strikethrough; |
| 31 | import org.commonmark.ext.gfm.tables.TableBlock; |
| 32 | import org.commonmark.ext.gfm.tables.TableBody; |
| 33 | import org.commonmark.ext.gfm.tables.TableCell; |
| 34 | import org.commonmark.ext.gfm.tables.TableHead; |
| 35 | import org.commonmark.ext.gfm.tables.TableRow; |
| 36 | import org.commonmark.node.Block; |
| 37 | import org.commonmark.node.BlockQuote; |
| 38 | import org.commonmark.node.BulletList; |
| 39 | import org.commonmark.node.Code; |
| 40 | import org.commonmark.node.CustomBlock; |
| 41 | import org.commonmark.node.CustomNode; |
| 42 | import org.commonmark.node.Document; |
| 43 | import org.commonmark.node.Emphasis; |
| 44 | import org.commonmark.node.FencedCodeBlock; |
| 45 | import org.commonmark.node.HardLineBreak; |
| 46 | import org.commonmark.node.Heading; |
| 47 | import org.commonmark.node.HtmlBlock; |
| 48 | import org.commonmark.node.HtmlInline; |
| 49 | import org.commonmark.node.Image; |
| 50 | import org.commonmark.node.IndentedCodeBlock; |
| 51 | import org.commonmark.node.Link; |
| 52 | import org.commonmark.node.ListBlock; |
| 53 | import org.commonmark.node.ListItem; |
| 54 | import org.commonmark.node.Node; |
| 55 | import org.commonmark.node.OrderedList; |
| 56 | import org.commonmark.node.Paragraph; |
| 57 | import org.commonmark.node.SoftLineBreak; |
| 58 | import org.commonmark.node.StrongEmphasis; |
| 59 | import org.commonmark.node.Text; |
| 60 | import org.commonmark.node.ThematicBreak; |
| 61 | import org.commonmark.node.Visitor; |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 62 | import org.eclipse.jgit.lib.ObjectReader; |
| 63 | import org.eclipse.jgit.revwalk.RevTree; |
| Shawn Pearce | c10cc99 | 2015-02-09 00:22:33 -0800 | [diff] [blame] | 64 | import prettify.parser.Prettify; |
| 65 | import syntaxhighlight.ParseResult; |
| 66 | |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 67 | /** |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 68 | * Formats parsed Markdown AST into HTML. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 69 | * |
| 70 | * <p>Callers must create a new instance for each document. |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 71 | */ |
| 72 | public class MarkdownToHtml implements Visitor { |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 73 | public static Builder builder() { |
| 74 | return new Builder(); |
| 75 | } |
| 76 | |
| 77 | public static class Builder { |
| Shawn Pearce | c68ad0b | 2016-05-28 16:52:47 -0700 | [diff] [blame] | 78 | private String requestUri; |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 79 | private GitilesView view; |
| 80 | private MarkdownConfig config; |
| 81 | private String filePath; |
| 82 | private ObjectReader reader; |
| 83 | private RevTree root; |
| 84 | |
| 85 | Builder() {} |
| 86 | |
| Shawn Pearce | c68ad0b | 2016-05-28 16:52:47 -0700 | [diff] [blame] | 87 | public Builder setRequestUri(@Nullable String uri) { |
| 88 | requestUri = uri; |
| 89 | return this; |
| 90 | } |
| 91 | |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 92 | public Builder setGitilesView(@Nullable GitilesView view) { |
| 93 | this.view = view; |
| 94 | return this; |
| 95 | } |
| 96 | |
| 97 | public Builder setConfig(@Nullable MarkdownConfig config) { |
| 98 | this.config = config; |
| 99 | return this; |
| 100 | } |
| 101 | |
| 102 | public Builder setFilePath(@Nullable String filePath) { |
| 103 | this.filePath = Strings.emptyToNull(filePath); |
| 104 | return this; |
| 105 | } |
| 106 | |
| 107 | public Builder setReader(ObjectReader reader) { |
| 108 | this.reader = reader; |
| 109 | return this; |
| 110 | } |
| 111 | |
| 112 | public Builder setRootTree(RevTree tree) { |
| 113 | this.root = tree; |
| 114 | return this; |
| 115 | } |
| 116 | |
| 117 | public MarkdownToHtml build() { |
| 118 | return new MarkdownToHtml(this); |
| 119 | } |
| 120 | } |
| 121 | |
| Shawn Pearce | 5c34e09 | 2017-06-29 21:18:30 -0700 | [diff] [blame] | 122 | private HtmlBuilder html; |
| 123 | private TocFormatter toc; |
| Shawn Pearce | c68ad0b | 2016-05-28 16:52:47 -0700 | [diff] [blame] | 124 | private final String requestUri; |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 125 | private final GitilesView view; |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 126 | private final MarkdownConfig config; |
| Shawn Pearce | c32894e | 2016-05-17 15:25:20 -0400 | [diff] [blame] | 127 | private final String filePath; |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 128 | private final ImageLoader imageLoader; |
| Shawn Pearce | 25d9196 | 2015-06-22 15:35:36 -0700 | [diff] [blame] | 129 | private boolean outputNamedAnchor = true; |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 130 | |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 131 | private MarkdownToHtml(Builder b) { |
| Shawn Pearce | c68ad0b | 2016-05-28 16:52:47 -0700 | [diff] [blame] | 132 | requestUri = b.requestUri; |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 133 | view = b.view; |
| 134 | config = b.config; |
| 135 | filePath = b.filePath; |
| 136 | imageLoader = newImageLoader(b); |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 139 | private static ImageLoader newImageLoader(Builder b) { |
| 140 | if (b.reader != null && b.view != null && b.config != null && b.root != null) { |
| 141 | return new ImageLoader(b.reader, b.view, b.config, b.root); |
| 142 | } |
| 143 | return null; |
| Shawn Pearce | f75a2c6 | 2015-02-12 21:39:00 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 146 | /** Render the document AST to sanitized HTML. */ |
| Shawn Pearce | 5c34e09 | 2017-06-29 21:18:30 -0700 | [diff] [blame] | 147 | public void renderToHtml(HtmlBuilder out, Node node) { |
| 148 | if (node != null) { |
| 149 | html = out; |
| 150 | toc = new TocFormatter(html, 3); |
| 151 | toc.setRoot(node); |
| 152 | node.accept(this); |
| 153 | html.finish(); |
| 154 | html = null; |
| 155 | toc = null; |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 156 | } |
| Shawn Pearce | 5c34e09 | 2017-06-29 21:18:30 -0700 | [diff] [blame] | 157 | } |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 158 | |
| Shawn Pearce | 5c34e09 | 2017-06-29 21:18:30 -0700 | [diff] [blame] | 159 | /** Render the document AST to sanitized HTML. */ |
| 160 | public SanitizedContent toSoyHtml(Node node) { |
| 161 | if (node != null) { |
| 162 | SoyHtmlBuilder out = new SoyHtmlBuilder(); |
| 163 | renderToHtml(out, node); |
| 164 | return out.toSoy(); |
| 165 | } |
| 166 | return null; |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 170 | public void visit(Document node) { |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 171 | visitChildren(node); |
| 172 | } |
| 173 | |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 174 | private void visit(BlockNote node) { |
| 175 | html.open("div").attribute("class", node.getClassName()); |
| 176 | Node f = node.getFirstChild(); |
| 177 | if (f == node.getLastChild() && f instanceof Paragraph) { |
| 178 | // Avoid <p> inside <div> if there is only one <p>. |
| 179 | visitChildren(f); |
| 180 | } else { |
| 181 | visitChildren(node); |
| 182 | } |
| Shawn Pearce | 96f6d5f | 2015-02-06 22:45:58 -0800 | [diff] [blame] | 183 | html.close("div"); |
| 184 | } |
| 185 | |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 186 | private void visit(MultiColumnBlock node) { |
| Shawn Pearce | 6f6f1cd | 2015-02-07 00:10:24 -0800 | [diff] [blame] | 187 | html.open("div").attribute("class", "cols"); |
| Shawn Pearce | ecddc61 | 2015-02-20 22:09:47 -0800 | [diff] [blame] | 188 | visitChildren(node); |
| 189 | html.close("div"); |
| 190 | } |
| 191 | |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 192 | private void visit(MultiColumnBlock.Column node) { |
| 193 | if (1 <= node.span && node.span <= MultiColumnBlock.GRID_WIDTH) { |
| Shawn Pearce | ecddc61 | 2015-02-20 22:09:47 -0800 | [diff] [blame] | 194 | html.open("div").attribute("class", "col-" + node.span); |
| 195 | visitChildren(node); |
| Shawn Pearce | 6f6f1cd | 2015-02-07 00:10:24 -0800 | [diff] [blame] | 196 | html.close("div"); |
| 197 | } |
| Shawn Pearce | 6f6f1cd | 2015-02-07 00:10:24 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 200 | private void visit(IframeBlock node) { |
| Shawn Pearce | ee0b06e | 2015-02-13 00:13:01 -0800 | [diff] [blame] | 201 | if (HtmlBuilder.isValidHttpUri(node.src) |
| 202 | && HtmlBuilder.isValidCssDimension(node.height) |
| 203 | && HtmlBuilder.isValidCssDimension(node.width) |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 204 | && config != null |
| 205 | && config.isIFrameAllowed(node.src)) { |
| Shawn Pearce | ee0b06e | 2015-02-13 00:13:01 -0800 | [diff] [blame] | 206 | html.open("iframe") |
| 207 | .attribute("src", node.src) |
| 208 | .attribute("height", node.height) |
| 209 | .attribute("width", node.width); |
| 210 | if (!node.border) { |
| 211 | html.attribute("class", "noborder"); |
| 212 | } |
| 213 | html.close("iframe"); |
| 214 | } |
| 215 | } |
| 216 | |
| Shawn Pearce | ee0b06e | 2015-02-13 00:13:01 -0800 | [diff] [blame] | 217 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 218 | public void visit(Heading node) { |
| Shawn Pearce | 263d674 | 2015-06-23 17:36:07 -0700 | [diff] [blame] | 219 | outputNamedAnchor = false; |
| 220 | String tag = "h" + node.getLevel(); |
| 221 | html.open(tag); |
| Shawn Pearce | d57ade0 | 2015-06-22 12:34:38 -0700 | [diff] [blame] | 222 | String id = toc.idFromHeader(node); |
| 223 | if (id != null) { |
| Shawn Pearce | 263d674 | 2015-06-23 17:36:07 -0700 | [diff] [blame] | 224 | html.open("a") |
| 225 | .attribute("class", "h") |
| 226 | .attribute("name", id) |
| 227 | .attribute("href", "#" + id) |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 228 | .open("span") |
| 229 | .close("span") |
| Shawn Pearce | 263d674 | 2015-06-23 17:36:07 -0700 | [diff] [blame] | 230 | .close("a"); |
| Darragh Bailey | bb828f2 | 2017-05-18 13:03:09 +0100 | [diff] [blame] | 231 | // github markdown compatibility |
| Darragh Bailey | d8d82fc | 2017-07-07 10:13:00 +0100 | [diff] [blame^] | 232 | if (id != id.toLowerCase()) { |
| 233 | html.open("a") |
| 234 | .attribute("class", "h") |
| 235 | .attribute("name", id.toLowerCase()) |
| 236 | .attribute("href", "#" + id.toLowerCase()) |
| 237 | .open("span") |
| 238 | .close("span") |
| 239 | .close("a"); |
| 240 | } |
| Shawn Pearce | d57ade0 | 2015-06-22 12:34:38 -0700 | [diff] [blame] | 241 | } |
| Shawn Pearce | 263d674 | 2015-06-23 17:36:07 -0700 | [diff] [blame] | 242 | visitChildren(node); |
| 243 | html.close(tag); |
| 244 | outputNamedAnchor = true; |
| Shawn Pearce | 25d9196 | 2015-06-22 15:35:36 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 247 | private void visit(NamedAnchor node) { |
| Shawn Pearce | 25d9196 | 2015-06-22 15:35:36 -0700 | [diff] [blame] | 248 | if (outputNamedAnchor) { |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 249 | html.open("a").attribute("name", node.getName()).close("a"); |
| Shawn Pearce | 25d9196 | 2015-06-22 15:35:36 -0700 | [diff] [blame] | 250 | } |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 254 | public void visit(Paragraph node) { |
| 255 | if (isInTightList(node)) { |
| 256 | // Avoid unnecessary <p> tags within <ol><li> structures. |
| 257 | visitChildren(node); |
| 258 | } else { |
| 259 | wrapChildren("p", node); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | private static boolean isInTightList(Paragraph c) { |
| 264 | Block b = c.getParent(); // b is probably a ListItem |
| 265 | if (b != null) { |
| 266 | Block a = b.getParent(); |
| 267 | return a instanceof ListBlock && ((ListBlock) a).isTight(); |
| 268 | } |
| 269 | return false; |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 273 | public void visit(BlockQuote node) { |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 274 | wrapChildren("blockquote", node); |
| 275 | } |
| 276 | |
| 277 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 278 | public void visit(OrderedList node) { |
| 279 | html.open("ol"); |
| 280 | if (node.getStartNumber() != 1) { |
| 281 | html.attribute("start", Integer.toString(node.getStartNumber())); |
| 282 | } |
| 283 | visitChildren(node); |
| 284 | html.close("ol"); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 288 | public void visit(BulletList node) { |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 289 | wrapChildren("ul", node); |
| 290 | } |
| 291 | |
| 292 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 293 | public void visit(ListItem node) { |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 294 | wrapChildren("li", node); |
| 295 | } |
| 296 | |
| 297 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 298 | public void visit(FencedCodeBlock node) { |
| 299 | codeInPre(node.getInfo(), node.getLiteral()); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 303 | public void visit(IndentedCodeBlock node) { |
| 304 | codeInPre(null, node.getLiteral()); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 307 | private void codeInPre(String lang, String text) { |
| Shawn Pearce | c10cc99 | 2015-02-09 00:22:33 -0800 | [diff] [blame] | 308 | html.open("pre").attribute("class", "code"); |
| 309 | text = printLeadingBlankLines(text); |
| 310 | List<ParseResult> parsed = parse(lang, text); |
| 311 | if (parsed != null) { |
| 312 | int last = 0; |
| 313 | for (ParseResult r : parsed) { |
| 314 | span(null, text, last, r.getOffset()); |
| 315 | last = r.getOffset() + r.getLength(); |
| 316 | span(r.getStyleKeysString(), text, r.getOffset(), last); |
| 317 | } |
| 318 | if (last < text.length()) { |
| 319 | span(null, text, last, text.length()); |
| 320 | } |
| 321 | } else { |
| 322 | html.appendAndEscape(text); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 323 | } |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 324 | html.close("pre"); |
| 325 | } |
| 326 | |
| Shawn Pearce | c10cc99 | 2015-02-09 00:22:33 -0800 | [diff] [blame] | 327 | private String printLeadingBlankLines(String text) { |
| 328 | int i = 0; |
| 329 | while (i < text.length() && text.charAt(i) == '\n') { |
| 330 | html.open("br"); |
| 331 | i++; |
| 332 | } |
| 333 | return text.substring(i); |
| 334 | } |
| 335 | |
| 336 | private void span(String classes, String s, int start, int end) { |
| 337 | if (end - start > 0) { |
| 338 | if (Strings.isNullOrEmpty(classes)) { |
| 339 | classes = Prettify.PR_PLAIN; |
| 340 | } |
| 341 | html.open("span").attribute("class", classes); |
| 342 | html.appendAndEscape(s.substring(start, end)); |
| 343 | html.close("span"); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | private List<ParseResult> parse(String lang, String text) { |
| 348 | if (Strings.isNullOrEmpty(lang)) { |
| 349 | return null; |
| 350 | } |
| 351 | try { |
| 352 | return ThreadSafePrettifyParser.INSTANCE.parse(lang, text); |
| 353 | } catch (StackOverflowError e) { |
| 354 | return null; |
| 355 | } |
| 356 | } |
| 357 | |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 358 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 359 | public void visit(Code node) { |
| Mark Mentovai | c4cfe00 | 2016-11-07 20:38:58 -0500 | [diff] [blame] | 360 | html.open("code").attribute("class", "code").appendAndEscape(node.getLiteral()).close("code"); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 364 | public void visit(Emphasis node) { |
| 365 | wrapChildren("em", node); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 369 | public void visit(StrongEmphasis node) { |
| 370 | wrapChildren("strong", node); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 374 | public void visit(Link node) { |
| 375 | html.open("a") |
| 376 | .attribute("href", href(node.getDestination())) |
| 377 | .attribute("title", node.getTitle()); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 378 | visitChildren(node); |
| 379 | html.close("a"); |
| 380 | } |
| 381 | |
| Shawn Pearce | c8fac64 | 2016-05-16 13:15:43 -0600 | [diff] [blame] | 382 | @VisibleForTesting |
| 383 | String href(String target) { |
| Shawn Pearce | 6b5c7d5 | 2016-05-20 16:37:00 -0600 | [diff] [blame] | 384 | if (target.startsWith("#") || HtmlBuilder.isValidHttpUri(target)) { |
| Shawn Pearce | c8fac64 | 2016-05-16 13:15:43 -0600 | [diff] [blame] | 385 | return target; |
| Shawn Pearce | 532b62f | 2016-06-05 12:20:38 -0700 | [diff] [blame] | 386 | } else if (target.startsWith("git:")) { |
| 387 | if (HtmlBuilder.isValidGitUri(target)) { |
| 388 | return target; |
| 389 | } |
| 390 | return FilterNormalizeUri.INSTANCE.getInnocuousOutput(); |
| Shawn Pearce | 978fe8e | 2015-03-25 16:49:41 -0700 | [diff] [blame] | 391 | } |
| Shawn Pearce | b7e872d | 2015-07-10 15:21:47 -0700 | [diff] [blame] | 392 | |
| Shawn Pearce | 6b5c7d5 | 2016-05-20 16:37:00 -0600 | [diff] [blame] | 393 | String anchor = ""; |
| 394 | int hash = target.indexOf('#'); |
| 395 | if (hash >= 0) { |
| 396 | anchor = target.substring(hash); |
| 397 | target = target.substring(0, hash); |
| 398 | } |
| 399 | |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 400 | String dest = PathResolver.resolve(filePath, target); |
| 401 | if (dest == null || view == null) { |
| 402 | return FilterNormalizeUri.INSTANCE.getInnocuousOutput(); |
| Shawn Pearce | b7e872d | 2015-07-10 15:21:47 -0700 | [diff] [blame] | 403 | } |
| Shawn Pearce | c8fac64 | 2016-05-16 13:15:43 -0600 | [diff] [blame] | 404 | |
| Shawn Pearce | c32894e | 2016-05-17 15:25:20 -0400 | [diff] [blame] | 405 | GitilesView.Builder b; |
| 406 | if (view.getType() == GitilesView.Type.ROOTED_DOC) { |
| 407 | b = GitilesView.rootedDoc(); |
| 408 | } else { |
| 409 | b = GitilesView.path(); |
| 410 | } |
| Shawn Pearce | c68ad0b | 2016-05-28 16:52:47 -0700 | [diff] [blame] | 411 | dest = b.copyFrom(view).setPathPart(dest).build().toUrl(); |
| 412 | |
| 413 | return PathResolver.relative(requestUri, dest) + anchor; |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 416 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 417 | public void visit(Image node) { |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 418 | html.open("img") |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 419 | .attribute("src", image(node.getDestination())) |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 420 | .attribute("title", node.getTitle()) |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 421 | .attribute("alt", getInnerText(node)); |
| 422 | } |
| 423 | |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 424 | String image(String dest) { |
| 425 | if (HtmlBuilder.isValidHttpUri(dest) || HtmlBuilder.isImageDataUri(dest)) { |
| 426 | return dest; |
| 427 | } else if (imageLoader != null) { |
| 428 | return imageLoader.inline(filePath, dest); |
| Shawn Pearce | f75a2c6 | 2015-02-12 21:39:00 -0800 | [diff] [blame] | 429 | } |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 430 | return FilterImageDataUri.INSTANCE.getInnocuousOutput(); |
| Shawn Pearce | f75a2c6 | 2015-02-12 21:39:00 -0800 | [diff] [blame] | 431 | } |
| 432 | |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 433 | public void visit(TableBlock node) { |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 434 | wrapChildren("table", node); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 437 | private void visit(TableRow node) { |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 438 | wrapChildren("tr", node); |
| 439 | } |
| 440 | |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 441 | private void visit(TableCell cell) { |
| 442 | String tag = cell.isHeader() ? "th" : "td"; |
| 443 | html.open(tag); |
| 444 | TableCell.Alignment alignment = cell.getAlignment(); |
| 445 | if (alignment != null) { |
| 446 | html.attribute("align", toHtml(alignment)); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 447 | } |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 448 | visitChildren(cell); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 449 | html.close(tag); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 452 | private static String toHtml(TableCell.Alignment alignment) { |
| 453 | switch (alignment) { |
| 454 | case LEFT: |
| 455 | return "left"; |
| 456 | case CENTER: |
| 457 | return "center"; |
| 458 | case RIGHT: |
| 459 | return "right"; |
| 460 | default: |
| 461 | throw new IllegalArgumentException("unsupported alignment " + alignment); |
| 462 | } |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 465 | private void visit(SmartQuoted node) { |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 466 | switch (node.getType()) { |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 467 | case DOUBLE: |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 468 | html.entity("“"); |
| 469 | visitChildren(node); |
| 470 | html.entity("”"); |
| 471 | break; |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 472 | case SINGLE: |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 473 | html.entity("‘"); |
| 474 | visitChildren(node); |
| 475 | html.entity("’"); |
| 476 | break; |
| 477 | default: |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 478 | throw new IllegalArgumentException("unsupported quote " + node.getType()); |
| 479 | } |
| 480 | } |
| 481 | |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 482 | @Override |
| 483 | public void visit(Text node) { |
| Shawn Pearce | b2ed49d | 2016-06-05 10:31:11 -0700 | [diff] [blame] | 484 | html.appendAndEscape(node.getLiteral()); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 488 | public void visit(SoftLineBreak node) { |
| 489 | html.space(); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 493 | public void visit(HardLineBreak node) { |
| 494 | html.open("br"); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 498 | public void visit(ThematicBreak thematicBreak) { |
| 499 | html.open("hr"); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 503 | public void visit(HtmlInline node) { |
| 504 | // Discard all HTML. |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | @Override |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 508 | public void visit(HtmlBlock node) { |
| 509 | // Discard all HTML. |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 512 | private void wrapChildren(String tag, Node node) { |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 513 | html.open(tag); |
| 514 | visitChildren(node); |
| 515 | html.close(tag); |
| 516 | } |
| 517 | |
| 518 | private void visitChildren(Node node) { |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 519 | for (Node c = node.getFirstChild(); c != null; c = c.getNext()) { |
| 520 | c.accept(this); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | @Override |
| 525 | public void visit(CustomNode node) { |
| 526 | if (node instanceof NamedAnchor) { |
| 527 | visit((NamedAnchor) node); |
| 528 | } else if (node instanceof SmartQuoted) { |
| 529 | visit((SmartQuoted) node); |
| 530 | } else if (node instanceof Strikethrough) { |
| 531 | wrapChildren("del", node); |
| 532 | } else if (node instanceof TableBody) { |
| 533 | wrapChildren("tbody", node); |
| 534 | } else if (node instanceof TableCell) { |
| 535 | visit((TableCell) node); |
| 536 | } else if (node instanceof TableHead) { |
| 537 | wrapChildren("thead", node); |
| 538 | } else if (node instanceof TableRow) { |
| 539 | visit((TableRow) node); |
| 540 | } else { |
| 541 | throw new IllegalArgumentException("cannot render " + node.getClass()); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | @Override |
| 546 | public void visit(CustomBlock node) { |
| 547 | if (node instanceof BlockNote) { |
| 548 | visit((BlockNote) node); |
| 549 | } else if (node instanceof IframeBlock) { |
| 550 | visit((IframeBlock) node); |
| 551 | } else if (node instanceof MultiColumnBlock) { |
| 552 | visit((MultiColumnBlock) node); |
| 553 | } else if (node instanceof MultiColumnBlock.Column) { |
| 554 | visit((MultiColumnBlock.Column) node); |
| 555 | } else if (node instanceof TableBlock) { |
| 556 | visit((TableBlock) node); |
| 557 | } else if (node instanceof TocBlock) { |
| 558 | toc.format(); |
| 559 | } else { |
| 560 | throw new IllegalArgumentException("cannot render " + node.getClass()); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | } |