Skip to content

Instantly share code, notes, and snippets.

@ar7hur
Created December 12, 2016 07:52
Show Gist options
  • Save ar7hur/b17965fc72f58df95bbfa5aa1fd8af0d to your computer and use it in GitHub Desktop.
Save ar7hur/b17965fc72f58df95bbfa5aa1fd8af0d to your computer and use it in GitHub Desktop.
(defn space-in-emph [[match g1 g2]]
(let [l (if (= " " g2) "" g2)]
(str "{" g1 l "}")))
(defn footnote [[match g1]]
(if (#{"..." "…"} g1)
" \\el "
(str "\\footnote{" g1 "}")))
(defn final-clean [txt]
(-> txt
(s/replace #"[ \t\xA0]+" " ") ; non breaking space
(s/replace #"\n +" "\n")
(s/replace #"[\r\n]{2,}" "\n\n")
(s/replace #"\[ " "[")
(s/replace #"\( " "(")
(s/replace #" \]" "]")
(s/replace #" \)" ")")
(s/replace #"\} ," "},")
(s/replace #" »" "\"")
(s/replace #"« " "\"")
(s/replace #" *\[([^\]]+)\]" footnote) ; [] -> footnote
(s/replace #"\" ?\\emph\{([^\}]+)\} ?\"" "\"$1\"") ; double quote
(s/replace #"\{ *([^\}]+)([^\}])\}" space-in-emph)
(s/replace #" ([,\.])" "$1") ; "hi , alex -> hi, alex"
(s/replace #" - " " \u2014 ")
(s/replace #"%" "\\%")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment