Search in sources :

Example 1 with Cache

use of org.b3log.latke.cache.Cache in project latke by b3log.

the class RedisCacheTestCase method put.

@Test
public void put() throws Exception {
    if (Latkes.RuntimeCache.REDIS != Latkes.getRuntimeCache()) {
        return;
    }
    final Cache cache = CacheFactory.getCache("test");
    Assert.assertNotNull(cache);
    final String k0 = Ids.genTimeMillisId();
    final JSONObject d0 = new JSONObject();
    d0.put("f0", "0");
    d0.put("f1", 1);
    try {
        cache.put(k0, d0);
        final JSONObject d00 = cache.get(k0);
        Assert.assertEquals(d00.toString(), d0.toString());
        cache.removeAll();
        Assert.assertFalse(cache.contains(k0));
    } catch (final Exception e) {
    }
}
Also used : JSONObject(org.json.JSONObject) Cache(org.b3log.latke.cache.Cache) Test(org.testng.annotations.Test)

Example 2 with Cache

use of org.b3log.latke.cache.Cache in project symphony by b3log.

the class Markdowns method toHTML.

/**
 * Converts the specified markdown text to HTML.
 *
 * @param markdownText the specified markdown text
 * @return converted HTML, returns an empty string "" if the specified markdown text is "" or {@code null}, returns
 * 'markdownErrorLabel' if exception
 */
public static String toHTML(final String markdownText) {
    if (Strings.isEmptyOrNull(markdownText)) {
        return "";
    }
    final String cachedHTML = getHTML(markdownText);
    if (null != cachedHTML) {
        return cachedHTML;
    }
    final ExecutorService pool = Executors.newSingleThreadExecutor();
    final long[] threadId = new long[1];
    final Callable<String> call = () -> {
        threadId[0] = Thread.currentThread().getId();
        String html = LANG_PROPS_SERVICE.get("contentRenderFailedLabel");
        if (MARKED_AVAILABLE) {
            try {
                html = toHtmlByMarked(markdownText);
                if (!StringUtils.startsWith(html, "<p>")) {
                    html = "<p>" + html + "</p>";
                }
            } catch (final Exception e) {
                LOGGER.log(Level.WARN, "Failed to use [marked] for markdown [md=" + StringUtils.substring(markdownText, 0, 256) + "]: " + e.getMessage());
                com.vladsch.flexmark.ast.Node document = PARSER.parse(markdownText);
                html = RENDERER.render(document);
                if (!StringUtils.startsWith(html, "<p>")) {
                    html = "<p>" + html + "</p>";
                }
            }
        } else {
            com.vladsch.flexmark.ast.Node document = PARSER.parse(markdownText);
            html = RENDERER.render(document);
            if (!StringUtils.startsWith(html, "<p>")) {
                html = "<p>" + html + "</p>";
            }
        }
        final Document doc = Jsoup.parse(html);
        final List<org.jsoup.nodes.Node> toRemove = new ArrayList<>();
        doc.traverse(new NodeVisitor() {

            @Override
            public void head(final org.jsoup.nodes.Node node, int depth) {
                if (node instanceof org.jsoup.nodes.TextNode) {
                    final org.jsoup.nodes.TextNode textNode = (org.jsoup.nodes.TextNode) node;
                    final org.jsoup.nodes.Node parent = textNode.parent();
                    if (parent instanceof Element) {
                        final Element parentElem = (Element) parent;
                        if (!parentElem.tagName().equals("code")) {
                            String text = textNode.getWholeText();
                            boolean nextIsBr = false;
                            final org.jsoup.nodes.Node nextSibling = textNode.nextSibling();
                            if (nextSibling instanceof Element) {
                                nextIsBr = "br".equalsIgnoreCase(((Element) nextSibling).tagName());
                            }
                            if (null != userQueryService) {
                                try {
                                    final Set<String> userNames = userQueryService.getUserNames(text);
                                    for (final String userName : userNames) {
                                        text = text.replace('@' + userName + (nextIsBr ? "" : " "), "@<a href='" + Latkes.getServePath() + "/member/" + userName + "'>" + userName + "</a> ");
                                    }
                                    text = text.replace("@participants ", "@<a href='https://hacpai.com/article/1458053458339' class='ft-red'>participants</a> ");
                                } finally {
                                    JdbcRepository.dispose();
                                }
                            }
                            if (text.contains("@<a href=")) {
                                final List<org.jsoup.nodes.Node> nodes = Parser.parseFragment(text, parentElem, "");
                                final int index = textNode.siblingIndex();
                                parentElem.insertChildren(index, nodes);
                                toRemove.add(node);
                            } else {
                                textNode.text(Pangu.spacingText(text));
                            }
                        }
                    }
                }
            }

            @Override
            public void tail(org.jsoup.nodes.Node node, int depth) {
            }
        });
        toRemove.forEach(node -> node.remove());
        doc.select("pre>code").addClass("hljs");
        doc.select("a").forEach(a -> {
            String src = a.attr("href");
            if (!StringUtils.startsWithIgnoreCase(src, Latkes.getServePath())) {
                try {
                    src = URLEncoder.encode(src, "UTF-8");
                } catch (final Exception e) {
                }
                a.attr("href", Latkes.getServePath() + "/forward?goto=" + src);
                a.attr("target", "_blank");
            }
        });
        doc.outputSettings().prettyPrint(false);
        String ret = doc.select("body").html();
        ret = StringUtils.trim(ret);
        // cache it
        putHTML(markdownText, ret);
        return ret;
    };
    Stopwatchs.start("Md to HTML");
    try {
        final Future<String> future = pool.submit(call);
        return future.get(MD_TIMEOUT, TimeUnit.MILLISECONDS);
    } catch (final TimeoutException e) {
        LOGGER.log(Level.ERROR, "Markdown timeout [md=" + StringUtils.substring(markdownText, 0, 256) + "]");
        Callstacks.printCallstack(Level.ERROR, new String[] { "org.b3log" }, null);
        final Set<Thread> threads = Thread.getAllStackTraces().keySet();
        for (final Thread thread : threads) {
            if (thread.getId() == threadId[0]) {
                thread.stop();
                break;
            }
        }
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Markdown failed [md=" + StringUtils.substring(markdownText, 0, 256) + "]", e);
    } finally {
        pool.shutdownNow();
        Stopwatchs.end();
    }
    return LANG_PROPS_SERVICE.get("contentRenderFailedLabel");
}
Also used : NodeVisitor(org.jsoup.select.NodeVisitor) HttpURLConnection(java.net.HttpURLConnection) StringUtils(org.apache.commons.lang.StringUtils) DataHolder(com.vladsch.flexmark.util.options.DataHolder) URL(java.net.URL) LatkeBeanManagerImpl(org.b3log.latke.ioc.LatkeBeanManagerImpl) Parser(org.jsoup.parser.Parser) ArrayList(java.util.ArrayList) Cache(org.b3log.latke.cache.Cache) JSONObject(org.json.JSONObject) Level(org.b3log.latke.logging.Level) Element(org.jsoup.nodes.Element) Logger(org.b3log.latke.logging.Logger) Whitelist(org.jsoup.safety.Whitelist) OutputStream(java.io.OutputStream) Common(org.b3log.symphony.model.Common) java.util.concurrent(java.util.concurrent) Set(java.util.Set) LangPropsService(org.b3log.latke.service.LangPropsService) LatkeBeanManager(org.b3log.latke.ioc.LatkeBeanManager) Lifecycle(org.b3log.latke.ioc.Lifecycle) Extensions(com.vladsch.flexmark.profiles.pegdown.Extensions) IOUtils(org.apache.commons.io.IOUtils) Latkes(org.b3log.latke.Latkes) Callstacks(org.b3log.latke.util.Callstacks) URLEncoder(java.net.URLEncoder) List(java.util.List) PegdownOptionsAdapter(com.vladsch.flexmark.profiles.pegdown.PegdownOptionsAdapter) Strings(org.b3log.latke.util.Strings) Document(org.jsoup.nodes.Document) LangPropsServiceImpl(org.b3log.latke.service.LangPropsServiceImpl) UserQueryService(org.b3log.symphony.service.UserQueryService) Stopwatchs(org.b3log.latke.util.Stopwatchs) Jsoup(org.jsoup.Jsoup) Elements(org.jsoup.select.Elements) DigestUtils(org.apache.commons.codec.digest.DigestUtils) CacheFactory(org.b3log.latke.cache.CacheFactory) JdbcRepository(org.b3log.latke.repository.jdbc.JdbcRepository) HtmlRenderer(com.vladsch.flexmark.html.HtmlRenderer) InputStream(java.io.InputStream) Set(java.util.Set) Element(org.jsoup.nodes.Element) Document(org.jsoup.nodes.Document) NodeVisitor(org.jsoup.select.NodeVisitor) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Cache (org.b3log.latke.cache.Cache)2 JSONObject (org.json.JSONObject)2 HtmlRenderer (com.vladsch.flexmark.html.HtmlRenderer)1 Extensions (com.vladsch.flexmark.profiles.pegdown.Extensions)1 PegdownOptionsAdapter (com.vladsch.flexmark.profiles.pegdown.PegdownOptionsAdapter)1 DataHolder (com.vladsch.flexmark.util.options.DataHolder)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 URLEncoder (java.net.URLEncoder)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Set (java.util.Set)1 java.util.concurrent (java.util.concurrent)1 DigestUtils (org.apache.commons.codec.digest.DigestUtils)1 IOUtils (org.apache.commons.io.IOUtils)1 StringUtils (org.apache.commons.lang.StringUtils)1 Latkes (org.b3log.latke.Latkes)1 CacheFactory (org.b3log.latke.cache.CacheFactory)1