Turning a comma-separated list into an <itemizedlist>

by

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

<itemizedlist>
    <listitem><para>publish</para></listitem>
    <listitem><para>publish</para></listitem>
    <listitem><para>security</para></listitem>
    <listitem><para>subscription</para></listitem>
    <listitem><para>subscription-listener</para></listitem>
    <listitem><para>and interface</para></listitem>
</itemizedlist>

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.

<itemizedlist>
$<
text = $GEDIT_SELECTED_TEXT
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;
>
</itemizedlist>

TextMate command script

#!/usr/bin/env ruby -w
text = ENV['TM_SELECTED_TEXT']
tab = "    "
joined = ""
text.each do |line|
	line = line.strip
	joined = joined+" "+line
end

puts "<itemizedlist>"

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

puts "</itemizedlist>"

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

About these ads

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: