-
Notifications
You must be signed in to change notification settings - Fork 53
/
render.py
112 lines (89 loc) · 3.59 KB
/
render.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import sublime
import sublime_plugin
import webbrowser
import tempfile
import os
import re
import os.path
import sys
import subprocess
class RenderRstCommand(sublime_plugin.TextCommand):
TARGETS = ['html (pandoc)', 'html (rst2html)', 'pdf (pandoc)',
'pdf (rst2pdf)', 'odt (pandoc)', 'odt (rst2odt)',
'docx (pandoc)']
def __init__(self, view):
sublime_plugin.TextCommand.__init__(self, view)
path_pieces = os.environ['PATH'].split(":")
new_path = []
def append_path(bit):
if bit != "" and bit not in new_path:
new_path.append(bit)
for bit in path_pieces:
append_path(bit)
settings = sublime.load_settings('sublime-rst-completion.sublime-settings');
for bit in settings.get('command_path', []):
append_path(bit)
os.environ['PATH'] = ":".join(new_path)
def is_enabled(self):
return True
def is_visible(self):
return True
def run(self, edit):
if not hasattr(self, 'targets'):
self.targets = RenderRstCommand.TARGETS[:]
self.view.window().show_quick_panel(self.targets, self.convert,
sublime.MONOSPACE_FONT)
def convert(self, target_index):
if target_index == -1:
# canceled
return
target, tool = re.match(r"(.*) \((.*)\)",
self.targets[target_index]).groups()
# update targets: last used turns the first option
self.targets.insert(0, self.targets.pop(target_index))
encoding = self.view.encoding()
if encoding == 'Undefined':
encoding = 'UTF-8'
elif encoding == 'Western (Windows 1252)':
encoding = 'windows-1252'
contents = self.view.substr(sublime.Region(0, self.view.size()))
contents = contents.encode(encoding)
file_name = self.view.file_name()
if file_name:
os.chdir(os.path.dirname(file_name))
# write buffer to temporary file
# This is useful because it means we don't need to save the buffer
with tempfile.NamedTemporaryFile(delete=False,
suffix=".rst") as tmp_rst:
tmp_rst.write(contents)
# output file...
suffix = "." + target
with tempfile.NamedTemporaryFile(delete=False,
suffix=suffix) as output:
output.close()
output_name = output.name
self.run_tool(tmp_rst.name, output_name, tool)
self.open_result(output_name, target)
def run_tool(self, infile, outfile, tool):
if tool in ("pandoc", "rst2pdf"):
cmd = [tool, infile, "-o", outfile]
else:
cmd = ["%s.py" % tool, infile, outfile]
try:
if sys.platform == "win32":
subprocess.call(cmd, shell=True)
else:
subprocess.call(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
except Exception as e:
sublime.error_message("Fail to generate output.\n{0}".format(e))
def open_result(self, outfile, target):
if target == "html":
webbrowser.open_new_tab('file://' + outfile)
elif sys.platform == "win32":
os.startfile(outfile)
elif "mac" in sys.platform or "darwin" in sys.platform:
os.system("open %s" % outfile)
print(outfile)
elif "posix" in sys.platform or "linux" in sys.platform:
os.system("xdg-open %s" % outfile)