Inserting code into blog entries

Found a bug? Tell us!!
Post Reply
Misha
Posts: 2
Joined: Tue Apr 11, 2006 8:29 am

Inserting code into blog entries

Post by Misha »

This probably isn't so much a bug as it could be a desired feature. But nevertheless I think it's worth pointing out.

I decided to go about and add a Java code snippet to my blog entry. I thought the way I'd achieve it is would be to surround it with a pre tags indicating preformatted text, but this didn't achieve the required result.

This is what I entered into the blog:

Code: Select all

<pre>
    // Remove duplicates from a list in-place
    private static <T extends Comparable<? super T>> 
        void removeDuplicates(List<T> list) {            
        Collections.sort(list);
        int i = 0;
        // once reached second last element, break
        while (i < list.size() - 1) {
            T t1 = list.get(i);
            T t2 = list.get(i+1);
            if (t1.compareTo(t2)) {
                list.remove(i+1);                
            } else {
                ++i;
            }
        }
    }
</pre>
NL2BR inserted br tags after each newline in my code block, and part of the code was garbled as it looks like HTML to a parser.

Is there an easy way to post code in S9Y blog entries? Something like the wayyou can insert code into posts on this forum. I'm a developer, so it's really handy for me to able to do that. I'm sure other people would have come across this problem, but I haven't seen this mentioned in the FAQ or on the forum. If I've missed it, my apologies.

Thanks in advance!
Misha
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Inserting code into blog entries

Post by garvinhicking »

Hi!

The easiest way of inserting code is to use a plugin like GeShi or BBCode with the

Code: Select all

 tag.

However, the nl2br plugin will always interfer, so will need to disable that plugin. You can also conditionally disable this plugin for specific entries by using the entryproperties plugin.

nl2br is pretty dumb, so it will never know whether it shall or shall not translate specific newlines. Sadly finding an algorithm where nl2br shall be done and where not requires a huge regular expression overhead, and many rules that are a pain to figure out ; so in most cases, nl2br is not suitable for advanced users...

The "Wiki" plugin f.e. offers both nl2br transformation and <code> blocks, but is hard to use in some other instances - maybe this one helps you along?

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Misha
Posts: 2
Joined: Tue Apr 11, 2006 8:29 am

Thank you!

Post by Misha »

Hello,

Thank you for your quick reply. I installed the BBCode plugin and it's working great.

Thanks again!
Cheers,
Misha
gizmola
Regular
Posts: 37
Joined: Mon Oct 25, 2004 11:54 pm

Re: Thank you!

Post by gizmola »

Misha wrote:Hello,

Thank you for your quick reply. I installed the BBCode plugin and it's working great.

Thanks again!
Cheers,
Misha
As the author of the GeSHi plugin, I thought I'd point out that it does color syntax highlighting for a bunch of different languages, including Java. It can also provide line numbering, while still allowing someone to cut the code without the line numbers. Give it a try if any of these features appeals to you.
Post Reply