Browse code

1. Added new include_code tag to allow auhtors to insert files from the file system with syntax highligting and a download link 2. Improved the gist tag to properly insert the sources in <noscript> tags 3. Improved semantics in the blockquote plugin and DRYed things up. 4. Pygments caching now stores to the _code_cache directory by default 5. Added a configuration for the default include_code directory 6. Updated the .gitignore

Brandon Mathis authored on 15/06/2011 at 22:31:22
Showing 6 changed files
... ...
@@ -1,8 +1,8 @@
1 1
 .bundle
2 2
 .DS_Store
3 3
 .sass-cache
4
-.gist_cache
5
-_cache
4
+_gist_cache
5
+_code_cache
6 6
 _assets
7 7
 public
8 8
 source/_stash
... ...
@@ -1,16 +1,17 @@
1 1
 # Required configuration
2 2
 source: source
3 3
 destination: public
4
+code_dir: downloads/code
4 5
 port: 4000
5 6
 
6
-url: http://yoursite.com
7
+url: http://octopress.dev
7 8
 title: My Octopress Blog
8 9
 subtitle: A blogging framework for hackers.
9 10
 author: Your Name
10 11
 subscribe_rss: /atom.xml
11 12
 subscribe_email: http://feedburner.com/asdfasdf
12 13
 
13
-markdown: rdiscount
14
+#markdown: rdiscount
14 15
 pygments: true
15 16
 recent_posts: 1
16 17
 simple_search: http://google.com/search
... ...
@@ -1,21 +1,20 @@
1 1
 #
2
-# Author: Josediaz Gonzalez - https://github.com/josegonzalez
3
-# Source URL: https://github.com/josegonzalez/josediazgonzalez.com/blob/master/_plugins/blockquote.rb
4
-# Modified by Brandon Mathis removed pullquotes and added simple cite paramaters
2
+# Author: Brandon Mathis
3
+# Based on the work of: Josediaz Gonzalez - https://github.com/josegonzalez/josediazgonzalez.com/blob/master/_plugins/blockquote.rb
5 4
 #
6 5
 require './_plugins/titlecase.rb'
7 6
 module Jekyll
8 7
 
9 8
   # Outputs a string with a given attribution as a quote
10 9
   #
11
-  #   {% blockquote John Paul Jones %}
12
-  #     Monkeys!
10
+  #   {% blockquote Bobby Willis http://google.com/blah the search for bobby's mom %}
11
+  #   Wheeee!
13 12
   #   {% endblockquote %}
14 13
   #   ...
15 14
   #   <blockquote>
16
-  #     Monkeys!
17
-  #     <br />
18
-  #     John Paul Jones
15
+  #     <p>Wheeee!</p>
16
+  #     <footer>
17
+  #     <strong>John Paul Jones</strong><cite><a href="http://google.com/blah">The Search For Bobby's Mom</a>
19 18
   #   </blockquote>
20 19
   #
21 20
   class Blockquote < Liquid::Block
... ...
@@ -42,15 +41,16 @@ module Jekyll
42 42
 
43 43
     def render(context)
44 44
       output = super
45
-      if @by.nil?
46
-        '<blockquote><p>' + output.join + '</p></blockquote>'
47
-      elsif !@title.nil?
48
-        '<blockquote><p>' + output.join + '</p>' + '<p><strong>' + @by + '</strong>' + '<cite><a class="source" href="' + @source + '">' + @title + '</a></cite></p></blockquote>'
45
+      author = "<strong>#{@by}</strong>"
46
+      cite = "<cite><a class='source' href='#{@source}'>#{(@title || 'source')}</a></cite>"
47
+      reply = if @by.nil?
48
+        "<p>#{output.join.gsub(/\n\n/, '</p><p>')}</p>"
49 49
       elsif !@source.nil?
50
-        '<blockquote><p>' + output.join + '</p>' + '<p><strong>' + @by + '</strong>' + '<cite><a class="source" href="' + @source + '">source</a></cite></p></blockquote>'
50
+        "<p>#{output.join.gsub(/\n\n/, '</p><p>')}</p><footer>#{author + cite}</footer>"
51 51
       else
