Skip to content

Instantly share code, notes, and snippets.

@hazybluedot
Last active August 29, 2015 14:00
Show Gist options
  • Save hazybluedot/11147345 to your computer and use it in GitHub Desktop.
Save hazybluedot/11147345 to your computer and use it in GitHub Desktop.

A list of snippets:

  • snippet1

    [[snippet:files/snippet1.rb]]

    This is a paragraph under list item 'snippet1'

  • snippet2

    [[snippet:files/snippet2.txt]]

    This is a paragraph under list item 'snippet2'

module ECE2524Site
class CodeSnippetFilter < Nanoc::Filter
identifier :snippet
def run(content, params={})
content.gsub(/\[\[snippet:([^\]]*)\]\]/) do
code_snippet($1)
end
end
end
end
# encoding: utf-8
module SnippetHelper
def lexer_for_mime(mime_type)
case
when mime_type.match(/python/)
'python'
when mime_type.match(/ruby/)
'ruby'
else
'text'
end
end
def lexer_by_ext(fname)
case File.extname(fname)
when '.yaml'
'yaml'
when '.json'
'json'
when '.rb'
'ruby'
else
'text'
end
end
def code_snippet(fname)
prefix = config[:prefix] || 'snippets'
item_id = '/' + File.join(prefix,fname) + '/'
snippet = @site.items.find{ |item| item.identifier == item_id }
if snippet
render 'snippet', :code=>snippet.compiled_content, :mime_type => snippet[:mime_type], :file_name => fname, :snippet => snippet
else
$stderr.puts fname + ": could not find snippet"
end
end
end
include SnippetHelper
section class="snippet"
header
== link_to(@file_name, @snippet)
pre
code[class="language-#{@lexer || lexer_for_mime(@mime_type)}"]
== @code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment