├── .gitignore ├── test └── cegdown │ └── core_test.clj ├── project.clj ├── README.md └── src └── me └── raynes └── cegdown.clj /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /lib 3 | /classes 4 | /checkouts 5 | pom.xml 6 | *.jar 7 | *.class 8 | .lein-deps-sum 9 | .lein-failures 10 | .lein-plugins 11 | -------------------------------------------------------------------------------- /test/cegdown/core_test.clj: -------------------------------------------------------------------------------- 1 | (ns cegdown.core-test 2 | (:use clojure.test 3 | cegdown.core)) 4 | 5 | (deftest a-test 6 | (testing "FIXME, I fail." 7 | (is (= 0 1)))) -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject me.raynes/cegdown "0.1.0" 2 | :description "A simple Clojure pegdown wrapper." 3 | :url "https://github.com/Raynes/cegdown" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :dependencies [[org.clojure/clojure "1.4.0"] 7 | [org.pegdown/pegdown "1.2.1"]]) 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cegdown 2 | 3 | Cegdown is a simple little Clojure wrapper for the excellent 4 | [Pegdown](https://github.com/sirthias/pegdown) markdown parsing library. 5 | 6 | ## Usage 7 | 8 | You'll want to require it first: 9 | 10 | ```clojure 11 | user> (require '[me.raynes.cegdown :as md]) 12 | nil 13 | ``` 14 | 15 | Now you can generate some HTML from a bit of markdown: 16 | 17 | ```clojure 18 | user> (md/to-html "# hi\nhow are you") 19 | "

hi

how are you

" 20 | ``` 21 | 22 | Pegdown has lots of 23 | [extensions](http://www.decodified.com/pegdown/api/org/pegdown/Extensions.html). You 24 | can use them with `to-html`. Just pass it a sequence of keywords. For example, if 25 | we wanted fenced code blocks (like this README uses): 26 | 27 | ```clojure 28 | user> (md/to-html "```clojure\n(+ 3 3)\n```" [:fenced-code-blocks]) 29 | "
(+ 3 3)\n
" 30 | ``` 31 | 32 | Easy enough, right? 33 | 34 | ## License 35 | 36 | Copyright © 2012 Anthony Grimes 37 | 38 | Distributed under the Eclipse Public License, the same as Clojure. 39 | -------------------------------------------------------------------------------- /src/me/raynes/cegdown.clj: -------------------------------------------------------------------------------- 1 | (ns me.raynes.cegdown 2 | (:import (org.pegdown PegDownProcessor Extensions LinkRenderer))) 3 | 4 | (def extensions 5 | "Mappings of keywords to extension constants." 6 | {:abbreviations Extensions/ABBREVIATIONS 7 | :all Extensions/ALL 8 | :autolinks Extensions/AUTOLINKS 9 | :definitions Extensions/DEFINITIONS 10 | :fenced-code-blocks Extensions/FENCED_CODE_BLOCKS 11 | :hardwraps Extensions/HARDWRAPS 12 | :none Extensions/NONE 13 | :quotes Extensions/QUOTES 14 | :smarts Extensions/SMARTS 15 | :smartypants Extensions/SMARTYPANTS 16 | :suppress-all-html Extensions/SUPPRESS_ALL_HTML 17 | :suppress-html-blocks Extensions/SUPPRESS_HTML_BLOCKS 18 | :suppress-inline-html Extensions/SUPPRESS_INLINE_HTML 19 | :tables Extensions/TABLES 20 | :wikilinks Extensions/WIKILINKS}) 21 | 22 | (defn select-extensions 23 | "Take a list of extension keywords and get the bit-or'd extensions to pass 24 | to make-pegdown." 25 | [exts] 26 | (let [exts (filter identity (map extensions exts))] 27 | (int 28 | (if (> (count exts) 1) 29 | (apply bit-or exts) 30 | (first exts))))) 31 | 32 | (defn make-pegdown 33 | "Make a PegDownProcessor instance with the provided extensions or a Parser 34 | instance." 35 | [exts-or-parser] 36 | (PegDownProcessor. 37 | (if (sequential? exts-or-parser) 38 | (select-extensions exts-or-parser) 39 | exts-or-parser))) 40 | 41 | (defn to-html 42 | "Convert markdown to HTML. Takes at least one argument, a string of markdown. 43 | Optionally takes a 'target' which is either a PegDownProcessor instance, 44 | a sequence of extension keys, or a Parser object. Optional third argument is 45 | a LinkRenderer." 46 | ([s target link-renderer] 47 | (.markdownToHtml 48 | (if (instance? PegDownProcessor target) 49 | target 50 | (make-pegdown target)) 51 | s 52 | link-renderer)) 53 | ([s target] 54 | (to-html s target (LinkRenderer.))) 55 | ([s] 56 | (to-html s (make-pegdown [:none]) (LinkRenderer.)))) 57 | --------------------------------------------------------------------------------