aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-09-15 00:13:35 +0300
committerGravatar Vitaly Takmazov2016-09-15 00:13:35 +0300
commit4db3d0d9c7625b6d1b521158e7b9e900c667f286 (patch)
treeeb374abaeb62e2fdf3bbd7d0d6fa38463e631e5f
parent5a14a313d6bc70f821e1bd513d025109a5c15906 (diff)
rendering help using pebble
-rw-r--r--.gitmodules5
-rw-r--r--juick-www/src/main/java/com/juick/www/Help.java49
-rw-r--r--juick-www/src/main/java/com/juick/www/Main.java11
m---------juick-www/src/main/resources/help0
-rw-r--r--juick-www/src/main/resources/views/help.html10
5 files changed, 46 insertions, 29 deletions
diff --git a/.gitmodules b/.gitmodules
index fa6a79a6..4c3e6649 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,3 +1,6 @@
[submodule "deps/com.juick.xmpp"]
path = deps/com.juick.xmpp
- url = https://github.com/juick/com.juick.xmpp.git \ No newline at end of file
+ url = https://github.com/juick/com.juick.xmpp.git
+[submodule "juick-www/src/main/resources/help"]
+ path = juick-www/src/main/resources/help
+ url = https://github.com/juick/help.git
diff --git a/juick-www/src/main/java/com/juick/www/Help.java b/juick-www/src/main/java/com/juick/www/Help.java
index a8bfaf3f..301d2edc 100644
--- a/juick-www/src/main/java/com/juick/www/Help.java
+++ b/juick-www/src/main/java/com/juick/www/Help.java
@@ -17,12 +17,22 @@
*/
package com.juick.www;
+import com.mitchellbosecke.pebble.error.PebbleException;
+import com.mitchellbosecke.pebble.template.PebbleTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import java.io.*;
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
/**
*
@@ -34,7 +44,7 @@ public class Help {
Utils.sendTemporaryRedirect(response, "/help/ru/");
}
- protected void doGetHelp(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ protected void doGetHelp(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, PebbleException, URISyntaxException {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
String path[] = request.getRequestURI().split("/");
@@ -54,37 +64,22 @@ public class Help {
page = "index";
}
- File f = new File("/var/www/juick.com/help/" + path[2] + "/" + page);
- if (!f.isFile()) {
+ URL f = getClass().getResource("/help/" + path[2] + "/" + page);
+ if (f == null) {
Errors.doGet404(sql, request, response);
return;
}
response.setContentType("text/html; charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
- PageTemplates.pageHead(out, visitor, "Помощь", null);
- PageTemplates.pageNavigation(out, visitor, null);
-
- out.println("<aside id=\"column\">");
- printFile(out, new File("/var/www/juick.com/help/" + path[2] + "/navigation"));
- out.println("</aside>");
-
- out.println("<section id=\"content\">");
- out.println("<article>");
- printFile(out, f);
- out.println("</article>");
- out.println("</section>");
-
- PageTemplates.pageFooter(request, out, visitor, false);
- PageTemplates.pageEnd(out);
- }
- }
-
- private void printFile(PrintWriter out, File f) throws IOException {
- BufferedReader br = new BufferedReader(new FileReader(f));
- String str;
- while ((str = br.readLine()) != null) {
- out.println(str);
+ PebbleTemplate template = Utils.getEngine().getTemplate("views/help.html");
+ Map<String, Object> context = new HashMap<>();
+ context.put("title", "Помощь");
+ context.put("visitor", visitor);
+ context.put("content", new String(Files.readAllBytes(Paths.get(f.toURI()))));
+ context.put("navigation", new String(Files.readAllBytes(
+ new File(getClass().getResource("/help/" + path[2] + "/navigation").toURI()).toPath())));
+ template.evaluate(out, context);
}
}
}
diff --git a/juick-www/src/main/java/com/juick/www/Main.java b/juick-www/src/main/java/com/juick/www/Main.java
index fa016edd..57704f36 100644
--- a/juick-www/src/main/java/com/juick/www/Main.java
+++ b/juick-www/src/main/java/com/juick/www/Main.java
@@ -38,6 +38,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.Socket;
+import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.Objects;
import java.util.Properties;
@@ -233,7 +234,15 @@ public class Main extends HttpServlet implements Stream.StreamListener {
} else if (uri.equals("/help") || uri.equals("/help/")) {
help.doRedirectToHelpIndex(response);
} else if (uri.startsWith("/help/")) {
- help.doGetHelp(sql, request, response);
+ try {
+ help.doGetHelp(sql, request, response);
+ } catch (PebbleException e) {
+ log("pebble exception", e);
+ response.sendError(500);
+ } catch (URISyntaxException e) {
+ log("help exception", e);
+ response.sendError(500);
+ }
} else if (uri.startsWith("/tag/")) {
discover.doGet(sql, sqlSearch, request, response);
} else if (uri.matches("^/\\d+$")) {
diff --git a/juick-www/src/main/resources/help b/juick-www/src/main/resources/help
new file mode 160000
+Subproject 491a2f167939a81fc6da354c67a1efdb7d60845
diff --git a/juick-www/src/main/resources/views/help.html b/juick-www/src/main/resources/views/help.html
new file mode 100644
index 00000000..3d9960d1
--- /dev/null
+++ b/juick-www/src/main/resources/views/help.html
@@ -0,0 +1,10 @@
+{% extends "layouts/content.html" %}
+{% block content %}
+<article>
+ {{ content | raw }}
+</article>
+{% endblock %}
+
+{% block "column" %}
+{{ navigation | raw }}
+{% endblock %} \ No newline at end of file