Permalink
Browse files

Switch to ``` clojure syntax for code snippets; requires re-running b…

…undle install!
  • Loading branch information...
1 parent 0f450d9 commit 6b21a83580045e5b65c8c3b0f3955a9c95253bbd Michael S. Klishin committed Oct 12, 2012
View
@@ -12,3 +12,4 @@ _site/*
_source/*
deploy.sh
repl*
+_cache/*
View
@@ -1,6 +1,7 @@
source :rubygems
gem "jekyll"
-gem "rdiscount"
+# gem "rdiscount"
+gem "redcarpet"
gem "haml"
gem "RedCloth"
View
@@ -21,7 +21,7 @@ GEM
maruku (0.6.0)
syntax (>= 1.0.0)
posix-spawn (0.3.6)
- rdiscount (1.6.8)
+ redcarpet (2.2.1)
syntax (1.0.0)
PLATFORMS
@@ -31,4 +31,4 @@ DEPENDENCIES
RedCloth
haml
jekyll
- rdiscount
+ redcarpet
View
@@ -3,6 +3,9 @@ destination: ./_site
plugins: ./_plugins
auto: true
exclude: [bin, CNAME, Gemfile, Gemfile.lock, README.md]
-markdown: rdiscount
permalink: /:title
-pygments: true
+pygments: true
+# markdown: rdiscount
+markdown: redcarpet2
+redcarpet:
+ extensions: ["no_intra_emphasis", "fenced_code_blocks", "autolink", "tables", "with_toc_data"]
@@ -0,0 +1,48 @@
+require 'fileutils'
+require 'digest/md5'
+require 'redcarpet'
+require 'albino'
+
+PYGMENTS_CACHE_DIR = File.expand_path('../../_cache', __FILE__)
+FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
+
+class Redcarpet2Markdown < Redcarpet::Render::HTML
+ def block_code(code, lang)
+ lang = lang || "text"
+ path = File.join(PYGMENTS_CACHE_DIR, "#{lang}-#{Digest::MD5.hexdigest code}.html")
+ cache(path) do
+ colorized = Albino.colorize(code, lang)
+ add_code_tags(colorized, lang)
+ end
+ end
+
+ def add_code_tags(code, lang)
+ code.sub(/<pre>/, "<pre><code class=\"#{lang}\">").
+ sub(/<\/pre>/, "</code></pre>")
+ end
+
+ def cache(path)
+ if File.exist?(path)
+ File.read(path)
+ else
+ content = yield
+ File.open(path, 'w') {|f| f.print(content) }
+ content
+ end
+ end
+end
+
+class Jekyll::MarkdownConverter
+ def extensions
+ Hash[ *@config['redcarpet']['extensions'].map {|e| [e.to_sym, true] }.flatten ]
+ end
+
+ def markdown
+ @markdown ||= Redcarpet::Markdown.new(Redcarpet2Markdown.new(extensions), extensions)
+ end
+
+ def convert(content)
+ return super unless @config['markdown'] == 'redcarpet2'
+ markdown.render(content)
+ end
+end
@@ -32,25 +32,25 @@ This guide covers Clojure 1.4.
The body of a (let ...) statement also provides an implicit (do ...) that allows for multiple statements in the body of (let ...).
A basic example:
-{% highlight clojure %}
+``` clojure
(let [x 1 y 2] (println x y)) ;; 1 2
-{% endhighlight %}
+```
Let can be nested, and the scope is lexically determined. This means that a binding's value is determined by the nearest binding form for that symbol.
This example basically demonstrates the lexical scoping of the let form.
-{% highlight clojure %}
+``` clojure
(let [x 1]
(println x) ;; prints 1
(let [x 2]
(println x))) ;; prints 2
-{% endhighlight %}
+```
Let bindings are immutable and can be destructured.
-{% highlight clojure %}
+``` clojure
todo - link to destructuring
-{% endhighlight %}
+```
### def
@@ -61,18 +61,18 @@ A root binding is a value that is shared across all threads.
The (let ...) form is the preferred method of creating local bindings. It is strongly suggested to prefer it where possible, and never use (def ...) within another form.
-{% highlight clojure %}
+``` clojure
;; todo - reference to var documentation, basic example
;; todo - metadata
-{% endhighlight %}
+```
### declare
(declare ...) provides a simple way of creating 'forward declarations'. (declare ...) defs the supplied symbols with no init values. This allows for referencing of a var before it has been supplied a value.
There are much better methods of value-based dispatch or code architecture in general, but this presents a simple situation forward declarations would be necessary.
-{% highlight clojure %}
+``` clojure
(declare func<10 func<20)
;; without declare you will receive an error similar to:
@@ -89,7 +89,7 @@ There are much better methods of value-based dispatch or code architecture in ge
(< x 10) (func10 x)
(< x 20) "More than 10, less than 20"
:else "too far!"))
-{% endhighlight %}
+```
No matter which order you put func<10 and func<20 in, there will be a reference to a var that does not yet exist when the compiler does the initial evaluation of top-level forms.
@@ -101,7 +101,7 @@ No matter which order you put func<10 and func<20 in, there will be a reference
Without (defn ..), a var would be directly bound to a function definition and explicit metadata about the doc string and argslits would be added manually.
-{% highlight clojure %}
+``` clojure
(def func (fn [x] x))
;; same as:
@@ -112,11 +112,11 @@ Without (defn ..), a var would be directly bound to a function definition and ex
;;same as
(defn func "documentation!" [x] x)
-{% endhighlight %}
+```
-{% highlight clojure %}
+``` clojure
;; todo - link to doc and metadata
-{% endhighlight %}
+```
### ns
@@ -131,7 +131,7 @@ TBD: [How to Contribute](https://github.com/clojuredocs/cds#how-to-contribute)
If a third expression is provided, when the first expression returns nil or false the third expression is evaluated and returned.
-{% highlight clojure %}
+``` clojure
user=> (if 0 "second") ;; 0 is a 'true' value. Only false or nil are 'false'
"second"
@@ -146,85 +146,85 @@ nil
user=> (if (nil? (= 1 2)) "second" "third") ;; differentiate between nil and false if needed
"third"
-{% endhighlight %}
+```
### when
(when ...) provides an implicit do form that is evaluated if an expression returns true, otherwise nil is returned. When does not provide an 'else'.
-{% highlight clojure %}
+``` clojure
user=> (when (= 1 2) (print "hey") 10)
nil
user=> (when (< 10 11) (print "hey") 10)
hey
10
-{% endhighlight %}
+```
### for
(for ...) allows for list comprehensions. (for ...) takes a vector of pairs of [binding collection]. (for ...) then assigns each sequential value in the collection to the binding form and evaluates them rightmost first. The results are returned in a lazy sequence.
(for ...) allows for explicit let, when and while through use of ":let []" ":when (expression)" ":while (expression)" in the binding vector.
-{% highlight clojure %}
+``` clojure
(for [x [1 2 3] y [4 5 6]]
[x y])
;; ([1 4] [1 5] [1 6] [2 4] [2 5] [2 6] [3 4] [3 5] [3 6])
-{% endhighlight %}
+```
:when only evaluates the body when a true value is returned by the expression provided
-{% highlight clojure %}
+``` clojure
(for [x [1 2 3] y [4 5 6]
:when (and
(even? x)
(odd? y))]
[x y])
;; ([2 5])
-{% endhighlight %}
+```
:while evaluates the body until a non-true value is reached. Note that the rightmost collection is fully bound to y before a non-true value of (< x 2) is reached. This demonstrates the order of the comprehension.
-{% highlight clojure %}
+``` clojure
(for [x [1 2 3] y [4 5 6]
:while (< x 2)]
[x y])
;; ([1 4] [1 5] [1 6])
-{% endhighlight %}
+```
### doseq
(doseq ...) is similar to (for ...) except it does not return a sequence of results. (doseq ...) is generally intended for execution of side-effects in the body, and thusly returns nil.
(doseq ...) supports the same bindings as for - :let :when :while. For examples of these, see for.
-{% highlight clojure %}
+``` clojure
(doseq [x [1 2 3] y [4 5 6]]
(println [x y]))
;; [1 4][1 5][1 6][2 4][2 5][2 6][3 4][3 5][3 6]
;; nil
-{% endhighlight %}
+```
### apply
(apply ...) effectively unrolls the supplied args and a collection into a list of arguments to the supplied function.
-{% highlight clojure %}
+``` clojure
(str ["Hel" "lo"])
"[\"Hel\" \"lo\"]" ;; not what we want, str is operating on the vector
user> (apply str ["Hel" "lo"]) ;; same as (str "Hel" "lo")
"Hello"
-{% endhighlight %}
+```
(apply ...) prepends any supplied arguments to the form as well.
-{% highlight clojure %}
+``` clojure
(map + [[1 2 3] [1 2 3]]) ;; This attempts to add 2 vectors with +
;; ClassCastException java.lang.Class.cast (Class.java:2990)
@@ -233,7 +233,7 @@ user> (apply str ["Hel" "lo"]) ;; same as (str "Hel" "lo")
(apply + 1 2 3 [4 5 6]) ;; same as (+ 1 2 3 4 5 6)
;; 21
-{% endhighlight %}
+```
Note that apply can not be used with macros.
@@ -257,17 +257,17 @@ TBD: [How to Contribute](https://github.com/clojuredocs/cds#how-to-contribute)
Returns a count of the number of items in a collection. Nil returns a value of 0.
-{% highlight clojure %}
+``` clojure
(count "Hello")
;; 5
(count [1 2 3 4 5 6 7])
;; 7
-{% endhighlight %}
+```
Note that count does not return in constant time for all collections. This can be determined with (counted? ...). Lazy sequences must be realized to get a count of the items.
-{% highlight clojure %}
+``` clojure
(counted? "Hello")
;; false
@@ -276,7 +276,7 @@ Note that count does not return in constant time for all collections. This can b
(counted? [1 2 3 4 5]) ;; Constant time return of (count)
;; true
-{% endhighlight %}
+```
### conj
Oops, something went wrong.

0 comments on commit 6b21a83

Please sign in to comment.