Dissolve gerrit-server top-level directory

Change-Id: I538512dfe0f1bea774c01fdd45fa410a45634011
This commit is contained in:
David Ostrovsky
2017-09-21 08:37:42 +02:00
committed by Dave Borowitz
parent 472396c797
commit 376a7bbb64
1549 changed files with 342 additions and 335 deletions

View File

@@ -0,0 +1,26 @@
// Copyright (C) 2013 The Android Open Source Project
//
// 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.gerrit.server.documentation;
public class Constants {
public static final String PACKAGE = "com/google/gerrit/server/documentation";
public static final String INDEX_ZIP = "index.zip";
public static final String DOC_FIELD = "doc";
public static final String TITLE_FIELD = "title";
public static final String URL_FIELD = "url";
private Constants() {}
}

View File

@@ -0,0 +1,158 @@
// Copyright (C) 2012 The Android Open Source Project
//
// 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.gerrit.server.documentation;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.pegdown.Extensions.ALL;
import static org.pegdown.Extensions.HARDWRAPS;
import static org.pegdown.Extensions.SUPPRESS_ALL_HTML;
import com.google.common.base.Strings;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.lang.StringEscapeUtils;
import org.eclipse.jgit.util.RawParseUtils;
import org.eclipse.jgit.util.TemporaryBuffer;
import org.pegdown.LinkRenderer;
import org.pegdown.PegDownProcessor;
import org.pegdown.ToHtmlSerializer;
import org.pegdown.ast.HeaderNode;
import org.pegdown.ast.Node;
import org.pegdown.ast.RootNode;
import org.pegdown.ast.TextNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MarkdownFormatter {
private static final Logger log = LoggerFactory.getLogger(MarkdownFormatter.class);
private static final String defaultCss;
static {
AtomicBoolean file = new AtomicBoolean();
String src;
try {
src = readPegdownCss(file);
} catch (IOException err) {
log.warn("Cannot load pegdown.css", err);
src = "";
}
defaultCss = file.get() ? null : src;
}
private static String readCSS() {
if (defaultCss != null) {
return defaultCss;
}
try {
return readPegdownCss(new AtomicBoolean());
} catch (IOException err) {
log.warn("Cannot load pegdown.css", err);
return "";
}
}
private boolean suppressHtml;
private String css;
public MarkdownFormatter suppressHtml() {
suppressHtml = true;
return this;
}
public MarkdownFormatter setCss(String css) {
this.css = StringEscapeUtils.escapeHtml(css);
return this;
}
public byte[] markdownToDocHtml(String md, String charEnc) throws UnsupportedEncodingException {
RootNode root = parseMarkdown(md);
String title = findTitle(root);
StringBuilder html = new StringBuilder();
html.append("<html>");
html.append("<head>");
if (!Strings.isNullOrEmpty(title)) {
html.append("<title>").append(title).append("</title>");
}
html.append("<style type=\"text/css\">\n");
if (css != null) {
html.append(css);
} else {
html.append(readCSS());
}
html.append("\n</style>");
html.append("</head>");
html.append("<body>\n");
html.append(new ToHtmlSerializer(new LinkRenderer()).toHtml(root));
html.append("\n</body></html>");
return html.toString().getBytes(charEnc);
}
public String extractTitleFromMarkdown(byte[] data, String charEnc) {
String md = RawParseUtils.decode(Charset.forName(charEnc), data);
return findTitle(parseMarkdown(md));
}
private String findTitle(Node root) {
if (root instanceof HeaderNode) {
HeaderNode h = (HeaderNode) root;
if (h.getLevel() == 1 && h.getChildren() != null && !h.getChildren().isEmpty()) {
StringBuilder b = new StringBuilder();
for (Node n : root.getChildren()) {
if (n instanceof TextNode) {
b.append(((TextNode) n).getText());
}
}
return b.toString();
}
}
for (Node n : root.getChildren()) {
String title = findTitle(n);
if (title != null) {
return title;
}
}
return null;
}
private RootNode parseMarkdown(String md) {
int options = ALL & ~(HARDWRAPS);
if (suppressHtml) {
options |= SUPPRESS_ALL_HTML;
}
return new PegDownProcessor(options).parseMarkdown(md.toCharArray());
}
private static String readPegdownCss(AtomicBoolean file) throws IOException {
String name = "pegdown.css";
URL url = MarkdownFormatter.class.getResource(name);
if (url == null) {
throw new FileNotFoundException("Resource " + name);
}
file.set("file".equals(url.getProtocol()));
try (InputStream in = url.openStream();
TemporaryBuffer.Heap tmp = new TemporaryBuffer.Heap(128 * 1024)) {
tmp.copy(in);
return new String(tmp.toByteArray(), UTF_8);
}
}
}