52
-        '<blockquote><p>' + output.join + '</p>' + '<p><strong>' + @by + '</strong></p></blockquote>'
52
+        "<p>#{output.join.gsub(/\n\n/, '</p><p>')}</p><footer>#{author}</footer>"
53 53
       end
54
+      "<blockquote>#{reply}</blockquote>"
54 55
     end
55 56
   end
56 57
 end
... ...
@@ -1,9 +1,11 @@
1
-# Nicked from Brandon Tilly
2
-# Gist https://gist.github.com/803483
1
+# A Liquid tag for Jekyll sites that allows embedding Gists and showing code for non-JavaScript enabled browsers and readers.
2
+# by: Brandon Tilly
3
+# Source URL: https://gist.github.com/1027674
3 4
 # Post http://brandontilley.com/2011/01/31/gist-tag-for-jekyll.html
4 5
 #
5
-# Example usage: {% gist 803483 gist_tag.rb %} //embeds a gist for this plugin
6
+# Example usage: {% gist 1027674 gist_tag.rb %} //embeds a gist for this plugin
6 7
 
8
+require 'cgi'
7 9
 require 'digest/md5'
8 10
 require 'net/https'
9 11
 require 'uri'
... ...
@@ -12,60 +14,70 @@ module Jekyll
12 12
   class GistTag < Liquid::Tag
13 13
     def initialize(tag_name, text, token)
14 14
       super
15
-      system('mkdir -p .gist_cache')
16
-      @text         = text
17
-      @cache        = true
18
-      @cache_folder = File.expand_path "../.gist_cache", File.dirname(__FILE__)
15
+      @text           = text
16
+      @cache_disabled = false
17
+      @cache_folder   = File.expand_path "../_gist_cache", File.dirname(__FILE__)
18
+      FileUtils.mkdir_p @cache_folder
19 19
     end
20 20
 
21 21
     def render(context)
22
-      return "" unless @text =~ /([\d]*) (.*)/
22
+      if parts = @text.match(/([\d]*) (.*)/)
23
+        gist, file = parts[1].strip, parts[2].strip
24
+        script_url = script_url_for gist, file
25
+        code       = get_cached_gist(gist, file) || get_gist_from_web(gist, file)
26
+        html_output_for script_url, code
27
+      else
28
+        ""
29
+      end
30
+    end
23 31
 
24
-      gist, file = $1.strip, $2.strip
25
-      script_url = "https://gist.github.com/#{gist}.js?file=#{file}"
32
+    def html_output_for(script_url, code)
33
+      code = CGI.escapeHTML code
34
+      <<-HTML
35
+<script src='#{script_url}'></script>
36
+<noscript><pre><code>#{code}</code></pre></noscript>
37
+      HTML
38
+    end
26 39
 
27
-      code       = get_cached_gist(gist, file) || get_gist_from_web(gist, file)
28
-      code       = code.gsub "<", "&lt;"
29
-      string     = "<script src='#{script_url}'></script>"
30
-      string    += "<noscript><pre><code>#{code}</code></pre></noscript>"
31
-      return string
40
+    def script_url_for(gist_id, filename)
41
+      "https://gist.github.com/#{gist_id}.js?file=#{filename}"
32 42
     end
33 43
 
34 44
     def get_gist_url_for(gist, file)
35
-      "https://gist.github.com/raw/#{gist}/#{file}"
45
+      "https://raw.github.com/gist/#{gist}/#{file}"
36 46
     end
37 47
 
38
-    def cache_gist(gist, file, data)
39
-      file = get_cache_file_for gist, file
40
-      File.open(file, "w+") do |f|
41
-        f.write(data)
48
+    def cache(gist, file, data)
49
+      cache_file = get_cache_file_for gist, file
50
+      File.open(cache_file, "w") do |io|
51
+        io.write data
42 52
       end
43 53
     end
44 54
 
45 55
     def get_cached_gist(gist, file)
46
-      return nil if @cache == false
47
-      file = get_cache_file_for gist, file
48
-      return nil unless File.exist?(file)
49
-      return File.new(file).readlines.join
56
+      return nil if @cache_disabled
57
+      cache_file = get_cache_file_for gist, file
58
+      File.read cache_file if File.exist? cache_file
50 59
     end
51 60
 
