From 4db3d0d9c7625b6d1b521158e7b9e900c667f286 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 15 Sep 2016 00:13:35 +0300 Subject: rendering help using pebble --- juick-www/src/main/java/com/juick/www/Help.java | 49 +++++++++++-------------- juick-www/src/main/java/com/juick/www/Main.java | 11 +++++- juick-www/src/main/resources/help | 1 + juick-www/src/main/resources/views/help.html | 10 +++++ 4 files changed, 43 insertions(+), 28 deletions(-) create mode 160000 juick-www/src/main/resources/help create mode 100644 juick-www/src/main/resources/views/help.html (limited to 'juick-www') 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(""); - - out.println("
"); - out.println("
"); - printFile(out, f); - out.println("
"); - out.println("
"); - - 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 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 index 00000000..491a2f16 --- /dev/null +++ b/juick-www/src/main/resources/help @@ -0,0 +1 @@ +Subproject commit 491a2f167939a81fc6da354c67a1efdb7d60845a 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 %} +
+ {{ content | raw }} +
+{% endblock %} + +{% block "column" %} +{{ navigation | raw }} +{% endblock %} \ No newline at end of file -- cgit v1.2.3