Wish List: Better Code Formatting in the Forums (Can You Help?)

I know it's been a "wish" of many folks for our forums to have better code formatting. Well, if you know some PHP and a little about WordPress, you can make it happen.

What we need is a WordPress plugin that hooks the action for post displays. The plugin needs to take the post body, and look for anything contained within HTML "code" tags or "pre" tags.

Within that content, the plugin needs to strip any further code/pre tags (WordPress has a bit of a glitch where it'll sometimes nest them). It should then HTML-encode the remaining content to turn any backticks into an HTML entity. Finally, it should color-code the content, or whatever, and hand it back to WordPress for display.

If you think you might be interested, let me know.

There ARE existing code formatters. But they have some weaknesses:

  • Many require you to use a custom shortcode, which our forums users won't pick up on. Getting folks to use the standard CODE tag, which is even on the toolbar, is hard enough.
  • Most require additional directives to specify the language and whatnot that will be formatted - that's a hurdle people, in the past, weren't able to grasp.
  • Some use extensive client-side JavaScript, which is heavy, performs poorly, and doesn't interact well with some of the other JavaScript on the site.
  • Many don't accommodate WordPress' treatment of backticks. WP wants them to be code delimiters, but obviously in PowerShell the backtick is important for other reasons.

What we need isn't giant, and it isn't complicated, it'll just require some time.

UPDATE: I'm working on it.

UPDATE: I think I got it. I'm using the GeSHi parser Joel uses on PoshCode.org, although I've applied different CSS style to it. If anyone would like to tackle improving that parser, or the CSS, you can hit me up and I'll give you the code as it stands. But as-is, we get line-numbered, colorized syntax in a scrollable window when you use


to enclose your code blocks. WordPress backticks aren't allowed for code, and inline code isn't supported. Older HTML-style CODE and PRE tags will be converted automatically. I think.

About the Author

Don Jones

Don Jones is a Windows PowerShell MVP, author of several Windows PowerShell books (and other IT books), Co-founder and President/CEO of PowerShell.org, PowerShell columnist for Microsoft TechNet Magazine, PowerShell educator, and designer/author of several Windows PowerShell courses (including Microsoft's). Power to the shell!

5 Comments

  1. On wordpress.com you can use [code language="powershell"]...[/code] and it makes PS code look beautiful!
    Since this is a wordpress site too, isn't it possible to make it work the same way here?
    I'd gladly help, but I don't know much HTML and even less PHP.

    • I'm not sure what plugin they're using, but as I indicated in the post, I don't want people to have to use shortcodes. I realize that they do work, but too many people are unfamiliar with them and don't use them. If nobody uses it, it doesn't matter how nice it works ;).

  2. I use the WP-Syntax plugin. It natively works in posts and pages but I'm unsure how you've got the forum setup. I do have to wrap it in the tag though. It's not necessarily a WP shortcode but I understand what you're saying. Currently, you've got an option in the forums for preformatting that allows the user to highlight a block of text, hit that button and it wraps it in a tag now. I wouldn't see how it would be very hard to add a button or modify that button to wrap the text in .