-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathNavbar.java
More file actions
156 lines (137 loc) · 4.42 KB
/
Navbar.java
File metadata and controls
156 lines (137 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gitiles.doc;
import static java.util.stream.Collectors.toSet;
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.gitiles.doc.html.HtmlBuilder;
import com.google.template.soy.shared.internal.Sanitizers;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import org.commonmark.node.Heading;
import org.commonmark.node.Node;
import org.eclipse.jgit.util.RawParseUtils;
class Navbar {
private static final Pattern META_LINK =
Pattern.compile(
"^\\[(logo|home|extensions)\\]:\\s*(.+)$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
private static final Pattern EXTENSIONS_LINK =
Pattern.compile("^\\[extensions\\]:\\s*(.+)$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
private MarkdownConfig cfg;
private MarkdownToHtml fmt;
private Node node;
private String siteTitle;
private String logoUrl;
private String homeUrl;
Navbar() {}
MarkdownConfig getConfig() {
return cfg;
}
Navbar setConfig(MarkdownConfig cfg) {
this.cfg = cfg;
return this;
}
Navbar setFormatter(MarkdownToHtml html) {
this.fmt = html;
return this;
}
Navbar setMarkdown(byte[] md) {
if (md != null && md.length > 0) {
parse(RawParseUtils.decode(md));
}
return this;
}
Map<String, Object> toSoyData() {
Map<String, Object> data = new HashMap<>();
data.put("siteTitle", siteTitle);
data.put("logoUrl", logo());
data.put("homeUrl", homeUrl != null ? fmt.href(homeUrl) : null);
data.put("navbarHtml", node != null ? fmt.toSoyHtml(node) : null);
return data;
}
private @Nullable Object logo() {
if (logoUrl == null) {
return null;
}
String url = fmt.image(logoUrl);
if (HtmlBuilder.isValidHttpUri(url)) {
return url;
} else if (HtmlBuilder.isImageDataUri(url)) {
return Sanitizers.filterImageDataUri(url);
} else {
return SoyConstants.IMAGE_URI_INNOCUOUS_OUTPUT;
}
}
private void parse(String markdown) {
String md = extractMetadata(markdown);
node = GitilesMarkdown.parse(cfg, md);
extractSiteTitle();
}
private void extractSiteTitle() {
for (Node c = node.getFirstChild(); c != null; c = c.getNext()) {
if (c instanceof Heading) {
Heading h = (Heading) c;
if (h.getLevel() == 1) {
siteTitle = MarkdownUtil.getInnerText(h);
h.unlink();
break;
}
}
}
}
private String extractMetadata(String markdown) {
boolean extensionsFound = false;
Matcher m = META_LINK.matcher(markdown);
while (m.find()) {
String key = m.group(1).toLowerCase();
String url = m.group(2).trim();
switch (key) {
case "logo":
logoUrl = url;
break;
case "home":
homeUrl = url;
break;
case "extensions":
extensionsFound = true;
Set<String> names = splitExtensionNames(url);
cfg = cfg.copyWithExtensions(enabled(names), disabled(names));
break;
}
}
if (extensionsFound) {
return EXTENSIONS_LINK.matcher(markdown).replaceAll("");
}
return markdown;
}
private static Set<String> splitExtensionNames(String url) {
return Splitter.on(CharMatcher.whitespace().or(CharMatcher.is(',')))
.trimResults()
.omitEmptyStrings()
.splitToList(url)
.stream()
.map(String::toLowerCase)
.collect(toSet());
}
private static Set<String> enabled(Set<String> names) {
return names.stream().filter(n -> !n.startsWith("!")).collect(toSet());
}
private static Set<String> disabled(Set<String> names) {
return names.stream().filter(n -> n.startsWith("!")).map(n -> n.substring(1)).collect(toSet());
}
}