Sharing code is important, and blogging about it should be easy and beautiful.
That's why Octopress is packed with features to make blogging your code a breeze.
Though Jekyll comes with support for Pygments syntax highlighting,
Octopress makes it way better. Here's how.
With the backtick_codeblock filter you can use Github's lovely back tick syntax highlighting blocks.
Simply start a line with three back ticks followed by a space and the language you're using.
[language] - Used by the syntax highlighter. Passing 'plain' disables highlighting. (Supported languages.)
[title] - Add a figcaption to your code block.
[url] - Download or reference link for your code.
[Link text] - Text for the link, defaults to 'link'.
Additional options:
These options don't depend on any previous option and order does not matter.
start:# - Line numbers begin at # (useful for using snippets to reference longer code).
mark:#,#-# - Mark one or more lines of code with the class name "marked". Accepts one number, numbers separated by commas, and number ranges. Example mark:1,5-8 will mark lines 1,5,6,7,8. Note: If you've changed the beginning line number be sure these match rendered line numbers
linenos:false - Do not add line numbers to highlighted code.
Examples
1. Here's an example without setting the language.
``` ruby Discover if a number is prime http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/ Source Article
class Fixnum
def prime?
('1' * self) !~ /^1?$|^(11+?)1円+$/
end
end
```
All you need is the gist's id and you can easily embed it in your page. This actually downloads a cache of the gist and embeds it in a <noscript> tag for RSS
readers and search engines, while still using Github's javascript gist embed code for browsers.
Syntax
{% gist gist_id [filename] %}
Example
{% gist 4321346 %}
@@ -590,7 +590,7 @@ class SpritesTest < Test::Unit::TestCase
it "should generate a sprite from nested folders" do
css = render <<-SCSS
- @import "nested/*.png";
+ @import "nested/**/*.png";
@include all-nested-sprites;
SCSS
assert_correct css, <<-CSS
If you want syntax highlighting (for a supported language), specify the filename (with extension):
{% gist 4321346 gistfile1.diff %}
@@ -590,7 +590,7 @@ class SpritesTest < Test::Unit::TestCase
it "should generate a sprite from nested folders" do
css = render <<-SCSS
- @import "nested/*.png";
+ @import "nested/**/*.png";
@include all-nested-sprites;
SCSS
assert_correct css, <<-CSS
If you have a gist with multiple files, you can include files one at a time by adding the name after the gist id.
This plugin was initially developed by Brandon Tilly for Jekyll blogs.
Include Code Snippets
Import files on your filesystem into any blog post as embedded code snippets with syntax highlighting and a download link.
In the _config.yml you can set your code_dir but the default is source/downloads/code. Simply put a file anywhere under that directory and
use the following tag to embed it in a post.
[title] - Add a custom figcaption to your code block (defaults to filename).
lang:language - Force the syntax highlighter to use this language. By default the file extension is used for highlighing, but not all extensions are known by Pygments.
Additional options:
These options don't depend on any previous option and order does not matter.
start:# - Render a snippet from the file beginning at the specified line.
end:# - Render a snippet from the file ending at the specified line.
range:#-# - Render a specified range of lines from a file (a shortcut for start:# end:#).
mark:#,#-# - Mark one or more lines of code with the class name "marked". Accepts one number, numbers separated by commas, and number ranges. Example mark:1,5-8 will mark lines 1,5,6,7,8. Note: If you've changed the beginning line number be sure these match rendered line numbers
linenos:false - Do not add line numbers to highlighted code.
Examples
1. This code snipped was included from source/downloads/code/test.js.
[lang:language] - Choose a language for the syntax highlighter. Passing 'plain' disables highlighting. (Supported languages.)
[title] - Add a figcaption to your code block.
[url] - Download or reference link for your code.
[link text] - Text for the link, defaults to 'link'.
Additional options:
These options don't depend on any previous option and order does not matter.
start:# - Render a snippet from the file beginning at the specified line.
end:# - Render a snippet from the file ending at the specified line.
range:#-# - Render a specified range of lines from a file (a shortcut for start:# end:#).
mark:#,#-# - Mark one or more lines of code with the class name "marked". Accepts one number, numbers separated by commas, and number ranges. Example mark:1,5-8 will mark lines 1,5,6,7,8. Note: If you've changed the beginning line number be sure these match rendered line numbers
linenos:false - Do not add line numbers to highlighted code.
Examples
1. Here's an example without setting the language.
Solarized has a beautiful syntax highlighting color scheme, but reproducing it requires a highly sophisticated highlighting engine.
Pygments (the highlighter Jekyll uses) processes code snippets into styleable HTML, but it isn't nearly as powerful as the highlighting engine in Vim for example.
In order to port Solarized theme to octopress, I processed its test files with Pygments and styled the output with Sass while comparing
them to the Vim rendered versions.
Spaces vs. Tabs
If you use tabs, there is an issue where the first level indentation is shorter than the rest. The cause is currently unknown since lots of text processors are involved. To fix, use spaces. You'll be happier anyway.