| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -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 | |
| David Pursehouse | f39cadc | 2017-07-07 08:47:51 +0900 | [diff] [blame] | 17 | import static java.util.stream.Collectors.toSet; |
| 18 | |
| Shawn Pearce | f485a31 | 2017-07-01 12:10:27 -0700 | [diff] [blame] | 19 | import com.google.common.base.CharMatcher; |
| 20 | import com.google.common.base.Splitter; |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 21 | import com.google.gitiles.doc.html.HtmlBuilder; |
| Soy Authors | bffb5fd | 2018-02-02 18:38:08 -0500 | [diff] [blame] | 22 | import com.google.template.soy.shared.internal.Sanitizers; |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 23 | import java.util.HashMap; |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 24 | import java.util.Map; |
| Shawn Pearce | f485a31 | 2017-07-01 12:10:27 -0700 | [diff] [blame] | 25 | import java.util.Set; |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 26 | import java.util.regex.Matcher; |
| 27 | import java.util.regex.Pattern; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 28 | import org.commonmark.node.Heading; |
| 29 | import org.commonmark.node.Node; |
| 30 | import org.eclipse.jgit.util.RawParseUtils; |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 31 | |
| 32 | class Navbar { |
| Shawn Pearce | f485a31 | 2017-07-01 12:10:27 -0700 | [diff] [blame] | 33 | private static final Pattern META_LINK = |
| 34 | Pattern.compile( |
| 35 | "^\\[(logo|home|extensions)\\]:\\s*(.+)$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 36 | |
| Shawn Pearce | f485a31 | 2017-07-01 12:10:27 -0700 | [diff] [blame] | 37 | private MarkdownConfig cfg; |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 38 | private MarkdownToHtml fmt; |
| 39 | private Node node; |
| 40 | private String siteTitle; |
| 41 | private String logoUrl; |
| 42 | private String homeUrl; |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 43 | |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 44 | Navbar() {} |
| 45 | |
| Shawn Pearce | f485a31 | 2017-07-01 12:10:27 -0700 | [diff] [blame] | 46 | MarkdownConfig getConfig() { |
| 47 | return cfg; |
| 48 | } |
| 49 | |
| 50 | Navbar setConfig(MarkdownConfig cfg) { |
| 51 | this.cfg = cfg; |
| 52 | return this; |
| 53 | } |
| 54 | |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 55 | Navbar setFormatter(MarkdownToHtml html) { |
| 56 | this.fmt = html; |
| 57 | return this; |
| 58 | } |
| 59 | |
| Shawn Pearce | f485a31 | 2017-07-01 12:10:27 -0700 | [diff] [blame] | 60 | Navbar setMarkdown(byte[] md) { |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 61 | if (md != null && md.length > 0) { |
| Shawn Pearce | f485a31 | 2017-07-01 12:10:27 -0700 | [diff] [blame] | 62 | parse(RawParseUtils.decode(md)); |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 63 | } |
| 64 | return this; |
| 65 | } |
| 66 | |
| 67 | Map<String, Object> toSoyData() { |
| 68 | Map<String, Object> data = new HashMap<>(); |
| 69 | data.put("siteTitle", siteTitle); |
| 70 | data.put("logoUrl", logo()); |
| 71 | data.put("homeUrl", homeUrl != null ? fmt.href(homeUrl) : null); |
| 72 | data.put("navbarHtml", node != null ? fmt.toSoyHtml(node) : null); |
| 73 | return data; |
| 74 | } |
| 75 | |
| 76 | private Object logo() { |
| 77 | if (logoUrl == null) { |
| 78 | return null; |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 81 | String url = fmt.image(logoUrl); |
| 82 | if (HtmlBuilder.isValidHttpUri(url)) { |
| 83 | return url; |
| 84 | } else if (HtmlBuilder.isImageDataUri(url)) { |
| 85 | return Sanitizers.filterImageDataUri(url); |
| 86 | } else { |
| Dave Borowitz | 1218194 | 2018-02-12 09:22:45 -0500 | [diff] [blame] | 87 | return SoyConstants.IMAGE_URI_INNOCUOUS_OUTPUT; |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| Shawn Pearce | f485a31 | 2017-07-01 12:10:27 -0700 | [diff] [blame] | 91 | private void parse(String markdown) { |
| 92 | extractMetadata(markdown); |
| Shawn Pearce | b55cf2b | 2017-06-29 21:56:37 -0700 | [diff] [blame] | 93 | node = GitilesMarkdown.parse(cfg, markdown); |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 94 | extractSiteTitle(); |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | private void extractSiteTitle() { |
| 98 | for (Node c = node.getFirstChild(); c != null; c = c.getNext()) { |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 99 | if (c instanceof Heading) { |
| 100 | Heading h = (Heading) c; |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 101 | if (h.getLevel() == 1) { |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 102 | siteTitle = MarkdownUtil.getInnerText(h); |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 103 | h.unlink(); |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 104 | break; |
| 105 | } |
| 106 | } |
| 107 | } |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 108 | } |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 109 | |
| Shawn Pearce | f485a31 | 2017-07-01 12:10:27 -0700 | [diff] [blame] | 110 | private void extractMetadata(String markdown) { |
| 111 | Matcher m = META_LINK.matcher(markdown); |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 112 | while (m.find()) { |
| 113 | String key = m.group(1).toLowerCase(); |
| 114 | String url = m.group(2).trim(); |
| 115 | switch (key) { |
| 116 | case "logo": |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 117 | logoUrl = url; |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 118 | break; |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 119 | case "home": |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 120 | homeUrl = url; |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 121 | break; |
| Shawn Pearce | f485a31 | 2017-07-01 12:10:27 -0700 | [diff] [blame] | 122 | case "extensions": |
| 123 | Set<String> names = splitExtensionNames(url); |
| 124 | cfg = cfg.copyWithExtensions(enabled(names), disabled(names)); |
| 125 | break; |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 128 | } |
| Shawn Pearce | f485a31 | 2017-07-01 12:10:27 -0700 | [diff] [blame] | 129 | |
| 130 | private static Set<String> splitExtensionNames(String url) { |
| 131 | return Splitter.on(CharMatcher.whitespace().or(CharMatcher.is(','))) |
| 132 | .trimResults() |
| 133 | .omitEmptyStrings() |
| 134 | .splitToList(url) |
| 135 | .stream() |
| 136 | .map(String::toLowerCase) |
| 137 | .collect(toSet()); |
| 138 | } |
| 139 | |
| 140 | private static Set<String> enabled(Set<String> names) { |
| 141 | return names.stream().filter(n -> !n.startsWith("!")).collect(toSet()); |
| 142 | } |
| 143 | |
| 144 | private static Set<String> disabled(Set<String> names) { |
| 145 | return names.stream().filter(n -> n.startsWith("!")).map(n -> n.substring(1)).collect(toSet()); |
| 146 | } |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 147 | } |