| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -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 | cb28501 | 2015-03-30 09:10:28 -0700 | [diff] [blame] | 17 | import com.google.common.base.Throwables; |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 18 | import com.google.gitiles.GitilesView; |
| 19 | |
| Shawn Pearce | 0e5fc54 | 2016-02-15 14:27:05 -0800 | [diff] [blame] | 20 | import org.joda.time.Duration; |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 21 | import org.parboiled.Rule; |
| Shawn Pearce | ecddc61 | 2015-02-20 22:09:47 -0800 | [diff] [blame] | 22 | import org.parboiled.common.Factory; |
| Shawn Pearce | cb28501 | 2015-03-30 09:10:28 -0700 | [diff] [blame] | 23 | import org.parboiled.errors.ParserRuntimeException; |
| Shawn Pearce | 96f6d5f | 2015-02-06 22:45:58 -0800 | [diff] [blame] | 24 | import org.parboiled.support.StringBuilderVar; |
| Shawn Pearce | ecddc61 | 2015-02-20 22:09:47 -0800 | [diff] [blame] | 25 | import org.parboiled.support.Var; |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 26 | import org.pegdown.Parser; |
| 27 | import org.pegdown.ParsingTimeoutException; |
| 28 | import org.pegdown.PegDownProcessor; |
| Shawn Pearce | 96f6d5f | 2015-02-06 22:45:58 -0800 | [diff] [blame] | 29 | import org.pegdown.ast.Node; |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 30 | import org.pegdown.ast.RootNode; |
| Shawn Pearce | 9119b64 | 2015-02-12 22:10:48 -0800 | [diff] [blame] | 31 | import org.pegdown.ast.SimpleNode; |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 32 | import org.pegdown.plugins.BlockPluginParser; |
| Shawn Pearce | 25d9196 | 2015-06-22 15:35:36 -0700 | [diff] [blame] | 33 | import org.pegdown.plugins.InlinePluginParser; |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 34 | import org.pegdown.plugins.PegDownPlugins; |
| 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
| Shawn Pearce | ecddc61 | 2015-02-20 22:09:47 -0800 | [diff] [blame] | 38 | import java.util.ArrayList; |
| Shawn Pearce | 96f6d5f | 2015-02-06 22:45:58 -0800 | [diff] [blame] | 39 | import java.util.List; |
| 40 | |
| Shawn Pearce | 6f6f1cd | 2015-02-07 00:10:24 -0800 | [diff] [blame] | 41 | /** Parses Gitiles extensions to markdown. */ |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 42 | public class GitilesMarkdown extends Parser implements BlockPluginParser, InlinePluginParser { |
| Shawn Pearce | 108599e | 2015-02-11 13:28:37 -0800 | [diff] [blame] | 43 | private static final Logger log = LoggerFactory.getLogger(MarkdownUtil.class); |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 44 | |
| 45 | // SUPPRESS_ALL_HTML is enabled to permit hosting arbitrary user content |
| 46 | // while avoiding XSS style HTML, CSS and JavaScript injection attacks. |
| 47 | // |
| 48 | // HARDWRAPS is disabled to permit line wrapping within paragraphs to |
| 49 | // make the source file easier to read in 80 column terminals without |
| 50 | // this impacting the rendered formatting. |
| 51 | private static final int MD_OPTIONS = (ALL | SUPPRESS_ALL_HTML) & ~(HARDWRAPS); |
| 52 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 53 | public static RootNode parseFile( |
| 54 | Duration parseTimeout, GitilesView view, String path, String md) { |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 55 | if (md == null) { |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | try { |
| Shawn Pearce | cb28501 | 2015-03-30 09:10:28 -0700 | [diff] [blame] | 60 | try { |
| Shawn Pearce | 0e5fc54 | 2016-02-15 14:27:05 -0800 | [diff] [blame] | 61 | return newParser(parseTimeout).parseMarkdown(md.toCharArray()); |
| Shawn Pearce | cb28501 | 2015-03-30 09:10:28 -0700 | [diff] [blame] | 62 | } catch (ParserRuntimeException e) { |
| 63 | Throwables.propagateIfInstanceOf(e.getCause(), ParsingTimeoutException.class); |
| 64 | throw e; |
| 65 | } |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 66 | } catch (ParsingTimeoutException e) { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 67 | log.error( |
| 68 | "timeout {} ms rendering {}/{} at {}", |
| Shawn Pearce | 0e5fc54 | 2016-02-15 14:27:05 -0800 | [diff] [blame] | 69 | parseTimeout.getMillis(), |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 70 | view.getRepositoryName(), |
| 71 | path, |
| 72 | view.getRevision().getName()); |
| 73 | return null; |
| 74 | } |
| 75 | } |
| 76 | |
| Shawn Pearce | 0e5fc54 | 2016-02-15 14:27:05 -0800 | [diff] [blame] | 77 | private static PegDownProcessor newParser(Duration parseDeadline) { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 78 | PegDownPlugins plugins = |
| 79 | new PegDownPlugins.Builder().withPlugin(GitilesMarkdown.class, parseDeadline).build(); |
| Shawn Pearce | 0e5fc54 | 2016-02-15 14:27:05 -0800 | [diff] [blame] | 80 | return new PegDownProcessor(MD_OPTIONS, parseDeadline.getMillis(), plugins); |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| Shawn Pearce | 0e5fc54 | 2016-02-15 14:27:05 -0800 | [diff] [blame] | 83 | private final Duration parseTimeout; |
| Shawn Pearce | 96f6d5f | 2015-02-06 22:45:58 -0800 | [diff] [blame] | 84 | private PegDownProcessor parser; |
| 85 | |
| Shawn Pearce | 0e5fc54 | 2016-02-15 14:27:05 -0800 | [diff] [blame] | 86 | GitilesMarkdown(Duration parseTimeout) { |
| 87 | super(MD_OPTIONS, parseTimeout.getMillis(), DefaultParseRunnerProvider); |
| 88 | this.parseTimeout = parseTimeout; |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public Rule[] blockPluginRules() { |
| Shawn Pearce | 6f6f1cd | 2015-02-07 00:10:24 -0800 | [diff] [blame] | 93 | return new Rule[] { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 94 | cols(), hr(), iframe(), note(), toc(), |
| Shawn Pearce | 96f6d5f | 2015-02-06 22:45:58 -0800 | [diff] [blame] | 95 | }; |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| Shawn Pearce | 25d9196 | 2015-06-22 15:35:36 -0700 | [diff] [blame] | 98 | @Override |
| 99 | public Rule[] inlinePluginRules() { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 100 | return new Rule[] { |
| 101 | namedAnchorHtmlStyle(), namedAnchorMarkdownExtensionStyle(), |
| Shawn Pearce | 25d9196 | 2015-06-22 15:35:36 -0700 | [diff] [blame] | 102 | }; |
| 103 | } |
| 104 | |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 105 | public Rule toc() { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 106 | return NodeSequence(string("[TOC]"), push(new TocNode())); |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 107 | } |
| Shawn Pearce | 96f6d5f | 2015-02-06 22:45:58 -0800 | [diff] [blame] | 108 | |
| Shawn Pearce | 9119b64 | 2015-02-12 22:10:48 -0800 | [diff] [blame] | 109 | public Rule hr() { |
| 110 | // GitHub flavor markdown recognizes "--" as a rule. |
| 111 | return NodeSequence( |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 112 | NonindentSpace(), |
| 113 | string("--"), |
| 114 | zeroOrMore('-'), |
| 115 | Newline(), |
| Shawn Pearce | 9119b64 | 2015-02-12 22:10:48 -0800 | [diff] [blame] | 116 | oneOrMore(BlankLine()), |
| 117 | push(new SimpleNode(SimpleNode.Type.HRule))); |
| 118 | } |
| 119 | |
| Shawn Pearce | 25d9196 | 2015-06-22 15:35:36 -0700 | [diff] [blame] | 120 | public Rule namedAnchorHtmlStyle() { |
| 121 | StringBuilderVar name = new StringBuilderVar(); |
| 122 | return NodeSequence( |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 123 | Sp(), |
| 124 | string("<a"), |
| Shawn Pearce | 25d9196 | 2015-06-22 15:35:36 -0700 | [diff] [blame] | 125 | Spn1(), |
| 126 | sequence(string("name="), attribute(name)), |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 127 | Spn1(), |
| 128 | '>', |
| 129 | Spn1(), |
| 130 | string("</a>"), |
| Shawn Pearce | 25d9196 | 2015-06-22 15:35:36 -0700 | [diff] [blame] | 131 | push(new NamedAnchorNode(name.getString()))); |
| 132 | } |
| 133 | |
| 134 | public Rule namedAnchorMarkdownExtensionStyle() { |
| 135 | StringBuilderVar name = new StringBuilderVar(); |
| 136 | return NodeSequence( |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 137 | Sp(), string("{#"), anchorId(name), '}', push(new NamedAnchorNode(name.getString()))); |
| Shawn Pearce | 25d9196 | 2015-06-22 15:35:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | public Rule anchorId(StringBuilderVar name) { |
| 141 | return sequence(zeroOrMore(testNot('}'), ANY), name.append(match())); |
| 142 | } |
| 143 | |
| Shawn Pearce | ee0b06e | 2015-02-13 00:13:01 -0800 | [diff] [blame] | 144 | public Rule iframe() { |
| 145 | StringBuilderVar src = new StringBuilderVar(); |
| 146 | StringBuilderVar h = new StringBuilderVar(); |
| 147 | StringBuilderVar w = new StringBuilderVar(); |
| 148 | StringBuilderVar b = new StringBuilderVar(); |
| 149 | return NodeSequence( |
| 150 | string("<iframe"), |
| 151 | oneOrMore( |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 152 | sequence( |
| 153 | Spn1(), |
| 154 | firstOf( |
| 155 | sequence(string("src="), attribute(src)), |
| 156 | sequence(string("height="), attribute(h)), |
| 157 | sequence(string("width="), attribute(w)), |
| 158 | sequence(string("frameborder="), attribute(b))))), |
| 159 | Spn1(), |
| 160 | '>', |
| 161 | Spn1(), |
| 162 | string("</iframe>"), |
| 163 | push(new IframeNode(src.getString(), h.getString(), w.getString(), b.getString()))); |
| Shawn Pearce | ee0b06e | 2015-02-13 00:13:01 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | public Rule attribute(StringBuilderVar var) { |
| 167 | return firstOf( |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 168 | sequence('"', zeroOrMore(testNot('"'), ANY), var.append(match()), '"'), |
| 169 | sequence('\'', zeroOrMore(testNot('\''), ANY), var.append(match()), '\'')); |
| Shawn Pearce | ee0b06e | 2015-02-13 00:13:01 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| Shawn Pearce | 96f6d5f | 2015-02-06 22:45:58 -0800 | [diff] [blame] | 172 | public Rule note() { |
| 173 | StringBuilderVar body = new StringBuilderVar(); |
| 174 | return NodeSequence( |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 175 | string("***"), |
| 176 | Sp(), |
| 177 | typeOfNote(), |
| 178 | Newline(), |
| 179 | oneOrMore(testNot(string("***"), Newline()), Line(body)), |
| 180 | string("***"), |
| 181 | Newline(), |
| Shawn Pearce | 96f6d5f | 2015-02-06 22:45:58 -0800 | [diff] [blame] | 182 | push(new DivNode(popAsString(), parse(body)))); |
| 183 | } |
| 184 | |
| 185 | public Rule typeOfNote() { |
| 186 | return firstOf( |
| 187 | sequence(string("note"), push(match())), |
| 188 | sequence(string("promo"), push(match())), |
| 189 | sequence(string("aside"), push(match()))); |
| 190 | } |
| 191 | |
| Shawn Pearce | ecddc61 | 2015-02-20 22:09:47 -0800 | [diff] [blame] | 192 | @SuppressWarnings("unchecked") |
| Shawn Pearce | 6f6f1cd | 2015-02-07 00:10:24 -0800 | [diff] [blame] | 193 | public Rule cols() { |
| 194 | StringBuilderVar body = new StringBuilderVar(); |
| 195 | return NodeSequence( |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 196 | colsTag(), |
| 197 | columnWidths(), |
| 198 | Newline(), |
| 199 | oneOrMore(testNot(colsTag(), Newline()), Line(body)), |
| 200 | colsTag(), |
| 201 | Newline(), |
| Shawn Pearce | ecddc61 | 2015-02-20 22:09:47 -0800 | [diff] [blame] | 202 | push(new ColsNode((List<ColsNode.Column>) pop(), parse(body)))); |
| Shawn Pearce | 6f6f1cd | 2015-02-07 00:10:24 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | public Rule colsTag() { |
| 206 | return string("|||---|||"); |
| 207 | } |
| 208 | |
| Shawn Pearce | ecddc61 | 2015-02-20 22:09:47 -0800 | [diff] [blame] | 209 | public Rule columnWidths() { |
| 210 | ListVar widths = new ListVar(); |
| 211 | return sequence( |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 212 | zeroOrMore(sequence(Sp(), optional(ch(',')), Sp(), columnWidth(widths))), |
| 213 | push(widths.get())); |
| Shawn Pearce | ecddc61 | 2015-02-20 22:09:47 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | public Rule columnWidth(ListVar widths) { |
| 217 | StringBuilderVar s = new StringBuilderVar(); |
| 218 | return sequence( |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 219 | optional(sequence(ch(':'), s.append(':'))), |
| 220 | oneOrMore(digit()), |
| 221 | s.append(match()), |
| 222 | widths.get().add(parse(s.get().toString()))); |
| Shawn Pearce | ecddc61 | 2015-02-20 22:09:47 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static ColsNode.Column parse(String spec) { |
| 226 | ColsNode.Column c = new ColsNode.Column(); |
| 227 | if (spec.startsWith(":")) { |
| 228 | c.empty = true; |
| 229 | spec = spec.substring(1); |
| 230 | } |
| 231 | c.span = Integer.parseInt(spec, 10); |
| 232 | return c; |
| 233 | } |
| 234 | |
| Shawn Pearce | 96f6d5f | 2015-02-06 22:45:58 -0800 | [diff] [blame] | 235 | public List<Node> parse(StringBuilderVar body) { |
| 236 | // The pegdown code doesn't provide enough visibility to directly |
| 237 | // use its existing parsing rules. Recurse manually for inner text |
| 238 | // parsing within a block. |
| 239 | if (parser == null) { |
| Shawn Pearce | 0e5fc54 | 2016-02-15 14:27:05 -0800 | [diff] [blame] | 240 | parser = newParser(parseTimeout); |
| Shawn Pearce | 96f6d5f | 2015-02-06 22:45:58 -0800 | [diff] [blame] | 241 | } |
| 242 | return parser.parseMarkdown(body.getChars()).getChildren(); |
| 243 | } |
| Shawn Pearce | ecddc61 | 2015-02-20 22:09:47 -0800 | [diff] [blame] | 244 | |
| 245 | public static class ListVar extends Var<List<Object>> { |
| 246 | @SuppressWarnings({"rawtypes", "unchecked"}) |
| 247 | public ListVar() { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 248 | super( |
| 249 | new Factory() { |
| 250 | @Override |
| 251 | public Object create() { |
| 252 | return new ArrayList<>(); |
| 253 | } |
| 254 | }); |
| Shawn Pearce | ecddc61 | 2015-02-20 22:09:47 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 257 | } |