Blog »

New OborWiki feature: syntax highlighting with pygments

December 24, 2017, at 07:42 PM by Obormot in Features, Updates (0 comments)

The Pm Pygment recipe is now installed on OborWiki (off by default; you can enable it via the Configurator). It adds syntax highlighting for code blocks, via the Pygments syntax highlighter.

The basic usage is simple:

(:sourcecode lang="go" style="rainbow_dash":)
package main
import "fmt"

func main() {
    fmt.Println("Hello World")
}
(:sourcecodeend:)
New OborWiki feature: syntax highlighting with pygments — OborWiki
Blog »

New OborWiki feature: syntax highlighting with pygments

December 24, 2017, at 07:42 PM by Obormot in Features, Updates (0 comments)

The Pm Pygment recipe is now installed on OborWiki (off by default; you can enable it via the Configurator). It adds syntax highlighting for code blocks, via the Pygments syntax highlighter.

The basic usage is simple:

(:sourcecode lang="go" style="rainbow_dash":)
package main
import "fmt"

func main() {
    fmt.Println("Hello World")
}
(:sourcecodeend:)
package main
import "fmt"

func main() {
    fmt.Println("Hello World")
}

A neat trick, however, is that you can also use it with the Pastebin Embed (Added 2017-12-25: or Gist Embed) recipe—retrieving the raw text of the paste, and letting Pm Pygment do the syntax highlighting for you:

(:sourcecode lang="go" style="rainbow_dash":)
(:pastebin-embed T3rxzk4N raw no-pre:)
(:sourcecodeend:)
package main
import "fmt"

func main() {
    fmt.Println("Hello World")
}

(Note that the no-pre argument to the (:pastebin-embed:) directive is required for this to work.)

Here’s a longer example:

(:sourcecode lang="cpp" style="manni":)
(:pastebin-embed svHLwueV raw no-pre:)
(:sourcecodeend:)
/*
    Copyright (C) 2014 Dario Oliveri

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to
    deal in the Software without restriction, including without limitation the
    rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    sell copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.
*/

//created for article on GPI (gameprog.it)

#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <cmath>
#include <memory>

class Image{

    unsigned int SizeX = 0;
    unsigned int SizeY = 0;
    std::unique_ptr<int[]> data = nullptr;

public:

    Image(const std::string & file_name){
        loadFromFile(file_name);
    }

    bool loadFromFile(const std::string & file_name){
        using namespace std;
        ifstream file(file_name.c_str());
            if(!file) {cout<<"file not found!"<<endl; return false;}

        string values[5];
        int i = 0;
        /// ammette commenti nell'header del file che iniziano con "#"
        while(i<4 && !!file){
            std::string token;
            int pos = file.tellg();
            file>>token;
            if(token[0]=='#'){ ///salta commento
                file.seekg(pos);
                getline(file,token);
                continue;
            }
            values[i++] = std::move(token);
        }
        if(!file)
            return false; //EOF prematura

        if(values[0]!=string("P3"))
            return false; //non è un file PPM

        unsigned int colors;
        unsigned int * pints[] = {&SizeX, &SizeY, &colors};

        stringstream ss;
        for (int i = 0; i<3 && !!file; i++){
            ss<<values[i+1];
            ss>>(*pints[i]);
            ss.str("");
            ss.clear();
        }

        if(!file)
            return false; //file PPM troncato, inutilizzabile.

        if(colors>255 || SizeX == 0 || SizeY == 0)
            return false; // dati dell'header non validi

        /// DA QUI IN POI I COMMENTI NON SONO PIU' SUPPORTATI (come richiesto da specifiche)

        int totalSize = SizeX*SizeY*3;
        data.reset(new int[totalSize]);

        i = 0;
        while(i<totalSize && !!file){
            file>>data[i++];
            cout<<data[i-1];
        }

        if(file.eof() && i<totalSize){
            data.reset(nullptr);
            return false;
        }
        return true;
    }

