| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,81 @@ |
| 0 |
+# Title: Simple Code Blocks for Jekyll |
|
| 1 |
+# Author: Brandon Mathis http://brandonmathis.com |
|
| 2 |
+# Description: Write codeblocks with semantic HTML5 <figure> and <figcaption> elements and optional syntax highlighting — all with a simple, intuitive interface. |
|
| 3 |
+# |
|
| 4 |
+# Syntax: {% codeblock [title] [url] [link text] %}
|
|
| 5 |
+# |
|
| 6 |
+# For syntax highlighting, put a file extension somewhere in the title. examples: |
|
| 7 |
+# {% codeblock file.sh %}
|
|
| 8 |
+# {% codeblock Time to be Awesome! (awesome.rb) %}
|
|
| 9 |
+# |
|
| 10 |
+# Example: |
|
| 11 |
+# |
|
| 12 |
+# {% codeblock Got pain? painreleif.sh http://site.com/painreleief.sh Download it! %}
|
|
| 13 |
+# $ rm -rf ~/PAIN |
|
| 14 |
+# {% endcodeblock %}
|
|
| 15 |
+# |
|
| 16 |
+# Output: |
|
| 17 |
+# |
|
| 18 |
+# <figure role=code> |
|
| 19 |
+# <figcaption><span>Got pain? painrelief.sh</span> <a href="http://site.com/painrelief.sh">Download it!</a> |
|
| 20 |
+# <div class="highlight"><pre><code class="sh"> |
|
| 21 |
+# -- nicely escaped highlighted code -- |
|
| 22 |
+# </code></pre></div> |
|
| 23 |
+# </figure> |
|
| 24 |
+# |
|
| 25 |
+# Example 2 (no syntax highlighting): |
|
| 26 |
+# |
|
| 27 |
+# {% codeblock %}
|
|
| 28 |
+# <sarcasm>Ooooh, sarcasm... How original!</sarcasm> |
|
| 29 |
+# {% endcodeblock %}
|
|
| 30 |
+# |
|
| 31 |
+# <figure role=code> |
|
| 32 |
+# <pre><code><sarcasm> Ooooh, sarcasm... How original!</sarcasm> |
|
| 33 |
+# </code></pre> |
|
| 34 |
+# </figure> |
|
| 35 |
+# |
|
| 36 |
+module Jekyll |
|
| 37 |
+ |
|
| 38 |
+ class CodeBlock < Liquid::Block |
|
| 39 |
+ CaptionUrlTitle = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)\s+(.+)/i |
|
| 40 |
+ CaptionUrl = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)/i |
|
| 41 |
+ Caption = /(\S[\S\s]*)/ |
|
| 42 |
+ def initialize(tag_name, markup, tokens) |
|
| 43 |
+ @title = nil |
|
| 44 |
+ @caption = nil |
|
| 45 |
+ @highlight = true |
|
| 46 |
+ if markup =~ CaptionUrlTitle |
|
| 47 |
+ @file = $1 |
|
| 48 |
+ @caption = "<figcaption><span>#{$1}</span><a href='#{$2 + $3}'>#{$4}</a</figcaption>"
|
|
| 49 |
+ elsif markup =~ CaptionUrl |
|
| 50 |
+ @file = $1 |
|
| 51 |
+ @caption = "<figcaption><span>#{$1}</span><a href='#{$2 + $3}'>link</a</figcaption>"
|
|
| 52 |
+ elsif markup =~ Caption |
|
| 53 |
+ @file = $1 |
|
| 54 |
+ @caption = "<figcaption><span>#{$1}</span></figcaption>\n"
|
|
| 55 |
+ end |
|
| 56 |
+ if @file =~ /\S[\S\s]*\.(\w+)/ |
|
| 57 |
+ @filetype = $1 |
|
| 58 |
+ end |
|
| 59 |
+ super |
|
| 60 |
+ end |
|
| 61 |
+ |
|
| 62 |
+ def render(context) |
|
| 63 |
+ output = super |
|
| 64 |
+ code = super.join |
|
| 65 |
+ source = "<figure role=code>\n" |
|
| 66 |
+ source += @caption if @caption |
|
| 67 |
+ if @filetype |
|
| 68 |
+ source += "{% highlight #{@filetype} %}\n" + code + "\n{% endhighlight %}\n</figure>"
|
|
| 69 |
+ else |
|
| 70 |
+ source += "<pre><code>" + code.gsub!(/</,'<') + "</code></pre>\n</figure>" |
|
| 71 |
+ end |
|
| 72 |
+ partial = Liquid::Template.parse(source) |
|
| 73 |
+ context.stack do |
|
| 74 |
+ partial.render(context) |
|
| 75 |
+ end |
|
| 76 |
+ end |
|
| 77 |
+ end |
|
| 78 |
+end |
|
| 79 |
+ |
|
| 80 |
+Liquid::Template.register_tag('codeblock', Jekyll::CodeBlock)
|