Turning a comma-separated list into an <itemizedlist>


Something I encounter occasionally is a comma-separated list of items in a paragraph that really should be an <itemizedlist>. So I created a script takes the selected text and creates an <itemizedlist> by splitting the text up where ever a comma appears.

I use TextMate for my editing and write scripts for it using ruby. Many of my colleagues use gedit so I have also created a gedit version. There are no instructions here on how to set these up in your editor, that is an exercise left up to the reader.

As an example:

publish, security, subscription, subscription-listener, and interface 

is turned into

    <listitem><para>and interface</para></listitem>

Note that it relies on the use of the serial comma to get the last item and doesn’t remove the “and” from the beginning of the last item. I have kept each <listitem> on a single line for neatness because the use-case for this script usually involves the items in the list being single words or very short phrases.

The gedit snippet

Note that this is Python, so the white space at the beginning of lines does matter.

tab = "    "
joined = ""
output = "";

for line in text.split("\n"):
  line = line.strip()
  joined = joined+" "+line

for item in joined.split(","):
  item = item.strip();
  if item != "" :
    output += tab+"<listitem\><para\>item</para\></listitem\>\n";

return output;

TextMate command script

#!/usr/bin/env ruby -w
tab = "    "
joined = ""
text.each do |line|
	line = line.strip
	joined = joined+" "+line

puts "<itemizedlist>"

joined.split(",").each  do |item|
  item = item.strip
  if item != "" then
    puts "#{tab}<listitem><para>#{item}</para></listitem>"

puts "</itemizedlist>"

Cross-posted from: http://shinysparkly.com/blog/2010/02/12/turning-a-comma-seperated-list-into-a-itemizedlist/


Tags: , , , , ,

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: