Completed
Push — master ( fc5951...3f456f )
by Dongxin
01:03
created

getBuff()   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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