| ... | ... |
@@ -1,3 +1,18 @@ |
| 1 |
+# Title: Include Code Tag for Jekyll |
|
| 2 |
+# Author: Brandon Mathis http://brandonmathis.com |
|
| 3 |
+# Description: Import files on your filesystem into any blog post as embedded code snippets with syntax highlighting and a download link. |
|
| 4 |
+# Configuration: You can set default import path in _config.yml (defaults to code_dir: downloads/code) |
|
| 5 |
+# |
|
| 6 |
+# Syntax {% include_code path/to/file %}
|
|
| 7 |
+# |
|
| 8 |
+# Example: |
|
| 9 |
+# {% include_code javascripts/test.js %}
|
|
| 10 |
+# |
|
| 11 |
+# This will import test.js from source/downloads/code/javascripts/test.js |
|
| 12 |
+# and output the contents in a syntax highlighted code block inside a figure, |
|
| 13 |
+# with a figcaption listing the file name and download link |
|
| 14 |
+# |
|
| 15 |
+ |
|
| 1 | 16 |
require 'pathname' |
| 2 | 17 |
|
| 3 | 18 |
module Jekyll |
| 4 | 19 |
deleted file mode 100644 |
| ... | ... |
@@ -1,31 +0,0 @@ |
| 1 |
-require 'pathname' |
|
| 2 |
- |
|
| 3 |
-module Jekyll |
|
| 4 |
- |
|
| 5 |
- class IncludePartialTag < Liquid::Tag |
|
| 6 |
- def initialize(tag_name, file, tokens) |
|
| 7 |
- super |
|
| 8 |
- @file = file.strip |
|
| 9 |
- end |
|
| 10 |
- |
|
| 11 |
- def render(context) |
|
| 12 |
- file_dir = (context.registers[:site].source || 'source') |
|
| 13 |
- file_path = Pathname.new(file_dir).expand_path |
|
| 14 |
- file = file_path + @file |
|
| 15 |
- |
|
| 16 |
- unless file.file? |
|
| 17 |
- return "File #{file} could not be found"
|
|
| 18 |
- end |
|
| 19 |
- |
|
| 20 |
- Dir.chdir(file_path) do |
|
| 21 |
- partial = Liquid::Template.parse(file.read) |
|
| 22 |
- context.stack do |
|
| 23 |
- partial.render(context) |
|
| 24 |
- end |
|
| 25 |
- end |
|
| 26 |
- end |
|
| 27 |
- end |
|
| 28 |
-end |
|
| 29 |
- |
|
| 30 |
-Liquid::Template.register_tag('include_partial', Jekyll::IncludePartialTag)
|
|
| 31 |
- |
| 32 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,52 @@ |
| 0 |
+# Title: Render Partial Tag for Jekyll |
|
| 1 |
+# Author: Brandon Mathis http://brandonmathis.com |
|
| 2 |
+# Description: Import files on your filesystem into any blog post and render them inline. |
|
| 3 |
+# Note: Paths are relative to the source directory |
|
| 4 |
+# |
|
| 5 |
+# Syntax {% render_partial path/to/file %}
|
|
| 6 |
+# |
|
| 7 |
+# Example 1: |
|
| 8 |
+# {% render_partial about/_bio.markdown %}
|
|
| 9 |
+# |
|
| 10 |
+# This will import source/about/_bio.markdown and render it inline. |
|
| 11 |
+# In this example I used an underscore at the beginning of the filename to prevent Jekyll |
|
| 12 |
+# from generating an about/bio.html (Jekyll doesn't convert files beginning with underscores) |
|
| 13 |
+# |
|
| 14 |
+# Example 2: |
|
| 15 |
+# {% render_partial ../README.markdown %}
|
|
| 16 |
+# |
|
| 17 |
+# You can use relative pathnames, to include files outside of the source directory. |
|
| 18 |
+# This might be useful if you want to have a page for a project's README without having |
|
| 19 |
+# to duplicated the contents |
|
| 20 |
+# |
|
| 21 |
+ |
|
| 22 |
+require 'pathname' |
|
| 23 |
+ |
|
| 24 |
+module Jekyll |
|
| 25 |
+ |
|
| 26 |
+ class RenderPartialTag < Liquid::Tag |
|
| 27 |
+ def initialize(tag_name, file, tokens) |
|
| 28 |
+ super |
|
| 29 |
+ @file = file.strip |
|
| 30 |
+ end |
|
| 31 |
+ |
|
| 32 |
+ def render(context) |
|
| 33 |
+ file_dir = (context.registers[:site].source || 'source') |
|
| 34 |
+ file_path = Pathname.new(file_dir).expand_path |
|
| 35 |
+ file = file_path + @file |
|
| 36 |
+ |
|
| 37 |
+ unless file.file? |
|
| 38 |
+ return "File #{file} could not be found"
|
|
| 39 |
+ end |
|
| 40 |
+ |
|
| 41 |
+ Dir.chdir(file_path) do |
|
| 42 |
+ partial = Liquid::Template.parse(file.read) |
|
| 43 |
+ context.stack do |
|
| 44 |
+ partial.render(context) |
|
| 45 |
+ end |
|
| 46 |
+ end |
|
| 47 |
+ end |
|
| 48 |
+ end |
|
| 49 |
+end |
|
| 50 |
+ |
|
| 51 |
+Liquid::Template.register_tag('render_partial', Jekyll::RenderPartialTag)
|