Skip to content
This repository has been archived by the owner on May 17, 2018. It is now read-only.

Commit

Permalink
One command to rule the all
Browse files Browse the repository at this point in the history
- Add MarkdownPreviewSelectCommand
- Quick panel option is now only MarkdownPreviewSelectCommand

When multiple parsers are enabled in “enabled_parsers” the user will be
prompted for which parser to use.  If only one is defined in the list,
that parser will automatically be run.
  • Loading branch information
facelessuser committed Jul 5, 2014
1 parent 9d27536 commit 48a005d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 53 deletions.
50 changes: 9 additions & 41 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
@@ -1,61 +1,29 @@
[
{
"caption": "Markdown Preview: Python Markdown: Preview in Browser",
"command": "markdown_preview",
"caption": "Markdown Preview: Preview in Browser",
"command": "markdown_preview_select",
"args": {
"target": "browser",
"parser": "markdown"
"target": "browser"
}
},
{
"caption": "Markdown Preview: Python Markdown: Export HTML in Sublime Text",
"command": "markdown_preview",
"caption": "Markdown Preview: Export HTML in Sublime Text",
"command": "markdown_preview_select",
"args": {
"target": "sublime",
"parser": "markdown"
"target": "sublime"
}
},
{
"caption": "Markdown Preview: Python Markdown: Copy to Clipboard",
"command": "markdown_preview",
"caption": "Markdown Preview: Copy to Clipboard",
"command": "markdown_preview_select",
"args": {
"target": "clipboard",
"parser": "markdown"
"target": "clipboard"
}
},


{
"caption": "Markdown Preview: Github Flavored Markdown: Preview in Browser",
"command": "markdown_preview",
"args": {
"target": "browser",
"parser": "github"
}
},
{
"caption": "Markdown Preview: Github Flavored Markdown: Export HTML in Sublime Text",
"command": "markdown_preview",
"args": {
"target": "sublime",
"parser": "github"
}
},
{
"caption": "Markdown Preview: Github Flavored Markdown: Copy to Clipboard",
"command": "markdown_preview",
"args": {
"target": "clipboard",
"parser": "github"
}
},
{
"caption": "Markdown Preview: Open Markdown Cheat sheet",
"command": "markdown_cheatsheet",
"args": {}
}




]
68 changes: 56 additions & 12 deletions MarkdownPreview.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def isurl(self, css_name):
return True
return False

def get_default_css(self, parser):
def get_default_css(self):
''' locate the correct CSS with the 'css' setting '''
css_name = self.settings.get('css', 'default')

Expand All @@ -231,7 +231,7 @@ def get_default_css(self, parser):
return u"<style>%s</style>" % load_utf8(os.path.expanduser(css_name))
elif css_name == 'default':
# use parser CSS file
return u"<style>%s</style>" % load_resource('github.css' if parser == 'github' else 'markdown.css')
return u"<style>%s</style>" % load_resource('github.css' if self.parser == 'github' else 'markdown.css')

return ''

Expand All @@ -250,9 +250,9 @@ def get_override_css(self):
return u"<style>%s</style>" % load_utf8(css_filename)
return ''

def get_stylesheet(self, parser):
def get_stylesheet(self):
''' return the correct CSS file based on parser and settings '''
return self.get_default_css(parser) + self.get_override_css()
return self.get_default_css() + self.get_override_css()

def get_javascript(self):
js_files = self.settings.get('js')
Expand Down Expand Up @@ -283,7 +283,7 @@ def get_highlight(self):
''' return the Highlight.js and css if enabled '''

highlight = ''
if self.settings.get('enable_highlight') is True and self.settings.get('parser') == 'default':
if self.settings.get('enable_highlight') is True and self.parser == 'default':
highlight += "<style>%s</style>" % load_resource('highlight.css')
highlight += "<script>%s</script>" % load_resource('highlight.js')
if self.settings.get("highlight_js_guess", True):
Expand Down Expand Up @@ -419,7 +419,7 @@ def process_extensions(self, extensions):
if filename and os.path.exists(filename):
base_path = os.path.dirname(filename)

