Skip to content

Commit

Permalink
🦄 fix otale#53
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Feb 28, 2017
1 parent f9d1c2b commit f18691d
Show file tree
Hide file tree
Showing 9 changed files with 384 additions and 225 deletions.
204 changes: 1 addition & 203 deletions src/main/java/com/tale/ext/Commons.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,19 @@
import com.blade.jdbc.model.Paginator;
import com.blade.kit.*;
import com.tale.controller.BaseController;
import com.tale.dto.Comment;
import com.tale.dto.MetaDto;
import com.tale.dto.Types;
import com.tale.init.TaleConst;
import com.tale.model.Comments;
import com.tale.model.Contents;
import com.tale.service.SiteService;
import com.tale.utils.TaleUtils;
import com.vdurmont.emoji.EmojiParser;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* 主题公共函数
* 公共函数
* <p>
* Created by biezhi on 2017/2/21.
*/
Expand Down Expand Up @@ -148,27 +141,6 @@ public static String gravatar(String email) {
return avatarUrl + "/" + hash;
}

/**
* 返回文章链接地址
*
* @param contents
* @return
*/
public static String permalink(Contents contents) {
return permalink(contents.getCid(), contents.getSlug());
}

/**
* 返回文章链接地址
*
* @param cid
* @param slug
* @return
*/
public static String permalink(Integer cid, String slug) {
return site_url("/article/" + (StringKit.isNotBlank(slug) ? slug : cid.toString()));
}

/**
* 格式化unix时间戳为日期
*
Expand All @@ -193,97 +165,6 @@ public static String fmtdate(Integer unixTime, String patten) {
return "";
}

/**
* 显示分类
*
* @param categories
* @return
*/
public static String show_categories(String categories) throws UnsupportedEncodingException {
if (StringKit.isNotBlank(categories)) {
String[] arr = categories.split(",");
StringBuffer sbuf = new StringBuffer();
for (String c : arr) {
sbuf.append("<a href=\"/category/" + URLEncoder.encode(c, "UTF-8") + "\">" + c + "</a>");
}
return sbuf.toString();
}
return show_categories("默认分类");
}

/**
* 显示标签
*
* @param tags
* @return
*/
public static String show_tags(String tags) throws UnsupportedEncodingException {
if (StringKit.isNotBlank(tags)) {
String[] arr = tags.split(",");
StringBuffer sbuf = new StringBuffer();
for (String c : arr) {
sbuf.append("<a href=\"/tag/" + URLEncoder.encode(c, "UTF-8") + "\">" + c + "</a>");
}
return sbuf.toString();
}
return "";
}

/**
* 截取文章摘要
*
* @param value 文章内容
* @param len 要截取文字的个数
* @return
*/
public static String intro(String value, int len) {
int pos = value.indexOf("<!--more-->");
if (pos != -1) {
String html = value.substring(0, pos);
return TaleUtils.htmlToText(TaleUtils.mdToHtml(html));
} else {
String text = TaleUtils.htmlToText(TaleUtils.mdToHtml(value));
if (text.length() > len) {
return text.substring(0, len);
}
return text;
}
}

/**
* 显示文章内容,转换markdown为html
*
* @param value
* @return
*/
public static String article(String value) {
if (StringKit.isNotBlank(value)) {
value = value.replace("<!--more-->", "\r\n");
return TaleUtils.mdToHtml(value);
}
return "";
}

/**
* 显示文章缩略图,顺序为:文章第一张图 -> 随机获取
*
* @return
*/
public static String show_thumb(Contents contents) {
if (null == contents) {
return "";
}
String content = article(contents.getContent());
String img = show_thumb(content);
if (StringKit.isNotBlank(img)) {
return img;
}
int cid = contents.getCid();
int size = cid % 20;
size = size == 0 ? 1 : size;
return "/static/user/img/rand/" + size + ".jpg";
}

/**
* 获取随机数
* @param max
Expand All @@ -294,77 +175,6 @@ public static String random(int max, String str){
return UUID.random(1, max) + str;
}

/**
* 最新文章
*
* @param limit
* @return
*/
public static List<Contents> recent_articles(int limit) {
if (null == siteService) {
return EMPTY;
}
return siteService.recentContents(limit);
}

/**
* 最新评论
*
* @param limit
* @return
*/
public static List<Comments> recent_comments(int limit) {
if (null == siteService) {
return EMPTY;
}
return siteService.recentComments(limit);
}

/**
* 获取分类列表
* @return
*/
public static List<MetaDto> categries(int limit){
return siteService.metas(Types.CATEGORY, null, limit);
}

/**
* 获取所有分类
* @return
*/
public static List<MetaDto> categries(){
return categries(TaleConst.MAX_POSTS);
}

/**
* 获取标签列表
* @return
*/
public static List<MetaDto> tags(int limit){
return siteService.metas(Types.TAG, null, limit);
}

/**
* 获取所有标签
* @return
*/
public static List<MetaDto> tags(){
return tags(TaleConst.MAX_POSTS);
}

/**
* 获取评论at信息
* @param coid
* @return
*/
public static String comment_at(Integer coid){
Comments comments = siteService.getComment(coid);
if(null != comments){
return "<a href=\"#comment-" + coid + "\">@" + comments.getAuthor() + "</a>";
}
return "";
}

/**
* An :grinning:awesome :smiley:string &#128516;with a few :wink:emojis!
*
Expand Down Expand Up @@ -401,16 +211,4 @@ public static String show_thumb(String content) {
return "";
}

private static final String[] ICONS = {"bg-ico-book", "bg-ico-game", "bg-ico-note", "bg-ico-chat", "bg-ico-code", "bg-ico-image", "bg-ico-web", "bg-ico-link", "bg-ico-design", "bg-ico-lock"};

/**
* 显示文章图标
*
* @param cid
* @return
*/
public static String show_icon(int cid) {
return ICONS[cid % ICONS.length];
}

}
Loading

0 comments on commit f18691d

Please sign in to comment.