Skip to content

Instantly share code, notes, and snippets.

@aslakknutsen
Last active December 16, 2015 04:19
Show Gist options
  • Save aslakknutsen/5375839 to your computer and use it in GitHub Desktop.
Save aslakknutsen/5375839 to your computer and use it in GitHub Desktop.
Sublime AsciiDoctor Plugin
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))
[
{"keys": ["super+a"], "command": "asciidoc_browser" }
]
@aslakknutsen
Copy link
Author

Add file under sublimes package folder "Packages/Asciidoctor/"

e.g. on linux:

~/.config/sublime-text-2/Packages

@aheusingfeld
Copy link

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