Skip to content

Commit

Permalink
First working version
Browse files Browse the repository at this point in the history
  • Loading branch information
cvladan committed Oct 2, 2012
1 parent 2433e45 commit cd155a2
Show file tree
Hide file tree
Showing 8 changed files with 1,333 additions and 25 deletions.
28 changes: 4 additions & 24 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
# Compiled Python Code
*.py[co]

# Packages
*.egg
*.egg-info
dist
build
eggs
parts
bin
var
sdist
develop-eggs
.installed.cfg
# Sublime Text 2 files
*.sublime-project
*.sublime-workspace

# Installer logs
pip-log.txt

# Unit test / coverage reports
.coverage
.tox

#Translations
*.mo

#Mr Developer
.mr.developer.cfg
10 changes: 10 additions & 0 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"caption": "Strapdown Preview: preview in a browser",
"command": "strapdown_preview", "args": { "target": "browser" }
},
{
"caption": "Strapdown Preview: preview in Sublime Text",
"command": "strapdown_preview", "args": { "target": "sublime" }
}
]
63 changes: 62 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,65 @@
StrapdownPreview
================

Markdown preview in Sublime Text 2 using Strapdown.js (Twitter Bootstrap + Markdown)
Markdown preview in Sublime Text 2 using Strapdown.js (Twitter Bootstrap + Markdown)




<!--
title: Strapdown.js preview for Markdown files in Sublime Text 2
theme: cerulean
Theme can be any of the following: amelia, cerulean, cyborg, journal, readable, simplex, slate, spacelab, spruce, superhero, united
-->

Strapdown.js preview for Markdown files in Sublime Text 2
=========================================================

Bootstrap (Twitter HTML5 & CSS framework) + Markdown

The plugin expects the top of your text file/active window to have optional tags to define blog post details:





A simple ST2 plugin to help you preview your markdown files quickly in you web browser.

You can use builtin [python-markdown2][0] parser (default) or use the [github markdown API][5] for the conversion (edit your settings to select it).

If you have the ST2 LiveReload plugin, your browser will autorefresh the display when you save your file :)

NOTE: If you choose the GitHub API for conversion, your code will be sent through https to github for live conversion. You'll have Github flavored markdown and syntax highlighting for free.

## Installation :

- you should use [sublime package manager][3]
- use `cmd+shift+P` then `Package Control: Install Package`
- look for `MarkdownPreview` and install it.

## Usage :

- use `cmd+shift+P` then `MarkdownPreview` to launch a preview
- or bind some key in your user key binding, using a line like this one:
`{ "keys": ["alt+m"], "command": "markdown_preview", "args": {"target": "browser"} },`
- once converted a first time, the output HTML will be updated on each file save (with LiveReload plugin)

## Uses :

- [python-markdown2][0] for markdown parsing **OR** the GitHub markdown API.
- [clownfart markown.css][1] for markdown styling


## Licence :

The code is available at github [https://github.com/revolunet/sublimetext-markdown-preview][2] under MIT licence : [http://revolunet.mit-license.org][4]

[0]: https://github.com/trentm/python-markdown2
[1]: https://github.com/clownfart/Markdown-CSS
[2]: https://github.com/revolunet/sublimetext-markdown-preview
[3]: http://wbond.net/sublime_packages/package_control
[4]: http://revolunet.mit-license.org
[5]: http://developer.github.com/v3/markdown
117 changes: 117 additions & 0 deletions StrapdownPreview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import sublime, sublime_plugin
import desktop
import tempfile
import os
import sys
import re

settings = sublime.load_settings('StrapdownPreview.sublime-settings')

def getTempFilename(view):
return os.path.join(tempfile.gettempdir(), '%s.html' % view.id())

class StrapdownPreviewListener(sublime_plugin.EventListener):
""" update the output html when file has already been saved once """

def on_post_save(self, view):
if view.file_name().endswith(('.md', '.markdown', '.mdown')):
temp_file = getTempFilename(view)

if os.path.isfile(temp_file):
# reexec markdown conversion
print 'started again but on disk'
view.run_command('strapdown_preview', {'target': 'disk'})
sublime.status_message('Strapdown.js preview file updated')

class StrapdownPreviewCommand(sublime_plugin.TextCommand):
def run(self, edit, target = 'browser'):

contents = self.view.substr(sublime.Region(0, self.view.size()))
encoding = self.view.encoding()

if encoding == 'Undefined':
encoding = 'utf-8'
elif encoding == 'Western (Windows 1252)':
encoding = 'windows-1252'

# read header content
title, theme = self.getMeta(contents)

# check if LiveReload ST2 extension installed
livereload_installed = ('LiveReload' in os.listdir(sublime.packages_path()))

# 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'
html += '<script src="http://strapdownjs.com/v/0.1/strapdown.js"></script>\n'

if livereload_installed:
html += '<script>document.write(\'<script src="http://\' + (location.host || \'localhost\').split(\':\')[0] + \':35729/livereload.js?snipver=1"></\' + \'script>\')</script>\n'

html += '</html>'

if target in ['disk', 'browser']:
# update output html file (executed both for disk and browser targets)

tmp_fullpath = getTempFilename(self.view)
tmp_html = open(tmp_fullpath, 'w')
tmp_html.write(html.encode(encoding))
tmp_html.close()

if target == 'browser':
config_browser = settings.get('browser', 'default')

if config_browser and config_browser != 'default':
cmd = '"%s" %s' % (config_browser, tmp_fullpath)

if sys.platform == 'darwin':
# Mac OSX
cmd = "open -a %s" % cmd
print "Strapdown.js Preview: executing", cmd
result = os.system(cmd)

if result != 0:
sublime.error_message('cannot execute "%s" Please check your Markdown Preview settings' % config_browser)
else:
sublime.status_message('Strapdown.js preview launched in %s' % config_browser)

else:
# open default browser
desktop.open(tmp_fullpath)
sublime.status_message('Strapdown.js preview launched in default HTML viewer')

elif target == 'sublime':
new_view = self.view.window().new_file()
new_edit = new_view.begin_edit()
new_view.insert(new_edit, 0, html)
new_view.end_edit(new_edit)
sublime.status_message('Strapdown.js preview launched in sublime')

def getMeta(self, string):

filename = self.view.file_name()

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

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

match = re.search(re.compile(r'<!--.*title:\s*([^\n]*)\s*\n.*-->', re.IGNORECASE | re.DOTALL), string)
if match:
result["title"] = match.group(1)

match = re.search(re.compile(r'<!--.*theme:\s*([^\n]*)\s*\n.*-->', re.IGNORECASE | re.DOTALL), string)
if match:
result["theme"] = match.group(1)

return result["title"], result["theme"]

22 changes: 22 additions & 0 deletions StrapdownPreview.sublime-settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Strapdown.js Preview default settings
*/
{
/*
Sets the default opener for html files
default - Use the system default HTML viewer
other - Set a full path to any executable. ex: /Applications/Google Chrome Canary.app or /Applications/Firefox.app
*/

"browser": "default",

/*
Default Bootstrap theme.
Possible values: amelia, cerulean, cyborg, journal, readable, simplex, slate, spacelab, spruce, superhero, united
*/

"theme": "united"
}


Loading

0 comments on commit cd155a2

Please sign in to comment.