Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up site generation #6

Merged
merged 6 commits into from Jul 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions .gitignore
@@ -0,0 +1,10 @@
# Automatic backup files
*~

# Desktop preferences
.Desktop
.DS_Store

.idea
node_modules
vimhl.js.map
11 changes: 10 additions & 1 deletion package.json
Expand Up @@ -19,13 +19,22 @@
"email": "ppwwyyxxc@gmail.com",
"url": "http://ppwwyyxx.com"
},
"contributors": [
{
"name": "Stuart McCulloch Anderson",
"url": "http://nxfifteen.me.uk",
"email": "stuart@nxfifteen.me.uk"
}
],
"license": "GPLv2",
"engines": {
"hexo": ">= 1.0.0"
},
"dependencies": {
"cheerio": "*",
"crypto": "0.0.3",
"temp": "*",
"cheerio": "*"
"wrench": "^1.5.8"
},
"readme": "## Introduction\n\nThis is a [hexo](https://github.com/tommy351/hexo)\ntag plugin which allows you to use vim syntax highlight to highlight code inside markdown.\n\nHexo is a static blogging system written in Node.js, it uses [highlight.js](http://softwaremaniacs.org/soft/highlight/en/)\nby default to render code.\nBut lots of languages are not supported by highlight.js (but always supported by vim with proper plugins).\nIn that case, you can use this instead.\n\n## Installation\n\nYou need to have vim properly installed and configured, since this program will directly call vim.\n\n<!--\n -Also, you should have [hexo-renderer-coffeescript](https://npmjs.org/package/hexo-renderer-coffeescript) installed.\n -->\n\nTo install, run the following command in the root directory of hexo:\n```\nnpm install hexo-tag-vimhighlight --save\n```\n\nAnd add this plugin in your ``_config.yml``.\n\n## Usage\n\nSpecify the code filetype, and whether to use line number in your markdown source file, in\nthe common format of tag plugins:\n\n\t{% vimhl vim true %}\n\tif ! has(\"gui_running\") \" fix alt key under terminal\n\t\tfor i in range(48, 57) + range(65, 90) + range(97, 122)\n\t\t\texec \"set <A-\" . nr2char(i) . \">=\u001b\" . nr2char(i)\n\t\tendfor\n\tendif\n\t{% endvimhl %}\n\nBy default, line number will not be displayed, you can use ``{% vimhl vim %}`` for short.\n\nThis program will run vim to highlight the code, therefore the generating of pages\nwill probably take much longer time. (It seems that hexo doesn't support parallel rendering.)\n",
"readmeFilename": "README.md",
Expand Down
57 changes: 41 additions & 16 deletions vimhl.coffee
Expand Up @@ -6,6 +6,8 @@ execSync = require('child_process').execSync
fs = require 'fs'
temp = require 'temp'
cheerio = require 'cheerio'
crypto = require('crypto')
wrench = require('wrench')

formatFilter = (data) ->
# trim the extra empty line
Expand All @@ -24,28 +26,51 @@ formatFilter = (data) ->
return data

vimHighlight = (data, ft, useLineN) ->
if useLineN
lineOpt = ' +"let g:html_number_lines=1" '
cacheFileHash = crypto.createHash('md5');
cacheFileHash.update(data);
cacheFileHash.update(ft);
if (useLineN)
cacheFileHash.update("show line numbers");
else
lineOpt = ' +"let g:html_number_lines=0" '
cacheFileHash.update("no line numbers");

info = temp.openSync({suffix: '.' + ft})
fs.writeSync(info.fd, data)
fs.closeSync info.fd
cacheFileName = cacheFileHash.digest('hex') + '.html';

opt = ' +"let g:html_no_progress=1" +"let g:html_ignore_folding=1" +"let g:html_use_css=0" +"let g:html_pre_wrap=0" ' + lineOpt
execSync 'vim -X -i NONE -f ' + opt + ' +"TOhtml" -cwqa ' + info.path + ' > /dev/null 2>&1'
_storageTemplateFolder = __dirname + '/../../data/vimHighlight/';
if (!fs.existsSync(_storageTemplateFolder))
wrench.mkdirSyncRecursive(_storageTemplateFolder, '0777');

htmlPath = info.path + '.html'
result = fs.readFileSync htmlPath
result = String result
fs.unlink htmlPath, (err) ->
if (!fs.existsSync(_storageTemplateFolder + '/' + cacheFileName))
if useLineN
lineOpt = ' +"let g:html_number_lines=1" '
else
lineOpt = ' +"let g:html_number_lines=0" '

fs.unlink info.path, (err) ->
$ = cheerio.load result
info = temp.openSync({suffix: '.' + ft})
fs.writeSync(info.fd, data)
fs.closeSync info.fd

opt = ' +"let g:html_no_progress=1" +"let g:html_ignore_folding=1" +"let g:html_use_css=0" +"let g:html_pre_wrap=0" ' + lineOpt
execSync 'vim -X -i NONE -f ' + opt + ' +"TOhtml" -cwqa ' + info.path + ' > /dev/null 2>&1'

htmlPath = info.path + '.html'
result = fs.readFileSync htmlPath
result = String result
fs.unlink htmlPath, (err) ->

fs.unlink info.path, (err) ->
$ = cheerio.load result

ret = $('body')
ret = formatFilter ret.html()

cacheFileFd = fs.openSync(_storageTemplateFolder + '/' + cacheFileName, 'w', '0666');
fs.writeSync(cacheFileFd, ret);
fs.closeSync(cacheFileFd);
else
result = fs.readFileSync(_storageTemplateFolder + '/' + cacheFileName);
ret = String(result);

ret = $('body')
ret = formatFilter ret.html()
return ret

exports.vimHighlight = vimHighlight
Expand Down
67 changes: 47 additions & 20 deletions vimhl.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.