clean code, working, add auto close/open preview

This commit is contained in:
math2001
2016-11-25 17:57:59 +11:00
parent 35e53cb683
commit af3ea839c3
3 changed files with 116 additions and 81 deletions

View File

@ -1,7 +1,7 @@
import sublime
import sublime_plugin
import html
from . import markdown2
import os.path
# Main sublime tools function
@ -14,89 +14,100 @@ def sm(*t, **kwargs):
def em(*t, **kwargs):
sublime.error_message(kwargs.get('sep', ' ').join([str(el) for el in t]))
def get_style():
"""Of course, this is temporal, there will be an option to customize the CSS"""
return """<style>
body {
padding:10px;
font-family: "Open Sans", sans-serif;
background-color: #fff;
font-size: 15px;
}
blockquote {
font-style: italic;
display: block;
margin-left: 30px;
border: 1px solid red;
}
</style>"""
def close_preview(md_view_settings, preview):
preview.close()
md_view_settings.erase('markdown_preview_id')
md_view_settings.erase('markdown_preview_enabled')
def create_preview(window, md_view):
focus_group, focus_view = window.get_view_index(md_view)
preview = window.new_file()
window.run_command('new_pane') # move the preview to a new group
preview.set_name(os.path.basename(md_view.file_name()) + ' - Preview')
preview_settings = preview.settings()
preview_settings.set('gutter', False)
preview_settings.set('is_markdown_preview', True)
preview_settings.set('markdown_view_id', md_view.id())
md_view.settings().set('markdown_preview_id', preview.id())
window.focus_group(focus_group)
window.focus_view(md_view)
return preview, preview_settings
def show_html(md_view, preview):
html = get_style() + markdown2.markdown(get_view_content(md_view))
preview.erase_phantoms('markdown_preview')
preview.add_phantom('markdown_preview',
sublime.Region(0),
html,
sublime.LAYOUT_INLINE,
lambda href: sublime.run_command('open_url', {'url': href}))
def get_view_content(view):
return view.substr(sublime.Region(0, view.size()))
def get_view_from_id(window, id):
for view in window.views():
if view.id() == id:
return view
class MarkdownInPopupCommand(sublime_plugin.EventListener):
def run(self, view):
view.show_popup('<h1> hello </h1>', sublime.HIDE_ON_MOUSE_MOVE_AWAY, 1)
return
ph = sublime.Phantom(sublime.Region(0), "<h1> hello </h1>", sublime.LAYOUT_INLINE, None)
ph_set = sublime.PhantomSet(sublime.active_window().active_view(), 'tests')
ph_set.update([ph])
def get_view_content(self, view):
return view.substr(sublime.Region(0, view.size()))
def on_modified(self, current_view):
current_view_settings = current_view.settings()
if 'markdown' not in current_view_settings.get('syntax').lower():
def on_load(self, view):
settings = view.settings()
if not 'markdown' in settings.get('syntax').lower():
return
if current_view_settings.get('markdown_preview_enabled', False) is not True:
settings.add_on_change('markdown_preview_enabled', lambda: self.on_modified(view))
def on_modified(self, md_view):
window = md_view.window()
md_view_settings = md_view.settings()
if not 'markdown' in md_view_settings.get('syntax').lower():
return
w = current_view.window()
html = """<style>
body {
padding:10px;
font-family: "Open Sans", sans-serif;
background-color: #fff;
font-size: 15px;
}
blockquote {
font-style: italic;
display: block;
margin-left: 30px;
border: 1px solid red;
}
</style>"""
html += markdown2.markdown(self.get_view_content(current_view))
view_id = current_view_settings.get('markdown_preview_id', None)
def create_preview_panel():
focus_group, focus_view = w.get_view_index(current_view)
preview = w.new_file()
w.run_command('new_pane')
view_id = preview.id()
current_view_settings.set('markdown_preview_id', view_id)
w.focus_group(focus_group)
w.focus_view(current_view)
return preview
def show_html(view, html):
view.settings().set('gutter', False)
view.erase_phantoms('markdown_preview')
self.phantom_id = view.add_phantom('markdown_preview',
sublime.Region(0),
html,
sublime.LAYOUT_INLINE,
lambda href: sublime.run_command('open_url', {'url': href}))
markdown_preview_enabled = md_view_settings.get('markdown_preview_enabled') is True
preview_id = md_view_settings.get('markdown_preview_id', None)
for view in w.views():
if view.id() == view_id:
show_html(view, html)
break
if not markdown_preview_enabled:
if preview_id is not None:
close_preview(md_view_settings, get_view_from_id(window, preview_id))
return
if preview_id is None:
preview, preview_settings = create_preview(window, md_view)
else:
preview = create_preview_panel()
show_html(preview, html)
preview = get_view_from_id(window, preview_id)
if not preview:
md_view_settings.erase('markdown_preview_id')
md_view_settings.erase('markdown_preview_enabled')
return
preview_settings = preview.settings()
show_html(md_view, preview)
# class MarkdownInPopupCommand(sublime_plugin.EventListener):
class MarkdownInPopupCommandc:
def update_phantom(self, content, preview_view):
ph = sublime.Phantom(sublime.Region(0), content, sublime.LAYOUT_BLOCK)
self.phantom_set.update([ph])
def on_modified(self, view):
content = """
<style>
body {
margin:10px;
}
blockquote {
font-style: italic;
display: block;
margin: 10px;
}
</style>
"""
content += markdown2.markdown(view.substr(sublime.Region(0, view.size())))
# self.update_phantom(content)
def on_pre_close(self, view):
settings = view.settings()
if settings.get('markdown_preview_enabled') is True:
preview = get_view_from_id(view.window(), settings.get('markdown_preview_id'))
if preview:
sublime.set_timeout_async(lambda: preview.close(), 100)

View File

@ -2,6 +2,27 @@
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
> Explicit is better than implicit
#### The Zen of Python, by Tim Peters
> Beautiful is better than ugly.
> Explicit is better than implicit.
> Simple is better than complex.
> Complex is better than complicated.
> Flat is better than nested.
> Sparse is better than dense.
> Readability counts.
> Special cases aren't special enough to break the rules.
> Although practicality beats purity.
> Errors should never pass silently.
> Unless explicitly silenced.
> In the face of ambiguity, refuse the temptation to guess.
> There should be one-- and preferably only one --obvious way to do it.
> Although that way may not be obvious at first unless you're Dutch.
> Now is better than never.
> Although never is often better than *right* now.
> If the implementation is hard to explain, it's a bad 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!
> Code tells you how, comments tells you why
Hi!

View File

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