Peter's Blog

Redefining the Impossible

The VIM Outliner


I like the concept of outliners, previously I used Bonsai but today I've been giving The Vim Outliner a try. It works as a very simple markup language and then adds syntax highlighting to make headers more visible. For example:

This is a Heading.
   This is a Subheading.
        | This is some text within the subheading.
        | Blah Blah Blah Blah
   This is another subheading
        | More plain text.
        | Blah Blah Blah

gets displayed like this:

This is a Heading.
    This is a Subheading.
        | This is some text within the subheading.
        | Blah Blah Blah Blah
    This is another subheading
        | More plain text.
        | Blah Blah Blah

(notice how I can use Vim's built-in syntax highlighting to html conversion to generate this).

This makes outlining in VIM very easy and the syntax highlighting helps readability a great deal. TVL defines some useful shortcut keys such as - and + to fold and unfold sections easily. The outline text is stored in .otl files and Vim's file type system automatically applies the outlining features to these files when they are opened.

I find it interesting to compare what is effectively a new markup language with other markup languages I have encountered such as reStructuredText and indeed I am writing this using my Wilki Module which is another markup language. These have all used markup on the headers and plain body text but in TVO the body text is both marked with a '|' character and the headers are plain text. This would make TVO files tedious to type except Vim has been set up to automatically insert the indents and the '|' character so it is easy to type away as normal.

Markup languages are most useful when they can be easily converted into other formats. The Vim Outliner comes with an otl to html converter written in Ruby (python ripoff programming language named after a Kenny Rogers song about a prostitute). It is not hard to do simplistic conversion in python:

   1  #
   2  # otl2html.py: convert .otl files to html
   3  #
   4  import re
   5  
   6  oREIndent = re.compile( '^(\s*)(\|\s)?(.*)')
   7  
   8  nHeaderStack = [ 0]
   9  nTextIndent = None
  10  
  11  for strLine in open( r'c:\tmp\try2.otl').readlines():
  12    oMatch = oREIndent.match( strLine)
  13    if oMatch:
  14      strIndent = oMatch.group(1)
  15      strTextLeader = oMatch.group(2)
  16      strText = oMatch.group(3)
  17    else:
  18      strIndent = ''
  19      strTextLeader = ''
  20      strText = strLine
  21    if strTextLeader != None:
  22      #
  23      # This is a text block
  24      #
  25      if nTextIndent == None:
  26        nTextIndent = len( strTextLeader.expandtabs(4))
  27        print "<p>"
  28      else:
  29        strText = (strTextLeader + strText)[nTextIndent:]
  30      print strText
  31    else:
  32      #
  33      # Dealing with header
  34      #
  35  
  36      #
  37      # Terminate text block.
  38      #
  39      if nTextIndent != None:
  40        print "</p>"
  41        nTextIndent = None
  42  
  43      #
  44      # Lets assume it's me and a tab = 4 spaces
  45      #
  46      nIndent = len( strIndent.expandtabs(4))
  47      while nHeaderStack[-1] > nIndent:
  48        nHeaderStack.pop()
  49      if nHeaderStack[0] < nIndent:
  50        nHeaderStack.append( nIndent)
  51      print '<h%d>%s</h%d>' % (len(nHeaderStack), strText, len(nHeaderStack))

Add a comment

Have Your Say

I welcome constructive comments or questions but I reserve the right to delete any comments that displease me.

Who are you?

(Optional) If you enter an email address here I might email you back. Your email address will not be sold to spammers or shown anywhere

What do you have to say?