typecho文章迁快速迁移到Hexo

最近研究了下静态博客Hexo,觉得挺有意思,刚好自己所有博文都是用Markdown方式来编写的,所以自己用java写了个快速迁移的工具。

依赖Jar包如下:

  • mysql-connector-java MySQL驱动,这个是必须的
  • druid 阿里巴巴数据库连接池,主要是为了创建链接方便
  • beetl Beetl模板,快速渲染MD文件
  • commons-dbutils 用于操作数据库的简单工具

话不多说,直接上代码:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.StringTemplateResourceLoader;

import com.alibaba.druid.pool.DruidDataSource;

public class Client {
    
    public static DruidDataSource dataSource;
    static{
        dataSource = new DruidDataSource();
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/typecho");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
    }
    
    public static final String OUT_PATH="C:\\Users\\darren\\Desktop\\post\\";//输出的文件目录
    public static final String EXT=".md";//输出的文件后缀
    public static final String SQL = "SELECT title,text,created FROM myblog_contents WHERE `type` = 'post'";//查询的SQL语句
    //输出用的模板
    public static final String TMPL="---\r\ntitle: ${post.title}\r\ndate: ${post.date,'yyyy-MM-dd HH:mm:ss'}\r\ntags: typecho\r\ncategories: default\r\n---\r\n\r\n${strutil.subString(post.text,15)}";

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args) throws IOException, SQLException {
        File dir = new File(OUT_PATH);
        if(!dir.exists()) dir.mkdirs();
        GroupTemplate gt = new GroupTemplate(new StringTemplateResourceLoader(), Configuration.defaultConfiguration());
        Template t = gt.getTemplate(TMPL);
        QueryRunner query = new QueryRunner(dataSource);
        List<Post> list = (List<Post>)query.query(SQL, new BeanListHandler(Post.class));
        for (Post post : list) {
            String fileName = post.title.replaceAll("[\\.\\/\\+!,!,。、::“”()【】\\(\\)\\[\\]]", "_");//文件名替换掉一些特殊字符为_
            if(fileName.startsWith("_"))fileName = fileName.substring(1);
            if(fileName.endsWith("_"))fileName = fileName.substring(0,fileName.length()-1);
            t.binding("post", post);
            t.renderTo(new FileOutputStream(OUT_PATH+fileName+EXT));
            System.out.println("成功输出:【"+post.title+"】");
        }
    }

    //输出的文章模型,暂时只支持title、日期、和正文
    public static class Post{
        private String title;
        private String text;
        private Long created;
        public Date getDate(){
            return new Date(created*1000);
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public void setText(String text) {
            this.text = text;
        }
        public void setCreated(Long created) {
            this.created = created;
        }
        public String getTitle() {
            return title;
        }
        public String getText() {
            return text;
        }
        public Long getCreated() {
            return created;
        }
    }
}

本文地址:http://darren.ink/archives/146

相关文章:

标签: java, typecho, nodejs, hexo

添加新评论