A better approach for code samples?
Posted May 31, 2005 at 02:39 PM
I have a tendency to work with code quite a bit, and I will often refer to code samples within my blog posts. This can be a real mess to author, especially XHTML ones, because the necessary encoding has had to be authored manually. Well, leave it to the internet—came across a new Textpattern plugin today called glx-code that allows me to point to an external text file from within my blog post, and have it properly encoded and assembled into an unordered list along the way.
At first glance, this seems quite nice, and includes the following benefits:
- Line numbering gets provided, making it easier to call out specific lines of attention in the blog post.
- Indenting seems to be supported for certain types of content, assuming that the source file is already indented with tabs.
- Support for optionally linking directly to the file containing the code sample, nice for readers wishing to download the content.
Testing it out
This is a test of the new plugin pointing to various text files containing sample code.
For example, consider this markup example of a simple XHTML document:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>XHTML 1.0 Strict</title><!-- Single-line comment --></head><body><p>Page template output goes here.</p><!--Multi-line comment--></body></html>- Download this code: /code/codesample.html
Or this example for a list of links:
<ul class="list1 link1"><li class="first"><a href="#">Search</a></li><li><a href="#"><span>Highlight</span></a></li><li><a href="#">Options</a></li><li><a href="#"><em>Hotmail</em></a></li><li><a href="#">Messenger</a></li><li class="selected"><a href="#"><strong>My MSN</strong></a></li><li class="last"><a href="#">Pop-ups Blocked</a></li></ul>- Download this code: /code/Data-LinkList.xml
Of course, every item in the list is really just a link:
<div class="link"><a href="#">Red Sox are just better than Yanks</a></div>- Download this code: /code/Data-Link.xml
It could be styled via a CSS file, something along the lines of this:
element {color: #f00;}.class {color: #f00;}#id {color: #f00;}element#id {color: #f00;}element.class {color: #f00;}#id element .class {color: #f00;}element {color: #f00; /* single-line comment */}/* multi-line commentelement {color: #f00;}*/- Download this code: /code/codesample.css
And it might have behavior applied as well, using something like the following javascript:
// single-line commentfunction alert1(str) {window.alert(str);}function alert2(str) {window.alert(str); // single-line comment}/* multi-line commentfunction alert3(str) {window.alert(str);}*/- Download this code: /code/codesample.js
Of course, this is just an example, the CSS and JS files really have nothing to do with the sample markup. Just playing around with it.
About this page
This page contains a single post from Daniel Boerner's blog, of which Boot Camp + Windows Vista = no more Airport Extreme reboots is the latest post.
Are there more posts like this one?
Possibly. Within this blog, this post is categorized under danboenet and it was posted on May 31, 2005. Those would be good places to start looking for related posts.
Next post (newer)
My Textpattern setup and plugins
This post is closed to new comments.