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

Commit

Permalink
Merge pull request #212 from facelessuser/master
Browse files Browse the repository at this point in the history
Bug Fixes and Usability Changes
  • Loading branch information
facelessuser committed Jul 5, 2014
2 parents 4224359 + 48a005d commit 2045df2
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 58 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": {}
}




]
70 changes: 57 additions & 13 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 @@ -373,7 +373,7 @@ def postprocessor_base64(self, html):
def b64(m):
import base64
src = m.group('src')
data = src
data = m.group('tag')
filename = self.view.file_name()
base_path = ""
if filename and os.path.exists(filename):
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
23 changes: 23 additions & 0 deletions markdown/extensions/github.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from __future__ import unicode_literals
from ..extensions import Extension

extensions = [
'delete',
'githubemoji',
'magiclink',
'tasklist',
'headeranchor',
'nl2br'
]


class GithubExtension(Extension):
"""Add various extensions to Markdown class"""

def extendMarkdown(self, md, md_globals):
"""Register extension instances"""
md.registerExtensions(extensions, self.config)


def makeExtension(configs={}):
return GithubExtension(configs=dict(configs))
12 changes: 9 additions & 3 deletions markdown/extensions/headeranchor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from __future__ import absolute_import
from ..extensions import Extension
from ..treeprocessors import Treeprocessor
from .headerid import slugify, stashedHTML2text, itertext
from .headerid import slugify, stashedHTML2text, itertext, unique

LINK = '<a name="user-content-%(id)s" href="#%(id)s" class="headeranchor-link" aria-hidden="true"><span class="headeranchor"></span></a>'

Expand All @@ -26,13 +26,19 @@ class HeaderAnchorTreeprocessor(Treeprocessor):
def run(self, root):
""" Add header anchors """

# Get a list of id attributes
used_ids = set()
for tag in root.getiterator():
if "id" in tag.attrib:
used_ids.add(tag.attrib["id"])

for tag in root.getiterator():
if tag.tag in ('h1', 'h2', 'h3', 'h4', 'h5', 'h6'):
if "id" in tag.attrib:
id = tag.get('id')
else:
id = stashedHTML2text(''.join(itertext(tag)), self.md)
id = slugify(id, self.config.get('sep'))
id = unique(slugify(id, self.config.get('sep')), used_ids)
tag.set('id', id)
tag.text = self.markdown.htmlStash.store(
LINK % {"id": id},
Expand All @@ -59,7 +65,7 @@ def extendMarkdown(self, md, md_globals):
if 'toc' in md.treeprocessors.keys():
insertion = ">toc"
else:
insertion = ">_end"
insertion = "_end"
md.treeprocessors.add("headeranchor", self.processor, insertion)
md.registerExtension(self)

Expand Down
6 changes: 5 additions & 1 deletion markdown/extensions/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,11 @@ def extendMarkdown(self, md, md_globals):
# by the header id extension) if both are used. Same goes for
# attr_list extension. This must come last because we don't want
# to redefine ids after toc is created. But we do want toc prettified.
md.treeprocessors.add("toc", tocext, "_end")
if 'headeranchor' in md.treeprocessors.keys():
insertion = "<headeranchor"
else:
insertion = "_end"
md.treeprocessors.add("toc", tocext, insertion)


def makeExtension(configs={}):
Expand Down

0 comments on commit 2045df2

Please sign in to comment.