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