-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkdoc.py
140 lines (113 loc) · 3.9 KB
/
mkdoc.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# deprecated, use mkdoc.js instead :)
#
# mkdoc.py
#
# Copyright 2013 Átila Camurça <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
import sys
import os
import shutil
import json
appdir, filename = os.path.split(os.path.abspath(__file__))
curdir = os.getcwd()
CONFIG_FILE = ".config.json"
def dir_is_empty():
return os.listdir(curdir) == []
def init():
sys.stdout.write("initializing project... ")
if dir_is_empty():
if len(sys.argv) > 2:
type = sys.argv[2]
else:
type = "beamer"
shutil.copy(appdir + "/" + type + ".tex", curdir + "/main.tex")
filename = curdir + "/content.md"
file = open(filename, 'w')
file.write("""<!-- your text here -->""")
file.close()
# config file
config = {"type": type}
data_string = json.dumps(config)
filename = "%s/%s" % (curdir, CONFIG_FILE)
file = open(filename, 'w')
file.write(data_string)
file.close()
print "done."
else:
print "[ERROR] directory is not empty."
def edit():
print 'editing project ...'
if len(sys.argv) > 2:
opt = sys.argv[2]
filename = 'main.tex'
if not opt in ('-t', '--template'): print '[ERROR] option %s not found.' % opt; return
else:
filename = 'content.md'
editor = raw_input('open with [blank for default]: ')
if not editor:
editor = 'xdg-open'
os.system("%s %s &" % (editor, filename))
def view():
json_data = open(CONFIG_FILE)
data = json.load(json_data)
print "compiling project ..."
os.system("pandoc -t %s content.md -o content.tex" % data["type"])
os.system("pdflatex -shell-escape -interaction=nonstopmode main.tex")
os.system("xdg-open main.pdf")
json_data.close()
def editor():
print "opening mkdoc-editor ... "
editor_path = "%s/editor/" % appdir
os.system("xdg-open http://localhost:9669")
os.system("cd %s && nodemon server.js %s" % (editor_path, curdir))
def help():
print """
mkdoc(1)
USAGE
mkdoc command [options]
COMMANDS
init - start a project in an empty directory
options: beamer|latex
default: beamer
editor - open a web editor
edit - open file to edit
options:
-t, --template - open template file to edit instead
view - generate file and open pdf
help - show this help
docs - show the documentation of mkdoc in the default browser
cleanup - remove log and aux files
"""
def docs():
url = "file://%s/docs/index.html" % appdir
os.system("xdg-open %s" % url)
def cleanup():
os.system("cd %s && ls *.aux *.log *.nav *.out *.snm *.toc" % curdir)
answer = raw_input("Confirm remove? _yes/[no]: ")
if answer in ('y', 'yes'):
os.system("cd %s && rm *.aux *.log *.nav *.out *.snm *.toc" % curdir)
if __name__ == '__main__':
opts = {'init': init, 'edit': edit, 'view': view, 'help': help,
'docs': docs, 'cleanup': cleanup, 'editor': editor}
if len(sys.argv) > 1:
command = sys.argv[1]
else:
command = None
opts.get(command, help)()