forked from revolunet/sublimetext-markdown-preview
/
MarkdownPreview.py
1291 lines (1092 loc) · 47 KB
/
MarkdownPreview.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- encoding: UTF-8 -*-
import sublime
import sublime_plugin
import os
import sys
import traceback
import tempfile
import re
import json
import time
import codecs
import cgi
def is_ST3():
''' check if ST3 based on python version '''
return sys.version_info >= (3, 0)
if is_ST3():
from . import desktop
from . import yaml
from .markdown_settings import Settings
from .markdown_wrapper import StMarkdown as Markdown
from .lib.markdown_preview_lib.pygments.formatters import get_formatter_by_name
from .helper import INSTALLED_DIRECTORY
from urllib.request import urlopen, url2pathname, pathname2url
from urllib.parse import urlparse, urlunparse
from urllib.error import HTTPError, URLError
from urllib.parse import quote
from .markdown.extensions import codehilite
try:
PYGMENTS_AVAILABLE = codehilite.pygments
except:
PYGMENTS_AVAILABLE = False
def Request(url, data, headers):
''' Adapter for urllib2 used in ST2 '''
import urllib.request
return urllib.request.Request(url, data=data, headers=headers, method='POST')
unicode_str = str
else:
import desktop
import yaml
from markdown_settings import Settings
from markdown_wrapper import StMarkdown as Markdown
from lib.markdown_preview_lib.pygments.formatters import get_formatter_by_name
from helper import INSTALLED_DIRECTORY
from urllib2 import Request, urlopen, HTTPError, URLError
from urllib import quote, url2pathname, pathname2url
from urlparse import urlparse, urlunparse
import markdown.extensions.codehilite as codehilite
try:
PYGMENTS_AVAILABLE = codehilite.pygments
except:
PYGMENTS_AVAILABLE = False
unicode_str = unicode
_CANNOT_CONVERT = u'cannot convert markdown'
PATH_EXCLUDE = tuple(
[
'file://', 'https://', 'http://', '/', '#',
"data:image/jpeg;base64,", "data:image/png;base64,", "data:image/gif;base64,"
] + ['\\'] if sys.platform.startswith('win') else []
)
ABS_EXCLUDE = tuple(
[
'file://', '/'
] + (['\\'] if sys.platform.startswith('win') else [])
)
DEFAULT_EXT = [
"extra", "github", "toc",
"meta", "sane_lists", "smarty", "wikilinks",
"admonition"
]
def getTempMarkdownPreviewPath(view):
''' return a permanent full path of the temp markdown preview file '''
settings = sublime.load_settings('MarkdownPreview.sublime-settings')
tmp_filename = '%s.html' % view.id()
tmp_dir = tempfile.gettempdir()
if settings.get('path_tempfile'):
if os.path.isabs(settings.get('path_tempfile')): # absolute path or not
tmp_dir = settings.get('path_tempfile')
else:
tmp_dir = os.path.join(os.path.dirname(view.file_name()), settings.get('path_tempfile'))
if not os.path.isdir(tmp_dir): # create dir if not exsits
os.makedirs(tmp_dir)
tmp_fullpath = os.path.join(tmp_dir, tmp_filename)
return tmp_fullpath
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 load_resource(name):
''' return file contents for files within the package root folder '''
try:
if is_ST3():
return sublime.load_resource('Packages/MarkdownPreviewForAirTrack/{0}'.format(name))
else:
filename = os.path.join(sublime.packages_path(), INSTALLED_DIRECTORY, name)
return load_utf8(filename)
except:
print("Error while load_resource('%s')" % name)
traceback.print_exc()
return ''
def exists_resource(resource_file_path):
filename = os.path.join(os.path.dirname(sublime.packages_path()), resource_file_path)
return os.path.isfile(filename)
def new_view(window, text, scratch=False):
''' create a new view and paste text content
return the new view.
Optionally can be set as scratch.
'''
new_view = window.new_file()
if scratch:
new_view.set_scratch(True)
if is_ST3():
new_view.run_command('append', {
'characters': text,
})
else: # 2.x
new_edit = new_view.begin_edit()
new_view.insert(new_edit, 0, text)
new_view.end_edit(new_edit)
return new_view
def get_references(file_name, encoding="utf-8"):
""" Get footnote and general references from outside source """
text = ''
if file_name is not None:
if os.path.exists(file_name):
try:
with codecs.open(file_name, "r", encoding=encoding) as f:
text = f.read()
except:
print(traceback.format_exc())
else:
print("Could not find reference file %s!", file_name)
return text
def parse_url(url):
"""
Parse the url and
try to determine if the following is a file path or
(as we will call anything else) a url
"""
RE_PATH = re.compile(r'file|[A-Za-z]')
RE_WIN_DRIVE = re.compile(r"[A-Za-z]:?")
RE_URL = re.compile('(http|ftp)s?|data|mailto|tel|news')
is_url = False
is_absolute = False
scheme, netloc, path, params, query, fragment = urlparse(url)
if RE_URL.match(scheme):
# Clearly a url
is_url = True
elif scheme == '' and netloc == '' and path == '':
# Maybe just a url fragment
is_url = True
elif scheme == '' or RE_PATH.match(scheme):
if sublime.platform() == "windows":
if scheme == 'file' and RE_WIN_DRIVE.match(netloc):
# file://c:/path
path = netloc + path
netloc = ''
scheme = ''
is_absolute = True
elif RE_WIN_DRIVE.match(scheme):
# c:/path
path = '%s:%s' % (scheme, path)
scheme = ''
is_absolute = True
elif scheme != '' or netloc != '':
# Unknown url scheme
is_url = True
elif path.startswith('//'):
# //Some/Network/location
is_absolute = True
else:
if scheme not in ('', 'file') and netloc != '':
# A non-nix filepath or strange url
is_url = True
else:
# Check if nix path is absolute or not
if path.startswith('/'):
is_absolute = True
scheme = ''
return (scheme, netloc, path, params, query, fragment, is_url, is_absolute)
def repl_relative(m, base_path, relative_path):
""" Replace path with relative path """
RE_WIN_DRIVE_PATH = re.compile(r"(^(?P<drive>[A-Za-z]{1}):(?:\\|/))")
link = m.group(0)
try:
scheme, netloc, path, params, query, fragment, is_url, is_absolute = parse_url(m.group('path')[1:-1])
except:
# Parsing crashed an burned; no need to continue.
return link
if not is_url:
# Get the absolute path of the file or return
# if we can't resolve the path
path = url2pathname(path)
abs_path = None
if (not is_absolute):
# Convert current relative path to absolute
temp = os.path.normpath(os.path.join(base_path, path))
if os.path.exists(temp):
abs_path = temp.replace("\\", "/")
elif os.path.exists(path):
abs_path = path
if abs_path is not None:
convert = False
# Determine if we should convert the relative path
# (or see if we can realistically convert the path)
if (sublime.platform() == "windows"):
# Make sure basepath starts with same drive location as target
# If they don't match, we will stay with absolute path.
if (base_path.startswith('//') and base_path.startswith('//')):
convert = True
else:
base_drive = RE_WIN_DRIVE_PATH.match(base_path)
path_drive = RE_WIN_DRIVE_PATH.match(abs_path)
if (
(base_drive and path_drive) and
base_drive.group('drive').lower() == path_drive.group('drive').lower()
):
convert = True
else:
# OSX and Linux
convert = True
# Convert the path, url encode it, and format it as a link
if convert:
path = pathname2url(os.path.relpath(abs_path, relative_path).replace('\\', '/'))
else:
path = pathname2url(abs_path)
link = '%s"%s"' % (m.group('name'), urlunparse((scheme, netloc, path, params, query, fragment)))
return link
def repl_absolute(m, base_path):
""" Replace path with absolute path """
link = m.group(0)
scheme, netloc, path, params, query, fragment, is_url, is_absolute = parse_url(m.group('path')[1:-1])
path = url2pathname(path)
if (not is_absolute and not is_url):
temp = os.path.normpath(os.path.join(base_path, path))
if os.path.exists(temp):
path = pathname2url(temp.replace("\\", "/"))
link = '%s"%s"' % (m.group('name'), urlunparse((scheme, netloc, path, params, query, fragment)))
return link
class CriticDump(object):
RE_CRITIC = re.compile(
r'''
((?P<open>\{)
(?:
(?P<ins_open>\+{2})(?P<ins_text>.*?)(?P<ins_close>\+{2})
| (?P<del_open>\-{2})(?P<del_text>.*?)(?P<del_close>\-{2})
| (?P<mark_open>\={2})(?P<mark_text>.*?)(?P<mark_close>\={2})
| (?P<comment>(?P<com_open>\>{2})(?P<com_text>.*?)(?P<com_close>\<{2}))
| (?P<sub_open>\~{2})(?P<sub_del_text>.*?)(?P<sub_mid>\~\>)(?P<sub_ins_text>.*?)(?P<sub_close>\~{2})
)
(?P<close>\})|.)
''',
re.MULTILINE | re.DOTALL | re.VERBOSE
)
def process(self, m):
if self.accept:
if m.group('ins_open'):
return m.group('ins_text')
elif m.group('del_open'):
return ''
elif m.group('mark_open'):
return m.group('mark_text')
elif m.group('com_open'):
return ''
elif m.group('sub_open'):
return m.group('sub_ins_text')
else:
return m.group(0)
else:
if m.group('ins_open'):
return ''
elif m.group('del_open'):
return m.group('del_text')
elif m.group('mark_open'):
return m.group('mark_text')
elif m.group('com_open'):
return ''
elif m.group('sub_open'):
return m.group('sub_del_text')
else:
return m.group(0)
def dump(self, source, accept):
text = ''
self.accept = accept
for m in self.RE_CRITIC.finditer(source):
text += self.process(m)
return text
class MarkdownPreviewListener(sublime_plugin.EventListener):
''' auto update the output html if markdown file has already been converted once '''
def on_post_save(self, view):
settings = sublime.load_settings('MarkdownPreview.sublime-settings')
if settings.get('enable_autoreload', True):
filetypes = settings.get('markdown_filetypes')
if filetypes and view.file_name().endswith(tuple(filetypes)):
temp_file = getTempMarkdownPreviewPath(view)
if os.path.isfile(temp_file):
# reexec markdown conversion
# todo : check if browser still opened and reopen it if needed
view.run_command('markdown_preview', {
'target': 'disk',
'parser': view.settings().get('parser')
})
sublime.status_message('Markdown preview file updated')
class MarkdownCheatsheetCommand(sublime_plugin.TextCommand):
''' open our markdown cheat sheet in ST2 '''
def run(self, edit):
lines = '\n'.join(load_resource('sample.md').splitlines())
view = new_view(self.view.window(), lines, scratch=True)
view.set_name("Markdown Cheatsheet")
# Set syntax file
syntax_files = ["Packages/Markdown Extended/Syntaxes/Markdown Extended.tmLanguage", "Packages/Markdown/Markdown.tmLanguage"]
for file in syntax_files:
if exists_resource(file):
view.set_syntax_file(file)
break # Done if any syntax is set.
sublime.status_message('Markdown cheat sheet opened')
class Compiler(object):
''' Do the markdown converting '''
default_css = "markdown.css"
def isurl(self, css_name):
match = re.match(r'https?://', css_name)
if match:
return True
return False
def get_font_css(self):
fonts = ''
css_names = [
"fonts/migu-1c-regular.css",
]
for css_name in css_names:
fonts = u"%s%s" % (fonts, load_resource(css_name))
return u"<style>%s</style>" % fonts
def get_default_css(self):
''' locate the correct CSS with the 'css' setting '''
css_name = self.settings.get('css', 'default')
if self.isurl(css_name):
# link to remote URL
return u"<link href='%s' rel='stylesheet' type='text/css'>" % css_name
elif os.path.isfile(os.path.expanduser(css_name)):
# use custom CSS file
return u"<style>%s</style>" % load_utf8(os.path.expanduser(css_name))
elif css_name == 'default':
# use parser CSS file
return u"<style>%s</style>" % load_resource(self.default_css)
return ''
def get_override_css(self):
''' handls allow_css_overrides setting. '''
if self.settings.get('allow_css_overrides'):
filename = self.view.file_name()
filetypes = self.settings.get('markdown_filetypes')
if filename and filetypes:
for filetype in filetypes:
if filename.endswith(filetype):
css_filename = filename.rpartition(filetype)[0] + '.css'
if (os.path.isfile(css_filename)):
return u"<style>%s</style>" % load_utf8(css_filename)
return ''
def get_stylesheet(self):
''' return the correct CSS file based on parser and settings '''
return self.get_font_css() + self.get_default_css() + self.get_override_css()
def get_javascript(self):
js_files = self.settings.get('js')
scripts = ''
if js_files is not None:
# Ensure string values become a list.
if isinstance(js_files, str) or isinstance(js_files, unicode_str):
js_files = [js_files]
# Only load scripts if we have a list.
if isinstance(js_files, list):
for js_file in js_files:
if os.path.isabs(js_file):
# Load the script inline to avoid cross-origin.
scripts += u"<script>%s</script>" % load_utf8(js_file)
else:
scripts += u"<script type='text/javascript' src='%s'></script>" % js_file
return scripts
def get_mathjax(self):
''' return the MathJax script if enabled '''
if self.settings.get('enable_mathjax') is True:
return load_resource('mathjax.html')
return ''
def get_uml(self):
''' return the uml scripts if enabled '''
if self.settings.get('enable_uml') is True:
flow = load_resource('flowchart-min.js')
return load_resource('uml.html').replace('{{ flowchart }}', flow, 1)
return ''
def get_highlight(self):
return ''
def get_contents(self, wholefile=False):
''' Get contents or selection from view and optionally strip the YAML front matter '''
region = sublime.Region(0, self.view.size())
contents = self.view.substr(region)
if not wholefile:
# use selection if any
selection = self.view.substr(self.view.sel()[0])
if selection.strip() != '':
contents = selection
# Remove yaml front matter
if self.settings.get('strip_yaml_front_matter') and contents.startswith('---'):
frontmatter, contents = self.preprocessor_yaml_frontmatter(contents)
self.settings.apply_frontmatter(frontmatter)
references = self.settings.get('builtin').get('references', [])
for ref in references:
contents += get_references(ref)
contents = self.parser_specific_preprocess(contents)
return contents
def parser_specific_preprocess(self, text):
return text
def preprocessor_yaml_frontmatter(self, text):
""" Get frontmatter from string """
frontmatter = {}
if text.startswith("---"):
m = re.search(r'^(---(.*?)---[ \t]*\r?\n)', text, re.DOTALL)
if m:
try:
frontmatter = yaml.load(m.group(2))
except:
print(traceback.format_exc())
text = text[m.end(1):]
return frontmatter, text
def parser_specific_postprocess(self, text):
return text
def postprocessor_pathconverter(self, html, image_convert, file_convert, absolute=False):
RE_TAG_HTML = r'''(?xus)
(?:
(?P<comments>(\r?\n?\s*)<!--[\s\S]*?-->(\s*)(?=\r?\n)|<!--[\s\S]*?-->)|
(?P<open><(?P<tag>(?:%s)))
(?P<attr>(?:\s+[\w\-:]+(?:\s*=\s*(?:"[^"]*"|'[^']*'))?)*)
(?P<close>\s*(?:\/?)>)
)
'''
RE_TAG_LINK_ATTR = re.compile(
r'''(?xus)
(?P<attr>
(?:
(?P<name>\s+(?:href|src)\s*=\s*)
(?P<path>"[^"]*"|'[^']*')
)
)
'''
)
RE_SOURCES = re.compile(
RE_TAG_HTML % (
(r"img" if image_convert else "") +
(r"|" if image_convert and file_convert else "") +
(r"script|a|link" if file_convert else "")
)
)
def repl(m, base_path, rel_path=None):
if m.group('comments'):
tag = m.group('comments')
else:
tag = m.group('open')
if rel_path is None:
tag += RE_TAG_LINK_ATTR.sub(lambda m2: repl_absolute(m2, base_path), m.group('attr'))
else:
tag += RE_TAG_LINK_ATTR.sub(lambda m2: repl_relative(m2, base_path, rel_path), m.group('attr'))
tag += m.group('close')
return tag
basepath = self.settings.get('builtin').get("basepath")
if basepath is None:
basepath = ""
if absolute:
if basepath:
return RE_SOURCES.sub(lambda m: repl(m, basepath), html)
else:
if self.preview:
relativepath = getTempMarkdownPreviewPath(self.view)
else:
relativepath = self.settings.get('builtin').get("destination")
if not relativepath:
mdfile = self.view.file_name()
if mdfile is not None and os.path.exists(mdfile):
relativepath = os.path.splitext(mdfile)[0] + '.html'
if relativepath:
relativepath = os.path.dirname(relativepath)
if basepath and relativepath:
return RE_SOURCES.sub(lambda m: repl(m, basepath, relativepath), html)
return html
def postprocessor_base64(self, html):
''' convert resources (currently images only) to base64 '''
file_types = {
(".png",): "image/png",
(".jpg", ".jpeg"): "image/jpeg",
(".gif",): "image/gif"
}
exclusion_list = tuple(
['https://', 'http://', '#'] +
["data:%s;base64," % ft for ft in file_types.values()]
)
RE_WIN_DRIVE = re.compile(r"(^[A-Za-z]{1}:(?:\\|/))")
RE_TAG_HTML = re.compile(
r'''(?xus)
(?:
(?P<comments>(\r?\n?\s*)<!--[\s\S]*?-->(\s*)(?=\r?\n)|<!--[\s\S]*?-->)|
(?P<open><(?P<tag>img))
(?P<attr>(?:\s+[\w\-:]+(?:\s*=\s*(?:"[^"]*"|'[^']*'))?)*)
(?P<close>\s*(?:\/?)>)
)
'''
)
RE_TAG_LINK_ATTR = re.compile(
r'''(?xus)
(?P<attr>
(?:
(?P<name>\s+src\s*=\s*)
(?P<path>"[^"]*"|'[^']*')
)
)
'''
)
def b64(m):
import base64
src = url2pathname(m.group('path')[1:-1])
data = m.group(0)
base_path = self.settings.get('builtin').get("basepath")
if base_path is None:
base_path = ""
# Format the link
absolute = False
if src.startswith('file://'):
src = src.replace('file://', '', 1)
if sublime.platform() == "windows" and not src.startswith('//'):
src = src.lstrip("/")
absolute = True
elif sublime.platform() == "windows" and RE_WIN_DRIVE.match(src) is not None:
absolute = True
# Make sure we are working with an absolute path
if not src.startswith(exclusion_list):
if absolute:
src = os.path.normpath(src)
else:
src = os.path.normpath(os.path.join(base_path, src))
if os.path.exists(src):
ext = os.path.splitext(src)[1].lower()
for b64_ext in file_types:
if ext in b64_ext:
try:
with open(src, "rb") as f:
data = " src=\"data:%s;base64,%s\"" % (
file_types[b64_ext],
base64.b64encode(f.read()).decode('ascii')
)
except Exception:
pass
break
return data
def repl(m):
if m.group('comments'):
tag = m.group('comments')
else:
tag = m.group('open')
tag += RE_TAG_LINK_ATTR.sub(lambda m2: b64(m2), m.group('attr'))
tag += m.group('close')
return tag
return RE_TAG_HTML.sub(repl, html)
def postprocessor_simple(self, html):
''' Strip out ids and classes for a simplified HTML output '''
def repl(m):
if m.group('comments'):
tag = ''
else:
tag = m.group('open')
tag += RE_TAG_BAD_ATTR.sub('', m.group('attr'))
tag += m.group('close')
return tag
# Strip out id, class, on<word>, and style attributes for a simple html output
RE_TAG_HTML = re.compile(
r'''(?x)
(?:
(?P<comments>(\r?\n?\s*)<!--[\s\S]*?-->(\s*)(?=\r?\n)|<!--[\s\S]*?-->)|
(?P<open><[\w\:\.\-]+)
(?P<attr>(?:\s+[\w\-:]+(?:\s*=\s*(?:"[^"]*"|'[^']*'))?)*)
(?P<close>\s*(?:\/?)>)
)
''',
re.DOTALL | re.UNICODE
)
RE_TAG_BAD_ATTR = re.compile(
r'''(?x)
(?P<attr>
(?:
\s+(?:id|class|style|on[\w]+)
(?:\s*=\s*(?:"[^"]*"|'[^']*'))
)*
)
''',
re.DOTALL | re.UNICODE
)
return RE_TAG_HTML.sub(repl, html)
def convert_markdown(self, markdown_text):
''' convert input markdown to HTML, with github or builtin parser '''
markdown_html = self.parser_specific_convert(markdown_text)
image_convert = self.settings.get("image_path_conversion", "absolute")
file_convert = self.settings.get("file_path_conversions", "absolute")
markdown_html = self.parser_specific_postprocess(markdown_html)
if "absolute" in (image_convert, file_convert):
markdown_html = self.postprocessor_pathconverter(markdown_html, image_convert, file_convert, True)
if "relative" in (image_convert, file_convert):
markdown_html = self.postprocessor_pathconverter(markdown_html, image_convert, file_convert, False)
if image_convert == "base64":
markdown_html = self.postprocessor_base64(markdown_html)
if self.settings.get("html_simple", False):
markdown_html = self.postprocessor_simple(markdown_html)
return markdown_html
def get_title(self):
if self.meta_title is not None:
title = self.meta_title
else:
title = self.view.name()
if not title:
fn = self.view.file_name()
title = 'untitled' if not fn else os.path.splitext(os.path.basename(fn))[0]
return '<title>%s</title>' % cgi.escape(title)
def get_meta(self):
self.meta_title = None
meta = []
for k, v in self.settings.get("meta", {}).items():
if k == "title":
if isinstance(v, list):
if len(v) == 0:
v = ""
else:
v = v[0]
self.meta_title = unicode_str(v)
continue
if isinstance(v, list):
v = ','.join(v)
if v is not None:
meta.append(
'<meta name="%s" content="%s">' % (cgi.escape(k, True), cgi.escape(v, True))
)
return '\n'.join(meta)
def run(self, view, wholefile=False, preview=False):
''' return full html and body html for view. '''
self.settings = Settings('MarkdownPreview.sublime-settings', view.file_name())
self.preview = preview
self.view = view
contents = self.get_contents(wholefile)
body = self.convert_markdown(contents)
html_template = self.settings.get('html_template')
# use customized html template if given
if self.settings.get('html_simple', False):
html = body
elif html_template and os.path.exists(html_template):
head = u''
head += self.get_meta()
if not self.settings.get('skip_default_stylesheet'):
head += self.get_stylesheet()
head += self.get_javascript()
head += self.get_highlight()
head += self.get_mathjax()
head += self.get_uml()
head += self.get_title()
html = load_utf8(html_template)
html = html.replace('{{ HEAD }}', head, 1)
html = html.replace('{{ BODY }}', body, 1)
else:
html = u'<!DOCTYPE html>'
html += '<html><head><meta charset="utf-8">'
html += self.get_meta()
html += self.get_stylesheet()
html += self.get_javascript()
html += self.get_highlight()
html += self.get_mathjax()
html += self.get_uml()
html += self.get_title()
html += '</head><body>'
html += '<article class="markdown-body">'
html += body
html += '</article>'
html += '</body>'
html += '</html>'
return html, body
class GithubCompiler(Compiler):
default_css = "github.css"
def curl_convert(self, data):
try:
import subprocess
# It looks like the text does NOT need to be escaped and
# surrounded with double quotes.
# Tested in ubuntu 13.10, python 2.7.5+
shell_safe_json = data.decode('utf-8')
curl_args = [
'curl',
'-H',
'Content-Type: application/json',
'-d',
shell_safe_json,
'https://api.github.com/markdown'
]
github_oauth_token = self.settings.get('github_oauth_token')
if github_oauth_token:
curl_args[1:1] = [
'-u',
github_oauth_token
]
markdown_html = subprocess.Popen(curl_args, stdout=subprocess.PIPE).communicate()[0].decode('utf-8')
return markdown_html
except subprocess.CalledProcessError:
sublime.error_message('cannot use github API to convert markdown. SSL is not included in your Python installation. And using curl didn\'t work either')
return None
def preprocessor_critic(self, text):
''' Stip out multi-markdown critic marks. Accept changes by default '''
return CriticDump().dump(text, self.settings.get("strip_critic_marks", "accept") == "accept")
def parser_specific_preprocess(self, text):
if self.settings.get("strip_critic_marks", "accept") in ["accept", "reject"]:
text = self.preprocessor_critic(text)
return text
def parser_specific_postprocess(self, html):
''' Post-processing for github API '''
if self.settings.get("github_inject_header_ids", False):
html = self.postprocess_inject_header_id(html)
return html
def postprocess_inject_header_id(self, html):
''' Insert header ids when no anchors are present '''
unique = {}
def header_to_id(text):
if text is None:
return ''
# Strip html tags and lower
id = RE_TAGS.sub('', text).lower()
# Remove non word characters or non spaces and dashes
# Then convert spaces to dashes
id = RE_WORD.sub('', id).replace(' ', '-')
# Encode anything that needs to be
return quote(id)
def inject_id(m):
id = header_to_id(m.group('text'))
if id == '':
return m.group(0)
# Append a dash and number for uniqueness if needed
value = unique.get(id, None)
if value is None:
unique[id] = 1
else:
unique[id] += 1
id += "-%d" % value
return m.group('open')[:-1] + (' id="%s">' % id) + m.group('text') + m.group('close')
RE_TAGS = re.compile(r'''</?[^>]*>''')
RE_WORD = re.compile(r'''[^\w\- ]''')
RE_HEADER = re.compile(r'''(?P<open><h([1-6])>)(?P<text>.*?)(?P<close></h\2>)''', re.DOTALL)
return RE_HEADER.sub(inject_id, html)
def parser_specific_convert(self, markdown_text):
''' convert input markdown to HTML, with github or builtin parser '''
markdown_html = _CANNOT_CONVERT
github_oauth_token = self.settings.get('github_oauth_token')
# use the github API
sublime.status_message('converting markdown with github API...')
github_mode = self.settings.get('github_mode', 'gfm')
data = {
"text": markdown_text,
"mode": github_mode
}
data = json.dumps(data).encode('utf-8')
try:
headers = {
'Content-Type': 'application/json'
}
if github_oauth_token:
headers['Authorization'] = "token %s" % github_oauth_token
url = "https://api.github.com/markdown"
sublime.status_message(url)
request = Request(url, data, headers)
markdown_html = urlopen(request).read().decode('utf-8')
except HTTPError:
e = sys.exc_info()[1]
if e.code == 401:
sublime.error_message('github API auth failed. Please check your OAuth token.')
else:
sublime.error_message('github API responded in an unfriendly way :/')
except URLError:
# Maybe this is a Linux-install of ST which doesn't bundle with SSL support
# So let's try wrapping curl instead
markdown_html = self.curl_convert(data)
except:
e = sys.exc_info()[1]
print(e)
traceback.print_exc()
sublime.error_message('cannot use github API to convert markdown. Please check your settings.')
else:
sublime.status_message('converted markdown with github API successfully')
return markdown_html
class MultiMarkdownCompiler(Compiler):
default_css = "markdown.css"
def parser_specific_convert(self, markdown_text):
import subprocess
binary = self.settings.get("multimarkdown_binary", "")
if os.path.exists(binary):
cmd = [binary]
critic_mode = self.settings.get("strip_critic_marks", "accept")
if critic_mode in ("accept", "reject"):
cmd.append('-a' if critic_mode == "accept" else '-r')
sublime.status_message('converting markdown with multimarkdown...')
if sublime.platform() == "windows":
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
p = subprocess.Popen(
cmd, startupinfo=startupinfo,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
else:
p = subprocess.Popen(
cmd,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
for line in markdown_text.split('\n'):
p.stdin.write((line + '\n').encode('utf-8'))
markdown_html = p.communicate()[0].decode("utf-8")
if p.returncode:
# Log info to console
sublime.error_message("Could not convert file! See console for more info.")
print(markdown_html)
markdown_html = _CANNOT_CONVERT
else:
sublime.error_message("Cannot find multimarkdown binary!")
markdown_html = _CANNOT_CONVERT
return markdown_html
class MarkdownCompiler(Compiler):
default_css = "markdown.css"
def get_highlight(self):
''' return the Pygments css if enabled '''
highlight = ''
if self.pygments_style and not self.noclasses:
highlight += '<style>%s</style>' % get_formatter_by_name('html', style=self.pygments_style).get_style_defs('.codehilite pre')
return highlight
def preprocessor_critic(self, text):
''' Stip out multi-markdown critic marks. Accept changes by default '''
return CriticDump().dump(text, self.settings.get("strip_critic_marks", "accept") == "accept")
def parser_specific_preprocess(self, text):
if self.settings.get("strip_critic_marks", "accept") in ["accept", "reject"]:
text = self.preprocessor_critic(text)
return text
def process_extensions(self, extensions):
re_pygments = re.compile(r"pygments_style\s*=\s*([a-zA-Z][a-zA-Z_\d]*)")
re_use_pygments = re.compile(r"use_pygments\s*=\s*(True|False)")