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