Problem
You want to include code snippets in this Cookbook (or anywhere else on this wiki).
Solution
There are several approaches to including code, depending on how much code you want to include and what language it is written in.
Keywords and Inline Expressions
Keywords and other inline expressions can be highlighted and rendered in fixed font by surrounding them with backtick characters (`), or by surrounding them with tripled curly braces ({{{ and }}}). For example, the method name toString() was just embedded in this paragraph using the following syntax:
`toString()`
Small Code Snippets Embedded in a Page
A multi-line segment of code can be included by starting a special code block that contains a language parser's name. For example, this markup could be entered in the plain textbox editor to display a code block using the Java parser:
{{{#!java
class Foo
{
public int callMe()
{
return 42;
}
}
}}}
Result:
1 class Foo
2 {
3 public int callMe()
4 {
5 return 42;
6 }
7 }
Use cplusplus instead of java as the parser name for C++ code. See HelpOnParsers for other supported parsers, or to find out how to turn off line numbers, start line numbers with a value other than 1, and other options.
Including Separate Code Files
You can attach code files to wiki pages if needed. You can use the Attach File action available via the "More Actions" dropdown list (in the default theme for this wiki, perhaps located elsewhere if you have chosen a different theme in your UserPreferences).
The [[AttachList]] macro lists files attached to a page, like this:
To refer to attachments on a page, use attachment:filename.ext. For a file called main.cpp attached to this page, the markup attachment:main.cpp produces a link like this: main.cpp. A link of this kind follows all the rules for normal links, i.e. pictures are automatically inlined. To refer to files attached to other pages, use attachment:WikiName/filename.ext.
Note that it is often easiest to add file attachments just by editing the current topic and inserting the attachement:filename.ext link directly to the page text, then save the page. When the attachment doesn't exist, instead of a link to the attached file, you'll see a link that allows you to upload the attachment itself. This can be a convenient shortcut.
Attaching files is the best approach if you want to provide code that readers can download. If, instead, you want to provide a larger piece of code that you wish to have rendered in-line and color-highlighted directly on a page, simply place the code on a separate wiki page. There is no need for the triple-brace code block notation. Instead, just place a format directive as the first line of the new page:
#format cplusplus
This instructs the wiki to use the specified parser to format the entire contents of the whole page. The MainCppExample page is written this way. You can also view its raw text markup. Once you have done this, you can even include this separate page in-line in your recipe using the include macro, which has the following syntax:
[[Include(MainCppExample)]]
This would produce the following result:
1 int main( int argc, char **argv )
2 {
3 return 0;
4 }
5
Read HelpOnMacros/Include for more information on the include macro.
Discussion
Contributors
-- StephenEdwards 2006-03-24 04:50:25
Comments