The <<code>> Macro

You are viewing an old version of this article. View the current version here.

Syntax

<<code [lang=name] [inline={true|false}] [indent=L] [wrap={true|false}]>>
Code goes here
<</code>>
  • 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})

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 "include=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
  1. One
  2. Two
    int main(void) {
        return;
    }
    
  3. 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

LanguageAllowed Short Name(s)
JavaScript+Makojs+mako javascript+mako
Brainfuckbrainfuck bf
HTML+Cheetahhtml+cheetah html+spitfire
Bashbash sh ksh
CSS+Myghtycss+myghty
Coldufsion HTMLcfm
Smartysmarty
ApacheConfapacheconf aconf apache
Javajava
JavaScript+Genshi Textjs+genshitext js+genshi javascript+genshitext javascript+genshi
Erlangerlang
CoffeeScriptcoffee-script coffeescript
ANTLR With ActionScript Targetantlr-as antlr-actionscript
d-objdumpd-objdump
CSS+Rubycss+erb css+ruby
MySQLmysql
C++cpp c++
XML+Smartyxml+smarty
Felixfelix flx
CMakecmake
Makomako
Pythonpython py
JavaScript+Myghtyjs+myghty javascript+myghty
XML+Makoxml+mako
Booboo
ActionScriptas actionscript
VB.NETvb.net vbnet
SquidConfsquidconf squid.conf squid
Dd
Logtalklogtalk
BBCodebbcode
Hamlhaml HAML
MuPADmupad
XML+Rubyxml+erb xml+ruby
Debian Control filecontrol
RHTMLrhtml html+erb html+ruby
JavaScript+Cheetahjs+cheetah javascript+cheetah js+spitfire javascript+spitfire
Python Tracebackpytb
cfstatementcfs
Adaada ada95ada2005
objdumpobjdump
CSS+Makocss+mako
GherkinCucumber cucumber Gherkin gherkin
Ragel in Java Hostragel-java
Ioio
Valavala vapi
Haskellhaskell hs
Lualua
Sasssass SASS
ANTLR With ObjectiveC Targetantlr-objc
XMLxml
Groffgroff nroff man
GLSLglsl
Genshi Textgenshitext
Objective-Jobjective-j objectivej obj-j objj
GASgas
Python console sessionpycon
MXMLmxml
XML+Cheetahxml+cheetah xml+spitfire
Gogo
Ragel in C Hostragel-c
aspx-csaspx-cs
Ragel in Ruby Hostragel-ruby ragel-rb
HTML+Genshihtml+genshi html+kid
Perlperl pl
INIini cfg
c-objdumpc-objdump
Tcshtcsh csh
RConsolerconsole rout
CSS+Smartycss+smarty
ANTLR With C# Targetantlr-csharp antlr-c#
Darcs Patchdpatch
LLVMllvm
Nginx configuration filenginx
AppleScriptapplescript
Text onlytext
Literate Haskelllhs literate-haskell
PHPphp php3 php4 php5
MiniDminid
Oocooc
SQLsql
NASMnasm
Debian Sourcelistsourceslist sources.list
Delphidelphi pas pascal objectpascal
aspx-vbaspx-vb
HTML+Evoquehtml+evoque
NumPynumpy
Modula-2modula2 m2
HTML+Django/Jinjahtml+django html+jinja
CSS+PHPcss+php
VimLvim
CSS+Genshi Textcss+genshitext css+genshi
Ragelragel
XML+Evoquexml+evoque
haXehx haXe
Redcoderedcode
Django/Jinjadjango jinja
Scalascala
Lighttpd configuration filelighty lighttpd
Ragel in D Hostragel-d
Ruby irb sessionrbcon irb
CSScss
JavaScript+Smartyjs+smarty javascript+smarty
Asymptoteasy asymptote
XML+PHPxml+php
Fortranfortran
Gnuplotgnuplot
REBOLrebol
ERBerb
Befungebefunge
Dylandylan
MoinMoin/Trac Wiki markuptrac-wiki moin
Matlabmatlab octave
Cc
HTMLhtml
Genshigenshi kid xml+genshi xml+kid
reStructuredTextrst rest restructuredtext
IRC logsirc
Prologprolog
CSS+Django/Jinjacss+django css+jinja
Smalltalksmalltalk squeak
YAMLyaml
HTML+Myghtyhtml+myghty
Makefilemake makefile mf bsdmake
XSLTxslt
Ssplus s r
sqlite3consqlite3
OCamlocaml
Gettext Catalogpot po
Rubyrb ruby
HTML+Smartyhtml+smarty
POVRaypov
Ragel in CPP Hostragel-cpp
Evoqueevoque
Java Server Pagejsp
ABAPabap
HTML+Makohtml+mako
Diffdiff udiff
Matlab sessionmatlabsession
HTML+PHPhtml+php
Python 3.0 Tracebackpy3tb
ANTLR With Java Targetantlr-java
JavaScript+Rubyjs+erb javascript+erb js+ruby javascript+ruby
Makefilebasemake
ANTLR With Python Targetantlr-python
cpp-objdumpcpp-objdump c++-objdumb cxx-objdump
Python 3python3 py3
Batchfilebat
ANTLRantlr
JavaScript+Django/Jinjajs+django javascript+django js+jinja javascript+jinja
Cythoncython pyx
Schemescheme scm
Erlang erl sessionerl
Clojureclojure clj
Modelicamodelica
ANTLR With Perl Targetantlr-perl
Myghtymyghty
Tcltcl
MOOCodemoocode
Newspeaknewspeak
Bash Sessionconsole
Raw token dataraw
C#csharp c#
TeXtex latex
Cheetahcheetah spitfire
Objective-Cobjective-c objectivec obj-c objc
JavaScriptjs javascript
Common Lispcommon-lisp cl
Embedded Ragelragel-em
ActionScript 3as3 actionscript3
ANTLR With CPP Targetantlr-cpp
JavaScript+PHPjs+php javascript+php
Ragel in Objective C Hostragel-objc
XML+Django/Jinjaxml+django xml+jinja
ANTLR With Ruby Targetantlr-ruby antlr-rb
XML+Myghtyxml+myghty

Comments

Comments loading...
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.