Including source code within the wiki: Difference between revisions

From Alliance Doc
Jump to navigation Jump to search
(Created page with "__NOTOC__ <languages /> <translate> To include source code within the wiki, we are using the extension [http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi SyntaxHi...")
 
(Marked this version for translation)
Line 3: Line 3:


<translate>
<translate>
<!--T:1-->
To include source code within the wiki, we are using the extension [http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi SyntaxHighlight_GeSHi]. You can easily include a code snippet using the tag '''<nowiki><syntaxhighlight> </syntaxhighlight></nowiki>'''.  
To include source code within the wiki, we are using the extension [http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi SyntaxHighlight_GeSHi]. You can easily include a code snippet using the tag '''<nowiki><syntaxhighlight> </syntaxhighlight></nowiki>'''.  


== Options of the <nowiki><syntaxhighlight></nowiki> tag ==
== Options of the <nowiki><syntaxhighlight></nowiki> tag == <!--T:2-->
For a complete list of options, please refer to [http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi this page].
For a complete list of options, please refer to [http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi this page].


=== ''lang'' option ===
=== ''lang'' option === <!--T:3-->
The '''lang''' option defines the language used for syntax highlighting. The default language, if this option is omitted, is C++. The complete list of supported languages is available  [http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi#Supported_languages here].  
The '''lang''' option defines the language used for syntax highlighting. The default language, if this option is omitted, is C++. The complete list of supported languages is available  [http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi#Supported_languages here].  


=== ''line'' option ===
=== ''line'' option === <!--T:4-->
The '''line''' option displays line numbers.  
The '''line''' option displays line numbers.  


== Example ==
== Example == <!--T:5-->
Here is an example of a C++ code snippet created with the <nowiki><syntaxhighlight lang="cpp" line> ... </syntaxhighlight></nowiki> tag.
Here is an example of a C++ code snippet created with the <nowiki><syntaxhighlight lang="cpp" line> ... </syntaxhighlight></nowiki> tag.


<!--T:6-->
<syntaxhighlight lang="cpp" line>
<syntaxhighlight lang="cpp" line>
#include <iostream>
#include <iostream>
Line 24: Line 26:
using namespace std;
using namespace std;


<!--T:7-->
void flushIfBig(ofstream & out, ostringstream & oss, int size, bool force=false) {
void flushIfBig(ofstream & out, ostringstream & oss, int size, bool force=false) {
if (oss.tellp() >= size) {
if (oss.tellp() >= size) {
Line 33: Line 36:
int buff_size = 50*1024*1024;
int buff_size = 50*1024*1024;


ofstream out ("file.dat");
<!--T:8-->
ofstream out ("file.dat");
ostringstream oss (ostringstream::app);
ostringstream oss (ostringstream::app);
oss.precision(5);
oss.precision(5);

Revision as of 15:42, 23 March 2016

Other languages:

To include source code within the wiki, we are using the extension SyntaxHighlight_GeSHi. You can easily include a code snippet using the tag <syntaxhighlight> </syntaxhighlight>.

Options of the <syntaxhighlight> tag

For a complete list of options, please refer to this page.

lang option

The lang option defines the language used for syntax highlighting. The default language, if this option is omitted, is C++. The complete list of supported languages is available here.

line option

The line option displays line numbers.

Example

Here is an example of a C++ code snippet created with the <syntaxhighlight lang="cpp" line> ... </syntaxhighlight> tag.

#include <iostream>
#include <fstream>
#include <unistd.h>
#include <sstream>
using namespace std;

void flushIfBig(ofstream & out, ostringstream & oss, int size, bool force=false) {
	if (oss.tellp() >= size) {
		out << oss.str();
		oss.str(""); //reset buffer
	}
}
int main() {
	int buff_size = 50*1024*1024;

	ofstream out ("file.dat");
	ostringstream oss (ostringstream::app);
	oss.precision(5);
	for (int i=0; i<100*buff_size; i++)
	{
		oss << i << endl;
		flushIfBig(out,oss,buff_size);
	}
	flushIfBig(out,oss,buff_size,true);
	out.close();
}