Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
52 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,5 @@ pom.xml.asc | |
*.class | ||
/.lein-* | ||
/.nrepl-port | ||
*.sw* | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
# clygments | ||
|
||
A Clojure library designed to ... well, that part is up to you. | ||
**clygments** is a Clojure bridge for [Pygments][]. | ||
|
||
[Pygments]: http://pygments.org/ | ||
|
||
## Usage | ||
|
||
FIXME | ||
|
||
## License | ||
|
||
Copyright © 2014 FIXME | ||
Copyright © 2014 Baptiste Fontaine | ||
|
||
Distributed under the Eclipse Public License either version 1.0 or (at | ||
your option) any later version. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
(defproject clygments "0.1.0-SNAPSHOT" | ||
:description "FIXME: write description" | ||
:url "http://example.com/FIXME" | ||
:description "Use Pygments from Clojure" | ||
:url "http://github.com/bfontaine/clygments" | ||
:license {:name "Eclipse Public License" | ||
:url "http://www.eclipse.org/legal/epl-v10.html"} | ||
:dependencies [[org.clojure/clojure "1.5.1"]]) | ||
:dependencies [[org.clojure/clojure "1.5.1"] | ||
[org.python/jython-standalone "2.5.3"] | ||
[org.pygments/pygments "1.5"]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,42 @@ | ||
(ns clygments.core) | ||
(ns clygments.core | ||
(:import [org.python.util PythonInterpreter])) | ||
|
||
(defn foo | ||
"I don't do a whole lot." | ||
[x] | ||
(println x "Hello, World!")) | ||
(def ^{:private true} python | ||
(doto (PythonInterpreter.) | ||
(.exec (str "from pygments import highlight\n" | ||
"from pygments.lexers import *\n" | ||
"from pygments.formatters import *\n")))) | ||
|
||
(defn- exec-code-in | ||
"put the result of the execution of a line of code in a variable, with | ||
an optional default value if it encounters an exception" | ||
([var-name code] | ||
(.exec python (str var-name "=" code))) | ||
([var-name code default] | ||
(.exec python (str "try:\n\t" var-name "=" code "\n" | ||
"except:\n\t" var-name "=" default "\n")))) | ||
|
||
(def ^{:private true} py-true (.eval python "True")) | ||
(defn- py-true? | ||
"test if an expression evals to True in Python" | ||
[expr] | ||
(= (.eval python expr) py-true)) | ||
|
||
(defn highlight | ||
"highlight a piece of code." | ||
([code] (highlight code :python :html)) | ||
([code lang output] | ||
(try | ||
(exec-code-in "_r" (str "get_lexer_by_name(\"" (name lang) "\")") "None") | ||
(exec-code-in | ||
"_f" (str "get_formatter_by_name(\"" (name output) "\")") "None") | ||
(if (and (py-true? "_r!=None") (py-true? "_f!=None")) | ||
(do | ||
;; XXX it won't work if 'code contains a sequence of three or more | ||
;; double quotes, e.g.: """ | ||
(.exec python (str "_res=highlight(\"\"\"" code "\"\"\",_r,_f)\n")) | ||
;; XXX it won't work with non-text formatters, such as gif | ||
(.get python "_res" String))) | ||
(catch Exception e | ||
(.printStackTrace e) | ||
(println (.getMessage e)))))) |