How to paste source code in blogs

  • Author
    Posts
  • #48094

    zdravko
    Member

    I have several examples in C++ programming languages, which I’d like to post on my blog. I tried to use the tag “". However, due to certain fragments of the code (templates, class keyword, etc.) the output looks awful. Do you know a universal way of parsing the .cpp file?

    #48236

    timethief
    Member

    From the forum search box several relevant threads http://wordpress.com/forums/search.php?q=source+code

    #48252

    zdravko
    Member

    It seems that the pre-tag is a partial solution to the problem. Templates keywords keep modifying the output. Can you help me?

    #48265

    devblog
    Member

    Nope…

    If you want something like:

    void main(){
    cout >> "Hello World";
    return;
    }

    where void, cout and return are blue; main() is black and “Hello World” is bold… the solution I see to this is you formatting the code in HTML first and then pasting the formatted code into the editor… this process can be very laborious, though.

    #48270

    you need to make sure all your code uses html characters. (e.g. &lt; instead of < ). easiest way is to paste your code into the visual editor, then switch to the code tab to add the <pre> tags

    #48273

    zdravko
    Member

    devblog, no – I am not talking about syntax coloring. The C++ language introduces templates – this technique means usage of “template <class T>” keywords etc.

    sunburntkamel, I’ll try it!

    #48275

    zdravko
    Member

    sunburntkamel, your way seems to work! But why does the tab character (by me it is 4 spaces) disappear? It is strange to loose the indentation of a source code…

    #48413

    wordpress does funny things to text when it’s posted. the <pre> tag used to preserve it, i don’t know what’s wrong with it these days.

    the tab character’s width may also be represented differently in HTML than in your editor.

The topic ‘How to paste source code in blogs’ is closed to new replies.