Skip to content

Commit

Permalink
🦄 add some theme functions and fix theme read
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Feb 28, 2017
1 parent 52fe28e commit 6f00e32
Show file tree
Hide file tree
Showing 40 changed files with 179 additions and 45 deletions.
15 changes: 11 additions & 4 deletions src/main/java/com/tale/controller/IndexController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.blade.mvc.view.RestResponse;
import com.tale.dto.*;
import com.tale.exception.TipException;
import com.tale.ext.Commons;
import com.tale.init.TaleConst;
import com.tale.model.Comments;
import com.tale.model.Contents;
Expand Down Expand Up @@ -69,8 +70,7 @@ public String page(@PathParam String pagename, Request request) {
}
if (contents.getAllow_comment()) {
int cp = request.queryInt("cp", 1);
Paginator<Comment> commentsPaginator = commentsService.getComments(contents.getCid(), cp, 6);
request.attribute("comments", commentsPaginator);
request.attribute("cp", cp);
}
request.attribute("article", contents);
Integer hits = cache.hget("page", "hits");
Expand Down Expand Up @@ -105,6 +105,7 @@ public String index(Request request, @PathParam int p, @QueryParam(value = "limi
this.title(request, "第" + p + "页");
}
request.attribute("is_home", true);
request.attribute("page_prefix", "/page");
return this.render("index");
}

