Help:Preformatted text: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
(Copying http://wiki.flightgear.org/index.php?title=Help:Formatting&oldid=89657#Pre-formatted_text here)
 
(+ Wiki help navbar template)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Wiki help navbar}}
Monospaced '''preformatted text''' can be added in many different ways on the FlightGear wiki.
Monospaced '''preformatted text''' can be added in many different ways on the FlightGear wiki.


Line 49: Line 51:
|}
|}


=== Wrapping in pre-formatted blocks ===
None of the above mentioned methods for pre-formatted text block wraps.  A line that is too long to fit the screen width would continue past the screen edge instead of wrapping around to continue on the following line.  A work around to this is to use the <nowiki><pre></nowiki> tag together with a style attribute to wrap the text.


{| class="wikitable" width="100%"
=== Syntax highlighting ===
! Description
''Syntax highlighting'' will make source code more readable.  This wiki has support for syntax highlighting of many programming languages, XML and C++ (see link at the bottom for a more complete list)The source code snippet to be highlighted is put between <nowiki><syntaxhighlight></nowiki> tagsThe tag can be given some attributes to control how the source code appears.
! width="30%" | What you type
! width="30%" | What you get
|-
| Using <nowiki>&lt;pre style="white-space: pre-wrap;"&gt;</nowiki> to wrap the text.
|
<nowiki><pre style="white-space: pre-wrap;">This is a very long...</pre></nowiki>
|<pre style="white-space: pre-wrap;">This is is a very long line of text that could continue past the screen edge.  I could have filled it with pure nonsense but I think I will instead just be lazy and copy what I got so far.  Sorry.  This is is a very long line of text that could continue past the screen edgeI could have filled it with pure nonsense but I think I will instead just be lazy and copy what I got so far.  Sorry.</pre>
|}
 
{| class="wikitable"
! width="40%" | Description
! width="30%" | What you type
! What you get
|-
| Using only <nowiki>&lt;pre&gt;</nowiki>.
|
<nowiki><pre>This is a very long...</pre></nowiki>
|<pre>This is is a very long line of text that could continue past the screen edgeI could have filled it with pure nonsense but I think I will instead just be lazy and copy what I got so far.  Sorry.  This is is a very long line of text that could continue past the screen edge.  I could have filled it with pure nonsense but I think I will instead just be lazy and copy what I got so far.  Sorry.</pre>
|}


=== Syntax highlighting ===
Unfortunately the lexer for [[Nasal]] was obsoleted in February 2016 as the newer version of the MediaWiki software the wiki is running had dropped the GeSHi highlighter in favor for the Pygmets highlighter.
''Syntax highlighting'' will make source code more readable.  This wiki has support for syntax highlighting of many programming languages, including [[nasal]], XML and C++ (see [[mediawikiwiki:Extension:SyntaxHighlight_GeSHi]] for a more complete list).  The source code snippet to be highlighted is put between <nowiki><syntaxhighlight></nowiki> tags.  The tag can be given some attributes to control how the source code appears.


Note that you can use syntax highlighting for command lines as well.
Note that you can use syntax highlighting for command lines as well.


  &lt;'''syntaxhighlight''' lang="" ''line start=""'' ''highlight=""'' ''enclose=""''&gt;&lt;/syntaxhighlight&gt;
  &lt;'''syntaxhighlight''' lang="" ''line start=""'' ''highlight=""'' ''inline''&gt;&lt;/syntaxhighlight&gt;


All attributes but '''lang''' is optional.  '''start''' have to be used together with '''line'''.  Values must be put inside quotation marks ('''"''').
All attributes but '''lang''' are optional.  '''start''' have to be used together with '''line'''.  Values must be put inside quotation marks ('''"''').


; lang:  The language to be highlighted, for example '''bash''', '''cpp''', '''dos''', '''nasal''' or '''xml'''.
; lang:  The language to be highlighted, for example '''bash''', '''cpp''' or '''xml'''.


; line:  Use line numbering.
; line:  Use line numbering.
Line 89: Line 69:
; start:  Used together with '''line''' if you want the line numbering to start somewhere else than one.  For example will '''42''' start the line numbering from 42 instead of 1.
; start:  Used together with '''line''' if you want the line numbering to start somewhere else than one.  For example will '''42''' start the line numbering from 42 instead of 1.


; highlight:  Used for highlighting a line.  Do note that lines are counted from the first line in a snippet disregarding '''start'''.  For example will '''3''' highlight the third line in a snippet.
; highlight:  Used for highlighting a line.  Do note that lines are counted from the first line in a snippet disregarding '''start'''.  For example will '''3,5-7''' highlight line 3 and 5-7 in a snippet.


; enclose
; inline
: Encloses the source code in a certain html tag.  Takes either '''pre''', '''div''' or '''none''', with '''pre''' being the default one.
: Used when syntax highlighting is needed in running text.
: Using '''div''' will wrap lines that are too long to fit the screen width so they continue on the following line.


{| class="wikitable" width="100%"
{| class="wikitable" width="100%"
Line 99: Line 78:
! width="30%" | What you type
! width="30%" | What you type
! width="30%" | What you get
! width="30%" | What you get
|-
| Nasal highlighting.
|
<pre style="white-space: pre-wrap;"><syntaxhighlight lang="nasal" enclose="div">
fgcommand("profiler-start"); # default file name is fgfs.profile
// or with a custom filename
fgcommand("profiler-start", props.Node.new({"filename": "output.profile"}));
</syntaxhighlight>
</pre>
| <syntaxhighlight lang="nasal" enclose="div">
fgcommand("profiler-start"); # default file name is fgfs.profile
// or with a custom filename
fgcommand("profiler-start", props.Node.new({"filename": "output.profile"}));
</syntaxhighlight>
|-
|-
| XML highlighting, here with line numbering, but without specifying the line number of the first line.
| XML highlighting, here with line numbering, but without specifying the line number of the first line.
|
|
  <pre style="white-space: pre-wrap;"><syntaxhighlight lang="xml" line enclose="div">
  <nowiki><syntaxhighlight lang="xml" line>
  <animation>
  <animation>
   <object-name>Object</object-name>
   <object-name>Object</object-name>
Line 123: Line 87:
  </animation>
  </animation>
</syntaxhighlight>
</syntaxhighlight>
</pre>
</nowiki>
| <syntaxhighlight lang="xml" line enclose="div">
| <syntaxhighlight lang="xml" line enclose="div">
  <animation>
  <animation>
Line 131: Line 95:
</syntaxhighlight>
</syntaxhighlight>
|-
|-
| C++ highlighting, arbitrarily starting from line 46, and highlighting the 4th line in the snippet.
| C++ highlighting, arbitrarily starting from line 46, and highlighting the 1st, 3rd and 4th line in the snippet.
|
|
  <pre style="white-space: pre-wrap;"><syntaxhighlight lang="cpp" line start="46" highlight="4" enclose="div">
  <nowiki><syntaxhighlight lang="cpp" line start="46" highlight="1,3-4">
globals->get_commands()->execute("profiler-start");
globals->get_commands()->execute("profiler-start");
// or with filename
// or with filename
Line 139: Line 103:
args.setStringValue("filename", "output.profile");
args.setStringValue("filename", "output.profile");
globals->get_commands()->execute("profiler-start", &args);
globals->get_commands()->execute("profiler-start", &args);
</syntaxhighlight></pre>
</syntaxhighlight></nowiki>
|<syntaxhighlight lang="cpp" line start="46" highlight="4" enclose="div">
| <syntaxhighlight lang="cpp" line start="46" highlight="1,3-4">
globals->get_commands()->execute("profiler-start");
globals->get_commands()->execute("profiler-start");
// or with filename
// or with filename
Line 146: Line 110:
args.setStringValue("filename", "output.profile");
args.setStringValue("filename", "output.profile");
globals->get_commands()->execute("profiler-start", &args);
globals->get_commands()->execute("profiler-start", &args);
</syntaxhighlight>
|}
==== Differentiating a bad and a good code snippet ====
One can make examples of bad code, for example with common coding errors or bad coding practices, stand out by adding the cascading style sheets (CSS) attribute <code>style="background: #ffdddd;"</code> which will give the background color a pink tint instead of the default gray <code>#dddddd</code>.
When illustrating comparisons between a good and a bad way of doing something the good example can be given a green tint with <code>#ddffdd</code>.  ''Only use the green tint when comparing against a snippet of bad code.''
{| class="wikitable" width="100%"
! Description
! width="30%" | What you type
! width="30%" | What you get
|-
| Highlighting bad coding practices or common errors
|
<pre style="white-space: pre-wrap;">A common pitfall is forgetting a slash in a closing tag:
<?xml version="1.0" encoding="UTF-8"?>
<syntaxhighlight lang="xml" style="background: #ffdddd;">
<PropertyList>
  <!-- Contents -->
<PropertyList>  <!-- Note missing slash -->
</syntaxhighlight>
</pre>
|
A common pitfall is forgetting a slash in a closing tag:
<syntaxhighlight lang="xml" style="background: #ffdddd;">
<?xml version="1.0" encoding="UTF-8"?>
<PropertyList>
  <!-- Contents -->
<PropertyList>  <!-- Note missing slash -->
</syntaxhighlight>
</syntaxhighlight>
|-
|-
| Good and bad examples next to each other
| Syntax highlighting in running text
|
<pre style="white-space: pre-wrap;">Bad [[PropertyList XML File|property-list XML file]] (missing an XML header).
<syntaxhighlight lang="xml" style="background: #ffdddd;">
<PropertyList>
  <!-- Contents -->
</PropertyList>
</syntaxhighlight>
 
Do it like this instead:
<syntaxhighlight lang="xml" style="background: #ddffdd;">
<?xml version="1.0" encoding="UTF-8"?>
<PropertyList>
  <!-- Contents -->
</PropertyList>
</syntaxhighlight>
</pre>
|
|
Bad [[PropertyList XML File|property-list XML file]] (without an XML header)
<nowiki>A typical C++ comment <syntaxhighlight lang="cpp" inline>// looks like this</syntaxhighlight>.</nowiki>
<syntaxhighlight lang="xml" style="background: #ffdddd;">
| A typical C++ comment <syntaxhighlight lang="cpp" inline>// looks like this</syntaxhighlight>.
<PropertyList>
  <!-- Contents -->
</PropertyList>
</syntaxhighlight>
 
Do it like this instead:
<syntaxhighlight lang="xml" style="background: #ddffdd;">
<?xml version="1.0" encoding="UTF-8"?>
<PropertyList>
  <!-- Contents -->
</PropertyList>
</syntaxhighlight>
|}
|}


== External links ==
== External links ==
=== MediaWiki wiki help pages ===
* [[mediawikiwiki:Extension:SyntaxHighlight]]
* [[mediawikiwiki:Extension:SyntaxHighlight]]
* [http://pygments.org/docs/lexers/ Available Pygments lexers]


[[Category:Help]]
[[Category:Help]]

Latest revision as of 11:52, 3 June 2018


Monospaced preformatted text can be added in many different ways on the FlightGear wiki.

Pre-formatted running text

Description What you type What you get
Using <code> HTML tags.
Here is a <code>'''Class()'''</code>.
Here is a Class().
Using <tt> HTML tags.
Here is a <tt>''variable''</tt>.
Here is a variable.

Pre-formatted blocks

Apart from using <tt> and <code> on entire blocks of text one one can use the below methods.

Description What you type What you get
The simplest way is with a leading blank space on each line.
 You '''''can''''' use '''wiki markup'''
 using this method.
You can use wiki markup
using this method.
Using <nowiki> tags with a leading blank space.
 <nowiki>Here is some text.
And some more.</nowiki>
Here is some text.
And some more.
Using <pre> tags.
<pre>Some text.
Some more text.</pre>
Some text.
Some more text.


Syntax highlighting

Syntax highlighting will make source code more readable. This wiki has support for syntax highlighting of many programming languages, XML and C++ (see link at the bottom for a more complete list). The source code snippet to be highlighted is put between <syntaxhighlight> tags. The tag can be given some attributes to control how the source code appears.

Unfortunately the lexer for Nasal was obsoleted in February 2016 as the newer version of the MediaWiki software the wiki is running had dropped the GeSHi highlighter in favor for the Pygmets highlighter.

Note that you can use syntax highlighting for command lines as well.

<syntaxhighlight lang="" line start="" highlight="" inline></syntaxhighlight>

All attributes but lang are optional. start have to be used together with line. Values must be put inside quotation marks (").

lang
The language to be highlighted, for example bash, cpp or xml.
line
Use line numbering.
start
Used together with line if you want the line numbering to start somewhere else than one. For example will 42 start the line numbering from 42 instead of 1.
highlight
Used for highlighting a line. Do note that lines are counted from the first line in a snippet disregarding start. For example will 3,5-7 highlight line 3 and 5-7 in a snippet.
inline
Used when syntax highlighting is needed in running text.
Description What you type What you get
XML highlighting, here with line numbering, but without specifying the line number of the first line.
<syntaxhighlight lang="xml" line>
 <animation>
  <object-name>Object</object-name>
  <enable-hot type="bool">false</enable-hot>
 </animation>
</syntaxhighlight>

 <animation>
  <object-name>Object</object-name>
  <enable-hot type="bool">false</enable-hot>
 </animation>
C++ highlighting, arbitrarily starting from line 46, and highlighting the 1st, 3rd and 4th line in the snippet.
<syntaxhighlight lang="cpp" line start="46" highlight="1,3-4">
globals->get_commands()->execute("profiler-start");
// or with filename
SGPropertyNode args;
args.setStringValue("filename", "output.profile");
globals->get_commands()->execute("profiler-start", &args);
</syntaxhighlight>
globals->get_commands()->execute("profiler-start");
// or with filename
SGPropertyNode args;
args.setStringValue("filename", "output.profile");
globals->get_commands()->execute("profiler-start", &args);
Syntax highlighting in running text
A typical C++ comment <syntaxhighlight lang="cpp" inline>// looks like this</syntaxhighlight>.
A typical C++ comment // looks like this.

External links