Code Blocks - <<code>>
Syntax
<<code [lang=name] [inline={true|false}] [indent=L] [wrap={true|false}]>> Code goes here <</code>>
Contents
- replace
name
with the language your example uses, supported languages are listed below - For
indent
, replace L with the indent level you want (corresponds to list indent levels) - For
wrap
, you can force the lines to wrap or not. The default is for code blocks to scroll.
About
The <<code>>
macro allows you to put source code into articles.
A simple example:
<<code>> from reversion.models import Revision from .helpers import ProcessedContent class Node(models.Model): language = models.CharField(max_length=10, default='en-us') <</code>>
...will display as:
from reversion.models import Revision from .helpers import ProcessedContent class Node(models.Model): language = models.CharField(max_length=10, default='en-us')
Wrapping vs. scrolling
<<code wrap=false>> if self.is_image: return reverse('nodes:image', kwargs={'slug': self.node.slug, 'image_slug': self.slug, 'language': self.node.language}) else: return reverse('nodes:attachment', kwargs={'slug': self.node.slug, 'attachment_slug': self.slug, 'language': self.node.language}) <</code>>
...will display as:
if self.is_image: return reverse('nodes:image', kwargs={'slug': self.node.slug, 'image_slug': self.slug, 'language': self.node.language}) else: return reverse('nodes:attachment', kwargs={'slug': self.node.slug, 'attachment_slug': self.slug, 'language': self.node.language})
<<code wrap=true>> if self.is_image: return reverse('nodes:image', kwargs={'slug': self.node.slug, 'image_slug': self.slug, 'language': self.node.language}) else: return reverse('nodes:attachment', kwargs={'slug': self.node.slug, 'attachment_slug': self.slug, 'language': self.node.language}) <</code>>
...will display as:
if self.is_image: return reverse('nodes:image', kwargs={'slug': self.node.slug, 'image_slug': self.slug, 'language': self.node.language}) else: return reverse('nodes:attachment', kwargs={'slug': self.node.slug, 'attachment_slug': self.slug, 'language': self.node.language})
<<code>> and links
The <<code>>
macro does not play nice with links. If you want to have inline code link to something else, use ##...##
.
For example, use:
##[[insert|INSERT]]##
instead of
[[insert|<<code>>INSERT<</code>>]]
or
<<code>>[[insert|INSERT]]<</code>>
<<code>> and lists
If you want to build a list and put a block of code inside your list element, especially if you want to resume numbering after your code, you have to put the first line of code on the same line as your list text. (You may need to include "inline=false", but some new/future version of the parser/renderer may be able to deal with this for you. Try it both ways if you're not sure.) For example:
# One # Two<<code inline=false>>int main(void) { return; } <</code>> # Three
- One
- Two
int main(void) { return; }
- Three
Syntax highlighting
The <<code>>
macro also allows you to syntax highlight code. Add syntax highlighting to the above Python example like so:
<<code lang=python>> from reversion.models import Revision from .helpers import ProcessedContent class Node(models.Model): language = models.CharField(max_length=10, default='en-us') <</code>>
from reversion.models import Revision from .helpers import ProcessedContent class Node(models.Model): language = models.CharField(max_length=10, default='en-us')
Inline or block text
When using the <<code>>
macro with the lang=
attribute, you can also specify whether you want the code inline with the text that surrounds it, or to have it display in a block. (The Creole parser will attempt to guess what you want if you don't specify this attribute.)
Valid values are true
and false
.
Here is some <<code lang=c inline=true>>true inline code<</code>>.
Here is some true inline code
.
Here is some <<code lang=c inline=false>>false inline code<</code>>.
Here is some
false inline code
.
Here is some <<code lang=c inline=none>>none inline code<</code>>.
Here is some
none inline code
.
Supported languages for syntax highlighting
Language | Allowed Short Name(s) |
---|---|
ABAP | abap |
ActionScript 3 | as3 actionscript3 |
ActionScript | as actionscript |
Ada | ada ada95ada2005 |
ANTLR With ActionScript Target | antlr-as antlr-actionscript |
ANTLR With C# Target | antlr-csharp antlr-c# |
ANTLR With CPP Target | antlr-cpp |
ANTLR With Java Target | antlr-java |
ANTLR With ObjectiveC Target | antlr-objc |
ANTLR With Perl Target | antlr-perl |
ANTLR With Python Target | antlr-python |
ANTLR With Ruby Target | antlr-ruby antlr-rb |
ANTLR | antlr |
ApacheConf | apacheconf aconf apache |
AppleScript | applescript |
aspx-cs | aspx-cs |
aspx-vb | aspx-vb |
Asymptote | asy asymptote |
Bash Session | console |
Bash | bash sh ksh |
Batchfile | bat |
BBCode | bbcode |
Befunge | befunge |
Boo | boo |
Brainfuck | brainfuck bf |
C# | csharp c# |
C++ | cpp c++ |
c-objdump | c-objdump |
cfstatement | cfs |
Cheetah | cheetah spitfire |
Clojure | clojure clj |
CMake | cmake |
CoffeeScript | coffee-script coffeescript |
Coldufsion HTML | cfm |
Common Lisp | common-lisp cl |
cpp-objdump | cpp-objdump c++-objdumb cxx-objdump |
CSS+Django/Jinja | css+django css+jinja |
CSS+Genshi Text | css+genshitext css+genshi |
CSS+Mako | css+mako |
CSS+Myghty | css+myghty |
CSS+PHP | css+php |
CSS+Ruby | css+erb css+ruby |
CSS+Smarty | css+smarty |
CSS | css |
Cython | cython pyx |
C | c |
d-objdump | d-objdump |
Darcs Patch | dpatch |
Debian Control file | control |
Debian Sourcelist | sourceslist sources.list |
Delphi | delphi pas pascal objectpascal |
Diff | diff udiff |
Django/Jinja | django jinja |
Dylan | dylan |
D | d |
Embedded Ragel | ragel-em |
ERB | erb |
Erlang erl session | erl |
Erlang | erlang |
Evoque | evoque |
Felix | felix flx |
Fortran | fortran |
GAS | gas |
Genshi Text | genshitext |
Genshi | genshi kid xml+genshi xml+kid |
Gettext Catalog | pot po |
Gherkin | Cucumber cucumber Gherkin gherkin |
GLSL | glsl |
Gnuplot | gnuplot |
Go | go |
Groff | groff nroff man |
Haml | haml HAML |
Haskell | haskell hs |
haXe | hx haXe |
HTML+Cheetah | html+cheetah html+spitfire |
HTML+Django/Jinja | html+django html+jinja |
HTML+Evoque | html+evoque |
HTML+Genshi | html+genshi html+kid |
HTML+Mako | html+mako |
HTML+Myghty | html+myghty |
HTML+PHP | html+php |
HTML+Smarty | html+smarty |
HTML | html |
INI | ini cfg |
Io | io |
IRC logs | irc |
Java Server Page | jsp |
JavaScript+Cheetah | js+cheetah javascript+cheetah js+spitfire javascript+spitfire |
JavaScript+Django/Jinja | js+django javascript+django js+jinja javascript+jinja |
JavaScript+Genshi Text | js+genshitext js+genshi javascript+genshitext javascript+genshi |
JavaScript+Mako | js+mako javascript+mako |
JavaScript+Myghty | js+myghty javascript+myghty |
JavaScript+PHP | js+php javascript+php |
JavaScript+Ruby | js+erb javascript+erb js+ruby javascript+ruby |
JavaScript+Smarty | js+smarty javascript+smarty |
JavaScript | js javascript |
Java | java |
Lighttpd configuration file | lighty lighttpd |
Literate Haskell | lhs literate-haskell |
LLVM | llvm |
Logtalk | logtalk |
Lua | lua |
Makefile | basemake |
Makefile | make makefile mf bsdmake |
Mako | mako |
Matlab session | matlabsession |
Matlab | matlab octave |
MiniD | minid |
Modelica | modelica |
Modula-2 | modula2 m2 |
MoinMoin/Trac Wiki markup | trac-wiki moin |
MOOCode | moocode |
MuPAD | mupad |
MXML | mxml |
Myghty | myghty |
MySQL | mysql |
NASM | nasm |
Newspeak | newspeak |
Nginx configuration file | nginx |
NumPy | numpy |
objdump | objdump |
Objective-C | objective-c objectivec obj-c objc |
Objective-J | objective-j objectivej obj-j objj |
OCaml | ocaml |
Ooc | ooc |
Perl | perl pl |
PHP | php php3 php4 php5 |
POVRay | pov |
Prolog | prolog |
Python 3.0 Traceback | py3tb |
Python 3 | python3 py3 |
Python console session | pycon |
Python Traceback | pytb |
Python | python py |
Ragel in C Host | ragel-c |
Ragel in CPP Host | ragel-cpp |
Ragel in D Host | ragel-d |
Ragel in Java Host | ragel-java |
Ragel in Objective C Host | ragel-objc |
Ragel in Ruby Host | ragel-ruby ragel-rb |
Ragel | ragel |
Raw token data | raw |
RConsole | rconsole rout |
REBOL | rebol |
Redcode | redcode |
reStructuredText | rst rest restructuredtext |
RHTML | rhtml html+erb html+ruby |
Ruby irb session | rbcon irb |
Ruby | rb ruby |
Sass | sass SASS |
Scala | scala |
Scheme | scheme scm |
Smalltalk | smalltalk squeak |
Smarty | smarty |
sqlite3con | sqlite3 |
SQL | sql |
SquidConf | squidconf squid.conf squid |
S | splus s r |
Tcl | tcl |
Tcsh | tcsh csh |
Text only | text |
TeX | tex latex |
Vala | vala vapi |
VB.NET | vb.net vbnet |
VimL | vim |
XML+Cheetah | xml+cheetah xml+spitfire |
XML+Django/Jinja | xml+django xml+jinja |
XML+Evoque | xml+evoque |
XML+Mako | xml+mako |
XML+Myghty | xml+myghty |
XML+PHP | xml+php |
XML+Ruby | xml+erb xml+ruby |
XML+Smarty | xml+smarty |
XML | xml |
XSLT | xslt |
YAML | yaml |
Formatting
In order to permit creole formatting within a code block, use the <<creole>><</creole>>
tags, for example:
<<code>> some code <<creole>>//italicise me//<</creole>> more code <<creole>>**bold me**<</creole>> yet more code <</code>>
displays as:
some code italicise me more code bold me yet more code
The tags need to appear on the same line.