<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <title>Death of a Gremmie</title>
    <link>http://deathofagremmie.com</link>
    <description>Brian Neal's blog about programming.</description>
    <pubDate>Sat, 21 Jan 2012 05:35:56 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>My newline-to-break extension now shipping with Python-Markdown</title>
      <link>http://deathofagremmie.com/2011/06/21/my-newline-to-break-extension-now-shipping-with-python-markdown</link>
      <pubDate>Tue, 21 Jun 2011 22:15:00 CDT</pubDate>
      <category><![CDATA[Python]]></category>
      <category><![CDATA[Markdown]]></category>
      <guid isPermaLink="true">http://deathofagremmie.com/2011/06/21/my-newline-to-break-extension-now-shipping-with-python-markdown</guid>
      <description>My newline-to-break extension now shipping with Python-Markdown</description>
      <content:encoded><![CDATA[<div class="document">
<p>Here is a quick update on a <a class="reference external" href="http://deathofagremmie.com/2011/05/09/a-newline-to-break-python-markdown-extension/">previous post</a> I made about a newline-to-break
extension for <a class="reference external" href="http://www.freewisdom.org/projects/python-markdown/">Python-Markdown</a>. I'm very happy to report that the extension will
now be <a class="reference external" href="https://github.com/waylan/Python-Markdown/issues/13">shipping with Python-Markdown</a>! Thanks to developer Waylan Limberg for
including it!</p>
</div>
]]></content:encoded>
    </item>
    <item>
      <title>A newline-to-break Python-Markdown extension</title>
      <link>http://deathofagremmie.com/2011/05/09/a-newline-to-break-python-markdown-extension</link>
      <pubDate>Mon, 09 May 2011 22:40:00 CDT</pubDate>
      <category><![CDATA[Python]]></category>
      <category><![CDATA[Markdown]]></category>
      <guid isPermaLink="true">http://deathofagremmie.com/2011/05/09/a-newline-to-break-python-markdown-extension</guid>
      <description>A newline-to-break Python-Markdown extension</description>
      <content:encoded><![CDATA[<div class="document">
<p>When I launched a new version of my website, I decided the new forums would use
<a class="reference external" href="http://daringfireball.net/projects/markdown/">Markdown</a> instead of <a class="reference external" href="http://en.wikipedia.org/wiki/BBCode">BBCode</a> for the markup. This decision was mainly a personal
one for aesthetic reasons. I felt that Markdown was more natural to write compared
to the clunky square brackets of BBCode.</p>
<p>My new site is coded in <a class="reference external" href="http://python.org">Python</a> using the <a class="reference external" href="http://djangoproject.com">Django</a> framework. For a Markdown implementation
I chose <a class="reference external" href="http://www.freewisdom.org/projects/python-markdown/">Python-Markdown</a>.</p>
<p>My mainly non-technical users seemed largely ambivalent to the change from
BBCode to Markdown. This was probably because I gave them a nice Javascript editor
(<a class="reference external" href="http://markitup.jaysalvat.com/home/">MarkItUp!</a>) which inserted the correct markup for them.</p>
<p>However, shortly after launch, one particular feature of Markdown really riled up
some users: the default line break behavior. In strict Markdown, to create a new
paragraph, you must insert a blank line between paragraphs. Hard returns (newlines)
are simply ignored, just like they are in HTML. You can, however, force a break by
ending a line with two blank spaces. This isn't very intuitive, unlike the rest of
Markdown.</p>
<p>Now I agree the default behavior is useful if you are creating an online document, like a blog post.
However, non-technical users really didn't understand this behavior at all in the context
of a forum post. For example, many of my users post radio-show playlists, formatted with
one song per line. When such a playlist was pasted into a forum post, Markdown made it
all one giant run-together paragraph. This did not please my users. Arguably, they should
have used a Markdown list. But it became clear teaching people the new syntax wasn't
going to work, especially when it used to work just fine in BBCode and they had created
their playlists in the same way for several years.</p>
<p>It turns out I am not alone in my observations (or on the receiving end of user wrath). Other,
much larger sites, like <a class="reference external" href="http://blog.stackoverflow.com/2009/10/markdown-one-year-later/">StackOverflow</a> and <a class="reference external" href="http://github.github.com/github-flavored-markdown/">GitHub</a>, have altered their Markdown parsers
to treat newlines as hard breaks. How can this be done with Python-Markdown?</p>
<p>It turns out this is really easy. Python-Markdown was designed with user customization
in mind by offering an extension facility. The <a class="reference external" href="http://www.freewisdom.org/projects/python-markdown/Writing_Extensions">extension documentation</a> is good,
and you can find extension writing help on the friendly <a class="reference external" href="http://lists.sourceforge.net/lists/listinfo/python-markdown-discuss">mailing list</a>.</p>
<p>Here is a simple extension for Python-Markdown that turns newlines into HTML &lt;br /&gt; tags.</p>
<div class="highlight"><pre><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">A python-markdown extension to treat newlines as hard breaks; like</span>
<span class="sd">StackOverflow and GitHub flavored Markdown do.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">markdown</span>


<span class="n">BR_RE</span> <span class="o">=</span> <span class="s">r&#39;\n&#39;</span>

<span class="k">class</span> <span class="nc">Nl2BrExtension</span><span class="p">(</span><span class="n">markdown</span><span class="o">.</span><span class="n">Extension</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">extendMarkdown</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">md</span><span class="p">,</span> <span class="n">md_globals</span><span class="p">):</span>
        <span class="n">br_tag</span> <span class="o">=</span> <span class="n">markdown</span><span class="o">.</span><span class="n">inlinepatterns</span><span class="o">.</span><span class="n">SubstituteTagPattern</span><span class="p">(</span><span class="n">BR_RE</span><span class="p">,</span> <span class="s">&#39;br&#39;</span><span class="p">)</span>
        <span class="n">md</span><span class="o">.</span><span class="n">inlinePatterns</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s">&#39;nl&#39;</span><span class="p">,</span> <span class="n">br_tag</span><span class="p">,</span> <span class="s">&#39;_end&#39;</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">makeExtension</span><span class="p">(</span><span class="n">configs</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">Nl2BrExtension</span><span class="p">(</span><span class="n">configs</span><span class="p">)</span>
</pre></div>
<p>I saved this code in a file called <tt class="docutils literal">mdx_nl2br.py</tt> and put it on my <tt class="docutils literal">PYTHONPATH</tt>. You can then use
it in a Django template like this:</p>
<div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">markdown</span><span class="s2">:&quot;nl2br&quot;</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
<p>To use the extension in Python code, something like this should do the trick:</p>
<div class="highlight"><pre><span class="kn">import</span> <span class="nn">markdown</span>
<span class="n">md</span> <span class="o">=</span> <span class="n">markdown</span><span class="o">.</span><span class="n">Markdown</span><span class="p">(</span><span class="n">safe_mode</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">extensions</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;nl2br&#39;</span><span class="p">])</span>
<span class="n">converted_text</span> <span class="o">=</span> <span class="n">md</span><span class="o">.</span><span class="n">convert</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
</pre></div>
<p><strong>Update (June 21, 2011):</strong> This extension is now being distributed with
Python-Markdown! See <a class="reference external" href="https://github.com/waylan/Python-Markdown/issues/13">issue 13 on github</a> for the details. Thanks to Waylan
Limberg for the help in creating the extension and for including it with
Python-Markdown.</p>
</div>
]]></content:encoded>
    </item>
  </channel>
</rss>

