Skip to content

Instantly share code, notes, and snippets.

@andresteingress
Created January 24, 2014 10:53
Show Gist options
  • Save andresteingress/8595350 to your computer and use it in GitHub Desktop.
Save andresteingress/8595350 to your computer and use it in GitHub Desktop.
my personal clj wordpress to jekyll import
(ns clj-wp-import.core
(:require [clojure.java.io :as jio]
[clojure.string :as str]))
(defn post-files
[f]
(file-seq (jio/file f)))
(defn remove-pre-code
[txt]
(str/replace txt #"(?s)(?i)<pre>(.+?)</pre>" "{% highlight groovy %} $1 {% endhighlight %}"))
(defn remove-code-language
([tag attr txt] (remove-code-language (vector "groovy") tag attr txt))
([lang-coll tag attr txt]
(reduce #(str/replace %1
(re-pattern (str "(?s)(?i)\\[" tag " " attr "=\"" %2 "\"\\](.+?)\\[/" tag "\\]"))
(str "{% highlight " %2 " %} $1 {% endhighlight %}")) txt lang-coll)))
(defn format-link-section [txt]
(str/replace txt
(re-pattern "(\\[\\d{1,2}\\] <a.*)")
(str "<div>$1</div>")))
(defn format-first-link-of-link-section [txt]
(str/replace txt
(re-pattern "(<div>\\[0\\] <a.*)")
(str "<br><br>$1")))
(defn remove-double-curly-braces [txt]
(str/replace (str/replace txt "{{" "") "}}" ""))
(defn replace-html-entites [txt]
(let [entities [["&quot;" "\""] ["&lt;" "<"] ["&gt;" ">"]]]
(reduce #(str/replace %1 (nth %2 0) (nth %2 1)) txt entities)))
(defn replace-html-entites-in-highlight [txt]
(reduce #(str/replace %1 (first %2) (replace-html-entites (first %2))) txt (re-seq (re-pattern "(?s)\\{% highlight (.*?) %\\} (.*?) \\{% endhighlight %\\}") txt)))
(defn process-post-file [file]
(let [s (slurp file)]
(->> (remove-pre-code s)
(remove-code-language ["groovy", "xml", "java", "scala", "kotlin"] "code" "language")
(remove-code-language ["groovy", "xml", "java", "scala", "kotlin"] "code" "lang")
(remove-code-language ["groovy", "xml", "java", "scala", "kotlin"] "sourcecode" "language")
(remove-code-language ["groovy", "xml", "java", "scala", "kotlin"] "sourcecode" "lang")
(format-link-section)
(format-first-link-of-link-section)
(remove-double-curly-braces)
(replace-html-entites-in-highlight)
)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment