I’ve been making the best use I can of the default wordpress features for a while now, but I’m still not really happy with the degree of control over the resulting pages, particularly when it comes to any code listings or console output.
To see if I could I find a solution started to experiment a bit and discovered found that by replacing the ‘<code>’ and ‘<pre>’ tags with a ‘<div>’ tag you can specify the exact style you want which gives much more control over the layout and appearance of the page.
I originally used the wordpress ‘<code>’ and ‘<pre>’ tags to format things like source code as shown below.
This will result in the source code being shown on a grey background using a monospaced font. You could argue that there isn’t really anything wrong with this, but I wanted a bit more control over the layout of the text.
while [[ $x -gt 0 ]] ;do
x=$(($x >> 1))
Specifying each of the modified style properties individually does involve some extra work which does negate some of the advantages of using wordpress in the first place, but I was spending quite a lot of time editing each page, checking the layout and modifying it until I got the results I wanted anyway. This does make the formatting of each page a lot more complicated and quite verbose, but it isn't that hard to do.
You will also discover that to get some of the special (and not so special) characters to display properly you will have to replace them with their corresponding HTML or hexadecimal character codes as wordpress tries to 'intelligently' substitute things like pairs of quotes with opening and closing quotes and multiple hyphens with a long dash. This can be rather awkward when cutting and paste any text directly from the web page, and it isn't always easy to see what has been changed.
Using the following substitutions resolves these formatting issues:
- Ampersands (&) must be replaced with '&'.
- Hash characters (#) may need to be replaced with '#' when being used in HTML codes.
- Multiple spaces need to be replaced with '&#nbsp;&#nbsp;' to preserve layout.
- Less than (<) and greater than (>) symbols need to be replaced with '<' and '>'.
- Any hyphens (-) need to be replaced with '-' and multiple full stops (...) with '...'.
- Double quotes (") should be replaced with '"' or '"' and single quotes (') with '''.
- Lastly you need to replace the colon (:) after http or https with ':' if you don't want URLs to appear as links.
Note - Do not be tempted to use '<' or '>' in place of '<' and '>' when replacing (<) or (>) characters, or to use 'white-space:pre' instead of 'white-space:nowrap' - as even if text looks right even when pasted the code may not work as expected. You can have hours of fun tracking down problems like that! (I know, as it has happened to me more than once).
After making these changes the page source for the program listing above becomes:
This may look like a lot of work for a grey box with rounded corners, particularly as this isn't very different from the previous version, but now I can decide how thick the border should be, how much space there should be around the text, what font size to use, and so on.
You can add a title to the box by nesting two '<div>' tags inside another, creating an outer section with a grey border and rounded corners, and two inner sections with no borders, one containing the title on a blue background and another with a grey background that contains the listing.
Since the outer section has no padding the inner sections completely fill it, making it look like a text box with a title bar. I've kept the text in the title bar in the same font as the rest of the page, but made it the same size as the font in the text box.
To highlight the keywords and values you can used the HTML 'span' tag to give individual elements a distinct colour, this is great for highlighting key words but it does make the original text quite difficult to read. (If you think the listing below is a bit convoluted, then you don't want think about the HTML need to produce it!).
Up until now any changes simply involved some editing of the text using search and replace and pasting in the boiler plate at the beginning and end. Highlighting individual words is a lot of extra effort, but I think it is worth it as using different colours for any reserved words and numeric constants improves the appearance of the code listing considerably.
Using different colour combinations also allows the text to be formatted to match different types of output.
Results in a black back background with white text suitable for showing what the output on a console would look like.
Getting the formatting just right does involve quite a bit more work but I think the results are worth the extra effort. Figuring out how to make everything work the way I wanted it to has been strangely absorbing and a little frustrating but was quite fun and as a result I now know a lot more about HTML styles than I used to!