    bool saveToFile(const std::string &filename){
        std::ofstream file(filename.c_str());
        if(!file)
            return false;

        file<<"P3\n";
        file<<"#PPM image loader/saver by Dario Oliveri\n";
        file<<SizeX<<" "<<SizeY<<"\n";
        file<<"#http://gameprog.it/ tutorials\n";
        file<<"255\n";

        int index = 0;
        for(unsigned int Y = 0; Y<SizeY; Y++){
            for(unsigned int X = 0; X<SizeX; X++){

                file<<data[index++]<<" ";
                file<<data[index++]<<" ";
                file<<data[index++]<<" ";
            }
            file<<"\n";
        }
        return true;
    }

    template < typename Functor >
    Image& fill(Functor func){
        unsigned int max_i = SizeY*SizeX*3;
        for(int i=0; i < max_i; i+=3 )
            func(data[i],data[i+1],data[i+2]);
        return (*this);
    }

    template < typename Functor >
    Image & combine2(const Image & other, Functor func){
    unsigned int max_i = SizeY*SizeX*3;
    for(int i=0; i < max_i; i+=3 )
        func(data[i],data[i+1],data[i+2],
             other.data[i],other.data[i+1],other.data[i+2]);
    return (*this);
}

};

void invertiColori(int &R, int &G, int &B){ //SEMPLICE FUNZIONE: RIUTILIZZABILE!
    R = 255-R;
    G = 255-G;
    B = 255-B;  //DEBUGGARE E' UNA GIOIA COSì
}

int main(){
    using namespace std;

    Image I(string("logoGPI.ppm"));
    I.fill(invertiColori);
    I.saveToFile("invertedGPIlogo.ppm");
    return 0;
}

See the Pm Pygment info page for more detailed documentation/instructions.

Leave a reply
Your name (required):

Your comment (required):


Enter value: Captcha

A neat trick, however, is that you can also use it with the Pastebin Embed (Added 2017-12-25: or Gist Embed) recipe—retrieving the raw text of the paste, and letting Pm Pygment do the syntax highlighting for you:

(:sourcecode lang="go" style="rainbow_dash":)
(:pastebin-embed T3rxzk4N raw no-pre:)
(:sourcecodeend:)
New OborWiki feature: syntax highlighting with pygments — OborWiki
Blog »

New OborWiki feature: syntax highlighting with pygments

December 24, 2017, at 07:42 PM by Obormot in Features, Updates (0 comments)

The Pm Pygment recipe is now installed on OborWiki (off by default; you can enable it via the Configurator). It adds syntax highlighting for code blocks, via the Pygments syntax highlighter.

The basic usage is simple:

(:sourcecode lang="go" style="rainbow_dash":)
package main
import "fmt"

func main() {
    fmt.Println("Hello World")
}
(:sourcecodeend:)
package main
import "fmt"

func main() {
    fmt.Println("Hello World")
}

A neat trick, however, is that you can also use it with the Pastebin Embed (Added 2017-12-25: or Gist Embed) recipe—retrieving the raw text of the paste, and letting Pm Pygment do the syntax highlighting for you:

(:sourcecode lang="go" style="rainbow_dash":)
(:pastebin-embed T3rxzk4N raw no-pre:)
(:sourcecodeend:)
package main
import "fmt"

func main() {
    fmt.Println("Hello World")
}

(Note that the no-pre argument to the (:pastebin-embed:) directive is required for this to work.)

Here’s a longer example:

(:sourcecode lang="cpp" style="manni":)
(:pastebin-embed svHLwueV raw no-pre:)
(:sourcecodeend:)
/*
    Copyright (C) 2014 Dario Oliveri

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to
    deal in the Software without restriction, including without limitation the
    rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    sell copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.
*/

//created for article on GPI (gameprog.it)

#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <cmath>
#include <memory>

class Image{

    unsigned int SizeX = 0;
    unsigned int SizeY = 0;
    std::unique_ptr<int[]> data = nullptr;

public:

    Image(const std::string & file_name){
        loadFromFile(file_name);
    }

