Commit 0a4fe843 authored by Liang Ding's avatar Liang Ding

🎨 #12721

parent b2d645f4
...@@ -28,7 +28,7 @@ import org.jsoup.safety.Whitelist; ...@@ -28,7 +28,7 @@ import org.jsoup.safety.Whitelist;
* This class defines all article model relevant keys. * This class defines all article model relevant keys.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.5.0.0, Mar 4, 2019 * @version 1.5.0.1, Mar 19, 2019
* @since 0.3.1 * @since 0.3.1
*/ */
public final class Article { public final class Article {
...@@ -170,6 +170,12 @@ public final class Article { ...@@ -170,6 +170,12 @@ public final class Article {
*/ */
public static final int ARTICLE_STATUS_C_DRAFT = 1; public static final int ARTICLE_STATUS_C_DRAFT = 1;
//// Transient ////
/**
* Key of article ToC.
*/
public static final String ARTICLE_T_TOC = "articleToC";
//// Other constants //// Other constants
/** /**
......
...@@ -18,10 +18,8 @@ ...@@ -18,10 +18,8 @@
package org.b3log.solo.plugin.list; package org.b3log.solo.plugin.list;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.b3log.latke.Latkes;
import org.b3log.latke.event.AbstractEventListener; import org.b3log.latke.event.AbstractEventListener;
import org.b3log.latke.event.Event; import org.b3log.latke.event.Event;
import org.b3log.latke.logging.Logger;
import org.b3log.solo.event.EventTypes; import org.b3log.solo.event.EventTypes;
import org.b3log.solo.model.Article; import org.b3log.solo.model.Article;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -31,21 +29,19 @@ import org.jsoup.nodes.Element; ...@@ -31,21 +29,19 @@ import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser; import org.jsoup.parser.Parser;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
import java.util.ArrayList;
import java.util.List;
/** /**
* List (table of contents of an article) handler. * List (table of contents of an article) handler.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author <a href="http://www.annpeter.cn">Ann Peter</a> * @author <a href="http://www.annpeter.cn">Ann Peter</a>
* @version 1.0.2.1, Oct 24, 2018 * @version 1.0.2.2, Mar 19, 2019
* @since 0.6.7 * @since 0.6.7
*/ */
public class ListHandler extends AbstractEventListener<JSONObject> { public class ListHandler extends AbstractEventListener<JSONObject> {
/**
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(ListHandler.class);
@Override @Override
public String getEventType() { public String getEventType() {
return EventTypes.BEFORE_RENDER_ARTICLE; return EventTypes.BEFORE_RENDER_ARTICLE;
...@@ -55,36 +51,24 @@ public class ListHandler extends AbstractEventListener<JSONObject> { ...@@ -55,36 +51,24 @@ public class ListHandler extends AbstractEventListener<JSONObject> {
public void action(final Event<JSONObject> event) { public void action(final Event<JSONObject> event) {
final JSONObject data = event.getData(); final JSONObject data = event.getData();
final JSONObject article = data.optJSONObject(Article.ARTICLE); final JSONObject article = data.optJSONObject(Article.ARTICLE);
String content = article.optString(Article.ARTICLE_CONTENT); final String content = article.optString(Article.ARTICLE_CONTENT);
if (StringUtils.containsIgnoreCase(content, "plugins/list/style.css")) {
// LOGGER.log(Level.WARN, "ToC hit twice, please report this \"ghosty\" issue to developer team: https://github.com/b3log/solo/issues/new");
return;
}
final Document doc = Jsoup.parse(content, StringUtils.EMPTY, Parser.htmlParser()); final Document doc = Jsoup.parse(content, StringUtils.EMPTY, Parser.htmlParser());
doc.outputSettings().prettyPrint(false); doc.outputSettings().prettyPrint(false);
final StringBuilder listBuilder = new StringBuilder(); final List<JSONObject> toc = new ArrayList<>();
listBuilder.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + Latkes.getStaticServePath() + "/plugins/list/style.css\" />");
final Elements hs = doc.select("h1, h2, h3, h4, h5"); final Elements hs = doc.select("h1, h2, h3, h4, h5");
listBuilder.append("<ul class='b3-solo-list'>");
for (int i = 0; i < hs.size(); i++) { for (int i = 0; i < hs.size(); i++) {
final Element element = hs.get(i); final Element element = hs.get(i);
final String tagName = element.tagName().toLowerCase(); final String tagName = element.tagName().toLowerCase();
final String text = element.text(); final String text = element.text();
final String id = "b3_solo_" + tagName + "_" + i; final String id = "b3_solo_" + tagName + "_" + i;
element.before("<span id='" + id + "'></span>"); element.before("<span id='" + id + "'></span>");
final JSONObject li = new JSONObject().
listBuilder.append("<li class='b3-solo-list-").append(tagName).append("'><a href='#").append(id).append("'>"). put("class", "b3-solo-list-" + tagName).
append(text).append("</a></li>"); put("id", id).
put("text", text);
toc.add(li);
} }
listBuilder.append("</ul>"); article.put(Article.ARTICLE_T_TOC, (Object) toc);
final Element body = doc.getElementsByTag("body").get(0);
content = listBuilder.toString() + body.html();
article.put(Article.ARTICLE_CONTENT, content);
} }
} }
...@@ -18,13 +18,13 @@ ...@@ -18,13 +18,13 @@
# #
# Description: Table of contents generator. # Description: Table of contents generator.
# Version: 1.0.0.0, May 30, 2014 # Version: 1.0.0.1, Mar 19, 2019
# Author: Liang Ding # Author: Liang Ding
# #
rendererId=footer.ftl rendererId=footer.ftl
author=<a href="http://88250.b3log.org">88250</a> author=<a href="http://88250.b3log.org">88250</a>
name=Table of Contents Generator name=Table of Contents Generator
version=0.0.1 version=1.0.1
types=PUBLIC types=PUBLIC
classesDirPath=/WEB-INF/classes/ classesDirPath=/WEB-INF/classes/
......
/**
* List plugin style.
*
* @author <a href="http://vanessa.b3log.org">Liyuan Li</a>
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.1.0.0, Jul 14, 2014
*/
.b3-solo-list {
margin: 20px 30px;
list-style: none;
font-size: 0.8em;
}
.b3-solo-list-h1 {
}
.b3-solo-list-h2 {
margin-left: 16px;
}
.b3-solo-list-h3 {
margin-left: 32px;
}
.b3-solo-list-h4 {
margin-left: 48px;
}
.b3-solo-list-h5 {
margin-left: 62px;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment