怎么把VimOutliner转换成Markdown?

时间:2012-03-23 14:10:13

标签: regex vim markdown outlining

如何将VimOutliner文件转换为Markdown? 换句话说,我如何像这样转换制表符的轮廓......

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

...进入哈希样式的标题,用空行分隔,如下所示:

# Heading 1

## Heading 2

### Heading 3

Body text.

## Heading 4

我尝试过定义一个宏,但我对Vim(而不是编码器)相当新,所以到目前为止我还没有成功。谢谢你的帮助!

(PS - 至于Markdown,我确实知道了很棒的VOoM插件,但我仍然更喜欢为没有哈希字符的文档做初始大纲。另外,我也喜欢VimOutliner强调不同的方式标题的级别。)

1 个答案:

答案 0 :(得分:4)

将此功能放在您的vimrc中,并根据需要使用:call VO2MD():call MD2VO()

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