Expand All @@ -121,8 +122,7 @@ public String post(Request request, @PathParam String cid) {
request.attribute("is_post", true);
if (contents.getAllow_comment()) {
int cp = request.queryInt("cp", 1);
Paginator<Comment> commentsPaginator = commentsService.getComments(contents.getCid(), cp, 6);
request.attribute("comments", commentsPaginator);
request.attribute("cp", cp);
}
Integer hits = cache.hget("article", "hits");
hits = null == hits ? 1 : hits + 1;
Expand Down Expand Up @@ -164,6 +164,7 @@ public String categories(Request request, @PathParam String keyword,
request.attribute("type", "分类");
request.attribute("keyword", keyword);
request.attribute("is_category", true);
request.attribute("page_prefix", "/category/" + keyword);

return this.render("page-category");
}
Expand Down Expand Up @@ -203,6 +204,7 @@ public String tags(Request request, @PathParam String name, @PathParam int page,
request.attribute("type", "标签");
request.attribute("keyword", name);
request.attribute("is_tag", true);
request.attribute("page_prefix", "/tag/" + name);

return this.render("page-category");
}
Expand Down Expand Up @@ -230,6 +232,7 @@ public String search(Request request, @PathParam String keyword, @PathParam int

request.attribute("type", "搜索");
request.attribute("keyword", keyword);
request.attribute("page_prefix", "/search/" + keyword);
return this.render("page-category");
}

Expand Down Expand Up @@ -301,6 +304,10 @@ public RestResponse comment(Request request, Response response,
return RestResponse.fail(ErrorCode.BAD_REQUEST);
}

if(!ref.startsWith(Commons.site_url())){
return RestResponse.fail("非法评论来源");
}

String token = cache.hget(Types.CSRF_TOKEN, _csrf_token);
if (StringKit.isBlank(token)) {
return RestResponse.fail(ErrorCode.BAD_REQUEST);
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/com/tale/dto/Archive.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,32 @@
import com.tale.model.Contents;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
* Created by biezhi on 2017/2/23.
*/
public class Archive implements Serializable {

private String date;
private String date_str;
private Date date;
private String count;
private List<Contents> articles;

public String getDate() {
public String getDate_str() {
return date_str;
}

public void setDate_str(String date_str) {
this.date_str = date_str;
}

public Date getDate() {
return date;
}

public void setDate(String date) {
public void setDate(Date date) {
this.date = date;
}

Expand All @@ -38,12 +48,4 @@ public void setArticles(List<Contents> articles) {
this.articles = articles;
}

@Override
public String toString() {
return "Archive [" +
"date='" + date + '\'' +
", count='" + count + '\'' +
", articles=" + articles +
']';
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/tale/dto/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ public interface Types {

String C_STATISTICS = "sys:statistics";

String NEXT = "next";
String PREV = "prev";
}
11 changes: 11 additions & 0 deletions src/main/java/com/tale/ext/Commons.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.vdurmont.emoji.EmojiParser;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -159,6 +160,16 @@ public static String fmtdate(Integer unixTime) {
return fmtdate(unixTime, "yyyy-MM-dd");
}

/**
* 格式化日期
* @param date
* @param fmt
* @return
*/
public static String fmtdate(Date date, String fmt) {
return DateKit.dateFormat(date, fmt);
}

/**
* 格式化unix时间戳为日期
*
Expand Down
102 changes: 88 additions & 14 deletions src/main/java/com/tale/ext/Theme.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.tale.ext;

import com.blade.jdbc.model.Paginator;
import com.blade.kit.StringKit;
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.model.Metas;
import com.tale.service.SiteService;
import com.tale.utils.TaleUtils;
import jetbrick.template.runtime.InterpretContext;
Expand All @@ -15,7 +16,6 @@
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
* 主题函数
Expand All @@ -28,8 +28,6 @@ public final class Theme {

public static final List EMPTY = new ArrayList(0);

private static final Random rand = new Random();

public static void setSiteService(SiteService ss) {
siteService = ss;
}
Expand Down Expand Up @@ -74,6 +72,17 @@ public static String head_title(){
}
return p + " - " + Commons.site_option("site_title", "Tale 博客");
}

/**
* 返回主题所在的url
*
* @param sub
* @return
*/
public static String theme_url(String sub){
return Commons.site_url("/templates/themes/" + Commons.site_option("site_theme") + sub);
}

/**
* 返回文章链接地址
*
Expand Down Expand Up @@ -177,29 +186,38 @@ public static String show_categories(String categories) throws UnsupportedEncodi
/**
* 显示标签
*
* @param tags
* @param split 每个标签之间的分隔符
* @return
*/
public static String show_tags(String tags) throws UnsupportedEncodingException {
if (StringKit.isNotBlank(tags)) {
String[] arr = tags.split(",");
public static String show_tags(String split) throws UnsupportedEncodingException {
Contents contents = current_article();
if (StringKit.isNotBlank(contents.getTags())) {
String[] arr = contents.getTags().split(",");
StringBuffer sbuf = new StringBuffer();
for (String c : arr) {
sbuf.append("<a href=\"/tag/" + URLEncoder.encode(c, "UTF-8") + "\">" + c + "</a>");
sbuf.append(split).append("<a href=\"/tag/" + URLEncoder.encode(c, "UTF-8") + "\">" + c + "</a>");
}
return sbuf.toString();
return split.length() > 0 ? sbuf.substring(split.length() - 1) : sbuf.toString();
}
return "";
}

/**
* 显示文章浏览量
* @return
*/
public static String views(){
Contents contents = current_article();
return null != contents ? contents.getHits().toString() : "0";
}

/**
* 显示标签
*
* @return
*/
public static String show_tags() throws UnsupportedEncodingException {
Contents contents = current_article();
return null != contents ? show_tags(contents.getTags()) : "";
return show_tags("");
}

/**
Expand All @@ -213,6 +231,16 @@ public static String show_content() {
}

/**
* 获取文章摘要
* @param len
* @return
*/
public static String excerpt(int len){
return intro(len);
}

/**
* 获取文章摘要
* @param len
* @return
*/
Expand Down Expand Up @@ -276,7 +304,26 @@ public static String show_thumb(Contents contents) {
int cid = contents.getCid();
int size = cid % 20;
size = size == 0 ? 1 : size;
return "/static/user/img/rand/" + size + ".jpg";
return "/templates/themes/default/static/img/rand/" + size + ".jpg";
}

/**
* 获取当前文章的上一篇
* @return
*/
public static Contents article_next(){
Contents cur = current_article();
return null != cur ? siteService.getNhContent(Types.NEXT, cur.getCid()) : null;
}

/**
* 获取当前文章的下一篇
*
* @return
*/
public static Contents article_prev(){
Contents cur = current_article();
return null != cur ? siteService.getNhContent(Types.PREV, cur.getCid()) : null;
}

/**
Expand Down Expand Up @@ -431,7 +478,15 @@ public static String show_icon(int cid) {
* @return
*/
public static String title() {
Contents contents = current_article();
return title(current_article());
}

/**
* 返回文章标题
* @param contents
* @return
*/
public static String title(Contents contents) {
return null != contents ? contents.getTitle() : Commons.site_title();
}

Expand Down Expand Up @@ -464,6 +519,25 @@ public static String social_link(String socialtype) {
return "";
}

/**
* 获取当前文章/页面的评论
* @param limit
* @return
*/
public static Paginator<Comment> comments(int limit){
Contents contents = current_article();
if(null == contents){
return new Paginator<>(0,limit);
}
InterpretContext ctx = InterpretContext.current();
Object value = ctx.getValueStack().getValue("cp");
int page = 1;
if (null != value) {
page = (int) value;
}
return siteService.getComments(contents.getCid(), page, limit);
}

/**
* 获取当前上下文的文章对象
*
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/tale/init/WebContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void init(BConfig bConfig, ServletContext sec) {
if(f.isDirectory() && FileKit.exist(f.getPath() + "/macros.html")){
templateEngine.addConfig("jetx.import.macros", "/themes/" + f.getName() + "/macros.html");
}
bConfig.addStatic(new String[]{"/templates/themes/"+ f.getName() +"/static"});
}
GlobalResolver resolver = templateEngine.getGlobalResolver();
resolver.registerFunctions(Commons.class);
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/tale/service/SiteService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tale.service;

import com.blade.jdbc.model.Paginator;
import com.tale.dto.*;
import com.tale.model.Comments;
import com.tale.model.Contents;
Expand Down Expand Up @@ -82,4 +83,21 @@ public interface SiteService {
*/
void cleanCache(String key);

/**
* 获取相邻的文章
*
* @param type 上一篇:prev 下一篇:next
* @param cid 当前文章id
* @return
*/
Contents getNhContent(String type, Integer cid);

/**
* 获取文章的评论
* @param cid
* @param page
* @param limit
* @return
*/
Paginator<Comment> getComments(Integer cid, int page, int limit);
}
Loading

0 comments on commit 6f00e32

Please sign in to comment.