From df58ac0c9854cc33606e7379f1e87a00b0ef8a2d Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 18 Aug 2017 16:22:05 +0300 Subject: www: render help from markdown --- .../src/main/java/com/juick/www/HelpService.java | 6 ++++- .../www/configuration/WwwAppConfiguration.java | 25 +++++++++++++++++++ .../www/configuration/WwwServletConfiguration.java | 12 +++++++++ .../main/java/com/juick/www/controllers/Help.java | 29 ++++++++++------------ juick-www/src/main/resources/help | 2 +- 5 files changed, 56 insertions(+), 18 deletions(-) (limited to 'juick-www/src') diff --git a/juick-www/src/main/java/com/juick/www/HelpService.java b/juick-www/src/main/java/com/juick/www/HelpService.java index e588cd50..8e56916f 100644 --- a/juick-www/src/main/java/com/juick/www/HelpService.java +++ b/juick-www/src/main/java/com/juick/www/HelpService.java @@ -44,7 +44,7 @@ public class HelpService { @Cacheable("help") public String getHelp(final String page, final String lang) { if (canBePage(page) && canBeLang(lang)) { - String path = StringUtils.joinWith("/", helpPath, lang, page); + String path = StringUtils.joinWith("/", helpPath, lang, page + ".md"); try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path)) { if (is != null) @@ -62,4 +62,8 @@ public class HelpService { public boolean canBeLang(final String anything) { return anything != null && LANG_PATTERN.matcher(anything).matches(); } + + public String getHelpPath() { + return helpPath; + } } diff --git a/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java index 3198df2d..d79ae636 100644 --- a/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java +++ b/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java @@ -24,6 +24,10 @@ import com.juick.service.UserService; import com.juick.www.HelpService; import com.juick.www.WebApp; import org.apache.commons.io.IOUtils; +import org.commonmark.ext.autolink.AutolinkExtension; +import org.commonmark.node.Link; +import org.commonmark.parser.Parser; +import org.commonmark.renderer.html.HtmlRenderer; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.context.annotation.Bean; @@ -33,6 +37,7 @@ import org.springframework.context.annotation.PropertySource; import javax.inject.Inject; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.util.Collections; import java.util.HashMap; /** @@ -69,4 +74,24 @@ public class WwwAppConfiguration { return new HelpService("help"); } + @Bean + public Parser cmParser() { + return Parser.builder().extensions(Collections.singletonList(AutolinkExtension.create())).build(); + } + @Bean + public HtmlRenderer helpRenderer() { + return HtmlRenderer.builder() + .attributeProviderFactory(context -> (node, tagName, attributes) -> { + if (node instanceof Link) { + Link link = (Link) node; + if (link.getDestination().startsWith("/")) { + String destination = "/" + helpService().getHelpPath() + link.getDestination(); + link.setDestination(destination); + attributes.put("href", destination); + } + } + }) + .build(); + } + } diff --git a/juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java index 814eb8e4..1e684fa6 100644 --- a/juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java +++ b/juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java @@ -24,6 +24,15 @@ import com.mitchellbosecke.pebble.loader.ServletLoader; import com.mitchellbosecke.pebble.spring4.PebbleViewResolver; import com.mitchellbosecke.pebble.spring4.extension.SpringExtension; import org.apache.commons.codec.CharEncoding; +import org.commonmark.ext.autolink.AutolinkExtension; +import org.commonmark.node.Image; +import org.commonmark.node.Link; +import org.commonmark.node.Node; +import org.commonmark.parser.Parser; +import org.commonmark.renderer.html.AttributeProvider; +import org.commonmark.renderer.html.AttributeProviderContext; +import org.commonmark.renderer.html.AttributeProviderFactory; +import org.commonmark.renderer.html.HtmlRenderer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -37,6 +46,9 @@ import org.springframework.web.servlet.resource.PathResourceResolver; import javax.inject.Inject; import javax.servlet.ServletContext; +import java.util.Arrays; +import java.util.Collections; +import java.util.Map; import java.util.concurrent.TimeUnit; /** diff --git a/juick-www/src/main/java/com/juick/www/controllers/Help.java b/juick-www/src/main/java/com/juick/www/controllers/Help.java index 4198b23a..335f4b7a 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/Help.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Help.java @@ -18,23 +18,22 @@ package com.juick.www.controllers; import com.juick.server.util.HttpNotFoundException; -import com.juick.service.MessagesService; import com.juick.server.util.UserUtils; +import com.juick.service.MessagesService; import com.juick.www.HelpService; import com.juick.www.WebApp; +import org.commonmark.parser.Parser; +import org.commonmark.renderer.html.HtmlRenderer; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import javax.inject.Inject; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URISyntaxException; import java.util.Locale; import java.util.Objects; -import java.util.Optional; /** * Created by aalexeev on 11/21/16. @@ -47,25 +46,23 @@ public class Help { private MessagesService messagesService; @Inject private WebApp webApp; + @Inject + private Parser cmParser; + @Inject + private HtmlRenderer helpRenderer; @GetMapping({"/help/", "/help", "/help/{langOrPage}", "/help/{lang}/{page}"}) public String showHelp( - HttpServletRequest request, - HttpServletResponse response, Locale locale, - @PathVariable("lang") Optional langParam, - @PathVariable("page") Optional pageParam, - @PathVariable("langOrPage") Optional langOrPageParam, + @PathVariable(required = false, name = "lang") String lang, + @PathVariable(required = false, name = "page") String page, + @PathVariable(required = false, name = "langOrPage") String langOrPage, Model model) throws IOException, URISyntaxException { com.juick.User visitor = UserUtils.getCurrentUser(); - String page = pageParam.orElse("index"); - String lang = langParam.orElse(locale.getLanguage()); String navigation = null; - if (langOrPageParam.isPresent()) { - String langOrPage = langOrPageParam.get(); - + if (langOrPage != null) { if (helpService.canBeLang(langOrPage)) { navigation = helpService.getHelp("navigation", langOrPage); if (navigation != null) @@ -86,8 +83,8 @@ public class Help { if (content == null || navigation == null) throw new HttpNotFoundException(); - model.addAttribute("navigation", navigation); - model.addAttribute("content", content); + model.addAttribute("navigation", helpRenderer.render(cmParser.parse(navigation))); + model.addAttribute("content", helpRenderer.render(cmParser.parse(content))); model.addAttribute("visitor", visitor); return "views/help"; diff --git a/juick-www/src/main/resources/help b/juick-www/src/main/resources/help index 881879c9..f15d02cb 160000 --- a/juick-www/src/main/resources/help +++ b/juick-www/src/main/resources/help @@ -1 +1 @@ -Subproject commit 881879c96cbc8a642daf951ddd51c585f3b592ce +Subproject commit f15d02cb75e1ccf2f991f4900d0efcfdf351b07f -- cgit v1.2.3