52 61
     def get_cache_file_for(gist, file)
53
-      gist.gsub! /[^a-zA-Z0-9\-_\.]/, ''
54
-      file.gsub! /[^a-zA-Z0-9\-_\.]/, ''
55
-      md5 = Digest::MD5.hexdigest "#{gist}-#{file}"
62
+      bad_chars = /[^a-zA-Z0-9\-_.]/
63
+      gist      = gist.gsub bad_chars, ''
64
+      file      = file.gsub bad_chars, ''
65
+      md5       = Digest::MD5.hexdigest "#{gist}-#{file}"
56 66
       File.join @cache_folder, "#{gist}-#{file}-#{md5}.cache"
57 67
     end
58 68
 
59 69
     def get_gist_from_web(gist, file)
60
-      gist_url          = get_gist_url_for(gist, file)
61
-      raw_uri           = URI.parse(gist_url)
62
-      https             = Net::HTTP.new(raw_uri.host, raw_uri.port)
70
+      gist_url          = get_gist_url_for gist, file
71
+      raw_uri           = URI.parse gist_url
72
+      https             = Net::HTTP.new raw_uri.host, raw_uri.port
63 73
       https.use_ssl     = true
64 74
       https.verify_mode = OpenSSL::SSL::VERIFY_NONE
65
-      request           = Net::HTTP::Get.new(raw_uri.request_uri)
66
-      data              = https.request(request)
75
+      request           = Net::HTTP::Get.new raw_uri.request_uri
76
+      data              = https.request request
67 77
       data              = data.body
68
-      cache_gist(gist, file, data) unless @cache == false
78
+      cache gist, file, data unless @cache_disabled
69 79
       data
70 80
     end
71 81
   end
... ...
@@ -73,11 +85,10 @@ module Jekyll
73 73
   class GistTagNoCache < GistTag
74 74
     def initialize(tag_name, text, token)
75 75
       super
76
-      @cache = false
76
+      @cache_disabled = true
77 77
     end
78 78
   end
79 79
 end
80 80
 
81 81
 Liquid::Template.register_tag('gist', Jekyll::GistTag)
82 82
 Liquid::Template.register_tag('gistnocache', Jekyll::GistTagNoCache)
83
-
84 83
new file mode 100644
... ...
@@ -0,0 +1,40 @@
0
+require 'pathname'
1
+
2
+module Jekyll
3
+
4
+  class IncludeCodeTag < Liquid::Tag
5
+    def initialize(tag_name, file, tokens)
6
+      super
7
+      @file = file.strip
8
+    end
9
+
10
+    def render(context)
11
+      code_dir = (context.registers[:site].config['code_dir'] || 'downloads/code')
12
+      code_path = (Pathname.new(context.registers[:site].source) + code_dir).expand_path
13
+      file = code_path + @file
14
+
15
+      if File.symlink?(code_path)
16
+        return "Code directory '#{code_path}' cannot be a symlink"
17
+      end
18
+
19
+      unless file.file?
20
+        return "File #{file} could not be found"
21
+      end
22
+
23
+      Dir.chdir(code_path) do
24
+        code = file.read
25
+        file_type = file.extname
26
+        url = "#{context.registers[:site].config['url']}/#{code_dir}/#{@file}"
27
+        source = "<figure><figcaption><span>#{file.basename}</span><a href='#{url}'>download</a></figcaption>\n"
28
+        source += "{% highlight #{file_type} %}\n" + code + "\n{% endhighlight %}</figure>"
29
+        partial = Liquid::Template.parse(source)
30
+        context.stack do
31
+          partial.render(context)
32
+        end
33
+      end
34
+    end
35
+  end
36
+
37
+end
38
+
39
+Liquid::Template.register_tag('include_code', Jekyll::IncludeCodeTag)
... ...
@@ -6,7 +6,7 @@
6 6
 require 'fileutils'
7 7
 require 'digest/md5'
8 8
 
9
-PYGMENTS_CACHE_DIR = File.expand_path('../../_cache', __FILE__)
9
+PYGMENTS_CACHE_DIR = File.expand_path('../../_code_cache', __FILE__)
10 10
 FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
11 11
 
12 12
 Jekyll::HighlightBlock.class_eval do