fix pre block indentation/spaces

This commit is contained in:
math2001
2016-11-26 14:25:24 +11:00
parent 5c194de103
commit 1113fda8a1
4 changed files with 81 additions and 62 deletions

View File

@ -1740,7 +1740,7 @@ class Markdown(object):
codeblock = codeblock.replace(old, new) codeblock = codeblock.replace(old, new)
return codeblock return codeblock
lexer = self._get_pygments_lexer(lexer_name) lexer = self._get_pygments_lexer(lexer_name)
if lexer: if lexer and self.extras.get('no-code-highlighting', True) is True:
codeblock = unhash_code( codeblock ) codeblock = unhash_code( codeblock )
colored = self._color_with_pygments(codeblock, lexer, colored = self._color_with_pygments(codeblock, lexer,
**formatter_opts) **formatter_opts)

View File

@ -24,19 +24,19 @@ def mini(val, min):
STYLE_FILE = os.path.join(sublime.packages_path(), 'User', 'MarkdownLivePreview.css') 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""" content = None
if os.path.exists(STYLE_FILE): if os.path.exists(STYLE_FILE):
with open(STYLE_FILE) as fp: with open(STYLE_FILE) as fp:
content = fp.read() content = fp.read()
if content:
return content return content
if not content:
return """ content = """
html { html {
--light-bg: color(var(--background) blend(#aaa 80%)) --light-bg: color(var(--background) blend(#999 85%))
} }
body { body {
padding:10px; padding:10px;
padding-top: 0px;
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;
@ -61,25 +61,30 @@ def get_style():
pre { pre {
display: block; display: block;
margin-top: 20px; margin-top: 20px;
line-height: 2; line-height: 1.7;
background-color: var(--light-bg); background-color: var(--light-bg);
padding-left: 10px; padding-left: 10px;
width: 100%;
border-radius: 3px;
} }
pre code { pre code {
padding-left: 0; padding-left: 0;
} }
""" """
return content + "pre code .space {color: var(--light-bg)}"
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>"""
while True: while True:
obj = re.search(r'<pre>.*?</pre>', html, re.DOTALL) obj = re.search(r'<pre>(.*?)</pre>', html, re.DOTALL)
if not obj: if not obj:
break break
html = list(html) 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[obj.start(0):obj.end(0)] = '<pre >' + ''.join(html[obj.start(1):obj.end(1)]) \
.replace('\n', '<br>') \
.replace(' ', '&nbsp;') + '</pre>'
html = ''.join(html) html = ''.join(html)
return html return html
@ -108,18 +113,32 @@ def create_preview(window, md_view):
def show_html(md_view, preview): def show_html(md_view, preview):
html = ('<style>{}</style>'.format(get_style()) + html = ('<style>{}</style>'.format(get_style()) +
pre_with_br(markdown2.markdown(get_view_content(md_view), pre_with_br(markdown2.markdown(get_view_content(md_view),
extras=['fenced-code-blocks']))) extras=['fenced-code-blocks', 'no-code-highlighting'])))
# the option no-code-highlighting does not exists
# in the official version of markdown2 for now
# I personaly edited the file (markdown2.py:1743)
html = html.replace('&nbsp;', '&nbspespace;') # save where are the spaces
html = HTMLParser().unescape(html) html = HTMLParser().unescape(html)
# exception, again, because <pre> aren't supported by the phantoms
html = html.replace('&nbspespace;', '<i class="space">.</i>')
print(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(-1),
html, html,
sublime.LAYOUT_INLINE, sublime.LAYOUT_BLOCK,
lambda href: sublime.run_command('open_url', {'url': href})) lambda href: sublime.run_command('open_url', {'url': href}))
# 0 < y < 1 # 0 < y < 1
y = md_view.text_to_layout(md_view.sel()[0].begin())[1] / md_view.layout_extent()[1] y = md_view.text_to_layout(md_view.sel()[0].begin())[1] / md_view.layout_extent()[1]
vector = [0, y * preview.layout_extent()[1]]
# remove half of the viewport_extent.y to center it on the screen (verticaly) # remove half of the viewport_extent.y to center it on the screen (verticaly)
vector = 0, y * preview.layout_extent()[1] - preview.viewport_extent()[1] / 2 vector[1] -= preview.viewport_extent()[1] / 2
vector[1] = mini(vector[1], 0)
vector[1] += preview.line_height()
preview.set_viewport_position(vector, animate=False) preview.set_viewport_position(vector, animate=False)
def get_view_content(view): def get_view_content(view):
@ -180,3 +199,12 @@ class MarkdownInPopupCommand(sublime_plugin.EventListener):
md_view_settings.erase('markdown_preview_enabled') md_view_settings.erase('markdown_preview_enabled')
md_view_settings.erase('markdown_preview_id') md_view_settings.erase('markdown_preview_id')
sublime.set_timeout_async(callback, 250) sublime.set_timeout_async(callback, 250)
class MarkdownInPopupTestCommand(sublime_plugin.ApplicationCommand):
def run(self):
md(markdown2.markdown("""
```python
print("hello world")
```
""", extras=['no-code-highlighting', 'fenced-code-blocks']))

View File

@ -32,9 +32,12 @@ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
```python ```python
print('This is some pretty') print('This is some pretty')
print('cool stuff') print('cool stuff')
if test:
print('hello world')
``` ```
This is some `code` ccc This is some `code`
- a - a
- list - list

14
todo.md
View File

@ -2,20 +2,8 @@
- add message in status bar - add message in status bar
- add **custom css** feature @done - add **custom css** feature @done
- sync scroll @done @improve: scroll preview to center when possible - sync scroll @done
- regive focus to the right markdown view @done - regive focus to the right markdown view @done
- set the title of the preview @done - set the title of the preview @done
- disable previewing when the preview is closed @done - disable previewing when the preview is closed @done
- check when setting is activated and create panel and stuff @done - check when setting is activated and create panel and stuff @done
This is pretty cool
```ruby
print('hello world')
def test():
puts 'hello'
if test:
print('hi')
```
this is some `code`!