Template:Code: Difference between revisions

From LabRPS Documentation
Jump to navigation Jump to search
No edit summary
No edit summary
Line 7: Line 7:


Use this template when you want to display a block of code.
Use this template when you want to display a block of code.
This template should not be used to contain verbatim macro code in the [[Macros_recipes|Macros recipes]] pages. Macro code must be plain, free of any syntax highlight. For this, use [[:Template:MacroCode]].


This template essentially creates a block of text surrounded by wiki <code>&lt;sintaxhighlight&gt; ... &lt;/sintaxhighlight&gt;</code> tags, which are converted into appropriate <code>&lt;span&gt; ... &lt;/span&gt;</code> tags with the right color. See the full documentation in [https://www.mediawiki.org/wiki/Extension:SyntaxHighlight Extension:SyntaxHighlight].
This template essentially creates a block of text surrounded by wiki <code>&lt;sintaxhighlight&gt; ... &lt;/sintaxhighlight&gt;</code> tags, which are converted into appropriate <code>&lt;span&gt; ... &lt;/span&gt;</code> tags with the right color. See the full documentation in [https://www.mediawiki.org/wiki/Extension:SyntaxHighlight Extension:SyntaxHighlight].
Line 95: Line 93:


  <nowiki><pre>
  <nowiki><pre>
a = {{"A"}, "B"}
a = "A";
if a:
if (b)
     Q = Qt.Ok | Qt.Cancel
     {Q = Qt.Ok | Qt.Cancel;}
</pre></nowiki>
</pre></nowiki>


Line 103: Line 101:


<pre>
<pre>
a = {{"A"}, "B"}
a = "A";
if a:
if (b)
     Q = Qt.Ok | Qt.Cancel
     {Q = Qt.Ok | Qt.Cancel;}
</pre>
</pre>
== Older information ==
''(2020) Note:'' After the migration to the DigitalOcean servers, the wiki software once again handles syntax highlighting, so this template once more uses it.
''(2018) Note:'' This template was modified in the past because the wiki software couldn't handle syntax highlighting correctly. The following information refers to that older template. If the wiki software supports it once more, the template may be redefined to support syntax highlighting again.
----
Simplest template:
<nowiki>
{{#tag:pre|{{{code|{{{1}}}|style="color:black"}}}}}</nowiki>
----
Original template:
<nowiki>
{{#tag:syntaxhighlight|{{{code|{{{1}}}}}}|lang="{{{lang|{{{2|python}}}}}}"|enclose="div"}}</nowiki>
''26/09/2016 Mario52:'' The template was modified because the colour gives one inappropriate tab
<nowiki>
{{#tag:syntaxhighlight|{{{code|{{{1}}}}}}|enclose="div"}}</nowiki>


[[Category:Template:Text Format]]
[[Category:Template:Text Format]]

Revision as of 08:43, 12 August 2022

{{{1}}}

Use this template when you want to display a block of code.

This template essentially creates a block of text surrounded by wiki <sintaxhighlight> ... </sintaxhighlight> tags, which are converted into appropriate <span> ... </span> tags with the right color. See the full documentation in Extension:SyntaxHighlight.


Since content that includes an equals sign (=) is interpreted as a variable, you must specify the code= parameter explicitly to avoid issues.

The parameter lang= controls the coloration, depending on the programming language. The default option is lang=python.

  • lang=cpp
  • lang=xml
  • lang=java
  • lang=bash

Many more options are be supported.


Example:

{{Code|lang=cpp|code=
DEF_STD_CMD_A(WindSimulationMethod);

WindSimulationMethod::WindSimulationMethod()
  : methodName("Deodatis et al")
{
    computationTime = 0;
}
}}

Result:

DEF_STD_CMD_A(WindSimulationMethod);

WindSimulationMethod::WindSimulationMethod()
  : methodName("Deodatis et al")
{
    computationTime = 0;
}

Inside the code, neither the double nor the triple braces {{ }}, nor the vertical bar | are allowed because these are interpreted in special ways by the wiki software.

{{Code|code=
a = {{"A"}, "B"}
if a:
    Q = Qt.Ok | Qt.Cancel
}}

Using the Equals sign '=' within this template will result in a broken output. Use the https://wiki.freecadweb.org/Template:= to circumvent this



The <nowiki> ... </nowiki> tags can be added explicitly to allow displaying these special symbols.

{{Code|code=
<nowiki>
a = {{"A"}, "B"}
if a:
    Q = Qt.Ok | Qt.Cancel
</nowiki>
}}

Result:

'"`UNIQ--nowiki-00000005-QINU`"'

We can also display all symbols by using explicit <pre> ... </pre> tags. In this case, no syntax highlighting will exist, but the code will be displayed with all symbols intact.

<pre>
a = "A";
if (b)
    {Q = Qt.Ok | Qt.Cancel;}
</pre>

Result:

a = "A";
if (b)
    {Q = Qt.Ok | Qt.Cancel;}