Permalink
Browse files

First commit.

  • Loading branch information...
0 parents commit 90e4ebcca8faad16876d45aa0b15314020e1d966 @Raynes committed Dec 26, 2012
Showing with 92 additions and 0 deletions.
  1. +10 −0 .gitignore
  2. +13 −0 README.md
  3. +7 −0 project.clj
  4. +55 −0 src/me/raynes/cegdown.clj
  5. +7 −0 test/cegdown/core_test.clj
@@ -0,0 +1,10 @@
+/target
+/lib
+/classes
+/checkouts
+pom.xml
+*.jar
+*.class
+.lein-deps-sum
+.lein-failures
+.lein-plugins
@@ -0,0 +1,13 @@
+# cegdown
+
+A Clojure library designed to ... well, that part is up to you.
+
+## Usage
+
+FIXME
+
+## License
+
+Copyright © 2012 FIXME
+
+Distributed under the Eclipse Public License, the same as Clojure.
@@ -0,0 +1,7 @@
+(defproject cegdown "0.1.0-SNAPSHOT"
+ :description "A simple Clojure pegdown wrapper."
+ :url "https://github.com/Raynes/cegdown"
+ :license {:name "Eclipse Public License"
+ :url "http://www.eclipse.org/legal/epl-v10.html"}
+ :dependencies [[org.clojure/clojure "1.4.0"]
+ [org.pegdown/pegdown "1.2.1"]])
@@ -0,0 +1,55 @@
+(ns me.raynes.cegdown
+ (:import (org.pegdown PegDownProcessor Extensions LinkRenderer)))
+
+(def extensions
+ "Mappings of keywords to extension constants."
+ {:abbreviations Extensions/ABBREVIATIONS
+ :all Extensions/ALL
+ :autolinks Extensions/AUTOLINKS
+ :definitions Extensions/DEFINITIONS
+ :fenced-code-blocks Extensions/FENCED_CODE_BLOCKS
+ :hardwraps Extensions/HARDWRAPS
+ :none Extensions/NONE
+ :quotes Extensions/QUOTES
+ :smartypants Extensions/SMARTYPANTS
+ :suppress-all-html Extensions/SUPPRESS_ALL_HTML
+ :suppress-html-blocks Extensions/SUPPRESS_HTML_BLOCKS
+ :suppress-inline-html Extensions/SUPPRESS_INLINE_HTML
+ :tables Extensions/TABLES
+ :wikilinks Extensions/WIKILINKS})
+
+(defn select-extensions
+ "Take a list of extension keywords and get the bit-or'd extensions to pass
+ to make-pegdown."
+ [exts]
+ (let [exts (filter identity (map extensions exts))]
+ (int
+ (if (> (count exts) 1)
+ (apply bit-or exts)
+ (first exts)))))
+
+(defn make-pegdown
+ "Make a PegDownProcessor instance with the provided extensions or a Parser
+ instance."
+ [exts-or-parser]
+ (PegDownProcessor.
+ (if (sequential? exts-or-parser)
+ (select-extensions exts-or-parser)
+ exts-or-parser)))
+
+(defn to-html
+ "Convert markdown to HTML. Takes at least one argument, a string of markdown.
+ Optionally takes a 'target' which is either a PegDownProcessor instance,
+ a sequence of extension keys, or a Parser object. Optional third argument is
+ a LinkRenderer."
+ ([s target link-renderer]
+ (.markdownToHtml
+ (if (instance? PegDownProcessor target)
+ target
+ (make-pegdown target))
+ s
+ link-renderer))
+ ([s target]
+ (to-html s target (LinkRenderer.)))
+ ([s]
+ (to-html s (make-pegdown [:none]) (LinkRenderer.))))
@@ -0,0 +1,7 @@
+(ns cegdown.core-test
+ (:use clojure.test
+ cegdown.core))
+
+(deftest a-test
+ (testing "FIXME, I fail."
+ (is (= 0 1))))

0 comments on commit 90e4ebc

Please sign in to comment.