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:
@ -24,6 +24,10 @@ windows_phantom_set = {}
|
||||
|
||||
def plugin_loaded():
|
||||
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')
|
||||
|
||||
def get_preview_name(md_view):
|
||||
@ -65,7 +69,6 @@ def markdown2html(md, basepath):
|
||||
# exception, again, because <pre> aren't supported by the phantoms
|
||||
html = html.replace(' espace;', '<i class="space">.</i>')
|
||||
html = replace_img_src_base64(html, basepath=os.path.dirname(basepath))
|
||||
sublime.set_clipboard(html)
|
||||
return html
|
||||
|
||||
def show_html(md_view, preview):
|
||||
|
||||
@ -43,6 +43,14 @@ pre code {
|
||||
pre code .space {
|
||||
color: var(--light-bg)
|
||||
}
|
||||
pre.table {
|
||||
background-color: var(--backgroud);
|
||||
}
|
||||
pre.table code {
|
||||
background-color: var(--backgroud);
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
kbd {
|
||||
padding: 0 5px;
|
||||
|
||||
11
functions.py
11
functions.py
@ -4,7 +4,7 @@ import os.path
|
||||
import sublime
|
||||
import re
|
||||
from .image_manager import ImageManager
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
def plugin_loaded():
|
||||
global error404, loading
|
||||
@ -90,9 +90,16 @@ def get_settings():
|
||||
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>"""
|
||||
# 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:
|
||||
obj = re.search(r'<pre>(.*?)</pre>', html, re.DOTALL)
|
||||
obj = re.search(r'<pre (?:class="table")?>(.*?)</pre>', html, re.DOTALL)
|
||||
if not obj:
|
||||
break
|
||||
html = list(html)
|
||||
|
||||
@ -53,7 +53,7 @@ def pre_table(s_table):
|
||||
for i, cell in enumerate(row):
|
||||
if 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 j, cell in enumerate(row):
|
||||
text += '| ' + ''.join(str(node) for node in cell.contents) + ' ' * (cols_width[j] - len(cell.text))
|
||||
|
||||
Reference in New Issue
Block a user