首页 » 语言&开发 » Clojure » 自己写的一个简单的jekyll博客工具

自己写的一个简单的jekyll博客工具

 

将MarkDown转换jekyll Blog

  • 自动添加引擎需要的头信息
  • 校验笔记信息
  • 将代码不采用MarkDown的代码块,自动添加hilight.js代码块,并且适配编辑语言

比如~~~Clojure 会适配clojure的代码着色。~~~Java会适配成Java代码着色。

另外,打包成jar包之后。ps:打包方法参考本人blog以前的笔记。 在mac端设置alias。可以适配为一个快捷命令。

alias makeblog='java -jar /Users/sahinn/Documents/shell/study-0.1.0-SNAPSHOT-standalone.jar'

之后就可以
makeblog ****.md   直接转换md文件了

以下为源代码:

(ns net.zuobin.study
  (:gen-class :main true)
  (:use [clojure.java.io]))

(defn endComment?
  "是否是注释最后"
  [line]
  (if (and (= (.lastIndexOf line "~") (- (.length line) 1)) (not= (.lastIndexOf line "~") -1))
      "{ "%" endhighlight " % "}"
      (println-str "{ ""% "  " highlight " (.substring line (+ (.lastIndexOf line "~") 1) (.length line))  " %}")))

(defn writeFile
  "写文件操作"
  [line wtr]
  (if (.startsWith line "~~~")
    (.write wtr (endComment? line))
    (.write wtr (println-str line)))
  )

(defn fread [filename blogName header]
  (with-open [rdr (reader filename) wtr (writer blogName :append true)]
    (.write wtr header)
    (doseq [line (line-seq rdr)]
      (writeFile line wtr))))

(defn getUserDir []
  (System/getProperty "user.dir"))

(defn md? [filename]
  (let [fileLenth (.length filename)]
    (if (> fileLenth 3)
      ;.md后缀结尾必须大于3
      (if (= (.substring filename (- fileLenth 3) fileLenth) ".md") true) false)))

(defn checkArgs [& args]
  (let [filename (first args)]
    (condp = (first filename)
      nil (do (println "请输入当前目录下所需转换的文件名...") false)
      "help" (do (println "请输入待转换文件名(.md),可选参数[Title,分类,是否转载]") false)
      (do (if (md? (first filename)) true (do (println "非.md文件") false)))
      )
    ))

(defn formateDate
  "格式化时间!"
  [ss]
  (if ss
    (.format (java.text.SimpleDateFormat. "yyyy-MM-dd") (java.util.Date.))
    (.format (java.text.SimpleDateFormat. "yyyy-MM-dd hh:mm") (java.util.Date.)))
  )

(defn formateTitle
  "获取markdown博客文件的头信息!"
  [& args]
  (format "---\nlayout: post\ntitle: \"\" \nme: true\nmodified: %s \ndate: %s \ntags: [] \ncategories:  [] \ndescription: \"\"\n---\n\n"
          (formateDate true) (formateDate false))
  )

(defn getFileBlogName
  "获取输出的markdown博客文件名!"
  [& args]
  (str (formateDate true) "-" (first (first args))))

(defn getFileName
  "获取参数传递的文件名。"
  [& args]
  (first (first args))
  )

(defn -main
  [& args]
  (if (checkArgs args) (fread (getFileName args) (getFileBlogName args) (formateTitle args))))



原文链接:自己写的一个简单的jekyll博客工具,转载请注明来源!

1