Created
February 14, 2017 05:53
-
-
Save hayukleung/91e6ff978523c736a711f49524709b7a to your computer and use it in GitHub Desktop.
[Markdown-Link-String](link)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.chargerlink.app.utils; | |
import com.chargerlink.app.bean.BaseBean; | |
import com.chargerlink.app.json.LinkStringAdapter; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
/** | |
* chargerlink_v3 | |
* com.chargerlink.app.utils | |
* LinkString.java | |
* | |
* by hayukleung | |
* at 2017-01-20 16:56 | |
*/ | |
/** | |
* markdown like link string | |
* | |
* @see LinkStringAdapter | |
*/ | |
public class LinkString extends BaseBean implements Comparable<LinkString> { | |
/** | |
* [0] | |
*/ | |
public static final String REGEX = "\\[{1}([^\\[\\]\\(\\)]+)\\]\\({1}([^\\[\\]\\(\\)]+)\\)"; | |
/** | |
* [1] | |
*/ | |
public static final String REGEX_TEXT = "\\[{1}([^\\[\\]\\(\\)]+)\\]"; | |
/** | |
* [2] | |
*/ | |
public static final String REGEX_LINK = "\\({1}([^\\[\\]\\(\\)]+)\\)"; | |
/** | |
* LinkString在原始内容中的位置 | |
*/ | |
private int index; | |
private String string; | |
private String text; | |
private String link; | |
/** | |
* 是否是链接 | |
*/ | |
private boolean linkable = false; | |
private Pattern mPatternString, mPatternText, mPatternLink; | |
public LinkString(String string, int index) { | |
mPatternString = Pattern.compile(REGEX); | |
mPatternText = Pattern.compile(REGEX_TEXT); | |
mPatternLink = Pattern.compile(REGEX_LINK); | |
Matcher matcher = mPatternString.matcher(string); | |
if (linkable = matcher.find()) { | |
this.text = getText(string); | |
this.link = getLink(string); | |
} else { | |
this.text = string; | |
} | |
this.string = string; | |
this.index = index; | |
} | |
/** | |
* [text](link) | |
* | |
* @param string | |
* @return text | |
*/ | |
private String getText(String string) { | |
Matcher matcher = mPatternText.matcher(string); | |
if (matcher.find()) { | |
String text = matcher.group(); | |
int length = text.length(); | |
return text.substring(1, length - 1); | |
} | |
return ""; | |
} | |
/** | |
* [text](link) | |
* | |
* @param string | |
* @return link | |
*/ | |
private String getLink(String string) { | |
Matcher matcher = mPatternLink.matcher(string); | |
if (matcher.find()) { | |
String link = matcher.group(); | |
int length = link.length(); | |
return link.substring(1, length - 1); | |
} | |
return ""; | |
} | |
public int getIndex() { | |
return index; | |
} | |
public String getString() { | |
return string; | |
} | |
public String getText() { | |
return text; | |
} | |
public String getLink() { | |
return link; | |
} | |
public boolean isLinkable() { | |
return linkable; | |
} | |
@Override public int compareTo(LinkString o) { | |
return index - o.index; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.chargerlink.app.utils; | |
import java.util.ArrayList; | |
import java.util.List; | |
import static com.chargerlink.app.utils.LinkStringUtils.parse; | |
import static com.chargerlink.app.utils.LinkStringUtils.show; | |
/** | |
* chargerlink_v3 | |
* com.chargerlink.app.utils | |
* LinkStringTest.java | |
* | |
* by hayukleung | |
* at 2017-01-20 16:57 | |
*/ | |
public class LinkStringTest { | |
public static void main(String[] argv) { | |
/** | |
* 期望输出结果: | |
* | |
* 00-你好! | |
* 01-你好!充电网 | |
* 02-你好!充电网你好! | |
* 03-充电网你好! | |
* 04-充电网你好!充电网 | |
* 05-你好!充电网你好!充电网 | |
* 06-你好!充电网 | |
* 07-你好!@HayukLeung你好! | |
* 08-充电网你好! | |
* 09-#带你装逼带你飞#你好!充电网 | |
* 10-你好!充电网你好!@充电小助手 | |
*/ | |
List<String> stringList = new ArrayList<>(11); | |
stringList.add("00-你好!"); | |
stringList.add("01-你好![充电网](http://www.chargerlink.com)"); | |
stringList.add("02-你好![充电网](http://www.chargerlink.com)你好!"); | |
stringList.add("03-[充电网](http://www.chargerlink.com)你好!"); | |
stringList.add("04-[充电网](http://www.chargerlink.com)你好![充电网](http://www.chargerlink.com)"); | |
stringList.add("05-你好![充电网](http://www.chargerlink.com)你好![充电网](http://www.chargerlink.com)"); | |
stringList.add("06-你好![充电网](http://www.chargerlink.com)"); | |
stringList.add("07-你好![@HayukLeung](https://github.com/hayukleung)你好!"); | |
stringList.add("08-[充电网](http://www.chargerlink.com)你好!"); | |
stringList.add( | |
"09-[#带你装逼带你飞#](http://www.chargerlink.com)你好![充电网](http://www.chargerlink.com)"); | |
stringList.add( | |
"10-你好![充电网](http://www.chargerlink.com)你好![@充电小助手](http://www.chargerlink.com)"); | |
for (String string : stringList) { | |
List<LinkString> linkStringList = parse(string); | |
// 打印结果 | |
System.out.println(show(linkStringList)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.chargerlink.app.utils; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import static com.chargerlink.app.utils.LinkString.REGEX; | |
/** | |
* chargerlink_v3 | |
* com.chargerlink.app.utils | |
* LinkStringUtils.java | |
* | |
* by hayukleung | |
* at 2017-01-25 14:49 | |
*/ | |
public class LinkStringUtils { | |
/** | |
* 解析 | |
* | |
* @param string | |
* @see LinkStringTest | |
*/ | |
public static List<LinkString> parse(String string) { | |
List<LinkString> linkStringList = new ArrayList<>(); | |
Pattern patternString = Pattern.compile(REGEX); | |
Matcher matcher = patternString.matcher(string); | |
int index = 0; | |
while (matcher.find()) { | |
String linkString = matcher.group(); | |
int tempIndex = string.indexOf(linkString, index); | |
if (index == tempIndex && 0 != linkStringList.size() && tempIndex == linkStringList.get( | |
linkStringList.size() - 1).getIndex()) { | |
tempIndex = string.indexOf(linkString, index + 1); | |
} | |
index = tempIndex; | |
linkStringList.add(new LinkString(linkString, index)); | |
} | |
List<LinkString> tempList = new ArrayList<>(); | |
index = 0; | |
for (LinkString linkString : linkStringList) { | |
if (linkString.getIndex() > index) { | |
tempList.add(new LinkString(string.substring(index, linkString.getIndex()), index)); | |
} | |
index = linkString.getIndex() + linkString.getString().length(); | |
} | |
linkStringList.addAll(tempList); | |
if (index < string.length() - 1) { | |
linkStringList.add(new LinkString(string.substring(index, string.length()), index)); | |
} | |
// 排序 | |
Collections.sort(linkStringList); | |
return linkStringList; | |
} | |
/** | |
* 获取展示内容 | |
* TODO 超链接点击处理 | |
* TODO Emoji表情处理 | |
* | |
* @param linkStringList | |
*/ | |
public static String show(List<LinkString> linkStringList) { | |
if (null == linkStringList || 0 == linkStringList.size()) { | |
return ""; | |
} else { | |
String result = ""; | |
for (LinkString linkString : linkStringList) { | |
result += linkString.getText(); | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment