Vim :TOhtml

File this under New To Me. The Vim text editor includes a handy command to convert code to HTML.

The command, unsurprisingly, is: :TOhtml

This generates a formatted HTML file which looks like this:

And if I paste this code straight into this post (with minimal editing to make the colours fit a little better with the blog theme), I am reminded the Python style guide recommends a maximum line length of 79 characters.

<style type="text/css">
.lnr { color: #007700; }
.Identifier { color: #00ffff; }
.Statement { color: #ffff00; }
.PreProc { color: #ff40ff; }
.Constant { color: #ff6060; }
.Comment { color: #8080ff; }
<span class="lnr"> 1 </span><span class="Comment">#! /usr/bin/python</span>
<span class="lnr"> 2 </span><span class="Constant">&quot;&quot;&quot; XKComplete: A date reversing completeness counter inspired by XKCD</span>
<span class="lnr"> 3 </span><span class="Constant"><a href=""></a></span>
<span class="lnr"> 4 </span>
<span class="lnr"> 5 </span><span class="Constant">Copyright (C) 2012 Paul Pritchard</span>
<span class="lnr"> 6 </span>
<span class="lnr"> 7 </span><span class="Constant">This program is free software; you can redistribute it and/or modify</span>
<span class="lnr"> 8 </span><span class="Constant">it under the terms of the GNU General Public License as published by</span>
<span class="lnr"> 9 </span><span class="Constant">the Free Software Foundation; either version 2 of the License, or</span>
<span class="lnr">10 </span><span class="Constant">(at your option) any later version.</span>
<span class="lnr">11 </span>
<span class="lnr">12 </span><span class="Constant">This program is distributed in the hope that it will be useful,</span>
<span class="lnr">13 </span><span class="Constant">but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="lnr">14 </span><span class="Constant">MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="lnr">15 </span><span class="Constant">GNU General Public License for more details.</span>
<span class="lnr">16 </span>
<span class="lnr">17 </span><span class="Constant">You should have received a copy of the GNU General Public License</span>
<span class="lnr">18 </span><span class="Constant">along with this program. If not, see &lt;<a href=""></a>&gt;.&quot;&quot;&quot;</span>
<span class="lnr">19 </span>
<span class="lnr">20 </span><span class="PreProc">from</span> __future__ <span class="PreProc">import</span> division
<span class="lnr">21 </span><span class="PreProc">import</span> datetime
<span class="lnr">22 </span><span class="PreProc">import</span> math
<span class="lnr">23 </span>
<span class="lnr">24 </span><span class="Statement">def</span> <span class="Identifier">xkcdate</span>(percentage_complete):
<span class="lnr">25 </span>    <span class="Constant">&quot;&quot;&quot; Returns the percetange completed, either as a date or a year&quot;&quot;&quot;</span>
<span class="lnr">26 </span>    today =
<span class="lnr">27 </span>    completion = percentage_complete / <span class="Constant">100</span>
<span class="lnr">28 </span>    days = <span class="Identifier">float</span>(<span class="Constant">365.25</span> * (math.exp(<span class="Constant">3</span>+<span class="Constant">20.3444</span>*(completion)**<span class="Constant">3</span>) - math.exp(<span class="Constant">3</span>)))
<span class="lnr">29 </span>    <span class="Statement">try</span>:
<span class="lnr">30 </span>        date = (today - datetime.timedelta(days)).strftime(<span class="Constant">&quot;%A, %d %B %Y&quot;</span>)
<span class="lnr">31 </span>    <span class="Statement">except</span>:
<span class="lnr">32 </span>        dateval = - <span class="Identifier">int</span>(days / <span class="Constant">365.25</span>)
<span class="lnr">33 </span>        <span class="Statement">if</span> dateval &lt; <span class="Constant">0</span>:
<span class="lnr">34 </span>            dateval = <span class="Constant">0</span> - dateval
<span class="lnr">35 </span>            adbc = <span class="Constant">&quot; BC&quot;</span>
<span class="lnr">36 </span>        <span class="Statement">else</span>:
<span class="lnr">37 </span>            adbc = <span class="Constant">&quot; AD&quot;</span>
<span class="lnr">38 </span>        date = <span class="Identifier">str</span>(dateval) + adbc
<span class="lnr">39 </span>
<span class="lnr">40 </span>    <span class="Statement">return</span> date
<span class="lnr">41 </span>
<span class="lnr">42 </span><span class="Statement">if</span> __name__ == <span class="Constant">&quot;__main__&quot;</span>:
<span class="lnr">43 </span>    percentage_complete = <span class="Constant">0</span>
<span class="lnr">44 </span>    <span class="Statement">while</span> percentage_complete &lt; <span class="Constant">100</span>:
<span class="lnr">45 </span>        percentage_complete = <span class="Identifier">input</span>(<span class="Constant">&quot;Enter percentage complete: &quot;</span>)
<span class="lnr">46 </span>        <span class="Identifier">print</span> xkcdate(percentage_complete)

With thanks to @jk

Flattr this!

Leave a Reply

Your email address will not be published. Required fields are marked *