Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements #9

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ if you need it again.
- or bind some key in your user key binding, using a line like this
one:
`{ "keys": ["ctrl+alt+m"], "command": "strapdown_markdown_preview", "args": {"target": "browser"} },`


`target` argument can be:

* `browser`: Creates a HTML file in the `Packages` folder and opens it with the
configured browser. Temporary file is automatically deleted after 15 seconds.
* `disk`: Creates a HTML file within the folder of the original markdown file and
opens it with the configured browser.
* `sublime`: Opens a new view in Sublime Text and puts the HTML content in it.

### Metadata


Expand Down
62 changes: 34 additions & 28 deletions Strapdown Markdown Preview.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import sys
import os
import re
import tempfile

import urllib.request

import html
import webbrowser

import sublime
import sublime_plugin

from threading import Timer

# Check if this is Sublime Text 2
#
Expand All @@ -20,7 +21,12 @@
STRAPDOWN_LIB_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), 'strapdown'))

def getTempFilename(view):
return os.path.join(tempfile.gettempdir(), '%s.html' % view.id())
file = os.path.join(sublime.packages_path(), 'strapdown-preview-%s.html' % view.id())

# Remove the temporary file after 15 seconds.
Timer(15, lambda: os.remove(file)).start()

return file

class StrapdownMarkdownPreviewCommand(sublime_plugin.TextCommand):
def run(self, edit, target = 'browser'):
Expand All @@ -43,54 +49,54 @@ def run(self, edit, target = 'browser'):

# Construct the content
#
html = u'<!DOCTYPE html>\n'
html += '<html>\n'
html += '<head>\n'
html += '<meta charset="%s">\n' % encoding
html += '<title>%s</title>\n' % title
html += '</head>\n'
html += '<xmp theme="%s" style="display:none;">\n' % theme
html += contents
html += '\n</xmp>\n'

config_local = self.settings.get('strapdown', 'default')
output_html = u'<!DOCTYPE html><html><head><meta charset="%s"><title>%s</title></head>\n' % (encoding, title)
output_html += '<xmp theme="%s" style="display:none;">\n\n' % theme
output_html += contents
output_html += '\n</xmp>'

config_local = self.settings.get('strapdown', 'remote')
if config_local and config_local == 'local':
html += '<script src="%s"></script>\n' % urllib.request.pathname2url(os.path.join(STRAPDOWN_LIB_DIR, "strapdown.js"))
output_html += '<script src="%s"></script>' % urllib.request.pathname2url(os.path.join(STRAPDOWN_LIB_DIR, "strapdown.js"))
else:
html += '<script src="http://strapdownjs.com/v/0.2/strapdown.js"></script>\n'
output_html += '<script src="' + html.escape(self.settings.get('remote', 'http://strapdownjs.com/v/0.2/strapdown.js')) + '"></script>'

html += '</html>'
output_html += '</html>'

# Update output HTML file. It is executed both for disk and also for
# browser targets
#
if target in ['disk', 'browser']:

tmp_fullpath = getTempFilename(self.view)
tmp_html = open(tmp_fullpath, 'wt', encoding=encoding)
tmp_html.write(html)
tmp_html.close()
if not self.view.file_name():
target = "browser"

if target == "disk":
target_file = os.path.splitext(self.view.file_name())[0] + ".html"

if target == 'browser':
browser = self.settings.get('browser')
controller = webbrowser.get(browser)
controller.open(tmp_fullpath)
sublime.status_message('Preview launched in default browser')
elif target == "browser":
target_file = getTempFilename(self.view)

with open(target_file, 'wt', encoding=encoding) as f:
f.write(output_html)

browser = self.settings.get('browser')
controller = webbrowser.get(browser)
controller.open(target_file)
sublime.status_message('Preview launched in default browser')

elif target == 'sublime':
new_view = self.view.window().new_file()
new_view.set_name(title + ".html")
new_view.insert(edit, 0, html)
new_view.insert(edit, 0, output_html)
sublime.status_message('Preview launched in Sublime Text')

def getMeta(self, string):

filename = self.view.file_name()

if filename:
title, extension = os.path.splitext(os.path.basename(filename))
else:
title = 'Untitled document'
title = self.view.name() or 'Untitled document'

result = {"title": title, "theme" : self.settings.get('theme', 'united') }

Expand Down
15 changes: 12 additions & 3 deletions Strapdown Markdown Preview.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
/*
Sets which Strapdown.js version to use.

default: Use the remote version downloaded at
remote: Use the remote version downloaded at
a time of viewing a document
local: Use version from hard disk.

Expand All @@ -49,7 +49,16 @@
computer, this version will not work as expected.
*/

"strapdown": "local"
}
"strapdown": "local",

/*
URL to remote strapdown.js file. If you will upload your own remote strapdown.js,
don't forget to upload other files using the below hierarchy.

+ strapdown
+ themes
strapdown.js
strapdown.css
*/
"remote": "http://strapdownjs.com/v/0.2/strapdown.js"
}