Skip to content

Commit

Permalink
experimental 'highlight function
Browse files Browse the repository at this point in the history
  • Loading branch information
bfontaine committed Feb 12, 2014
1 parent 164d2df commit 39e924d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -7,3 +7,5 @@ pom.xml.asc
*.class
/.lein-*
/.nrepl-port
*.sw*
*~
6 changes: 4 additions & 2 deletions README.md
@@ -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.
3 changes: 0 additions & 3 deletions doc/intro.md

This file was deleted.

8 changes: 5 additions & 3 deletions project.clj
@@ -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"]])
46 changes: 41 additions & 5 deletions src/clygments/core.clj
@@ -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))))))

0 comments on commit 39e924d

Please sign in to comment.