Include Code
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.
Syntax
{% include_code [title] [lang:language] path/to/file %}
Basic options
[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. Examplemark: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 numberslinenos:false
- Do not add line numbers to highlighted code.
Examples
1. This code snipped was included from source/downloads/code/test.js
.
|
The source:
{% include_code test.js %}
2. Setting a custom caption.
|
The source:
{% include_code ruby/test.rb Add to_fraction for floats %}
This includes a file from source/downloads/code/ruby/test.rb
.
Other ways to embed code snippets
You might also like to use back tick code blocks or embed GitHub gists.