3

How could I convert a VimOutliner file to Markdown? In other words, I how to turn tab-indeted outlines like this...

Heading 1
    Heading 2
            Heading 3
            : Body text is separated by colons.
            : Another line of body text.
    Heading 4

...into hash-style headings separated by an empty line like this:

# Heading 1

## Heading 2

### Heading 3

Body text.

## Heading 4

I have tried defining a macro but I'm fairly new to Vim (and not a coder) so I've been unsuccessful so far. Thanks for any help!

(PS -- As for Markdown, I do know about the awesome VOoM plugin but I still prefer doing initial outlines for documents with no hash-characters in sight. Plus, I also like the way VimOutliner highlights different levels of headings.)

martz
  • 839
  • 6
  • 21

1 Answers1

4

Place this functions in your vimrc and just use :call VO2MD() or :call MD2VO() as needed.

function! VO2MD()
  let lines = []
  let was_body = 0
  for line in getline(1,'$')
    if line =~ '^\t*[^:\t]'
      let indent_level = len(matchstr(line, '^\t*'))
      if was_body " <= remove this line to have body lines separated
        call add(lines, '')
      endif " <= remove this line to have body lines separated
      call add(lines, substitute(line, '^\(\t*\)\([^:\t].*\)', '\=repeat("#", indent_level + 1)." ".submatch(2)', ''))
      call add(lines, '')
      let was_body = 0
    else
      call add(lines, substitute(line, '^\t*: ', '', ''))
      let was_body = 1
    endif
  endfor
  silent %d _
  call setline(1, lines)
endfunction

function! MD2VO()
  let lines = []
  for line in getline(1,'$')
    if line =~ '^\s*$'
      continue
    endif
    if line =~ '^#\+'
      let indent_level = len(matchstr(line, '^#\+')) - 1
      call add(lines, substitute(line, '^#\(#*\) ', repeat("\<Tab>", indent_level), ''))
    else
      call add(lines, substitute(line, '^', repeat("\<Tab>", indent_level) . ': ', ''))
    endif
  endfor
  silent %d _
  call setline(1, lines)
endfunction
Raimondi
  • 5,163
  • 31
  • 39
  • Works nicely, thank you! Meanwhile I already figured out the second part of your suggested regex myself, too. But the first part still remained a headache. – martz Mar 24 '12 at 12:22
  • One thing I forgot: how do I convert the same thing **back** from Markdown to VimOutliner? I'm only getting to know VIM's regex so I can't seem to get it entirely right. Thanks! – martz Mar 24 '12 at 12:53
  • I re-wrote it to make it easier to restore the correct indentation for body lines. Also, it simpler to just call a function instead of running multiple commands. – Raimondi Mar 24 '12 at 19:32
  • This is excellent, thank you very much for your effort! Enjoying my smoothened workflow right now. :) – martz Mar 26 '12 at 17:34