Skip to content

Commit

Permalink
1. 添加对于特殊字符和重复名称的处理
Browse files Browse the repository at this point in the history
  • Loading branch information
houbb committed Feb 28, 2018
1 parent c46ca5a commit 156b5c3
Show file tree
Hide file tree
Showing 11 changed files with 401 additions and 379 deletions.
603 changes: 303 additions & 300 deletions MD-Test.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ private TocConstant(){}
*/
public static final String STAR = "*";

/**
* 减号
*/
public static final String MINUS = "-";

/**
* TOC 格式化
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.houbb.markdown.toc.constant.TocConstant;
import com.github.houbb.markdown.toc.core.MarkdownToc;
import com.github.houbb.markdown.toc.exception.MarkdownTocRuntimeException;
import com.github.houbb.markdown.toc.support.IncreaseMap;
import com.github.houbb.markdown.toc.vo.TocVo;

import java.io.IOException;
Expand Down Expand Up @@ -127,12 +128,15 @@ private void initToc() {
}

//3. 构建 tocVo
TocVo root = TocVo.rootToc();
//3.1 创建一个自增 map,用来处理重复名称
IncreaseMap increaseMap = new IncreaseMap();

TocVo root = TocVo.rootToc(increaseMap);
root.setParent(null);
tocVoList.add(root); //初始化根节点
previous = root;
for(String string : tocStrList) {
addNewToc(string);
addNewToc(string, increaseMap);
}

//4. 展现
Expand Down Expand Up @@ -178,8 +182,8 @@ private String getSuffix(int level) {
*
* @param tocTrimStr toc trim str
*/
private void addNewToc(String tocTrimStr) {
TocVo current = new TocVo(tocTrimStr);
private void addNewToc(String tocTrimStr, IncreaseMap increaseMap) {
TocVo current = new TocVo(tocTrimStr, increaseMap);

if(current.getLevel() == 1) { //1 级目录
TocVo root = tocVoList.get(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.github.houbb.markdown.toc.support;

import com.github.houbb.markdown.toc.constant.TocConstant;
import com.github.houbb.markdown.toc.util.SpecialCharUtil;
import com.github.houbb.markdown.toc.util.StringUtil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class IncreaseMap {

private Map<String, AtomicInteger> map = new ConcurrentHashMap<>();


/**
* 构建确切的名称
* @param originalName 原始名称
* @return 最后的构建结果
*/
public String buildActualName(final String originalName) {
String actualName;
String tocHref = getTocHref(originalName);
AtomicInteger value = map.get(tocHref);
if(value != null) {
int count = value.getAndIncrement();
actualName = TocConstant.ASTERISK+tocHref+TocConstant.MINUS+count;
} else {
AtomicInteger one = new AtomicInteger(0);
map.put(tocHref, one);
actualName = TocConstant.ASTERISK+tocHref;
}
return actualName;
}

/**
* 对于首字母如果是
* @param tocTitle toc 标题
* @return 对应的连接名称
*/
private static String getTocHref(final String tocTitle) {
if(StringUtil.isEmpty(tocTitle)) {
return tocTitle;
}

//1. GITHUB 对于大写的处理
String result = tocTitle.toLowerCase();

//2. GITHUB 对于空格的处理
result = result.replace(' ', '-');

//3. 对于特殊字符的处理
for(String specialChar : SpecialCharUtil.getSpecialCharSet()) {
result = result.replace(specialChar, "");
}

return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@
* @since 1.0.1
* @version 1.0.1
*/
public class SpecialCharUtil {
public final class SpecialCharUtil {

/**
* 获取特殊字符的集合
*/
public static Set<String> getSpecialCharSet() {
Set<String> stringSet = new HashSet<>();
private static Set<String> stringSet = new HashSet<>();

static {
InputStream inputStream = SpecialCharUtil.class.getResourceAsStream("/special_char.txt");
List<String> stringList = FileUtil.getFileContentEachLine(inputStream, 0);
for(String string: stringList) {
String[] strings = StringUtil.splitByAnyBlank(string);
stringSet.add(strings[0].trim());
}
}

/**
* 获取特殊字符的集合
*/
public static Set<String> getSpecialCharSet() {
return stringSet;
}

Expand Down
17 changes: 0 additions & 17 deletions src/main/java/com/github/houbb/markdown/toc/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,6 @@
*/
public final class StringUtil {

/**
* 对于首字母如果是
* @param tocTitle toc 标题
* @return 对应的连接
*/
public static String getTocHref(final String tocTitle) {
if(isEmpty(tocTitle)) {
return tocTitle;
}

if (tocTitle.length() == 1) {
return tocTitle.toLowerCase();
}
String subEmptyHandle = tocTitle.substring(1).replace(' ', '-'); //GITHUB 对于空格处理
return tocTitle.substring(0, 1).toLowerCase() + subEmptyHandle;
}

/**
* 是否为空
* @param string 字符串
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/github/houbb/markdown/toc/vo/TocVo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.houbb.markdown.toc.vo;

import com.github.houbb.markdown.toc.constant.TocConstant;
import com.github.houbb.markdown.toc.support.IncreaseMap;
import com.github.houbb.markdown.toc.util.StringUtil;

import java.util.LinkedList;
Expand Down Expand Up @@ -49,13 +50,19 @@ public class TocVo {
*/
private List<TocVo> children = new LinkedList<>();

/**
* 自增 MAP
*/
private final IncreaseMap increaseMap;

/**
*  toc值对象
*
* @param origin 起源
*/
public TocVo(String origin) {
public TocVo(String origin, IncreaseMap increaseMap) {
this.origin = origin;
this.increaseMap = increaseMap;
init();
}

Expand All @@ -69,7 +76,7 @@ private void init() {

String[] strings = this.origin.split("\\s+");
this.tocTitle = this.origin.substring(strings[0].length()).trim();
this.tocHref = TocConstant.ASTERISK + StringUtil.getTocHref(tocTitle);
this.tocHref = increaseMap.buildActualName(tocTitle);
this.level = strings[0].length();
}

Expand All @@ -78,8 +85,8 @@ private void init() {
*
* @return com.github.houbb.markdown.toc.vo.TocVo
*/
public static TocVo rootToc() {
TocVo tocVo = new TocVo(ROOT_NAME);
public static TocVo rootToc(IncreaseMap increaseMap) {
TocVo tocVo = new TocVo(ROOT_NAME, increaseMap);
tocVo.setLevel(0);
return tocVo;
}
Expand Down
34 changes: 1 addition & 33 deletions src/main/resources/special_char.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ $
*
(
)
_
-
+
=
:
Expand Down Expand Up @@ -41,7 +39,6 @@ _
¢ &cent;
€ &euro;
„ &bdquo;
ˆ &circ;
† &dagger;
‡ &Dagger;
≈ &asymp;
Expand All @@ -50,10 +47,6 @@ _
↓ &darr;
♦ &diams;
≡ &equiv;
α &alpha;
β &beta;
χ &chi;
Χ &Chi;
© &copy;
° &deg;
÷ &divide;
Expand All @@ -71,7 +64,6 @@ _
♥ &hearts;
∞ &infin;
∫ &int;
γ &gamma;
> &gt;
¡ &iexcl;
¿ &iquest;
Expand All @@ -89,14 +81,6 @@ _
− &minus;
≠ &ne;
‾ &oline;
ν &nu;
ω &omega;
ο &omicron;
φ &phi;
Ν &Nu;
Ω &Omega;
Ο &Omicron;
Φ &Phi;
» &raquo;
¬ &not;
¶ &para;
Expand All @@ -105,29 +89,13 @@ _
∏ &prod;
√ &radic;
→ &rarr;
π &pi;
ψ &psi;
ρ &rho;
σ &sigma;
τ &tau;
Π &Pi;
Ψ &Psi;
Ρ &Rho;
Σ &Sigma;
Τ &Tau;
® &reg;
§ &sect;
¨ &uml;
× &times;
™ &trade;
ª &ordf;
” &rdquo;
˜ &tilde;
♠ &spades;
∑ &sum;
↑ &uarr;
υ &upsilon;
ξ &xi;
Υ &Upsilon;
Ξ &Xi;
ς &sigmaf;
↑ &uarr;
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class AtxMarkdownTocTest {
*/
@Test
public void genTocTest() throws Exception {
final String filePath = "D:\\CODE\\_OTHER\\markdown-toc\\README.md";
final String filePath = "D:\\CODE\\_OTHER\\markdown-toc\\MD-Test.md";
final String charsetStr = "UTF-8";

//测试时请将下面这段话取消注释(Please cancel comment when you test.)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public void getSpecialCharSetTest() {
@Test
public void buildSpecialTitle() {
Set<String> stringSet = SpecialCharUtil.getSpecialCharSet();
final String format = "# AABOUThello%sBDDDDout\n" +
"AABOUThello%sBDDDDout\n";
final String format = "# ILOVEMD%sILOVEMD\n" +
"ILOVEMD%sILOVEMD\n";
for(String string : stringSet) {
System.out.println(String.format(format, string, string));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@
*/
public class StringUtilTest {

/**
*
* Method: getTocHref(tocTitle)
*/
@Test
public void getTocHrefTest() throws Exception {
Assert.assertEquals("你好世界", StringUtil.getTocHref("你好世界"));
Assert.assertEquals("helloWorld", StringUtil.getTocHref("helloWorld"));
Assert.assertEquals("helloWorld", StringUtil.getTocHref("HelloWorld"));
Assert.assertEquals("english-title----test", StringUtil.getTocHref("english title test"));
}

/**
*
* Method: isEmpty(string)
Expand Down

0 comments on commit 156b5c3

Please sign in to comment.