    bool loadFromFile(const std::string & file_name){
        using namespace std;
        ifstream file(file_name.c_str());
            if(!file) {cout<<"file not found!"<<endl; return false;}

        string values[5];
        int i = 0;
        /// ammette commenti nell'header del file che iniziano con "#"
        while(i<4 && !!file){
            std::string token;
            int pos = file.tellg();
            file>>token;
            if(token[0]=='#'){ ///salta commento
                file.seekg(pos);
                getline(file,token);
                continue;
            }
            values[i++] = std::move(token);
        }
        if(!file)
            return false; //EOF prematura

        if(values[0]!=string("P3"))
            return false; //non è un file PPM

        unsigned int colors;
        unsigned int * pints[] = {&SizeX, &SizeY, &colors};

        stringstream ss;
        for (int i = 0; i<3 && !!file; i++){
            ss<<values[i+1];
            ss>>(*pints[i]);
            ss.str("");
            ss.clear();
        }

        if(!file)
            return false; //file PPM troncato, inutilizzabile.

        if(colors>255 || SizeX == 0 || SizeY == 0)
            return false; // dati dell'header non validi

        /// DA QUI IN POI I COMMENTI NON SONO PIU' SUPPORTATI (come richiesto da specifiche)

        int totalSize = SizeX*SizeY*3;
        data.reset(new int[totalSize]);

        i = 0;
        while(i<totalSize && !!file){
            file>>data[i++];
            cout<<data[i-1];
        }

        if(file.eof() && i<totalSize){
            data.reset(nullptr);
            return false;
        }
        return true;
    }

    bool saveToFile(const std::string &filename){
        std::ofstream file(filename.c_str());
        if(!file)
            return false;

        file<<"P3\n";
        file<<"#PPM image loader/saver by Dario Oliveri\n";
        file<<SizeX<<" "<<SizeY<<"\n";
        file<<"#http://gameprog.it/ tutorials\n";
        file<<"255\n";

        int index = 0;
        for(unsigned int Y = 0; Y<SizeY; Y++){
            for(unsigned int X = 0; X<SizeX; X++){

                file<<data[index++]<<" ";
                file<<data[index++]<<" ";
                file<<data[index++]<<" ";
            }
            file<<"\n";
        }
        return true;
    }

    template < typename Functor >
    Image& fill(Functor func){
        unsigned int max_i = SizeY*SizeX*3;
        for(int i=0; i < max_i; i+=3 )
            func(data[i],data[i+1],data[i+2]);
        return (*this);
    }

    template < typename Functor >
    Image & combine2(const Image & other, Functor func){
    unsigned int max_i = SizeY*SizeX*3;
    for(int i=0; i < max_i; i+=3 )
        func(data[i],data[i+1],data[i+2],
             other.data[i],other.data[i+1],other.data[i+2]);
    return (*this);
}

};

void invertiColori(int &R, int &G, int &B){ //SEMPLICE FUNZIONE: RIUTILIZZABILE!
    R = 255-R;
    G = 255-G;
    B = 255-B;  //DEBUGGARE E' UNA GIOIA COSì
}

int main(){
    using namespace std;

    Image I(string("logoGPI.ppm"));
    I.fill(invertiColori);
    I.saveToFile("invertedGPIlogo.ppm");
    return 0;
}

See the Pm Pygment info page for more detailed documentation/instructions.

Leave a reply
Your name (required):

Your comment (required):


Enter value: Captcha

(Note that the no-pre argument to the (:pastebin-embed:) directive is required for this to work.)

Here’s a longer example:

(:sourcecode lang="cpp" style="manni":)
(:pastebin-embed svHLwueV raw no-pre:)
(:sourcecodeend:)
New OborWiki feature: syntax highlighting with pygments — OborWiki
Blog »

New OborWiki feature: syntax highlighting with pygments

December 24, 2017, at 07:42 PM by Obormot in Features, Updates (0 comments)

The Pm Pygment recipe is now installed on OborWiki (off by default; you can enable it via the Configurator). It adds syntax highlighting for code blocks, via the Pygments syntax highlighter.

The basic usage is simple:

(:sourcecode lang="go" style="rainbow_dash":)
package main
import "fmt"

func main() {
    fmt.Println("Hello World")
}
(:sourcecodeend:)
package main
import "fmt"

func main() {
    fmt.Println("Hello World")
}

A neat trick, however, is that you can also use it with the Pastebin Embed (Added 2017-12-25: or Gist Embed) recipe—retrieving the raw text of the paste, and letting Pm Pygment do the syntax highlighting for you:

(:sourcecode lang="go" style="rainbow_dash":)
(:pastebin-embed T3rxzk4N raw no-pre:)
(:sourcecodeend:)
package main
import "fmt"

func main() {
    fmt.Println("Hello World")
}

