Code Blocks

How to display code blocks on your websites

Syntax Highlighting

Options for including code blocks with syntax highlighting on your website include:

The Nift documentation covers syntax highlighting code blocks on your site using Google Code Prettify. Using Google Code Prettify on your site is as simple as adding the following code to the head of each page:

<!-- uses google-code-prettify for syntax highlighting 
	 see https://github.com/google/code-prettify for more info -->
<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js">
</script>

See here for a gallery of themes for Google Code Prettify.

Note the examples on this site are using the included pre.css file.

If your code blocks with the template you use do not have nice margins (eg. overlap the text above or below) then try modifying margin-top and margin-bottom in the included pre.css file.

See the Google code prettifier README for supported languages and other relevant information.

Also see the Nift tutorial for a better idea of how to nicely display code blocks.

Example code blocks



int n;
vector<int> v;

<pre>
	int n;
	vector<int> v;

	for(int i=0; i < n; i++)
		cout << v[i] << endl;
</pre>

for(int i=0; i < n; i++)
	cout << v[i] << endl;

int n;
vector<int> v;

int n;
vector<int> v;

for(int i=0; i < n; i++)
	cout << v[i] << endl;

for(int i=0; i < n; i++)
	cout << v[i] << endl;

#include "Quoted.h"

//reading a string which may be surrounded by quotes with spaces, and strips surrounding quotes
//note mutates white space into single spaces
bool read_quoted(std::istream &ifs, std::string &s)
{
    //reads first string
    if(!(ifs >> s))
        return 0;

    std::string s2;
    if(s[0] == '"')
    {
        while(s[s.length()-1] != '"')
        {
            ifs >> s2;
            s += " " + s2;
        }

        s.replace(0, 1, "");
        s.replace(s.length()-1, 1, "");
    }
    else if(s[0] == '\'')
    {
        while(s[s.length()-1] != '\'')
        {
            ifs >> s2;
            s+=" " + s2;
        }

        s.replace(0, 1, "");
        s.replace(s.length()-1, 1, "");
    }

    return 1;
};

//outputting a string with quotes surrounded if it contains space(s)
std::string quote(const std::string &unquoted)
{
    if(unquoted.find(' ') == std::string::npos)
        return unquoted;
    else if(unquoted.find('"') == std::string::npos)
        return "\"" + unquoted + "\"";
    else
        return "'" + unquoted + "'";
};


#include "Quoted.h"

//reading a string which may be surrounded by quotes with spaces, and strips surrounding quotes
//note mutates white space into single spaces
bool read_quoted(std::istream &ifs, std::string &s)
{
    //reads first string
    if(!(ifs >> s))
        return 0;

    std::string s2;
    if(s[0] == '"')
    {
        while(s[s.length()-1] != '"')
        {
            ifs >> s2;
            s += " " + s2;
        }

        s.replace(0, 1, "");
        s.replace(s.length()-1, 1, "");
    }
    else if(s[0] == '\'')
    {
        while(s[s.length()-1] != '\'')
        {
            ifs >> s2;
            s+=" " + s2;
        }

        s.replace(0, 1, "");
        s.replace(s.length()-1, 1, "");
    }

    return 1;
};

//outputting a string with quotes surrounded if it contains space(s)
std::string quote(const std::string &unquoted)
{
    if(unquoted.find(' ') == std::string::npos)
        return unquoted;
    else if(unquoted.find('"') == std::string::npos)
        return "\"" + unquoted + "\"";
    else
        return "'" + unquoted + "'";
};


//adds insert file
if(read_and_process(insertLocation, antiDepsOfReadLocation, contentLocation, pageTitle, dateTimeInfo, indentAmount, codeBlockDepth, htmlCommentDepth, contentInserted, processedPage, pageDeps) > 0)
	return 1;
//indent amount updated inside read_and_process
linePos += posOffset;