| 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 | |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 17 | import com.google.gitiles.doc.html.HtmlBuilder; |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 18 | import com.google.template.soy.shared.restricted.EscapingConventions.FilterImageDataUri; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 19 | import com.google.template.soy.shared.restricted.Sanitizers; |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 20 | import java.util.HashMap; |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 21 | import java.util.Map; |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 22 | import java.util.regex.Matcher; |
| 23 | import java.util.regex.Pattern; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 24 | import org.commonmark.node.Heading; |
| 25 | import org.commonmark.node.Node; |
| 26 | import org.eclipse.jgit.util.RawParseUtils; |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 27 | |
| 28 | class Navbar { |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 29 | private static final Pattern REF_LINK = |
| 30 | Pattern.compile("^\\[(logo|home)\\]:\\s*(.+)$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); |
| 31 | |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 32 | private MarkdownToHtml fmt; |
| 33 | private Node node; |
| 34 | private String siteTitle; |
| 35 | private String logoUrl; |
| 36 | private String homeUrl; |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 37 | |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 38 | Navbar() {} |
| 39 | |
| 40 | Navbar setFormatter(MarkdownToHtml html) { |
| 41 | this.fmt = html; |
| 42 | return this; |
| 43 | } |
| 44 | |
| 45 | Navbar setMarkdown(byte[] md) { |
| 46 | if (md != null && md.length > 0) { |
| 47 | parse(RawParseUtils.decode(md)); |
| 48 | } |
| 49 | return this; |
| 50 | } |
| 51 | |
| 52 | Map<String, Object> toSoyData() { |
| 53 | Map<String, Object> data = new HashMap<>(); |
| 54 | data.put("siteTitle", siteTitle); |
| 55 | data.put("logoUrl", logo()); |
| 56 | data.put("homeUrl", homeUrl != null ? fmt.href(homeUrl) : null); |
| 57 | data.put("navbarHtml", node != null ? fmt.toSoyHtml(node) : null); |
| 58 | return data; |
| 59 | } |
| 60 | |
| 61 | private Object logo() { |
| 62 | if (logoUrl == null) { |
| 63 | return null; |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 66 | String url = fmt.image(logoUrl); |
| 67 | if (HtmlBuilder.isValidHttpUri(url)) { |
| 68 | return url; |
| 69 | } else if (HtmlBuilder.isImageDataUri(url)) { |
| 70 | return Sanitizers.filterImageDataUri(url); |
| 71 | } else { |
| 72 | return FilterImageDataUri.INSTANCE.getInnocuousOutput(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private void parse(String markdown) { |
| 77 | node = GitilesMarkdown.parse(markdown); |
| 78 | |
| 79 | extractSiteTitle(); |
| 80 | extractRefLinks(markdown); |
| 81 | } |
| 82 | |
| 83 | private void extractSiteTitle() { |
| 84 | for (Node c = node.getFirstChild(); c != null; c = c.getNext()) { |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 85 | if (c instanceof Heading) { |
| 86 | Heading h = (Heading) c; |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 87 | if (h.getLevel() == 1) { |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 88 | siteTitle = MarkdownUtil.getInnerText(h); |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 89 | h.unlink(); |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 90 | break; |
| 91 | } |
| 92 | } |
| 93 | } |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 94 | } |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 95 | |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 96 | private void extractRefLinks(String markdown) { |
| 97 | Matcher m = REF_LINK.matcher(markdown); |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 98 | while (m.find()) { |
| 99 | String key = m.group(1).toLowerCase(); |
| 100 | String url = m.group(2).trim(); |
| 101 | switch (key) { |
| 102 | case "logo": |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 103 | logoUrl = url; |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 104 | break; |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 105 | case "home": |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 106 | homeUrl = url; |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 107 | break; |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 108 | } |
| 109 | } |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 110 | } |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 111 | } |