Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@asaaki
Created June 4, 2011 00:57
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save asaaki/1007420 to your computer and use it in GitHub Desktop.
Save asaaki/1007420 to your computer and use it in GitHub Desktop.
Jekyll module for github like source code highlighting
.highlight { background-color: #49483e }
.c { color: #75715e } /* Comment */
.err { color: #960050; background-color: #1e0010 } /* Error */
.k { color: #66d9ef } /* Keyword */
.l { color: #ae81ff } /* Literal */
.n { color: #f8f8f2 } /* Name */
.o { color: #f92672 } /* Operator */
.p { color: #f8f8f2 } /* Punctuation */
.cm { color: #75715e } /* Comment.Multiline */
.cp { color: #75715e } /* Comment.Preproc */
.c1 { color: #75715e } /* Comment.Single */
.cs { color: #75715e } /* Comment.Special */
.ge { font-style: italic } /* Generic.Emph */
.gs { font-weight: bold } /* Generic.Strong */
.kc { color: #66d9ef } /* Keyword.Constant */
.kd { color: #66d9ef } /* Keyword.Declaration */
.kn { color: #f92672 } /* Keyword.Namespace */
.kp { color: #66d9ef } /* Keyword.Pseudo */
.kr { color: #66d9ef } /* Keyword.Reserved */
.kt { color: #66d9ef } /* Keyword.Type */
.ld { color: #e6db74 } /* Literal.Date */
.m { color: #ae81ff } /* Literal.Number */
.s { color: #e6db74 } /* Literal.String */
.na { color: #a6e22e } /* Name.Attribute */
.nb { color: #f8f8f2 } /* Name.Builtin */
.nc { color: #a6e22e } /* Name.Class */
.no { color: #66d9ef } /* Name.Constant */
.nd { color: #a6e22e } /* Name.Decorator */
.ni { color: #f8f8f2 } /* Name.Entity */
.ne { color: #a6e22e } /* Name.Exception */
.nf { color: #a6e22e } /* Name.Function */
.nl { color: #f8f8f2 } /* Name.Label */
.nn { color: #f8f8f2 } /* Name.Namespace */
.nx { color: #a6e22e } /* Name.Other */
.py { color: #f8f8f2 } /* Name.Property */
.nt { color: #f92672 } /* Name.Tag */
.nv { color: #f8f8f2 } /* Name.Variable */
.ow { color: #f92672 } /* Operator.Word */
.w { color: #f8f8f2 } /* Text.Whitespace */
.mf { color: #ae81ff } /* Literal.Number.Float */
.mh { color: #ae81ff } /* Literal.Number.Hex */
.mi { color: #ae81ff } /* Literal.Number.Integer */
.mo { color: #ae81ff } /* Literal.Number.Oct */
.sb { color: #e6db74 } /* Literal.String.Backtick */
.sc { color: #e6db74 } /* Literal.String.Char */
.sd { color: #e6db74 } /* Literal.String.Doc */
.s2 { color: #e6db74 } /* Literal.String.Double */
.se { color: #ae81ff } /* Literal.String.Escape */
.sh { color: #e6db74 } /* Literal.String.Heredoc */
.si { color: #e6db74 } /* Literal.String.Interpol */
.sx { color: #e6db74 } /* Literal.String.Other */
.sr { color: #e6db74 } /* Literal.String.Regex */
.s1 { color: #e6db74 } /* Literal.String.Single */
.ss { color: #e6db74 } /* Literal.String.Symbol */
.bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.vc { color: #f8f8f2 } /* Name.Variable.Class */
.vg { color: #f8f8f2 } /* Name.Variable.Global */
.vi { color: #f8f8f2 } /* Name.Variable.Instance */
.il { color: #ae81ff } /* Literal.Number.Integer.Long */
// SCSS file!
// Keep in mind, that you have to modify sass code!
.data {
@include zeros;
border: 1px solid #100804;
@include border-rad(10);
margin-bottom: 1em;
width: 99%;
background: darken(#261e16, 5%);
font-size: 80%;
color: #f70;
font-family: 'Bitstream Vera Sans Mono','Courier',monospace;
font-size: 14px;
overflow: auto;
}
.data pre {
@include zeros;
}
.data .highlight { padding: .5em 0; }
.data .highlight div { padding-left: 1em; white-space: nowrap; }
.data .highlight div:hover { background: #16261e; }
.data .line_numbers {
background: darken(#261e16, 7.5%);
padding: .5em .5em .5em 1em;
border-right: 1px solid #132;
text-align: right;
}
.data .line_numbers span {
color: #cba;
cursor: default;
}
module Jekyll
class SourceCoder < Liquid::Block
include Liquid::StandardFilters
# We need a language, but the linenos argument is optional.
SYNTAX = /(\w+)\s?([\w\s=]+)*/
def initialize(tag_name, markup, tokens)
super
if markup =~ SYNTAX
@lang = $1
else
raise SyntaxError.new("Syntax Error in 'sourcecode' - Valid syntax: sourcecode <lang>")
end
end
def render(context)
if context.registers[:site].pygments
render_pygments(context, super.join)
else
render_codehighlighter(context, super.join)
end
end
def render_pygments(context, code_string)
liners,source = "",""
code_array = code_string.strip.split("\n")
total_lines = code_array.size
total_lines.times do |ln|
liners += "<span id=\"L#{ln+1}\" rel=\"#L#{ln+1}\">#{ln+1}</span>\n"
end
code_array.each_with_index do |line,i|
code_line = Albino.new(line, @lang).to_s
code_line.gsub!(/<div class="highlight"><pre>|\n<\/pre>|\n<\/div>/,'')
code_line.gsub!(/^([ ])+/){|m| "&nbsp;"*m.size}
code_line = "<br/>" if code_line.strip==''
source += "<div class='line' id='LC#{i+1}'>#{code_line}</div>"
end
liners_block = <<-LINERS
<td><pre class='line_numbers'>#{liners}</pre></td>
LINERS
source_block = <<-SOURCE
<td width="100%">
<div class='highlight'><pre>#{source}</pre></div>
</td>
SOURCE
output = <<-OUTPUT
<div class='data type-#{@lang}'>
<table cellpadding='0' cellspacing='0'>
<tr>
#{liners_block}
#{source_block}
</tr>
</table>
</div>
OUTPUT
end
def render_codehighlighter(context, code)
#The div is required because RDiscount blows ass
<<-HTML
<!-- basic code highlighter / no pygments -->
<div class="code">
<pre>
<code class='#{@lang}'>#{h(code).strip}</code>
</pre>
</div>
HTML
end
end
end
Liquid::Template.register_tag('sourcecode', Jekyll::SourceCoder)
@asaaki
Copy link
Author

asaaki commented Jun 4, 2011

The Liquid tag is:

{% sourcecode :lang %}
... your code ...
{% endsourcecode %}

@nmenag
Copy link

nmenag commented May 10, 2015

Hi!, i have an error "Liquid Exception: undefined method `pygments' for #Jekyll::Site:0x00000001966eb8", Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment