Skip to content
Browse files

General tidy up.

  • Loading branch information...
1 parent 3d4e6f4 commit cc2743cd1d5c1b91caedec50f4238cbde8fc84df @d0ugal d0ugal committed
View
1 AUTHORS
@@ -0,0 +1 @@
+Dougal Matthews
View
4 Base File.sublime-settings
@@ -0,0 +1,4 @@
+
+{
+
+}
View
3 Default (Linux).sublime-keymap
@@ -0,0 +1,3 @@
+[
+ { "keys": ["ctrl+shift+r"], "command": "rstpreview"}
+]
View
3 Default (OSX).sublime-keymap
@@ -0,0 +1,3 @@
+[
+ { "keys": ["super+shift+r"], "command": "rstpreview"}
+]
View
3 Default (Windows).sublime-keymap
@@ -0,0 +1,3 @@
+[
+ { "keys": ["ctrl+shift+r"], "command": "rstpreview"}
+]
View
6 Default.sublime-commands
@@ -0,0 +1,6 @@
+[
+ {
+ "caption": "RST Preview",
+ "command": "rstpreview"
+ }
+]
View
21 LICENSE
@@ -0,0 +1,21 @@
+RstPreview
+-----------------
+Copyright (c) 2011 RstPreview authors (see AUTHORS file)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
7 README.rst
@@ -3,3 +3,10 @@ RstPreview - Sublime Text
This is a simple plugin, to convert the RST your reading into HTML and opening
it in your default browser.
+
+Shortcuts
+-------------------
+
+- Mac; cmd + shift + r
+- Windows; ctrl + shift + r
+- Linux; ctrl + shift + r
View
40 RstPreview.py
@@ -1,4 +1,8 @@
-from tempfile import NamedTemporaryFile
+"""
+RstPreview renders reStructuredText files as HTML and shows them in your
+default browser.
+"""
+from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from webbrowser import open as open_in_browser
from sublime_plugin import TextCommand
@@ -7,14 +11,38 @@
from docutils.core import publish_string
+def render_in_browser(html):
+ """
+ Starts a simple HTTP server, directs the browser to it and handles that
+ request before closing down. This avoids the need to create many temp
+ files. However, it does mean the page can't be reloaded after which is
+ a little odd.
+ """
+
+ class RequestHandler(BaseHTTPRequestHandler):
+
+ def do_GET(self):
+ """
+ Write the HTML to the request file
+ """
+ self.wfile.write(html)
+
+ # Start the server on a given random port
+ server = HTTPServer(('127.0.0.1', 0), RequestHandler)
+ # point the browser to that IP and port.
+ open_in_browser('http://127.0.0.1:%s' % server.server_port)
+ # handle the single request and then end.
+ server.handle_request()
+
+
class RstpreviewCommand(TextCommand):
- def run(self, edit):
- f = NamedTemporaryFile(delete=False)
+ def run(self, edit):
+ # Select all the text in the current document
text = self.view.substr(Region(0, self.view.size()))
- f.write(publish_string(text, writer_name='html'))
- f.close()
+ # Write that RST text as HTML
+ html = publish_string(text, writer_name='html')
- open_in_browser("file://%s" % f.name)
+ render_in_browser(html)

0 comments on commit cc2743c

Please sign in to comment.
Something went wrong with that request. Please try again.