Smalltalk code snippets in blogs

Preparing to write something on Smalltalk, Java, and the activity of design, I’ve been looking for a code formatter/syntax highlighter for the site. Glad to have found Alex Gorbatchev’s Syntax Highlighter, which does the job nicely, except that it’s missing a Smalltalk renderer (brush in the program’s terms). So here is one, which I’ll be tinkering with in the days to come, which does a comfortable if basic job of highlighting. Examples (plus the code of the brush) after the jump…

(I’m still working on the Smalltalk comment strings, and the stray CDATA ending at the end of the snippets. Like I said, work in progress…)

OK, CDATA fixed (if nastily: just dropping the close of the CDATA section works…) but the quote issue remains. The version of Mojo I’m using has a formatting function that “texturises” the output, which is what’s messing with the quote characters: odd thing is it doesn’t happen in the JavaScript example: I really don’t know why that should be the case…

And now (24 March) it’s all gone: I think because of issues with Alex Gorbatchev’s server. I’ll serve the components from this site in the next day or so. Normal service will be resumed…

Finally - scripts back online, and worked out that the >> in the method header were throwing the wptexturize function in the version of wordpress used by Joomla/MojoBlog. This and mojo’s poor handling of the end of the CDATA in the script tag means I’ve reverted to using the >pre< tag method, which means escaping angle brackets in the code: a bit of a a pain, but it’s now working.

Smalltalk example:

Dictionary>>associationsSelect: aBlock
	"Evaluate aBlock with each of my associations as
	the argument. Collect into a new dictionary, only
	those associations for which aBlock evaluates
	to true."

	| newCollection foo |
	foo := 'one two three'.
	newCollection := self species new.
	self associationsDo:
		[:each |
		(aBlock value: each) ifTrue: [newCollection add: each]].

	^newCollection

Javascript source for the brush:


/**
 * SyntaxHighlighter
 * http://alexgorbatchev.com/
 *
 * brush file for Smalltalk
 * David Harvey
 * http://www.teamsandtechnology.com
 */
SyntaxHighlighter.brushes.Smalltalk = function()
{
	var keywords =	'self super true false nil thisContext new';
		// Yes, I know "new" isn't a keyword, but it's
		// worth highligting along with the rest
	var messages =  'not value size species';
		// keyword messages covered by simple regex
		// This list should include all the common single-word selectors

	this.regexList = [
		{ regex: SyntaxHighlighter.regexLib.multiLineDoubleQuotedString,
				css: 'comments'},		// double quoted strings are comments
		{ regex: SyntaxHighlighter.regexLib.doubleQuotedString,
				css: 'comments'},		// double quoted strings are comments
		{ regex: SyntaxHighlighter.regexLib.singleQuotedString,
				css: 'string' },		// single quoted strings
		{ regex: new RegExp(this.getKeywords(keywords), 'gm'),
				css: 'keyword' },		// keywords
		{ regex: new RegExp(this.getKeywords(messages), 'gm'),
				css: 'color1 bold' },	// common messages
		{ regex: /[a-z][A-Za-z0-9]*:/g,
				css: 'color1' },		// keyword messages
		{ regex: /[A-Z][a-zA-Z0-9]+>>/gm,
				css: 'plain bold' },	// class at beginning of method text
		{ regex: /[A-Z][a-zA-Z0-9]+>>/gm,
				css: 'plain bold' },	// class at beginning of method text
		{ regex: /|[^|]+|/gm,
				css: 'color2' },		// locals
		{ regex: /(:[a-z][^:|]+)|/gm,
				css: 'color2' }			// block parameters
		];
};

SyntaxHighlighter.brushes.Smalltalk.prototype
	= new SyntaxHighlighter.Highlighter();
SyntaxHighlighter.brushes.Smalltalk.aliases	= ['st', 'smalltalk'];

4 Responses to “Smalltalk code snippets in blogs”

  • Joseph Pelrine responded:

    Hi David,

    Looking at the (generated) source for the page, I see that the CDATA section in both code snippets ends with “]]>” rather than with “]]>” (hope that translates right). Might that be the reason why the CDATA tag is showing up?

  • Joseph Pelrine responded:

    Oops, as I feared, the quote didn’t translate right. What I see in the source ends with “& gt;” (remove space), and not with “>”. Might be a code gen error?

  • David responded:

    Hi Joseph - yes, that’ll be it. The editor in Joomla trying to be helpful :-( Thanks!

  • David responded:

    Update on this: struggling with this because either Joomla or MojoBlog are playing around with single and double quotes, sometimes replacing code quotes with the left-right equivalents. Need to dig into the PHP, which is not where I want to be at all! Frustrating.

Add your own comment...