forked from SublimeText-Markdown/MarkdownEditing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_from_template.py
67 lines (49 loc) · 2.31 KB
/
prepare_from_template.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
import sublime, sublime_plugin
import sys
import os.path
from string import Template
DEFAULT_PAGE_TEMPLATE = "templates/PageTemplate.md"
PRESET_TEMPLATE_TEXT = "# $title\n\n"
class PrepareFromTemplateCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
"""Prepare a new page content from a named template triggered from run command 'prepare_from_template'.
:Example:
view.run_command('prepare_from_template', {
'title': pagename,
'template': 'default_page'
})
:param self: This command instance
:param edit: The sublime edit instance
:param args: The command arguments including 'title' and 'template'
"""
print("Running PrepareFromTemplateCommand")
template_name = args['template']
print("Creating new page from template: ", template_name)
text = self.generate_from_template(template_name, args)
self.view.insert(edit, 0, text)
def generate_from_template(self, template_name, args):
"""Generate the text using the template"""
template_text = self.retrieve_template_text(template_name)
template = Template(template_text)
return template.substitute(args)
def retrieve_template_text(self, template_name):
"""Retrieve the template text.
The setting 'mde.wikilinks.templates' may be configured with a filename for
the template. This file (if it exists) will be loaded otherwise the preset
template will be used
"""
template = self.view.settings().get("mde.wikilinks.templates", DEFAULT_PAGE_TEMPLATE)
if not os.path.isfile(template):
current_file = self.view.file_name()
current_dir = os.path.dirname(current_file)
template = os.path.join(current_dir, template)
if os.path.isfile(template):
print("Using template:", template)
try:
with open(template, 'rt') as f:
return f.read()
except:
print("Unable to read template:", sys.exc_info()[0])
# Unable to load template so using preset template
print("Template:", template, "not found. Using preset.")
return PRESET_TEMPLATE_TEXT