Permalink
Browse files

Marked HTTP 引擎

简单包装了一下 Marked 为常驻内存的 HTTP 服务
  • Loading branch information...
1 parent 5b0fcf1 commit db985d388f349dd662729dc9fc83eb90bb7e05b8 @88250 88250 committed Nov 29, 2016
Showing with 90 additions and 34 deletions.
  1. +6 −3 package.json
  2. +24 −31 src/main/java/org/b3log/symphony/util/Markdowns.java
  3. +60 −0 src/main/webapp/js/marked/http.js
View
@@ -1,7 +1,7 @@
{
"name": "Symphony",
- "version": "1.3.0",
- "description": "A real-time community forum written in Java. Java 实时社区论坛。 https://hacpai.com ",
+ "version": "1.7.0",
+ "description": "A modern community (forum/SNS/blog) platform written in Java. 一个用 Java 实现的现代化社区(论坛/社交网络/博客)平台。 https://hacpai.com",
"homepage": "https://github.com/b3log/symphony",
"repository": {
"type": "git",
@@ -10,7 +10,7 @@
"bugs": {
"url": "https://github.com/b3log/symphony/issues"
},
- "license": "Apache License",
+ "license": "GPLv3",
"private": true,
"author": "Daniel <dl882509@gmail.com> (http://88250.b3log.org) & Vanessa <lly219@gmail.com> (http://vanessa.b3log.org)",
"maintainers": [
@@ -31,5 +31,8 @@
"gulp-rename": "^1.2.2",
"gulp-sass": "^2.3.2",
"gulp-uglify": "^2.0.0"
+ },
+ "dependencies": {
+ "marked": "^0.3.6"
}
}
@@ -17,7 +17,10 @@
*/
package org.b3log.symphony.util;
+import java.io.InputStream;
import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
@@ -33,7 +36,6 @@
import java.util.concurrent.TimeoutException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
-import org.apache.commons.lang.SystemUtils;
import org.b3log.latke.Latkes;
import org.b3log.latke.cache.Cache;
import org.b3log.latke.cache.CacheFactory;
@@ -73,7 +75,7 @@
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author <a href="http://zephyr.b3log.org">Zephyr</a>
- * @version 1.9.9.16, Nov 28, 2016
+ * @version 1.10.9.16, Nov 29, 2016
* @since 0.2.0
*/
public final class Markdowns {
@@ -108,32 +110,25 @@
*/
public static boolean MARKED_AVAILABLE;
- /**
- * marked options.
- */
- private static final String MARKED_OPTIONS = "--gfm --breaks --tables --smart-lists";
+ private static final String MARKED_ENGINE_URL = "http://localhost:8250";
static {
try {
- Process p;
- if (SystemUtils.IS_OS_WINDOWS) {
- p = Runtime.getRuntime().exec("marked.cmd");
- } else {
- p = Runtime.getRuntime().exec("marked");
- }
+ final URL url = new URL(MARKED_ENGINE_URL);
+ final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ conn.setDoOutput(true);
- final OutputStream outputStream = p.getOutputStream();
+ final OutputStream outputStream = conn.getOutputStream();
IOUtils.write("Symphony 大法好", outputStream, "UTF-8");
IOUtils.closeQuietly(outputStream);
- //final String stderr = IOUtils.toString(p.getErrorStream());
- //LOGGER.info("stderr: " + stderr);
- final String stdout = IOUtils.toString(p.getInputStream(), "UTF-8");
- //LOGGER.info("stdout: " + stdout);
+ final InputStream inputStream = conn.getInputStream();
+ final String html = IOUtils.toString(inputStream, "UTF-8");
+ IOUtils.closeQuietly(inputStream);
- p.destroy();
+ conn.disconnect();
- MARKED_AVAILABLE = StringUtils.contains(stdout, "<p>Symphony 大法好</p>");
+ MARKED_AVAILABLE = StringUtils.contains(html, "<p>Symphony 大法好</p>");
if (MARKED_AVAILABLE) {
LOGGER.log(Level.INFO, "[marked] is available, uses it for markdown processing");
@@ -364,24 +359,22 @@ public String call() throws Exception {
}
private static String toHtmlByMarked(final String markdownText) throws Exception {
- final Process p;
- if (SystemUtils.IS_OS_WINDOWS) {
- p = Runtime.getRuntime().exec("marked.cmd " + MARKED_OPTIONS);
- } else {
- p = Runtime.getRuntime().exec("marked " + MARKED_OPTIONS);
- }
-
- final OutputStream outputStream = p.getOutputStream();
+ final URL url = new URL(MARKED_ENGINE_URL);
+ final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ conn.setDoOutput(true);
+ final OutputStream outputStream = conn.getOutputStream();
IOUtils.write(markdownText, outputStream, "UTF-8");
IOUtils.closeQuietly(outputStream);
- String ret = IOUtils.toString(p.getInputStream(), "UTF-8");
+ final InputStream inputStream = conn.getInputStream();
+ final String html = IOUtils.toString(inputStream, "UTF-8");
+ IOUtils.closeQuietly(inputStream);
- p.destroy();
+ conn.disconnect();
// Pangu space
- final Document doc = Jsoup.parse(ret);
+ final Document doc = Jsoup.parse(html);
doc.traverse(new NodeVisitor() {
@Override
public void head(final org.jsoup.nodes.Node node, int depth) {
@@ -400,7 +393,7 @@ public void tail(org.jsoup.nodes.Node node, int depth) {
doc.outputSettings().prettyPrint(false);
- ret = doc.html();
+ String ret = doc.html();
ret = StringUtils.substringBetween(ret, "<body>", "</body>");
ret = StringUtils.trim(ret);
@@ -0,0 +1,60 @@
+/*
+ * Symphony - A modern community (forum/SNS/blog) platform written in Java.
+ * Copyright (C) 2012-2016, b3log.org & hacpai.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * @fileoverview marked HTTP server.
+ *
+ * @author <a href="http://vanessa.b3log.org">Liyuan Li</a>
+ * @author <a href="http://88250.b3log.org">Liang Ding</a>
+ * @version 1.0.0.0, Nov 29, 2016
+ * @since 1.7.0
+ */
+
+var PORT = 8250;
+
+var http = require('http');
+var marked = require('marked');
+
+marked.setOptions({
+ renderer: new marked.Renderer(),
+ gfm: true,
+ tables: true,
+ breaks: true,
+ smartLists: true
+});
+
+process.on('uncaughtException', function (err) {
+ console.log(err);
+});
+
+var server = http.createServer(function (request, response) {
+ var mdContent = '';
+
+ request.on('data', function (data) {
+ mdContent += data;
+ });
+
+ request.on('end', function () {
+ response.write(marked(mdContent));
+
+ response.end();
+ });
+});
+
+server.listen(PORT);
+console.log("Marked engine is running at port: " + PORT);

0 comments on commit db985d3

Please sign in to comment.