From 27dcd57657e306e347d98cfc10c56baa4ae35f7d Mon Sep 17 00:00:00 2001 From: Ali Ayas Date: Tue, 3 Dec 2013 20:50:08 +0200 Subject: [PATCH 01/10] Make remote js URL customizable --- Strapdown Markdown Preview.py | 31 +++++++++++---------- Strapdown Markdown Preview.sublime-settings | 13 +++++++-- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/Strapdown Markdown Preview.py b/Strapdown Markdown Preview.py index e44cba1..5054595 100644 --- a/Strapdown Markdown Preview.py +++ b/Strapdown Markdown Preview.py @@ -5,13 +5,14 @@ import sublime_plugin import re -import json import tempfile import urllib.request import urllib.response import urllib.error +import html + from . import desktop # Check if this is Sublime Text 2 @@ -48,23 +49,23 @@ def run(self, edit, target = 'browser'): # Construct the content # - html = u'\n' - html += '\n' - html += '\n' - html += '\n' % encoding - html += '%s\n' % title - html += '\n' - html += '\n' % theme - html += contents - html += '\n\n' + output_html = u'\n' + output_html += '\n' + output_html += '\n' + output_html += '\n' % encoding + output_html += '%s\n' % title + output_html += '\n' + output_html += '\n' % theme + output_html += contents + output_html += '\n\n' config_local = settings.get('strapdown', 'default') if config_local and config_local == 'local': - html += '\n' % urllib.request.pathname2url(os.path.join(STRAPDOWN_LIB_DIR, "strapdown.js")) + output_html += '\n' % urllib.request.pathname2url(os.path.join(STRAPDOWN_LIB_DIR, "strapdown.js")) else: - html += '\n' + output_html += '\n' - html += '' + output_html += '' # Update output HTML file. It is executed both for disk and also for # browser targets @@ -73,7 +74,7 @@ def run(self, edit, target = 'browser'): tmp_fullpath = getTempFilename(self.view) tmp_html = open(tmp_fullpath, 'wt', encoding=encoding) - tmp_html.write(html) + tmp_html.write(output_html) tmp_html.close() if target == 'browser': @@ -104,7 +105,7 @@ def run(self, edit, target = '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): diff --git a/Strapdown Markdown Preview.sublime-settings b/Strapdown Markdown Preview.sublime-settings index c4bb8db..3a7bcf1 100644 --- a/Strapdown Markdown Preview.sublime-settings +++ b/Strapdown Markdown Preview.sublime-settings @@ -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" +} From 993712d26cad0caab1e6fffadf0abe2ba648a28f Mon Sep 17 00:00:00 2001 From: Ali Ayas Date: Tue, 3 Dec 2013 20:51:13 +0200 Subject: [PATCH 02/10] Rename `default` to `remote` in preferences --- Strapdown Markdown Preview.py | 2 +- Strapdown Markdown Preview.sublime-settings | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Strapdown Markdown Preview.py b/Strapdown Markdown Preview.py index 5054595..f9d1fa4 100644 --- a/Strapdown Markdown Preview.py +++ b/Strapdown Markdown Preview.py @@ -59,7 +59,7 @@ def run(self, edit, target = 'browser'): output_html += contents output_html += '\n\n' - config_local = settings.get('strapdown', 'default') + config_local = settings.get('strapdown', 'remote') if config_local and config_local == 'local': output_html += '\n' % urllib.request.pathname2url(os.path.join(STRAPDOWN_LIB_DIR, "strapdown.js")) else: diff --git a/Strapdown Markdown Preview.sublime-settings b/Strapdown Markdown Preview.sublime-settings index 3a7bcf1..818a140 100644 --- a/Strapdown Markdown Preview.sublime-settings +++ b/Strapdown Markdown Preview.sublime-settings @@ -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. From f90b345e1667d39f4038bc055e0bbdd5e3792db0 Mon Sep 17 00:00:00 2001 From: Ali Ayas Date: Tue, 3 Dec 2013 20:52:01 +0200 Subject: [PATCH 03/10] Small fix --- Strapdown Markdown Preview.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Strapdown Markdown Preview.py b/Strapdown Markdown Preview.py index f9d1fa4..c36a35d 100644 --- a/Strapdown Markdown Preview.py +++ b/Strapdown Markdown Preview.py @@ -109,6 +109,7 @@ def run(self, edit, target = 'browser'): sublime.status_message('Preview launched in Sublime Text') def getMeta(self, string): + settings = sublime.load_settings("Strapdown Markdown Preview.sublime-settings") filename = self.view.file_name() From d7bb88911c8a0ec470079b0b9e07f7cdce75c053 Mon Sep 17 00:00:00 2001 From: Ali Ayas Date: Sat, 28 Mar 2020 15:27:51 +0300 Subject: [PATCH 04/10] README: Document the `target` argument --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a0008e4..8de8d18 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,13 @@ 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 OS temporary folder and opens it with the configured browser. +* `disk`: Creates a HTML file in the OS temporary folder. +* `sublime`: Opens a new view in Sublime Text and puts the HTML content in it. + ### Metadata From e6a92006e5be9c00de1ba7edd469acec2fc4d80f Mon Sep 17 00:00:00 2001 From: Ali Ayas Date: Sat, 28 Mar 2020 15:37:15 +0300 Subject: [PATCH 05/10] For `target: disk`, create the HTML file within the same folder --- README.md | 2 +- Strapdown Markdown Preview.py | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8de8d18..e3cee20 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ if you need it again. `target` argument can be: * `browser`: Creates a HTML file in the OS temporary folder and opens it with the configured browser. -* `disk`: Creates a HTML file in the OS temporary folder. +* `disk`: Creates a HTML file within the folder of the original markdown file. * `sublime`: Opens a new view in Sublime Text and puts the HTML content in it. ### Metadata diff --git a/Strapdown Markdown Preview.py b/Strapdown Markdown Preview.py index 2856060..1cdde8d 100644 --- a/Strapdown Markdown Preview.py +++ b/Strapdown Markdown Preview.py @@ -67,15 +67,22 @@ def run(self, edit, target = 'browser'): # if target in ['disk', 'browser']: - tmp_fullpath = getTempFilename(self.view) - tmp_html = open(tmp_fullpath, 'wt', encoding=encoding) - tmp_html.write(output_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" + + elif target == "browser": + target_file = getTempFilename(self.view) + + with open(target_file, 'wt', encoding=encoding) as f: + f.write(output_html) if target == 'browser': browser = self.settings.get('browser') controller = webbrowser.get(browser) - controller.open(tmp_fullpath) + controller.open(target_file) sublime.status_message('Preview launched in default browser') elif target == 'sublime': From 5297bd7d471f5597647eaff3fa09532332cdcf38 Mon Sep 17 00:00:00 2001 From: Ali Ayas Date: Sat, 28 Mar 2020 18:07:41 +0300 Subject: [PATCH 06/10] For `target: disk`, open the HTML file after creating it --- README.md | 3 ++- Strapdown Markdown Preview.py | 9 ++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e3cee20..96a32d7 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,8 @@ if you need it again. `target` argument can be: * `browser`: Creates a HTML file in the OS temporary folder and opens it with the configured browser. -* `disk`: Creates a HTML file within the folder of the original markdown file. +* `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 diff --git a/Strapdown Markdown Preview.py b/Strapdown Markdown Preview.py index 1cdde8d..68fbdf8 100644 --- a/Strapdown Markdown Preview.py +++ b/Strapdown Markdown Preview.py @@ -79,11 +79,10 @@ def run(self, edit, target = 'browser'): with open(target_file, 'wt', encoding=encoding) as f: f.write(output_html) - if target == 'browser': - browser = self.settings.get('browser') - controller = webbrowser.get(browser) - controller.open(target_file) - sublime.status_message('Preview launched in default browser') + 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() From 04770eabde59ff9e07084834771cdf6f22a2e0df Mon Sep 17 00:00:00 2001 From: Ali Ayas Date: Sat, 28 Mar 2020 16:57:51 +0300 Subject: [PATCH 07/10] Automatically remove the temporary file after 15 seconds --- README.md | 3 ++- Strapdown Markdown Preview.py | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 96a32d7..91ca30d 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,8 @@ if you need it again. `target` argument can be: -* `browser`: Creates a HTML file in the OS temporary folder and opens it with the configured browser. +* `browser`: Creates a HTML file in the OS temporary 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. diff --git a/Strapdown Markdown Preview.py b/Strapdown Markdown Preview.py index 68fbdf8..036aaa3 100644 --- a/Strapdown Markdown Preview.py +++ b/Strapdown Markdown Preview.py @@ -11,6 +11,7 @@ import sublime import sublime_plugin +from threading import Timer # Check if this is Sublime Text 2 # @@ -21,7 +22,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(tempfile.gettempdir(), '%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'): From 20e827830f37ea3ce245d12455730beb84fba79e Mon Sep 17 00:00:00 2001 From: Ali Ayas Date: Sat, 28 Mar 2020 17:20:13 +0300 Subject: [PATCH 08/10] Don't use OS temporary folder Sublime Text can be used as a portable app, in which case leaving file traces in the OS directories would be a bad practice. For this reason, this commit uses `SublimeText/Data/Packages` folder instead. --- README.md | 2 +- Strapdown Markdown Preview.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 91ca30d..6bfe67e 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ if you need it again. `target` argument can be: -* `browser`: Creates a HTML file in the OS temporary folder and opens it with the +* `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. diff --git a/Strapdown Markdown Preview.py b/Strapdown Markdown Preview.py index 036aaa3..0f272d5 100644 --- a/Strapdown Markdown Preview.py +++ b/Strapdown Markdown Preview.py @@ -1,7 +1,6 @@ import sys import os import re -import tempfile import urllib.request @@ -22,7 +21,7 @@ STRAPDOWN_LIB_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), 'strapdown')) def getTempFilename(view): - file = 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() From efd1f33d35252abec67d604c0ede5eb54b5cbeb7 Mon Sep 17 00:00:00 2001 From: Ali Ayas Date: Sat, 28 Mar 2020 17:25:57 +0300 Subject: [PATCH 09/10] Use view name as document title if available --- Strapdown Markdown Preview.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Strapdown Markdown Preview.py b/Strapdown Markdown Preview.py index 0f272d5..07612ea 100644 --- a/Strapdown Markdown Preview.py +++ b/Strapdown Markdown Preview.py @@ -101,7 +101,7 @@ def getMeta(self, string): 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') } From c7de4e28f92fb8a216aa312b9ccae119e0572f1e Mon Sep 17 00:00:00 2001 From: Ali Ayas Date: Tue, 31 Mar 2020 18:27:17 +0300 Subject: [PATCH 10/10] Use more compact HTML --- Strapdown Markdown Preview.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/Strapdown Markdown Preview.py b/Strapdown Markdown Preview.py index 07612ea..b707dfe 100644 --- a/Strapdown Markdown Preview.py +++ b/Strapdown Markdown Preview.py @@ -49,21 +49,16 @@ def run(self, edit, target = 'browser'): # Construct the content # - output_html = u'\n' - output_html += '\n' - output_html += '\n' - output_html += '\n' % encoding - output_html += '%s\n' % title - output_html += '\n' - output_html += '\n' % theme + 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\n' + output_html += '\n' config_local = self.settings.get('strapdown', 'remote') if config_local and config_local == 'local': - output_html += '\n' % urllib.request.pathname2url(os.path.join(STRAPDOWN_LIB_DIR, "strapdown.js")) + output_html += '' % urllib.request.pathname2url(os.path.join(STRAPDOWN_LIB_DIR, "strapdown.js")) else: - output_html += '\n' + output_html += '' output_html += ''