Skip to content

Commit

Permalink
fixed #141. modify to return static html.
Browse files Browse the repository at this point in the history
  • Loading branch information
garbagetown committed Jul 10, 2012
1 parent 37915fc commit 2113f7f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -13,4 +13,5 @@ tmp/bytecode/*
test-result/* test-result/*
lib/* lib/*
modules/* modules/*
html/*


84 changes: 60 additions & 24 deletions app/controllers/Documentation.java
Expand Up @@ -14,6 +14,7 @@
import play.*; import play.*;
import play.libs.*; import play.libs.*;
import play.mvc.*; import play.mvc.*;
import play.templates.*;


/** /**
* Documentation controller. * Documentation controller.
Expand Down Expand Up @@ -47,40 +48,75 @@ public class Documentation extends Controller {
* @param id * @param id
*/ */
public static void page(String version, String id) { public static void page(String version, String id) {
List<String> versions = Documentation.versions;

if (isEmpty(version) || version.equals("latest")) { if (isEmpty(version) || version.equals("latest")) {
redirect(String.format("/documentation/%s/%s", latestVersion, id)); redirect(String.format("/documentation/%s/%s", latestVersion, id));
} }
if (isEmpty(id) || id.equalsIgnoreCase("null")) { if (isEmpty(id) || id.equalsIgnoreCase("null")) {
String home = version.startsWith("1") ? "home" : "Home"; String home = isTextile(version) ? "home" : "Home";
redirect(String.format("/documentation/%s/%s", version, home)); redirect(String.format("/documentation/%s/%s", version, home));
} }

String html = getHtml(version, id);
File root = new File(Play.applicationPath, String.format("documentation/%s/manual/", version)); if (Play.mode == Play.Mode.DEV && !isTextile(version)) {
String ext = version.startsWith("1") ? "textile" : "md"; // save static html file from pegdown template.
File page = findDown(root, id, ext); File dir = new File(Play.applicationPath, String.format("html/%s", version));
if (page == null || !page.exists()) { if (!dir.exists()) {
notFound(request.path); dir.mkdirs();
}
IO.writeContent(html, new File(dir, String.format("%s.html", id)));
} }
String article = null; renderHtml(getHtml(version, id));
String navigation = null; }
if (version.startsWith("1")) {
article = Textile.toHTML(IO.readContentAsString(page)); private static boolean isTextile(String version) {
navigation = null; return version.startsWith("1");
}

private static String getHtml(String version, String id) {
String html = null;
if (Play.mode == Play.Mode.PROD && !isTextile(version)) {
// get content from static html file because pegdown is not
// available on GAE/J due to security reason.
File file = new File(Play.applicationPath, String.format("html/%s/%s.html", version, id));
if (file == null || !file.exists()) {
notFound(request.path);
}
html = IO.readContentAsString(file);
} else { } else {
PegDownProcessor processor = new PegDownProcessor(Extensions.ALL); File root = new File(Play.applicationPath, String.format("documentation/%s/manual/", version));
LinkRenderer renderer = new GithubLinkRenderer(page); String ext = isTextile(version) ? "textile" : "md";
article = processor.markdownToHtml(IO.readContentAsString(page), renderer); File page = findDown(root, id, ext);
File sidebar = findUp(page.getParentFile(), "_Sidebar", "md"); if (page == null || !page.exists()) {
if (sidebar.exists()) { notFound(request.path);
navigation = processor.markdownToHtml(IO.readContentAsString(sidebar), renderer); }
String article = null;
String navigation = null;
if (isTextile(version)) {
article = Textile.toHTML(IO.readContentAsString(page));
navigation = null;
} else {
PegDownProcessor processor = new PegDownProcessor(Extensions.ALL);
LinkRenderer renderer = new GithubLinkRenderer(page);
article = processor.markdownToHtml(IO.readContentAsString(page), renderer);
File sidebar = findUp(page.getParentFile(), "_Sidebar", "md");
if (sidebar.exists()) {
navigation = processor.markdownToHtml(IO.readContentAsString(sidebar), renderer);
}
} }
article = replaceHref(version, article);
article = markAbsent(root, article, ext);
navigation = markAbsent(root, navigation, ext);

Template template = TemplateLoader.load("Documentation/page.html");
Map<String, Object> args = new HashMap<String, Object>();
args.put("request", request);
args.put("versions", versions);
args.put("version", version);
args.put("id", id);
args.put("article", article);
args.put("navigation", navigation);
html = template.render(args);
} }
article = replaceHref(version, article); return html;
article = markAbsent(root, article, ext);
navigation = markAbsent(root, navigation, ext);
render(versions, version, id, article, navigation);
} }


private static File findDown(File dir, String id, String ext) { private static File findDown(File dir, String id, String ext) {
Expand Down
2 changes: 1 addition & 1 deletion war/WEB-INF/appengine-web.xml
@@ -1,6 +1,6 @@
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> <appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>playdocja</application> <application>playdocja</application>
<version>20120705</version> <version>20120710</version>
<threadsafe>true</threadsafe> <threadsafe>true</threadsafe>
</appengine-web-app> </appengine-web-app>


0 comments on commit 2113f7f

Please sign in to comment.