Skip to content

Instantly share code, notes, and snippets.

@danneu
Created January 16, 2015 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danneu/2aaea5fb51d6659517e3 to your computer and use it in GitHub Desktop.
Save danneu/2aaea5fb51d6659517e3 to your computer and use it in GitHub Desktop.
(ns guild.markdown.render
(:require [hiccup.core]
;[markdown.core :as md]
[clojure.string :as str]
[guild.smilies :as smilies]
[guild.markdown.transformers :refer [autolink-transformer
escape-transformer]]
[markdown.transformers :refer [transformer-vector]]))
;; (defn bb-img [text]
;; (str/replace text
;; #"\[img\/]"
;; "<img src=\"$1\">"))
;; (defn bb-quote [text]
;; (clojure.string/replace
;; text
;; #"(?im)\[quote=([^\]]*)\]((?:[\s\S](?!\[quote=[^\]]*\]))*?)\[\/quote\]"
;; "<div class='panel panel-default bb-quote-box'>
;; <div class='panel-heading'>
;; Quote: <b>$1</b> said:
;; </div>
;; <div class='panel-body' style='background-color: #555'>
;; $2
;; </div>
;; </div>"))
;; (defn bb-quote [text]
;; (clojure.string/replace
;; text
;; #"(?im)\[quote=([^\]]*)\]((?:[\s\S](?!\[quote=[^\]]*\]))*?)\[\/quote\]"
;; "<div class='panel panel-default bb-quote-box'>
;; <div class='panel-body'>
;; $2
;; </div>
;; </div>"))
(defn bb-quote [text]
(clojure.string/replace
text
#"(?im)\[quote=([^\]]*)\]((?:[\s\S](?!\[quote=[^\]]*\]))*?)\[\/quote\]"
;#"(?im)\[quote=([^\]]*)\](.+?)\[\/quote\]"
"<blockquote class=\"bb-quote\">
<small><b>$1</b> said</small>
$2
</blockquote>"))
;; (let [text "[quote=d]a[quote=x]huh[/quote]asd[/quote]"]
;; (clojure.string/replace
;; text
;; #"(?mi)\[quote(:.*)?=(?:&quot;)?(.*?)(?:&quot;)?\](.*?)\[\/quote\1?\]"
;; "['$2' said '$3']"))
(defn bb-quote-unnamed [text]
(clojure.string/replace
text
#"(?im)\[quote\]((?:[\s\S](?!\[quote\]))*?)\[\/quote\]"
;#"(?im)\[quote\](.+?)\[\/quote\]"
"<blockquote class=\"bb-quote\">
$1
</blockquote>"))
;; (defn bb-hider [text]
;; (clojure.string/replace
;; text
;; #"(?im)\[hider=([^\]]*)\]((?:[\s\S](?!\[hider=[^\]]*\]))*?)\[\/hider\]"
;; "<div class=\"hider-panel\">
;; <div class='hider-heading'>
;; <a href='#' class='btn btn-default btn-xs hider-button' data-name='$1'>$1 [+]</a>
;; </div>
;; <div class=\"hider-body\" style='display: none;'>
;; $2
;; </div>
;; </div>"))
(defn bb-hider [text]
(str/replace
text
;#"(?im)\[hider=([^\]]*)\](.+?)\[\/hider\]"
#"(?im)\[hider=([^\]]*)\](.+?)\[\/hider\]"
"<div class=\"hider-panel\">
<div class='hider-heading'>
<a href='#' class='btn btn-default btn-xs hider-button' data-name='$1'>$1 [+]</a>
</div>
<div class=\"hider-body\" style='display: none;'>
$2
</div>
</div>"))
(defn bb-hider2 [text]
(clojure.string/replace
text
#"(?im)\[hider2=([^\]]*)\](.+?)\[\/hider2\]"
"<div class=\"hider-panel\">
<div class='hider-heading'>
<a href='#' class='btn btn-default btn-xs hider-button' data-name='$1'>$1 [+]</a>
</div>
<div class=\"hider-body\" style='display: none;'>
$2
</div>
</div>"))
(defn youtube [text]
(clojure.string/replace text ; ;
#"(?i)\[youtube\](.+?)\[\/youtube\]"
"<iframe width='560' height='315' src='//www.youtube.com/embed/$1?rel=0' frameborder='0' allowfullscreen></iframe>"))
;; (defn embolden [text]
;; (clojure.string/replace
;; text
;; #"\*\*(.+?)\*\*"
;; "<b>$1</b>"))
;; (defn italicize [text]
;; (clojure.string/replace
;; text
;; #"\*(.+?)\*"
;; "<i>$1</i>"))
(defn bb-img [text]
(clojure.string/replace text
#"(?i)\[img=([^\]]+)\]"
"<img src=\"$1\">"))
(defn bb-img-wrapped [text]
(clojure.string/replace text ; ;
#"(?i)\[img\]([^\[]+)\[\/img\]"
"<img src=\"$1\">"))
(defn bb-b [text]
(clojure.string/replace text ; ;
#"(?i)\[b\](.+?)\[\/b\]"
"<b>$1</b>"))
(defn bb-strike [text]
(clojure.string/replace text ; ;
#"(?i)\[s\](.+?)\[\/s\]"
"<span class='bb-strike'>$1</span>"))
(defn bb-i [text]
(clojure.string/replace text ; ;
#"(?i)\[i\](.+?)\[\/i\]"
"<i>$1</i>"))
(defn bb-u [text]
(str/replace text
#"(?i)\[u\](.+?)\[\/u\]"
"<span class='bb-u'>$1</span>"))
(defn bb-center [text]
(clojure.string/replace text ; ;
#"(?i)\[center\](.+?)\[\/center\]"
"<center>$1</center>"))
(defn bb-centre [text]
(clojure.string/replace text ; ;
#"(?i)\[centre\](.+?)\[\/centre\]"
"<center>$1</center>"))
(defn bb-url [text]
(clojure.string/replace text ; ;
#"(?i)\[url=([^\]]+)\]([^\[]+)\[\/url\]"
"<a href=\"$1\">$2</a>"))
(defn bb-url-unanchored [text]
(clojure.string/replace
text ; ;
#"(?i)\[url=([^\]]+)\]"
"<a href=\"$1\">$1</a>"))
(defn line-break [text]
(clojure.string/replace
text
"\n"
"<br>"))
(defn horizontal-rule [text]
(clojure.string/replace
text
#"\n{1,}-{3,}\n{1,}"
"<hr>"))
(defn supergray [text]
(clojure.string/replace
text
#"(?i):supergray"
"<img src='http://i.imgur.com/xiHgUej.gif'>"))
(defn smilies [text]
(str/replace text smilies/smilie-regex
(fn [[_ s]] (str "<img src=\"/smilies/"
(clojure.string/lower-case s) ".gif\">") )))
;; (defn bb-table [text]
;; (clojure.string/replace text ; ;
;; #"(?i)\[table\](.+)\[\/table\]"
;; "<table class='table'>$1</table>"))
;; (defn bb-table-row [text]
;; (clojure.string/replace text ; ;
;; #"(?i)\[row\](.+)\[\/row\]"
;; "<tr>$1</tr>"))
;; (defn bb-table-col [text]
;; (clojure.string/replace text ; ;
;; #"(?i)\[col\](+)\[\/col\]"
;; "<td>$1</td>"))
(defn autolink [text]
(str/replace text
#"(?i)((https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|])"
"<a href='$1'>$1</a>"))
(defn bb-list [text]
(str/replace text
#"(?i)\[list\](.+?)\[\/list\]"
"<ul>$1</ul>"))
(defn bb-list1 [text]
(str/replace text
#"(?i)\[list=1\](.+?)\[\/list\]"
"<ol>$1</ol>"))
(defn bb-li [text]
(str/replace text
#"(?i)\[\*\](.+?)\[\/\*\]"
"<li>$1</li>"))
(defn bb-indent [text]
(str/replace text
#"(?i)\[indent\](.+?)\[\/indent\]"
"<div class='bb-indent'>$1</div>"))
(defn to-html [markdown]
(-> markdown
hiccup.core/h
horizontal-rule ; must come beofre line-break
line-break
;; embolden
;; italicize
bb-quote
bb-quote-unnamed
bb-img
bb-img-wrapped
;auto-link
bb-url
bb-url-unanchored
bb-hider
bb-hider2
supergray
smilies
;smilies/replace-smilie
bb-i
bb-b
bb-strike
bb-center
bb-centre
bb-u
bb-list
bb-list1
bb-li
bb-indent
youtube
)
)
;(to-html "[quote][/quote]")
;; (to-html "Hello::)")
;; (to-html (str/join (repeat 1000 "::)")))
;; #_(defn to-html [markdown]
;; (md/md-to-html-string markdown
;; :replacement-transformers
;; (concat [escape-transformer
;; autolink-transformer
;; bb-quote-transformer
;; ]
;; transformer-vector)))
;; (spit "test-html.html" (to-html (slurp (clojure.java.io/resource "test.txt"))))
;; (println (slurp (clojure.java.io/resource "test-html.html")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment