use bs4 for pre2br; fix style sheet for tables

Also: load default.css from the actual file if the package is not
zipped.
This commit is contained in:
Mathieu PATUREL
2017-01-26 10:36:52 +11:00
parent bad1cb74c6
commit 48c1800065
5 changed files with 24 additions and 5 deletions

View File

@ -24,6 +24,10 @@ windows_phantom_set = {}
def plugin_loaded(): def plugin_loaded():
global DEFAULT_STYLE_FILE global DEFAULT_STYLE_FILE
if os.path.exists(os.path.join(__folder__, 'default.css')):
with open(os.path.join(__folder__, 'default.css')) as fp:
DEFAULT_STYLE_FILE = fp.read()
else:
DEFAULT_STYLE_FILE = sublime.load_resource('Packages/MarkdownLivePreview/default.css') DEFAULT_STYLE_FILE = sublime.load_resource('Packages/MarkdownLivePreview/default.css')
def get_preview_name(md_view): def get_preview_name(md_view):
@ -65,7 +69,6 @@ def markdown2html(md, basepath):
# exception, again, because <pre> aren't supported by the phantoms # exception, again, because <pre> aren't supported by the phantoms
html = html.replace('&nbspespace;', '<i class="space">.</i>') html = html.replace('&nbspespace;', '<i class="space">.</i>')
html = replace_img_src_base64(html, basepath=os.path.dirname(basepath)) html = replace_img_src_base64(html, basepath=os.path.dirname(basepath))
sublime.set_clipboard(html)
return html return html
def show_html(md_view, preview): def show_html(md_view, preview):

View File

@ -43,6 +43,14 @@ pre code {
pre code .space { pre code .space {
color: var(--light-bg) color: var(--light-bg)
} }
pre.table {
background-color: var(--backgroud);
}
pre.table code {
background-color: var(--backgroud);
padding: 0;
margin: 0;
}
kbd { kbd {
padding: 0 5px; padding: 0 5px;

View File

@ -4,7 +4,7 @@ import os.path
import sublime import sublime
import re import re
from .image_manager import ImageManager from .image_manager import ImageManager
from bs4 import BeautifulSoup
def plugin_loaded(): def plugin_loaded():
global error404, loading global error404, loading
@ -90,9 +90,16 @@ def get_settings():
def pre_with_br(html): def pre_with_br(html):
"""Because the phantoms of sublime text does not support <pre> blocks """Because the phantoms of sublime text does not support <pre> blocks
this function replaces every \n with a <br> in a <pre>""" this function replaces every \n with a <br> in a <pre>"""
# Need to use bs4
soup = BeautifulSoup(html)
for pre in soup.find_all('pre'):
code = pre.find('code')
code.replaceWith(BeautifulSoup(''.join(str(node) for node in pre.contents) \
.replace('\n', '<br/>').replace(' ', '<i class="space">.</i>'), 'html.parser'))
return soup.prettify().replace('<br/>', '<br />')
while True: while True:
obj = re.search(r'<pre>(.*?)</pre>', html, re.DOTALL) obj = re.search(r'<pre (?:class="table")?>(.*?)</pre>', html, re.DOTALL)
if not obj: if not obj:
break break
html = list(html) html = list(html)

View File

@ -53,7 +53,7 @@ def pre_table(s_table):
for i, cell in enumerate(row): for i, cell in enumerate(row):
if cols_width[i] < len(cell.text): if cols_width[i] < len(cell.text):
cols_width[i] = len(cell.text) cols_width[i] = len(cell.text)
text = '<pre><code>' text = '<pre class="table"><code>'
for i, row in enumerate(rows): for i, row in enumerate(rows):
for j, cell in enumerate(row): for j, cell in enumerate(row):
text += '| ' + ''.join(str(node) for node in cell.contents) + ' ' * (cols_width[j] - len(cell.text)) text += '| ' + ''.join(str(node) for node in cell.contents) + ' ' * (cols_width[j] - len(cell.text))

View File

@ -8,6 +8,7 @@ if you is moods.curious:
print('then do it!') print('then do it!')
``` ```
<kbd>ctrl+\`</kbd> or *View → Show Console* and paste `import this`! <kbd>ctrl+\`</kbd> or *View → Show Console* and paste `import this`!
> Perfect programmers do NOT need comments. > Perfect programmers do NOT need comments.