regex - 如何从 VimOutliner 转换为 Markdown?

标签 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 突出显示不同级别的方式标题。)

最佳答案

将这个函数放在你的 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

关于regex - 如何从 VimOutliner 转换为 Markdown?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9840644/

相关文章:

html - 如何在 Cocoa 中将 RTF 文本转换为 Markdown 语法的纯文本?

javascript - 强制 WMD 使用内置预处理器

html - 使用 Pandoc 将 Markdown 链接转换为 HTML

java - 正则表达式在 Java 中未按预期工作

javascript - 使用正则表达式将字符串与数组匹配

regex -\1 如何在 perl 正则表达式中工作?

java - 如何用maven编译单个文件?

javascript - Javascript 的 URL 解析问题

vim - 在错误的 vim 窗口中忽略某些命令

ubuntu - vim 8.0 e353 : nothing in register "