if self.settings.get("get_highlight") and self.settings.get("parser") == "default":
if self.settings.get("get_highlight") and self.parser == "default":
found = False
for e in extensions:
if e.startswith("codehilite"):
Expand Down Expand Up @@ -470,11 +470,11 @@ def curl_convert(self, data):
sublime.error_message('cannot use github API to convert markdown. SSL is not included in your Python installation. And using curl didn\'t work either')
return None

def convert_markdown(self, markdown_text, parser):
def convert_markdown(self, markdown_text):
''' convert input markdown to HTML, with github or builtin parser '''

markdown_html = _CANNOT_CONVERT
if parser == 'github':
if self.parser == 'github':
github_oauth_token = self.settings.get('github_oauth_token')

# use the github API
Expand Down Expand Up @@ -514,7 +514,7 @@ def convert_markdown(self, markdown_text, parser):
else:
sublime.status_message('converted markdown with github API successfully')

elif parser == 'markdown2':
elif self.parser == 'markdown2':
# convert the markdown
enabled_extras = set(self.get_config_extensions(['footnotes', 'toc', 'fenced-code-blocks', 'cuddled-lists']))
if self.settings.get("enable_mathjax") is True or self.settings.get("enable_highlight") is True:
Expand Down Expand Up @@ -553,18 +553,19 @@ def run(self, view, parser, wholefile=False):
''' return full html and body html for view. '''
self.settings = sublime.load_settings('MarkdownPreview.sublime-settings')
self.view = view
self.parser = parser

contents = self.get_contents(wholefile)

body = self.convert_markdown(contents, parser)
body = self.convert_markdown(contents)

html_template = self.settings.get('html_template')

# use customized html template if given
if html_template and os.path.exists(html_template):
head = u''
if not self.settings.get('skip_default_stylesheet'):
head += self.get_stylesheet(parser)
head += self.get_stylesheet()
head += self.get_javascript()
head += self.get_highlight()
head += self.get_mathjax()
Expand All @@ -576,7 +577,7 @@ def run(self, view, parser, wholefile=False):
else:
html = u'<!DOCTYPE html>'
html += '<html><head><meta charset="utf-8">'
html += self.get_stylesheet(parser)
html += self.get_stylesheet()
html += self.get_javascript()
html += self.get_highlight()
html += self.get_mathjax()
Expand All @@ -592,6 +593,49 @@ def run(self, view, parser, wholefile=False):
compiler = MarkdownCompiler()


class MarkdownPreviewSelectCommand(sublime_plugin.TextCommand):
def run(self, edit, target='browser'):
parsers = [
"markdown",
"markdown2",
"github"
]

self.target = target

settings = sublime.load_settings("MarkdownPreview.sublime-settings")
enabled_parsers = set()
for p in settings.get("enabled_parsers", ["markdown", "github"]):
if p in parsers:
enabled_parsers.add(p)

self.user_parsers = list(enabled_parsers)

window = self.view.window()
length = len(self.user_parsers)
if window is not None and length:
if length == 1:
self.view.run_command(
"markdown_preview",
{
"parser": self.user_parsers[0],
"target": self.target
}
)
else:
window.show_quick_panel(self.user_parsers, self.run_command)

def run_command(self, value):
if value > -1:
self.view.run_command(
"markdown_preview",
{
"parser": self.user_parsers[value],
"target": self.target
}
)


class MarkdownPreviewCommand(sublime_plugin.TextCommand):
def run(self, edit, parser='markdown', target='browser'):
settings = sublime.load_settings('MarkdownPreview.sublime-settings')
Expand Down
10 changes: 10 additions & 0 deletions MarkdownPreview.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@
*/
"enabled_extensions": "default",

/*
Enabled parsers for the parser "select parser" command
Available parsers: markdown, markdown2, github
When there are more than one parser in the list, Sublime will prompt
via the quick panel for which one to use. If there is only one, it
will automatically run that parser.
*/
"enabed_parsers": ["markdown", "github"],

/*
Default mode for the github Markdown parser : markdown (documents) or gfm (comments)
see http://developer.github.com/v3/markdown/#render-an-arbitrary-markdown-document
Expand Down

0 comments on commit 48a005d

Please sign in to comment.