Code Blocks - <<code>>

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

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.

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.