View File

@@ -0,0 +1,149 @@
// Copyright (C) 2013 The Android Open Source Project
//
// 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.gerrit.server.documentation;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.simple.SimpleQueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.RAMDirectory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
public class QueryDocumentationExecutor {
private static final Logger log = LoggerFactory.getLogger(QueryDocumentationExecutor.class);
private static Map<String, Float> WEIGHTS =
ImmutableMap.of(
Constants.TITLE_FIELD, 2.0f,
Constants.DOC_FIELD, 1.0f);
private IndexSearcher searcher;
private SimpleQueryParser parser;
public static class DocResult {
public String title;
public String url;
public String content;
}
@Inject
public QueryDocumentationExecutor() {
try {
Directory dir = readIndexDirectory();
if (dir == null) {
searcher = null;
parser = null;
return;
}
IndexReader reader = DirectoryReader.open(dir);
searcher = new IndexSearcher(reader);
parser = new SimpleQueryParser(new StandardAnalyzer(), WEIGHTS);
} catch (IOException e) {
log.error("Cannot initialize documentation full text index", e);
searcher = null;
parser = null;
}
}
public List<DocResult> doQuery(String q) throws DocQueryException {
if (!isAvailable()) {
throw new DocQueryException("Documentation search not available");
}
Query query = parser.parse(q);
try {
// TODO(fishywang): Currently as we don't have much documentation, we just use MAX_VALUE here
// and skipped paging. Maybe add paging later.
TopDocs results = searcher.search(query, Integer.MAX_VALUE);
ScoreDoc[] hits = results.scoreDocs;
int totalHits = results.totalHits;
List<DocResult> out = Lists.newArrayListWithCapacity(totalHits);
for (int i = 0; i < totalHits; i++) {
DocResult result = new DocResult();
Document doc = searcher.doc(hits[i].doc);
result.url = doc.get(Constants.URL_FIELD);
result.title = doc.get(Constants.TITLE_FIELD);
out.add(result);
}
return out;
} catch (IOException e) {
throw new DocQueryException(e);
}
}
protected Directory readIndexDirectory() throws IOException {
Directory dir = new RAMDirectory();
byte[] buffer = new byte[4096];
InputStream index = getClass().getResourceAsStream(Constants.INDEX_ZIP);
if (index == null) {
log.warn("No index available");
return null;
}
try (ZipInputStream zip = new ZipInputStream(index)) {
ZipEntry entry;
while ((entry = zip.getNextEntry()) != null) {
try (IndexOutput out = dir.createOutput(entry.getName(), null)) {
int count;
while ((count = zip.read(buffer)) != -1) {
out.writeBytes(buffer, count);
}
}
}
}
// We must NOT call dir.close() here, as DirectoryReader.open() expects an opened directory.
return dir;
}
public boolean isAvailable() {
return parser != null && searcher != null;
}
@SuppressWarnings("serial")
public static class DocQueryException extends Exception {
DocQueryException() {}
DocQueryException(String msg) {
super(msg);
}
DocQueryException(String msg, Throwable e) {
super(msg, e);
}
DocQueryException(Throwable e) {
super(e);
}
}
}