Representing code in HTML
While converting one of my older reviews of a programming book, most of the code examples I used were either enclosed in a PRE tag or in a BLOCKQUOTE tag with a bunch of BR tags to break the lines up. More recently, I’ve tried enclosing programming examples in a paragraph or blockquote with a special style attached to it setting white-space to pre.
I’ve never particularly liked either solution and because of that this blog doesn’t support either of those options particularly well. I wrote this software and I don’t like untagged text.
My first thought was simply to make a div.code style that doesn’t mean anything but would indent correctly. While working on that, I remembered Mark Newhouse’s article about how lists are underrated in HTML.
It occurred to me that HTML already has a tag that is practically designed for the display of programming code. Programming is nothing more than a list of instructions for the computer to perform. The HTML list tags, UL and LI, work perfectly for displaying programming code.
In the past, this didn’t work because the list tags would put a bullet next to each line of code. Today, however, we can use style sheets to rid ourselves of the bullet.
This style example will remove the bullet and cause code which wraps to be indented two em-spaces:
[toggle code]
-
ul.code li {
- list-style: none;
- text-indent: -2em;
- margin-left: 2em;
- }
-
ul.code ul {
- padding-left: 2em;
- }
If you want the code example to be set off, you can give it a margin and a border, and reduce the width:
[toggle code]
-
ul.code {
- margin-left: 2em;
- background-color: #EEEEFF;
- border-width: .2em;
- border-style: inset;
- border-color: #4444FF;
- width: 90%;
- }
This treats program lines as exactly what they are: a list of items. Programming code is a list of instructions, and CSS code is a list of rules.
Scripted conversion of code for display
Here’s a Perl script that will convert code to a list for display. I use it with SilverService on Mac OS X to easily convert a script into a list:
[toggle code]
- print '<ul class="code">', "\n";
- $tabs = "\t";
- $pci = 0;
-
while (<>) {
- chomp;
-
if (s/^(\t+)//) {
- $newtabs = $1;
- $nci = length($newtabs);
-
} else {
- $nci = 0;
- }
-
if ($nci != $pci) {
-
if ($nci < $pci) {
-
while ($pci > $nci) {
- $tabs = substr($tabs, 0, -1);
- print $tabs, '</ul>', "\n";
- $pci--;
- }
-
while ($pci > $nci) {
-
} else {
-
while ($nci > $pci) {
- print $tabs, '<ul>', "\n";
- $tabs .= "\t";
- $pci++;
- }
-
while ($nci > $pci) {
- }
- $pci = $nci;
-
if ($nci < $pci) {
- }
-
if (/^\t*$/) {
- $style = ' class="section"';
-
} else {
- print $tabs, '<li', $style, '>';
- s/&/&/g;
- s/</</g;
- s/>/>/g;
- print;
- print '</li>', "\n";
- $style = '';
- }
- }
-
while ($pci > 0) {
- $tabs = substr($tabs, 0, -1);
- print $tabs, '</ul>', "\n";
- $pci--;
- }
- print '</ul>', "\n";
- Taming Lists
- Mark Newhouse exhorts us to bring our lists under control: so many things that we put on our web pages are lists, and can be best displayed if we treat them as lists. Style sheets allow us to do this without having all of our lists display exactly the same.
- SilverService
- “SilverService is a Mac OS X application that allows you to use familiar unix shell commands from within any Services-aware application (i.e., almost any Cocoa application).” This is a very useful application if you do command-line scripting but also work in the GUI.
More HTML
- Nisus “clean HTML” macro
- The Nisus macro language is Perl; this means we can use all of Perl’s strengths as a text filter scripting language in Nisus.
- Flash on iPhone not in anybody’s interest
- Flash on iPhone is not in the interest of people who buy iPhones. The only people who really want it are poor web designers who can’t get out of 1992.
- Web display of Taskpaper file
- It is easy to use PHP to convert a Taskpaper task file into simple HTML conducive to styling via CSS.
- ELinks text-only web browser
- If you need a text browsers on Mac OS X, the ELinks browser compiles out of the box.
- iPhone development another FairPlay squeeze play?
- Why no iPhone-only applications? Is it short-sightedness on Apple’s part, or are they trying to encourage something big?
- Six more pages with the topic HTML, and other related pages
More Perl
- Simple .ics iCalendar file creator
- A simple Perl script to create an ics file from a human-readable text of events.
- No premature optimization
- Don’t optimize code before it needs optimization or you’re likely to create unoptimized code.
- Using Term::ANSIColor with GeekTool
- Rather than using the raw codes directly, Perl (at least on OS X) comes with Term::ANSIColor built in.
- Nisus HTML conversion
- New features in Nisus’s scripting language make HTML conversion almost a breeze.
- Nisus “clean HTML” macro
- The Nisus macro language is Perl; this means we can use all of Perl’s strengths as a text filter scripting language in Nisus.
- Three more pages with the topic Perl, and other related pages