vim2html.rb生成vim中文手册html版本

  1. 首先从http://vimcdoc.sourceforge.net/下载中文帮助手册,下载链接为:
    https://sourceforge.net/projects/vimcdoc/files/vimcdoc/vimcdoc-1.9.0.tar.gz/download
  2. 解压后进入其doc目录,然后在目录下面创建vim2html.rbvim.css这2个文件,文件内容分别如下源码。
  3. doc目录下直接运行ruby vim2html.rb即可生成每份cnx文件对应的html版本。

运行命令结束后生成的代码示例可参考这个页面

vim2html.rb

#!/usr/bin/env ruby# converts vim chinese documentation to simple html# author: yuweijun@live.com# date: Fri, 18 Dec 2015 20:34:44 +0800def mylength str    length = str.length    str.unpack("U*").each do |char|      length = length + 1 if char > 255    end    lengthenddef myexpand line  tabstop = 8  line = line.gsub(/([^\t]*)(\t+)/) do |match|    $1 + (' ' * (tabstop * $2.length - (mylength($1) % tabstop)))  end  lineenddef mark_anchors anchors, file  filename = file.sub(/\.cnx/, '')  outfile = filename + '.html'  IO.foreach(file) do |line|    line.scan(/\*([\w.\-]+?)\*/) do |m|      anchors[$1] = outfile + '#' + $1    end  endenddef vim2html anchors, file  filename = file.sub(/\.cnx/, '')  outfile = filename + '.html'  lines = []  IO.foreach(file) do |line|    line = myexpand(line)    if ( /^=+\s*$/ =~ line )      line = "\n</div>\n\n<hr class=\"doubleline\" />\n<div class=\"pre\">\n";    elsif ( /^\s*-+\s*$/ =~ line )      line = "\n</div>\n\n<hr class=\"singleline\" />\n<div class=\"pre\">\n";    elsif ( /^(>)$/ =~ line )      line = line.sub($1, "\n\n")    elsif ( /^(<)$/ =~ line )      line = line.sub($1, "\n\n")    else      # processing < and > for html tags      line = line.sub(/<([.='\s"\/\-\w]+?)>/, '&lt;\1&gt;')      line = line.sub(/(\s>)$/, "\n").sub(/^<(\s{3,})/, '\1')      line = line.gsub(/>/, '&gt;').gsub(/</, '&lt;')    end    lines << line  end  document = lines.join.gsub(/\n{3,}/, "\n\n")  document = document.gsub(/(\s+vim\w*\s+?)/, '<code class="vim">\1</code>')  document = document.gsub(/(\s+:[\w.,!^\/\[\]\(\)$]+)/, '<code class="help">\1</code>')  document = document.gsub(/\*([\w.\-]*?)\*/, '*<span id="\1" class="anchor">\1</span>*')  document = document.gsub(/\|(\w\S+?)\|/) do |m|    anchor = anchors[$1] || '#'    "|<a href=\"#{anchor}\">#{$1}</a>|"  end  html = <<EOF<!DOCTYPE html><html><head>    <meta charset="utf-8">    <title>vim中文手册</title>    <meta name="description" content="vim7.4中文帮助文档">    <meta name="viewport" content="width=device-width">    <link rel="stylesheet" href="vim.css" type="text/css"></head><body>    <header class="site-header">        <div class="wrap">            <div class="site-title"><a href="/vim-zh/usr_toc.html">Vim中文手册</a></div>            <div class="site-description">{"type":"programming"}</div>        </div>    </header>    <div class="page-content">        <div class="wrap">            <h2 class="vim-chapter">Vim documentation: #{filename}</h2>            <div class="vim-document">                <div class="pre">#{document}                </div>            </div>        </div>    </div>    <footer class="site-footer">        <div class="wrap">            <div class="footer-content">              <i>Generated by vim2html.rb on </i>              <a href="http://yuweijun.github.io/vim-zh/usr_toc.html">yuweijun.github.io</a>            </div>        </div>    </footer></body></html>EOF  File.open(outfile, "w") do |io|    io.puts html  endendanchors = {}Dir.glob('*.cnx').each_with_index do |file, index|  mark_anchors(anchors, file)endDir.glob('*.cnx').each_with_index do |file, index|  puts "#{index + 1}. processing #{file} ...\n";  vim2html(anchors, file)endputs "done.\n"

vim.css

* {    -moz-box-sizing: border-box;    -webkit-box-sizing: border-box;    box-sizing: border-box;}html, body {    height: 100%;}body {    margin: 0;    font-family: Helvetica, Arial, sans-serif;    background-color: #fdfdfd;    font-size: 16px;    line-height: 1.3;}a {    color: #2a7ae2;    text-decoration: none;}a:hover {    text-decoration: underline;}a:visited {    color: #205caa;}.pre, .vim-code {    font-family: monospace;    white-space: pre-wrap;    word-break: break-all;}.vim-code {  margin: 20px auto;}.vim-key {    font-family: monospace;    padding: 1px 4px;    margin: 0 8px;    border: 1px solid #d5d5e9;    border-radius: 2px;    font-size: 85%;    overflow: auto;    white-space: pre-wrap;    background-color: rgba(0, 0, 0, 0.04);}code {    font-family: monospace;}.doubleline {    border: none 0;    border-top: 3px double #eee;    width: 100%;    height: 3px;    margin: 10px auto 0 0;}.singleline {    width: 100%;    margin: 0px auto 0 0;    border-top: 1px solid #eee;}.site-header {    border-top: 5px solid #333;    border-bottom: 1px solid #e8e8e8;    background-color: transparent;    font-size: 26px;    width: 100%;}.wrap {    max-width: 840px;    padding: 0 12px;    margin: 0 auto;    zoom: 1;}.wrap:before, .wrap:after {    content: "";    display: table;}.wrap:after {    clear: both;}.site-header a {    color: #333;    text-decoration: none;}.site-title {    color: #333;    letter-spacing: -1px;    float: left;    margin: 5px 0;}.site-description {    float: left;    line-height: 30px;    margin-left: 20px;    font-size: 50%;    color: #999;}.page-content {    max-width: 840px;    margin: 0 auto;    padding: 10px 12px 10px;    background-color: white;    zoom: 1;}.vim-chapter {    margin: 0 auto;    padding: 10px 0;    text-align: center;}.anchor {    font-weight: bolder;}.vim, .help {    color: rgb(0, 136, 136);}.site-footer {    border-top: 1px solid #e8e8e8;    width: 100%;}.site-footer .footer-content {    float: right;    text-align: right;    max-width: 840px;    margin: 10px auto;    font-size: 50%;}

References

  1. http://vimdoc.sourceforge.net/htmldoc/usr_toc.html
  2. Vim中文手册
  3. Vim documentation
  4. Vim documentation single html page