Last active
December 16, 2015 04:19
-
-
Save aslakknutsen/5375839 to your computer and use it in GitHub Desktop.
Sublime AsciiDoctor Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import sublime | |
import sublime_plugin | |
import webbrowser | |
REG_RENAME = re.compile("\.(asciidoc|adoc|asc|ad)$") | |
EXT = re.compile(".*\.(asciidoc|adoc|asc|ad)$") | |
COMMAND = "asciidoctor -b html5" | |
def is_asciidoc_file(file_name): | |
return EXT.match(file_name) is not None | |
class AsciidocSaveListener(sublime_plugin.EventListener): | |
def on_post_save(self, view): | |
file_name = view.file_name() | |
if is_asciidoc_file(file_name): | |
sublime.status_message("Asciidoctor regenerating " + file_name) | |
self.run_shell_command(view, COMMAND + " " + file_name) | |
def run_shell_command(self, view, command): | |
if not command: | |
return False | |
view.window().run_command("exec", { | |
"cmd": [command], | |
"shell": True, | |
"quiet": True | |
}) | |
return True | |
class AsciidocBrowserCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
file_name = self.view.file_name() | |
if is_asciidoc_file(file_name): | |
webbrowser.open_new(REG_RENAME.sub('.html', file_name)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{"keys": ["super+a"], "command": "asciidoc_browser" } | |
] |
Works fine on OS X Mavericks, too! Thanks!
The only point I had to adjust is that webbrowser.open_new(REG_RENAME.sub('.html', file_name))
doesn't work since Mountain Lion. Therefore I changed this line to
import subprocess
subprocess.Popen(['open', REG_RENAME.sub('.html', file_name)])
sublime.status_message("opened " + file_name + " in your default browser!")
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add file under sublimes package folder "Packages/Asciidoctor/"
e.g. on linux: