-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarkdownExport.py
74 lines (61 loc) · 2.9 KB
/
MarkdownExport.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
import os
import sublime
import sublime_plugin
import webbrowser
import codecs
import re
from .markdown import markdown
from .markdown.extensions.tables import TableExtension
from .markdown.extensions.toc import TocExtension
def save_utf8(filename, text):
with codecs.open(filename, 'w', encoding='utf-8')as f:
f.write(text)
def load_utf8(filename):
with codecs.open(filename, 'r', encoding='utf-8') as f:
return f.read()
def remove_values_from_list(the_list, val):
return [value for value in the_list if value != val]
def process_markdown(self, print=False):
package_path = sublime.packages_path() + "/MarkdownExport/"
settings = sublime.load_settings('MarkdownExport.sublime-settings')
template_inuse = settings.get("template")
md_path = self.view.window().active_view().file_name()
if not os.path.splitext(md_path)[1] in [".md", ".MD", ".markdown", ".Markdown"]:
sublime.error_message(md_path + "\nis not a Markdown file\n\n")
return 1
html_template = load_utf8(package_path + "templates/" + settings.get("templates").get(template_inuse).get("html"))
template_load = re.findall('<%- (.*?) -%>', html_template, re.DOTALL)
template_insert = re.findall('<%= (.*?) =%>', html_template, re.DOTALL)
md = self.view.substr(sublime.Region(0, self.view.size()))
out = html_template
if 'EXTRA' in template_insert:
if print:
out = out.replace("<%= EXTRA =%>", "<script type='text/javascript'>window.onload = function() { window.print(); }</script>")
else:
out = out.replace("<%= EXTRA =%>", "")
if 'TITLE' in template_insert:
out = out.replace("<%= TITLE =%>", os.path.splitext(os.path.basename(md_path))[0])
if 'HTML' in template_insert:
out = out.replace("<%= HTML =%>", markdown(md, extensions=[TableExtension(), TocExtension()]))
while not template_load == []:
for item in template_load:
item_file_name = settings.get("templates").get(template_inuse).get(item)
item_file = load_utf8(package_path + "templates/" + item_file_name)
out = out.replace("<%- " + item + " -%>", item_file)
template_load = remove_values_from_list(template_load, item)
template_load.extend(re.findall('<%- (.*?) -%>', out, re.DOTALL))
out_path = os.path.splitext(md_path)[0] + '.html'
save_utf8(out_path, out)
webbrowser.open("file://" + out_path)
class markdown_export_command(sublime_plugin.TextCommand):
def run(self, edit):
try:
process_markdown(self)
except Exception as e:
sublime.error_message("Error in MarkdownExport package:\n\n" + str(e))
class markdown_print_command(sublime_plugin.TextCommand):
def run(self, edit):
try:
process_markdown(self, print=True)
except Exception as e:
sublime.error_message("Error in MarkdownExport package:\n\n" + str(e))