| | |
| | | */
|
| | | package com.gitblit.wicket;
|
| | |
|
| | | import static org.pegdown.FastEncoder.encode;
|
| | |
|
| | | import java.io.Serializable;
|
| | | import java.io.StringWriter;
|
| | | import java.io.UnsupportedEncodingException;
|
| | | import java.net.URLEncoder;
|
| | | import java.text.MessageFormat;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Arrays;
|
| | | import java.util.Collections;
|
| | | import java.util.HashMap;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | |
|
| | | import org.apache.wicket.Page;
|
| | | import org.apache.wicket.RequestCycle;
|
| | |
| | | import org.eclipse.mylyn.wikitext.textile.core.TextileLanguage;
|
| | | import org.eclipse.mylyn.wikitext.tracwiki.core.TracWikiLanguage;
|
| | | import org.eclipse.mylyn.wikitext.twiki.core.TWikiLanguage;
|
| | | import org.pegdown.DefaultVerbatimSerializer;
|
| | | import org.pegdown.LinkRenderer;
|
| | | import org.pegdown.ToHtmlSerializer;
|
| | | import org.pegdown.VerbatimSerializer;
|
| | | import org.pegdown.ast.ExpImageNode;
|
| | | import org.pegdown.ast.RefImageNode;
|
| | | import org.pegdown.ast.WikiLinkNode;
|
| | | import org.pegdown.plugins.ToHtmlSerializerPlugin;
|
| | | import org.slf4j.Logger;
|
| | | import org.slf4j.LoggerFactory;
|
| | |
|
| | |
| | | import com.gitblit.utils.StringUtils;
|
| | | import com.gitblit.wicket.pages.DocPage;
|
| | | import com.gitblit.wicket.pages.RawPage;
|
| | | import com.google.common.base.Joiner;
|
| | |
|
| | | /**
|
| | | * Processes markup content and generates html with repository-relative page and
|
| | |
| | | return list;
|
| | | }
|
| | |
|
| | | public List<String> getAllExtensions() {
|
| | | List<String> list = getMarkupExtensions();
|
| | | list.add("txt");
|
| | | list.add("TXT");
|
| | | return list;
|
| | | }
|
| | |
|
| | | private List<String> getRoots() {
|
| | | return settings.getStrings(Keys.web.documents);
|
| | | }
|
| | |
|
| | | private String [] getEncodings() {
|
| | | return settings.getStrings(Keys.web.blobEncodings).toArray(new String[0]);
|
| | | }
|
| | |
|
| | | private MarkupSyntax determineSyntax(String documentPath) {
|
| | | String ext = StringUtils.getFileExtension(documentPath).toLowerCase();
|
| | | if (StringUtils.isEmpty(ext)) {
|
| | |
| | | return MarkupSyntax.PLAIN;
|
| | | }
|
| | |
|
| | | public MarkupDocument parseReadme(Repository r, String repositoryName, String commitId) {
|
| | | String readme = null;
|
| | | RevCommit commit = JGitUtils.getCommit(r, commitId);
|
| | | List<PathModel> paths = JGitUtils.getFilesInPath(r, null, commit);
|
| | | public boolean hasRootDocs(Repository r) {
|
| | | List<String> roots = getRoots();
|
| | | List<String> extensions = getAllExtensions();
|
| | | List<PathModel> paths = JGitUtils.getFilesInPath(r, null, null);
|
| | | for (PathModel path : paths) {
|
| | | if (!path.isTree()) {
|
| | | String name = path.name.toLowerCase();
|
| | | if (name.equals("readme") || name.equals("readme.txt")) {
|
| | | readme = path.name;
|
| | | break;
|
| | | } else if (name.startsWith("readme.")) {
|
| | | String ext = StringUtils.getFileExtension(name).toLowerCase();
|
| | | if (getMarkupExtensions().contains(ext)) {
|
| | | readme = path.name;
|
| | | break;
|
| | | String ext = StringUtils.getFileExtension(path.name).toLowerCase();
|
| | | String name = StringUtils.stripFileExtension(path.name).toLowerCase();
|
| | |
|
| | | if (roots.contains(name)) {
|
| | | if (StringUtils.isEmpty(ext) || extensions.contains(ext)) {
|
| | | return true;
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | if (!StringUtils.isEmpty(readme)) {
|
| | | String [] encodings = settings.getStrings(Keys.web.blobEncodings).toArray(new String[0]);
|
| | | String markup = JGitUtils.getStringContent(r, commit.getTree(), readme, encodings);
|
| | | return parse(repositoryName, commitId, readme, markup);
|
| | | public List<MarkupDocument> getRootDocs(Repository r, String repositoryName, String commitId) {
|
| | | List<String> roots = getRoots();
|
| | | List<MarkupDocument> list = getDocs(r, repositoryName, commitId, roots);
|
| | | return list;
|
| | | }
|
| | |
|
| | | public MarkupDocument getReadme(Repository r, String repositoryName, String commitId) {
|
| | | List<MarkupDocument> list = getDocs(r, repositoryName, commitId, Arrays.asList("readme"));
|
| | | if (list.isEmpty()) {
|
| | | return null;
|
| | | }
|
| | | return list.get(0);
|
| | | }
|
| | |
|
| | | return null;
|
| | | private List<MarkupDocument> getDocs(Repository r, String repositoryName, String commitId, List<String> names) {
|
| | | List<String> extensions = getAllExtensions();
|
| | | String [] encodings = getEncodings();
|
| | | Map<String, MarkupDocument> map = new HashMap<String, MarkupDocument>();
|
| | | RevCommit commit = JGitUtils.getCommit(r, commitId);
|
| | | List<PathModel> paths = JGitUtils.getFilesInPath(r, null, commit);
|
| | | for (PathModel path : paths) {
|
| | | if (!path.isTree()) {
|
| | | String ext = StringUtils.getFileExtension(path.name).toLowerCase();
|
| | | String name = StringUtils.stripFileExtension(path.name).toLowerCase();
|
| | |
|
| | | if (names.contains(name)) {
|
| | | if (StringUtils.isEmpty(ext) || extensions.contains(ext)) {
|
| | | String markup = JGitUtils.getStringContent(r, commit.getTree(), path.name, encodings);
|
| | | MarkupDocument doc = parse(repositoryName, commitId, path.name, markup);
|
| | | map.put(name, doc);
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | // return document list in requested order
|
| | | List<MarkupDocument> list = new ArrayList<MarkupDocument>();
|
| | | for (String name : names) {
|
| | | if (map.containsKey(name)) {
|
| | | list.add(map.get(name));
|
| | | }
|
| | | }
|
| | | return list;
|
| | | }
|
| | |
|
| | | public MarkupDocument parse(String repositoryName, String commitId, String documentPath, String markupText) {
|
| | |
| | | */
|
| | | private void parse(final MarkupDocument doc, final String repositoryName, final String commitId) {
|
| | | LinkRenderer renderer = new LinkRenderer() {
|
| | |
|
| | | @Override
|
| | | public Rendering render(ExpImageNode node, String text) {
|
| | | if (node.url.indexOf("://") == -1) {
|
| | | // repository-relative image link
|
| | | String path = doc.getRelativePath(node.url);
|
| | | String url = getWicketUrl(RawPage.class, repositoryName, commitId, path);
|
| | | return new Rendering(url, text);
|
| | | }
|
| | | // absolute image link
|
| | | return new Rendering(node.url, text);
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Rendering render(RefImageNode node, String url, String title, String alt) {
|
| | | Rendering rendering;
|
| | | if (url.indexOf("://") == -1) {
|
| | | // repository-relative image link
|
| | | String path = doc.getRelativePath(url);
|
| | | String wurl = getWicketUrl(RawPage.class, repositoryName, commitId, path);
|
| | | rendering = new Rendering(wurl, alt);
|
| | | } else {
|
| | | // absolute image link
|
| | | rendering = new Rendering(url, alt);
|
| | | }
|
| | | return StringUtils.isEmpty(title) ? rendering : rendering.withAttribute("title", encode(title));
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Rendering render(WikiLinkNode node) {
|
| | | String path = doc.getRelativePath(node.getText());
|
| | |
| | | }
|
| | |
|
| | | String getRelativePath(String ref) {
|
| | | return ref.charAt(0) == '/' ? ref.substring(1) : (getCurrentPath() + ref);
|
| | | if (ref.charAt(0) == '/') {
|
| | | // absolute path in repository
|
| | | return ref.substring(1);
|
| | | } else {
|
| | | // resolve relative repository path
|
| | | String cp = getCurrentPath();
|
| | | if (StringUtils.isEmpty(cp)) {
|
| | | return ref;
|
| | | }
|
| | | // this is a simple relative path resolver
|
| | | List<String> currPathStrings = new ArrayList<String>(Arrays.asList(cp.split("/")));
|
| | | String file = ref;
|
| | | while (file.startsWith("../")) {
|
| | | // strip ../ from the file reference
|
| | | // drop the last path element
|
| | | file = file.substring(3);
|
| | | currPathStrings.remove(currPathStrings.size() - 1);
|
| | | }
|
| | | currPathStrings.add(file);
|
| | | String path = Joiner.on("/").join(currPathStrings);
|
| | | return path;
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | /**
|
| | | * This class implements a workaround for a bug reported in issue-379.
|
| | | * The bug was introduced by my own pegdown pull request #115.
|
| | | *
|
| | | * @author James Moger
|
| | | *
|
| | | */
|
| | | public static class WorkaroundHtmlSerializer extends ToHtmlSerializer {
|
| | |
|
| | | public WorkaroundHtmlSerializer(final LinkRenderer linkRenderer) {
|
| | | super(linkRenderer,
|
| | | Collections.<String, VerbatimSerializer>singletonMap(VerbatimSerializer.DEFAULT, DefaultVerbatimSerializer.INSTANCE),
|
| | | Collections.<ToHtmlSerializerPlugin>emptyList());
|
| | | }
|
| | | private void printAttribute(String name, String value) {
|
| | | printer.print(' ').print(name).print('=').print('"').print(value).print('"');
|
| | | }
|
| | |
|
| | | /* Reimplement print image tag to eliminate a trailing double-quote */
|
| | | @Override
|
| | | protected void printImageTag(LinkRenderer.Rendering rendering) {
|
| | | printer.print("<img");
|
| | | printAttribute("src", rendering.href);
|
| | | printAttribute("alt", rendering.text);
|
| | | for (LinkRenderer.Attribute attr : rendering.attributes) {
|
| | | printAttribute(attr.name, attr.value);
|
| | | }
|
| | | printer.print("/>");
|
| | | }
|
| | | }
|
| | | }
|