| 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 | |
| 17 | import com.google.gitiles.GitilesView; |
| 18 | import com.google.gitiles.doc.html.HtmlBuilder; |
| 19 | import com.google.template.soy.shared.restricted.Sanitizers; |
| 20 | |
| 21 | import org.pegdown.ast.HeaderNode; |
| 22 | import org.pegdown.ast.Node; |
| 23 | import org.pegdown.ast.ReferenceNode; |
| 24 | import org.pegdown.ast.RootNode; |
| 25 | |
| 26 | import java.util.HashMap; |
| 27 | import java.util.Iterator; |
| 28 | import java.util.Map; |
| 29 | |
| 30 | class Navbar { |
| 31 | static Map<String, Object> bannerSoyData(GitilesView view, RootNode nav) { |
| 32 | Map<String, Object> data = new HashMap<>(); |
| 33 | data.put("siteTitle", null); |
| 34 | data.put("logoUrl", null); |
| 35 | data.put("homeUrl", null); |
| 36 | |
| 37 | if (nav == null) { |
| 38 | return data; |
| 39 | } |
| 40 | |
| 41 | for (Iterator<Node> i = nav.getChildren().iterator(); i.hasNext();) { |
| 42 | Node n = i.next(); |
| 43 | if (n instanceof HeaderNode) { |
| 44 | HeaderNode h = (HeaderNode) n; |
| 45 | if (h.getLevel() == 1) { |
| Shawn Pearce | 108599e | 2015-02-11 13:28:37 -0800 | [diff] [blame] | 46 | data.put("siteTitle", MarkdownUtil.getInnerText(h)); |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 47 | i.remove(); |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | for (ReferenceNode r : nav.getReferences()) { |
| Shawn Pearce | 108599e | 2015-02-11 13:28:37 -0800 | [diff] [blame] | 54 | String key = MarkdownUtil.getInnerText(r); |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 55 | String url = r.getUrl(); |
| 56 | if ("logo".equalsIgnoreCase(key)) { |
| 57 | Object src; |
| 58 | if (HtmlBuilder.isImageDataUri(url)) { |
| 59 | src = Sanitizers.filterImageDataUri(url); |
| 60 | } else { |
| 61 | src = url; |
| 62 | } |
| 63 | data.put("logoUrl", src); |
| 64 | } else if ("home".equalsIgnoreCase(key)) { |
| Shawn Pearce | 108599e | 2015-02-11 13:28:37 -0800 | [diff] [blame] | 65 | if (MarkdownUtil.isAbsolutePathToMarkdown(url)) { |
| Shawn Pearce | 99cdbce | 2015-02-10 12:05:45 -0800 | [diff] [blame] | 66 | url = GitilesView.doc().copyFrom(view).setPathPart(url).toUrl(); |
| 67 | } |
| 68 | data.put("homeUrl", url); |
| 69 | } |
| 70 | } |
| 71 | return data; |
| 72 | } |
| 73 | |
| 74 | private Navbar() { |
| 75 | } |
| 76 | } |