Support macros to get project name and project web URL
In Markdown files '@PROJECT@' and '@PROJECT_URL@' can now be used as
placeholders for the name and the web URL of the project from which
the documentation is shown.
Change-Id: I24fac86b194847d4bc1df5de2387f6d88e68c648
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java
index 4f790d3..366ab22 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java
@@ -22,6 +22,7 @@
import com.google.common.collect.Maps;
import com.google.gerrit.httpd.resources.Resource;
import com.google.gerrit.httpd.resources.SmallResource;
+import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.cache.CacheModule;
import com.google.gerrit.server.config.CanonicalWebUrl;
import com.google.gerrit.server.documentation.MarkdownFormatter;
@@ -78,8 +79,8 @@
ObjectId objectId = tw.getObjectId(0);
ObjectLoader loader = repo.open(objectId);
byte[] md = loader.getBytes(Integer.MAX_VALUE);
- return getMarkdownAsHtmlResource(new String(md, UTF_8),
- commit.getCommitTime());
+ return getMarkdownAsHtmlResource(key.getProject(),
+ new String(md, UTF_8), commit.getCommitTime());
} finally {
tw.release();
}
@@ -91,24 +92,29 @@
}
}
- private Resource getMarkdownAsHtmlResource(String md, int lastModified)
+ private Resource getMarkdownAsHtmlResource(Project.NameKey project,
+ String md, int lastModified)
throws IOException {
byte[] html = new MarkdownFormatter().suppressHtml()
- .markdownToDocHtml(replaceMacros(md), UTF_8.name());
+ .markdownToDocHtml(replaceMacros(project, md), UTF_8.name());
return new SmallResource(html)
.setContentType("text/html")
.setCharacterEncoding(UTF_8.name())
.setLastModified(lastModified);
}
- private String replaceMacros(String md) {
+ private String replaceMacros(Project.NameKey project, String md) {
Map<String, String> macros = Maps.newHashMap();
+
String url = webUrl.get();
if (Strings.isNullOrEmpty(url)) {
url = "http://" + DEFAULT_HOST + "/";
}
macros.put("URL", url);
+ macros.put("PROJECT", project.get());
+ macros.put("PROJECT_URL", url + "#/admin/projects/" + project.get());
+
Matcher m = Pattern.compile("(\\\\)?@([A-Z_]+)@").matcher(md);
StringBuffer sb = new StringBuffer();
while (m.find()) {
diff --git a/src/main/resources/Documentation/user.md b/src/main/resources/Documentation/user.md
index d7c361a..1ea0c4f 100644
--- a/src/main/resources/Documentation/user.md
+++ b/src/main/resources/Documentation/user.md
@@ -73,3 +73,5 @@
replaced when the HTML pages are generated.
* `\@URL@`: The web URL of the Gerrit server.
+* `\@PROJECT@`: The name of the project from which the documentation is shown.
+* `\@PROJECT_URL@`: The web URL of the project from which the documentation is shown.