support pre block

This commit is contained in:
math2001
2016-11-26 09:15:25 +11:00
parent af3ea839c3
commit 48e0420816
3 changed files with 49 additions and 9 deletions

View File

@ -2,6 +2,9 @@ import sublime
import sublime_plugin import sublime_plugin
from . import markdown2 from . import markdown2
import os.path import os.path
import re
from html.parser import HTMLParser
# Main sublime tools function # Main sublime tools function
@ -14,9 +17,14 @@ def sm(*t, **kwargs):
def em(*t, **kwargs): def em(*t, **kwargs):
sublime.error_message(kwargs.get('sep', ' ').join([str(el) for el in t])) sublime.error_message(kwargs.get('sep', ' ').join([str(el) for el in t]))
STYLE_FILE = os.path.join(sublime.packages_path(), 'User', 'MarkdownLivePreview.css')
def get_style(): def get_style():
"""Of course, this is temporal, there will be an option to customize the CSS""" """Of course, this is temporal, there will be an option to customize the CSS"""
return """<style> if os.path.exists(STYLE_FILE):
with open(STYLE_FILE) as fp:
return fp.read()
return """
body { body {
padding:10px; padding:10px;
font-family: "Open Sans", sans-serif; font-family: "Open Sans", sans-serif;
@ -29,7 +37,20 @@ def get_style():
margin-left: 30px; margin-left: 30px;
border: 1px solid red; border: 1px solid red;
} }
</style>""" """
def pre_with_br(html):
"""Because the phantoms of sublime text does not support <pre> blocks
this function replaces every \n with a <br> in a <pre>"""
while True:
obj = re.search(r'<pre>.*?</pre>', html, re.DOTALL)
if not obj:
break
html = list(html)
html[obj.start(0):obj.end(0)] = ''.join(html[obj.start(0):obj.end(0)]).replace('\n', '<br>').replace('<pre>', '<pre class="pre">')
html = ''.join(html)
return html
def close_preview(md_view_settings, preview): def close_preview(md_view_settings, preview):
preview.close() preview.close()
@ -54,7 +75,8 @@ def create_preview(window, md_view):
return preview, preview_settings return preview, preview_settings
def show_html(md_view, preview): def show_html(md_view, preview):
html = get_style() + markdown2.markdown(get_view_content(md_view)) html = '<style>{}</style>'.format(get_style()) + pre_with_br(markdown2.markdown(get_view_content(md_view), extras=['fenced-code-blocks']))
html = HTMLParser().unescape(html)
preview.erase_phantoms('markdown_preview') preview.erase_phantoms('markdown_preview')
preview.add_phantom('markdown_preview', preview.add_phantom('markdown_preview',
sublime.Region(0), sublime.Region(0),

View File

@ -24,5 +24,23 @@ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
> If the implementation is easy to explain, it may be a good idea. > If the implementation is easy to explain, it may be a good idea.
> Namespaces are one honking great idea -- let's do more of those! > Namespaces are one honking great idea -- let's do more of those!
> Code tells you how, comments tells you why > Code tells you how, comments tells you why c
print('hello world')
print('hi')
```python
print('This is some pretty')
print('cool stuff')
```
This is some `code`
- a
- list
1. an
2. other
3. list

10
todo.md
View File

@ -1,9 +1,9 @@
# todo # todo
- add **custom css** feature
- sync scroll - sync scroll
- regive focus to the right markdown view @done
- set the title of the preview
- add message in status bar - add message in status bar
- disable previewing when the preview is closed - add **custom css** feature
- check when setting is activated and create panel and stuff - regive focus to the right markdown view @done
- set the title of the preview @done
- disable previewing when the preview is closed @done
- check when setting is activated and create panel and stuff @done