blob: 1f54845737d9b7174bdb32fa6d8df4bc8572030d [file] [log] [blame]
Shawn Pearce99cdbce2015-02-10 12:05:45 -08001// 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
15package com.google.gitiles.doc;
16
17import com.google.gitiles.GitilesView;
18import com.google.gitiles.doc.html.HtmlBuilder;
19import com.google.template.soy.shared.restricted.Sanitizers;
20
21import org.pegdown.ast.HeaderNode;
22import org.pegdown.ast.Node;
23import org.pegdown.ast.ReferenceNode;
24import org.pegdown.ast.RootNode;
25
26import java.util.HashMap;
27import java.util.Iterator;
28import java.util.Map;
29
30class 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 Pearce108599e2015-02-11 13:28:37 -080046 data.put("siteTitle", MarkdownUtil.getInnerText(h));
Shawn Pearce99cdbce2015-02-10 12:05:45 -080047 i.remove();
48 break;
49 }
50 }
51 }
52
53 for (ReferenceNode r : nav.getReferences()) {
Shawn Pearce108599e2015-02-11 13:28:37 -080054 String key = MarkdownUtil.getInnerText(r);
Shawn Pearce99cdbce2015-02-10 12:05:45 -080055 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 Pearce108599e2015-02-11 13:28:37 -080065 if (MarkdownUtil.isAbsolutePathToMarkdown(url)) {
Shawn Pearce99cdbce2015-02-10 12:05:45 -080066 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}