Skip to content

Commit

Permalink
Add basic markdown support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Raynes committed Dec 16, 2012
1 parent e07e839 commit a7e3af8
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 21 deletions.
4 changes: 3 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
[clavatar "0.2.0"]
[clj-time "0.3.3"]
[me.raynes/conch "0.4.0"]
[commons-codec/commons-codec "1.6"]]
[commons-codec/commons-codec "1.6"]
[org.pegdown/pegdown "1.2.1"]
[me.raynes/laser "0.1.4"]]
:plugins [[lein-ring "0.7.1"]]
:ring {:handler refheap.server/handler
:auto-reload? false})
Expand Down
10 changes: 10 additions & 0 deletions resources/public/css/refheap.css
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ div.syntax {
border-radius: 4px;
}

div.markdown {
padding-left: 20px;
}

div.syntax div.more {
position: relative;
top: -10px;
Expand Down Expand Up @@ -629,3 +633,9 @@ span.hotkey {
vertical-align: middle;
width: 80px;
}

div.md-code {
display: inline;
width: 300px;
overflow: auto;
}
37 changes: 23 additions & 14 deletions src/refheap/pygments.clj → src/refheap/highlight.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns refheap.pygments
(ns refheap.highlight
(:require [conch.sh :refer [let-programs]]
[clojure.string :as string]))
[clojure.string :as string]
[refheap.markdown :refer [to-html]]))

(def lexers
"A map of language names to pygments lexer names."
Expand Down Expand Up @@ -193,7 +194,9 @@
:exts #{"kt"}}
"Elixir" {:short "ex"
:exts #{"ex" "exs"}}
"Elixir Console" {:short "iex"}})
"Elixir Console" {:short "iex"}
"Markdown" {:short "Markdown"
:exts #{"md" "markdown"}}})

(defn lookup-lexer
"Selects a language."
Expand All @@ -209,15 +212,21 @@
[lang lang-map]))
["Plain Text" {:short "text"}]))

(defn pygmentize
"Syntax highlight some code."
(defn highlight
"Syntax highlight some code. If anything other than markdown, highlight
with pygments. If markdown, render with pegdown."
[language text & [anchor?]]
(let-programs [pygmentize "./pygmentize"]
(let [output (pygmentize "-fhtml" (str "-l" language)
(str "-Olinenos=table,stripnl=False,encoding=utf-8"
(when anchor? ",anchorlinenos=true,lineanchors=L"))
{:dir "resources/pygments"
:in text})]
(if (seq output)
{:success output}
{:error "There ws an error pasting."}))))
(if (= language "Markdown")
(try
{:success (to-html text)}
(catch Exception _
{:error "There was an error pasting."}))
(let-programs [pygmentize "./pygmentize"]
(let [output (pygmentize "-fhtml" (str "-l" language)
(str "-Olinenos=table,stripnl=False,encoding=utf-8"
(when anchor? ",anchorlinenos=true,lineanchors=L"))
{:dir "resources/pygments"
:in text})]
(if (seq output)
{:success output}
{:error "There was an error pasting."})))))
24 changes: 24 additions & 0 deletions src/refheap/markdown.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(ns refheap.markdown
(:require [me.raynes.laser :as laser])
(:import (org.pegdown PegDownProcessor Extensions)))

(defn make-pegdown
"Create a PegDownProcessor instance with the hardwraps and
fenced code blocks extensions"
[]
(PegDownProcessor.
(reduce bit-or 0 [Extensions/HARDWRAPS
Extensions/FENCED_CODE_BLOCKS])))

(defn wrap-code [html]
(laser/fragment-to-html
(laser/fragment
(laser/parse-fragment html)
(laser/child-of
(laser/negate (laser/element= :p))
(laser/element= :code)) (laser/wrap :div {:class "md-code"}))))

(defn to-html
"Convert markdown to html."
[s]
(wrap-code (.markdownToHtml (make-pegdown) s)))
6 changes: 3 additions & 3 deletions src/refheap/models/paste.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[refheap.messages :refer [error]]
[monger.collection :as mc]
[monger.query :refer [with-collection find sort limit skip]]
[refheap.pygments :refer [lookup-lexer pygmentize]])
[refheap.highlight :refer [lookup-lexer highlight]])
(:import java.io.StringReader
org.apache.commons.codec.digest.DigestUtils))

Expand Down Expand Up @@ -48,15 +48,15 @@
(let [[name {:keys [short]}] (lookup-lexer language)
private (boolean private)
random-id (or random-id (generate-id))
pygmentized (pygmentize short contents true)]
pygmentized (highlight short contents true)]
(if-let [highlighted (:success pygmentized)]
{:paste-id (if private random-id (str id))
:id id
:random-id random-id
:user (:id user)
:language name
:raw-contents contents
:summary (:success (pygmentize short (preview contents)))
:summary (:success (highlight short (preview contents)))
:private (boolean private)
:date date
:lines (let [lines (count (filter #{\newline} contents))]
Expand Down
6 changes: 5 additions & 1 deletion src/refheap/utilities.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@
"false" false
false))))

(def js-char-replacements
(merge char-escape-string
{\' "\\'"}))

(defn escape-string
"Escapes all escape sequences in a string to make it suitable
for passing to another programming language. Kind of like what
pr-str does for strings but without the wrapper quotes."
[s]
(join (map #(char-escape-string % %) s)))
(join (map #(js-char-replacements % %) s)))
6 changes: 5 additions & 1 deletion src/refheap/views/paste.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns refheap.views.paste
(:require [refheap.models.paste :as paste]
[refheap.models.users :as users]
[refheap.pygments :refer [lexers]]
[refheap.highlight :refer [lexers]]
[refheap.utilities :refer [to-booleany escape-string]]
[noir.session :as session]
[noir.response :refer [content-type]]
Expand Down Expand Up @@ -51,6 +51,8 @@
"refheap/views/templates/pasted"
{:language language
:private private
:extra (when (= language "Markdown")
"markdown")
:lines lines
:id id
:username (if user
Expand Down Expand Up @@ -199,3 +201,5 @@
(show-paste-page id))))
(GET "/pastes" {:keys [page]}
(all-pastes-page (paste/proper-page (Long. (or page "1"))))))


2 changes: 1 addition & 1 deletion src/refheap/views/templates/pasted.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
{{/fork}}
</div>
</div>
<div id="paste" class="syntax">
<div id="paste" class="syntax {{extra}}">
{{{contents}}}
</div>
</div>

0 comments on commit a7e3af8

Please sign in to comment.