1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# encoding: utf-8 |
3
|
|
|
|
4
|
|
|
import vim |
5
|
|
|
import webbrowser |
6
|
|
|
import os |
7
|
|
|
import platform |
8
|
|
|
import mistune |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
def markdown(text, escape=True): |
12
|
|
|
html = mistune.markdown(text, escape) |
13
|
|
|
return html |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
def markdownPreviewWithDefaultCodeStyle(): |
17
|
|
|
cssName = vim.eval("a:args1") |
18
|
|
|
currentpath = os.getcwd() |
19
|
|
|
|
20
|
|
|
content = getHead(cssName) |
21
|
|
|
content += getBuff() |
22
|
|
|
content += getBody() |
23
|
|
|
|
24
|
|
|
file = open(os.path.join(currentpath, 'tmp.html'), 'w') |
25
|
|
|
file.write(content) |
26
|
|
|
file.close() |
27
|
|
|
|
28
|
|
|
url = 'file:///' + currentpath + '/tmp.html' |
29
|
|
|
webbrowser.open(url) |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
def markdownPreviewWithCustomCodeStyle(): |
33
|
|
|
cssName = vim.eval("a:args1") |
34
|
|
|
codeName = vim.eval("a:args2") |
35
|
|
|
currentpath = os.getcwd() |
36
|
|
|
|
37
|
|
|
content = getHead(cssName, codeName) |
38
|
|
|
content += getBuff() |
39
|
|
|
content += getBody() |
40
|
|
|
|
41
|
|
|
file = open(os.path.join(currentpath, 'tmp.html'), 'w') |
42
|
|
|
file.write(content) |
43
|
|
|
file.close() |
44
|
|
|
|
45
|
|
|
url = 'file:///' + currentpath + '/tmp.html' |
46
|
|
|
webbrowser.open(url) |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
def getBuff(): |
50
|
|
|
buff = '' |
51
|
|
|
for line in vim.current.buffer: |
52
|
|
|
buff += line + '\n' |
53
|
|
|
buff = markdown(buff, False) |
54
|
|
|
return buff |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
def getHead(cssStyle='github', codeStyle='default'): |
58
|
|
|
if vim.eval("exists('g:PluginDir')") == '1': |
59
|
|
|
resDir = os.path.join(vim.eval('g:PluginDir'), 'resources') |
60
|
|
|
else: |
61
|
|
|
resDir = os.path.join('markdown-preview.vim', 'resources') |
62
|
|
|
resDir = os.path.join('bundle', resDir) |
63
|
|
|
if platform.system() == 'Windows': |
64
|
|
|
resDir = os.path.join(vim.eval('$HOME'), '.vim', resDir) |
65
|
|
|
elif vim.eval("has('nvim')") == '1': |
66
|
|
|
resDir = os.path.join(vim.eval('$HOME'), '.nvim', resDir) |
67
|
|
|
else: |
68
|
|
|
resDir = os.path.join(vim.eval('$HOME'), '.vim', resDir) |
69
|
|
|
# print(resDir) |
70
|
|
|
|
71
|
|
|
if cssStyle == '': |
72
|
|
|
cssStyle = 'github' |
73
|
|
|
if codeStyle == '': |
74
|
|
|
codeStyle = 'default' |
75
|
|
|
|
76
|
|
|
content = "<html>\n" |
77
|
|
|
content += '<meta charset="UTF-8" />\n' |
78
|
|
|
content += '<head>' |
79
|
|
|
content += '<style type="text/css"> svg { height:auto !important; } </style>\n' |
80
|
|
|
content += '<link rel="stylesheet" href="' + resDir + \ |
81
|
|
|
'/code-styles/' + codeStyle.lower() + '.css">\n' |
82
|
|
|
content += '<link rel="stylesheet" href="' + \ |
83
|
|
|
resDir + '/chart-styles/chart.css">\n' |
84
|
|
|
content += '<link href="' + resDir + '/' + \ |
85
|
|
|
cssStyle.lower() + '.css" media="all" rel="stylesheet"/>\n' |
86
|
|
|
content += '<script src="' + resDir + '/js/highlight.min.js"></script>\n' |
87
|
|
|
content += '<script src="' + resDir + '/js/highlight.pack.js"></script>\n' |
88
|
|
|
content += '<script src="' + resDir + '/js/jquery-1.11.3.min.js"></script>\n' |
89
|
|
|
content += '<script src="' + resDir + '/js/mermaid.js"></script>\n' |
90
|
|
|
content += '<script> hljs.initHighlightingOnLoad(); window.onload = function() { mermaid.init({noteMargin: 5}, ".lang-chart"); }; </script>\n' |
91
|
|
|
content += '</head>\n<body id="content">' |
92
|
|
|
return content |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
def getBody(): |
96
|
|
|
return "</body></html>\r\n\r\n\r\n\r\n" |
97
|
|
|
|