title: search and replace: convert square brackets to xml tags
body: <p>I will try and keep this short and to the point.</p>
  
  <p>Given the following</p>
  
  <pre><code>#!/usr/bin/python
  from lxml import etree
  
  root = etree.Element('root')
  sect = etree.SubElement(root,'sect')
  para = etree.SubElement(sect,'para')
  para.text = 'this is a [b]long[/b] block of text. Much longer than this example makes it out to be.'
  </code></pre>
  
  <p>how would I be best going about converting the output to what I have below. notice the [b]'s became element <code>&lt;b&gt;</code></p>
  
  <pre><code>&lt;root&gt; 
    &lt;sect&gt;
      &lt;para&gt;
         this is a &lt;b&gt;long&lt;/b&gt; block of text. 
        Much longer than this example makes it out to be.
      &lt;/para&gt;
    &lt;/sect&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>My real input and xml is considerably more complex. However, this is the gist of it. I have taken a standardly formatted text document and I am converting it to xml. The structure of the document is rather static. Therefore, this is not as crazy as it sounds. I currently have it broken into lines. This is relevant, because as I go through each line I have no trouble identifying <code>&lt;sect&gt;</code> or a <code>&lt;title&gt;</code>, but often times a <code>&lt;para&gt;</code> will have some extra formatting in its line. In this example, a <code>[b]</code>, that needs to be converted yet again. What would be the best way of accomplishing this?</p>
  
  <p><b>Items to keep in mind</b></p>
  
  <ol>
  <li><p>the authors of my input texts are not always consistent. therefore, it would be best to develop a lose regexp to find [b] WORD [/b] or when the authors errors something like [b[WORD[/b]. my current idea is to match something like [b or b] </p></li>
  <li><p>I am currently processing my input file line by line. I have removed any blank lines. should I consider processing this afterwards? I have no strong goal, but feel that this can be contained in a single loop through the text. </p></li>
  <li><p>This will need to play well with lxml when I output my document. for example see the edit below with my comment on the bbc parser</p></li>
  </ol>
  
  <p>I have worked on this most of the afternoon, and can discuss more of the routes I have taken. I will be working on this throughout the evening so if I come across other items to keep in mind I will update this question accordingly.  </p>
  
  <p><b>EDIT: </b><i>Or my problem with bbc parser</i></p>
  
  <p>Paul thoughtfully suggested <a href="http://code.google.com/p/postmarkup/wiki/Usage" rel="nofollow">postmarkup-1.1.4</a>, however, as you can see it does not play well with lxml. converting the elements to enities. This was a problem I ran into this afternoon when I did this through a search and replace. Ultimately, this is a perfect sed solution. As was pointed out. However, I was hoping to have not be the end user of this script and would rather everything contained within one command.</p>
  
  <pre><code>&gt;&gt;&gt; p.text = render_bbcode(p.text)
  &gt;&gt;&gt; p.text
  'this is a &lt;strong&gt;long&lt;/strong&gt; text string'
  &gt;&gt;&gt; etree.tostring(root)
  '&lt;root&gt;&lt;p&gt;this is a &amp;lt;strong&amp;gt;long&amp;lt;/strong&amp;gt; text string&lt;/p&gt;&lt;/root&gt;'
  </code></pre>
  
  <p>doing this in reverse returns equally poor results</p>
  
  <pre><code> &gt;&gt;&gt; p.text
   'this is a [b]long[/b] text string
   &gt;&gt;&gt; render_bbcode(etree.tostring(root))
   u'&amp;lt;root&amp;gt;&amp;lt;p&amp;gt;this is a &lt;strong&gt;long&lt;/strong&gt; string&amp;lt;/p&amp;gt;&amp;lt;/root&amp;gt;'
  </code></pre>