Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8cc6b2b263 | |||
| c7961ce94c | |||
| eae91fa428 | |||
| 6f18e8e4a2 | |||
| 48c1800065 | |||
| bad1cb74c6 |
40
MLPApi.py
40
MLPApi.py
@ -7,6 +7,7 @@ import os.path
|
|||||||
from html.parser import HTMLParser
|
from html.parser import HTMLParser
|
||||||
|
|
||||||
from .lib import markdown2 as md2
|
from .lib import markdown2 as md2
|
||||||
|
from .lib.pre_tables import pre_tables
|
||||||
from .escape_amp import *
|
from .escape_amp import *
|
||||||
from .functions import *
|
from .functions import *
|
||||||
from .setting_names import *
|
from .setting_names import *
|
||||||
@ -20,8 +21,13 @@ USER_STYLE_FILE = os.path.join(os.path.dirname(__folder__), 'User', 'MarkdownLiv
|
|||||||
# used to store the phantom's set
|
# used to store the phantom's set
|
||||||
windows_phantom_set = {}
|
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):
|
||||||
@ -48,35 +54,31 @@ def get_style():
|
|||||||
content += '\n' + fp.read() + '\n'
|
content += '\n' + fp.read() + '\n'
|
||||||
return content
|
return content
|
||||||
|
|
||||||
def show_html(md_view, preview):
|
def markdown2html(md, basepath):
|
||||||
global windows_phantom_set
|
html = ''
|
||||||
html = []
|
html += '<style>\n{}\n</style>\n'.format(get_style())
|
||||||
html.append('<style>\n{}\n</style>'.format(get_style()))
|
# pre_with_br
|
||||||
html.append(pre_with_br(md2.markdown(get_view_content(md_view),
|
html += pre_with_br(pre_tables(md2.markdown(md, extras=['fenced-code-blocks',
|
||||||
extras=['fenced-code-blocks',
|
'no-code-highlighting', 'tables'])))
|
||||||
'no-code-highlighting'])))
|
# the option no-code-highlighting does not exists in the official version of markdown2 for now
|
||||||
# the option no-code-highlighting does not exists
|
|
||||||
# in the official version of markdown2 for now
|
|
||||||
# I personaly edited the file (markdown2.py:1743)
|
# I personaly edited the file (markdown2.py:1743)
|
||||||
html = '\n'.join(html)
|
|
||||||
|
|
||||||
html = html.replace(' ', ' espace;') # save where are the spaces
|
html = html.replace(' ', ' espace;') # save where are the spaces
|
||||||
|
|
||||||
html = HTMLParser().unescape(html)
|
|
||||||
|
|
||||||
html = escape_amp(html)
|
|
||||||
|
|
||||||
# exception, again, because <pre> aren't supported by the phantoms
|
# exception, again, because <pre> aren't supported by the phantoms
|
||||||
html = html.replace(' espace;', '<i class="space">.</i>')
|
html = html.replace(' espace;', '<i class="space">.</i>')
|
||||||
html = replace_img_src_base64(html, basepath=os.path.dirname(
|
html = replace_img_src_base64(html, basepath=os.path.dirname(basepath))
|
||||||
md_view.file_name()))
|
return html
|
||||||
|
|
||||||
|
def show_html(md_view, preview):
|
||||||
|
global windows_phantom_set
|
||||||
|
html = markdown2html(get_view_content(md_view), os.path.dirname(md_view.file_name()))
|
||||||
|
|
||||||
phantom_set = windows_phantom_set.setdefault(preview.window().id(),
|
phantom_set = windows_phantom_set.setdefault(preview.window().id(),
|
||||||
sublime.PhantomSet(preview,
|
sublime.PhantomSet(preview, 'markdown_live_preview'))
|
||||||
'markdown_live_preview'))
|
|
||||||
phantom_set.update([sublime.Phantom(sublime.Region(0), html, sublime.LAYOUT_BLOCK,
|
phantom_set.update([sublime.Phantom(sublime.Region(0), html, sublime.LAYOUT_BLOCK,
|
||||||
lambda href: sublime.run_command('open_url',
|
lambda href: sublime.run_command('open_url', {'url': href}))])
|
||||||
{'url': href}))])
|
|
||||||
|
|
||||||
# lambda href: sublime.run_command('open_url', {'url': href})
|
# lambda href: sublime.run_command('open_url', {'url': href})
|
||||||
# get the "ratio" of the markdown view's position.
|
# get the "ratio" of the markdown view's position.
|
||||||
|
|||||||
18
default.css
18
default.css
@ -9,13 +9,13 @@ body {
|
|||||||
font-family: "Open Sans", sans-serif;
|
font-family: "Open Sans", sans-serif;
|
||||||
background-color: var(--background);
|
background-color: var(--background);
|
||||||
font-size: 15px;
|
font-size: 15px;
|
||||||
|
color: #333;
|
||||||
}
|
}
|
||||||
|
|
||||||
blockquote {
|
blockquote {
|
||||||
font-style: italic;
|
font-style: italic;
|
||||||
display: block;
|
display: block;
|
||||||
margin-left: 30px;
|
margin-left: 30px;
|
||||||
border: 1px solid red;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
code {
|
code {
|
||||||
@ -24,24 +24,32 @@ code {
|
|||||||
background-color: var(--light-bg);
|
background-color: var(--light-bg);
|
||||||
margin: 0;
|
margin: 0;
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
margin: 5px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pre {
|
pre {
|
||||||
display: block;
|
display: block;
|
||||||
margin-top: 20px;
|
margin-top: 20px;
|
||||||
line-height: 1.7;
|
|
||||||
background-color: var(--light-bg);
|
background-color: var(--light-bg);
|
||||||
padding-left: 10px;
|
padding-left: 10px;
|
||||||
|
padding-top: 5px;
|
||||||
|
padding-bottom: 5px;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
}
|
}
|
||||||
pre code {
|
pre code {
|
||||||
padding-left: 0;
|
padding: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
|||||||
7
dependencies.json
Normal file
7
dependencies.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"*": {
|
||||||
|
"*": [
|
||||||
|
"bs4"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -16,5 +16,6 @@ class MLPDevListener(sublime_plugin.EventListener):
|
|||||||
'MarkdownLivePreview.py'),
|
'MarkdownLivePreview.py'),
|
||||||
'scripts': ['image_manager', 'functions', 'MLPApi',
|
'scripts': ['image_manager', 'functions', 'MLPApi',
|
||||||
'setting_names'],
|
'setting_names'],
|
||||||
|
'folders': ['lib'],
|
||||||
'quiet': True
|
'quiet': True
|
||||||
})
|
})
|
||||||
|
|||||||
18
functions.py
18
functions.py
@ -4,6 +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
|
||||||
@ -89,14 +90,9 @@ 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>"""
|
||||||
|
soup = BeautifulSoup(html)
|
||||||
while True:
|
for pre in soup.find_all('pre'):
|
||||||
obj = re.search(r'<pre>(.*?)</pre>', html, re.DOTALL)
|
code = pre.find('code')
|
||||||
if not obj:
|
code.replaceWith(BeautifulSoup(''.join(str(node) for node in pre.contents) \
|
||||||
break
|
.replace('\n', '<br/>').replace(' ', '<i class="space">.</i>'), 'html.parser'))
|
||||||
html = list(html)
|
return str(soup).replace('<br/>', '<br />')
|
||||||
html[obj.start(0):obj.end(0)] = '<pre >' + ''.join(html[obj.start(1):obj.end(1)]) \
|
|
||||||
.replace('\n', '<br>') \
|
|
||||||
.replace(' ', ' ') + '</pre>'
|
|
||||||
html = ''.join(html)
|
|
||||||
return html
|
|
||||||
|
|||||||
46
lib/pre_tables.py
Normal file
46
lib/pre_tables.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
|
||||||
|
"""
|
||||||
|
'pre_tables' transform *html* tables into markdown tables, and put them in some <pre><code> tags
|
||||||
|
"""
|
||||||
|
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
|
def python_table(s_table):
|
||||||
|
"""Transform BeautifulSoup table into list of list"""
|
||||||
|
rows = []
|
||||||
|
for row in s_table.find_all('tr'):
|
||||||
|
# rows.append(list(map( lambda td: td.text, row.find_all(['th', 'td']) )))
|
||||||
|
rows.append(row.find_all(['th', 'td']))
|
||||||
|
return rows
|
||||||
|
|
||||||
|
def pre_table(s_table):
|
||||||
|
rows = python_table(s_table)
|
||||||
|
cols_width = [len(cell) for cell in rows[0]]
|
||||||
|
for j, row in enumerate(rows):
|
||||||
|
for i, cell in enumerate(row):
|
||||||
|
if cols_width[i] < len(cell.text):
|
||||||
|
cols_width[i] = len(cell.text)
|
||||||
|
text = '<pre class="table"><code>'
|
||||||
|
for i, row in enumerate(rows):
|
||||||
|
if i == 1:
|
||||||
|
for j, cell in enumerate(row):
|
||||||
|
text += '|' + '-' * (cols_width[j] + 2)
|
||||||
|
text += '|\n'
|
||||||
|
|
||||||
|
for j, cell in enumerate(row):
|
||||||
|
text += '| ' + ''.join(str(node) for node in cell.contents) \
|
||||||
|
+ ' ' * (cols_width[j] - len(cell.text) + 1)
|
||||||
|
text += '|\n'
|
||||||
|
text += '</pre></code>'
|
||||||
|
return text
|
||||||
|
|
||||||
|
def pre_tables(html):
|
||||||
|
soup = BeautifulSoup(html, 'html.parser')
|
||||||
|
for table in soup.find_all('table'):
|
||||||
|
table.replace_with(BeautifulSoup(pre_table(table), 'html.parser'))
|
||||||
|
return str(soup)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# CSW: ignore
|
||||||
|
print(pre_tables(html))
|
||||||
13
sample.md
13
sample.md
@ -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.
|
||||||
@ -16,7 +17,17 @@ if you is moods.curious:
|
|||||||
- you need
|
- you need
|
||||||
- todos
|
- todos
|
||||||
|
|
||||||

|
| ID | Name |
|
||||||
|
|-----------|-------|
|
||||||
|
| 56 | Matt |
|
||||||
|
| 42 | Colin |
|
||||||
|
| 23 | Lisa |
|
||||||
|
| 45 | John |
|
||||||
|
| `<table>` | `><` |
|
||||||
|
|
||||||
|
[Sublime Text Logo](https://upload.wikimedia.org/wikipedia/en/4/4c/Sublime_Text_Logo.png)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Some plugin I just *need*:
|
Some plugin I just *need*:
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user