I’m slowly getting into the swing of using IronPython to write small scripts to help me do things quicker. I’ve been updating some of the old posts on this blog to use syntaxhighlighter which is a much cleaner solution than the old mechanism. However, it did require a little bit of text manipulation of each code sample before updating it on the blog. The code simply reads the code snippet out the clipboard, performs a few basic transforms and then copies the transformed code back to the clipboard.
import io import clipboard code = clipboard.getText() brush = 'csharp' if not code == '<' else 'xml' substitutions = [('&','&'),('<','<'),('>','>')] for a,b in substitutions: code = code.replace(a,b) print code code = '<pre class="brush: %s">%s</pre>' % (brush, code) clipboard.setText(code)
The clipboard module is my own very small wrapper, which makes use of the
Clipboard object from
import clr clr.AddReference('System.Windows.Forms') from System.Windows.Forms import Clipboard def setText(text): Clipboard.SetText(text) def getText(): return Clipboard.GetText()