(Note that the no-pre argument to the (:pastebin-embed:) directive is required for this to work.)

Here’s a longer example:

(:sourcecode lang="cpp" style="manni":)
(:pastebin-embed svHLwueV raw no-pre:)
(:sourcecodeend:)
/*
    Copyright (C) 2014 Dario Oliveri

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to
    deal in the Software without restriction, including without limitation the
    rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    sell copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.
*/

//created for article on GPI (gameprog.it)

#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <cmath>
#include <memory>

class Image{

    unsigned int SizeX = 0;
    unsigned int SizeY = 0;
    std::unique_ptr<int[]> data = nullptr;

public:

    Image(const std::string & file_name){
        loadFromFile(file_name);
    }

    bool loadFromFile(const std::string & file_name){
        using namespace std;
        ifstream file(file_name.c_str());
            if(!file) {cout<<"file not found!"<<endl; return false;}

        string values[5];
        int i = 0;
        /// ammette commenti nell'header del file che iniziano con "#"
        while(i<4 && !!file){
            std::string token;
            int pos = file.tellg();
            file>>token;
            if(token[0]=='#'){ ///salta commento
                file.seekg(pos);
                getline(file,token);
                continue;
            }
            values[i++] = std::move(token);
        }
        if(!file)
            return false; //EOF prematura

        if(values[0]!=string("P3"))
            return false; //non è un file PPM

        unsigned int colors;
        unsigned int * pints[] = {&SizeX, &SizeY, &colors};

        stringstream ss;
        for (int i = 0; i<3 && !!file; i++){
            ss<<values[i+1];
            ss>>(*pints[i]);
            ss.str("");
            ss.clear();
        }

        if(!file)
            return false; //file PPM troncato, inutilizzabile.

        if(colors>255 || SizeX == 0 || SizeY == 0)
            return false; // dati dell'header non validi

        /// DA QUI IN POI I COMMENTI NON SONO PIU' SUPPORTATI (come richiesto da specifiche)

        int totalSize = SizeX*SizeY*3;
        data.reset(new int[totalSize]);

        i = 0;
        while(i<totalSize && !!file){
            file>>data[i++];
            cout<<data[i-1];
        }

        if(file.eof() && i<totalSize){
            data.reset(nullptr);
            return false;
        }
        return true;
    }

    bool saveToFile(const std::string &filename){
        std::ofstream file(filename.c_str());
        if(!file)
            return false;

        file<<"P3\n";
        file<<"#PPM image loader/saver by Dario Oliveri\n";
        file<<SizeX<<" "<<SizeY<<"\n";
        file<<"#http://gameprog.it/ tutorials\n";
        file<<"255\n";

        int index = 0;
        for(unsigned int Y = 0; Y<SizeY; Y++){
            for(unsigned int X = 0; X<SizeX; X++){

                file<<data[index++]<<" ";
                file<<data[index++]<<" ";
                file<<data[index++]<<" ";
            }
            file<<"\n";
        }
        return true;
    }

    template < typename Functor >
    Image& fill(Functor func){
        unsigned int max_i = SizeY*SizeX*3;
        for(int i=0; i < max_i; i+=3 )
            func(data[i],data[i+1],data[i+2]);
        return (*this);
    }

    template < typename Functor >
    Image & combine2(const Image & other, Functor func){
    unsigned int max_i = SizeY*SizeX*3;
    for(int i=0; i < max_i; i+=3 )
        func(data[i],data[i+1],data[i+2],
             other.data[i],other.data[i+1],other.data[i+2]);
    return (*this);
}

};

void invertiColori(int &R, int &G, int &B){ //SEMPLICE FUNZIONE: RIUTILIZZABILE!
    R = 255-R;
    G = 255-G;
    B = 255-B;  //DEBUGGARE E' UNA GIOIA COSì
}

int main(){
    using namespace std;

    Image I(string("logoGPI.ppm"));
    I.fill(invertiColori);
    I.saveToFile("invertedGPIlogo.ppm");
    return 0;
}

See the Pm Pygment info page for more detailed documentation/instructions.

Leave a reply
Your name (required):

Your comment (required):


Enter value: Captcha

See the Pm Pygment info page for more detailed documentation/instructions.

Leave a reply
Your name (required):

Your comment (required):


Enter value: Captcha