forked from carsonoid/sublime_man_page_support
-
Notifications
You must be signed in to change notification settings - Fork 1
/
new_page.py
59 lines (52 loc) · 1.98 KB
/
new_page.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
import sublime, sublime_plugin
import os, time
import subprocess
from sublime_lib.path import root_at_packages, get_package_name
class ManPagePreview(sublime_plugin.WindowCommand):
def run(self):
# exit if file is dirty, we can't run a man command against a file that doesn't exist
if self.window.active_view().is_dirty():
o = self.window.get_output_panel("manfail")
o.run_command("insert_snippet", {"contents": "Unable to preview unsaved file."})
self.window.run_command("show_panel", {"panel": "output.manfail"})
return
# process document with groff
curpath = self.window.active_view().file_name()
c = subprocess.Popen(["groff", "-Tascii", "-man", curpath], stdout=subprocess.PIPE)
output, err = c.communicate()
# run groff output through col to clean it up
col = subprocess.Popen(["col", "-bx"], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
cleanout, err = col.communicate(output)
# write clean output to new window
v = self.window.new_file()
v.settings().set('default_dir', root_at_packages('User'))
v.set_syntax_file('Packages/Man Page Support/man-preview.tmLanguage')
e = v.begin_edit()
p = v.text_point(0,0)
v.insert(e, p, cleanout)
v.end_edit(e)
class ManPageNewCommand(sublime_plugin.WindowCommand):
def run(self):
v = self.window.new_file()
v.settings().set('default_dir', root_at_packages('User'))
v.set_syntax_file('Packages/Man Page Support/man-groff.tmLanguage')
template = """.\\\" Manpage for ${1:<COMMAND>}.
.\\\" Contact ${2:<AUTHOR_EMAIL>} to correct errors or typos.
.TH man 8 "%s" "1.0" "${1:<COMMAND>}"
.SH NAME
${1:<COMMAND>}
.SH SYNOPSIS
.SY
${1:<COMMAND>}
.YS
.SH DESCRIPTION
${1:<COMMAND>}
.SH BUGS
No known bugs.
.SH SEE ALSO
.SH AUTHOR
.MT ${2:<AUTHOR_EMAIL>}
${3:<AUTHOR_NAME>}
.ME
""" %(time.strftime("%B %Y"))
v.run_command("insert_snippet", {"contents": template})