/usr/share/pyshared/PythonCard/tools/oneEditor/restEditor.py is in python-pythoncard 0.8.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | """
__version__ = "$Revision: 1.1 $"
__date__ = "$Date: 2004/10/03 23:58:01 $"
"""
import os
from PythonCard import dialog, model
from codeEditor import CodeEditor
from wx import stc
import webbrowser
# reST
from PythonCard.templates.htmlpreview import HtmlPreview
from snippet import restify
class RestEditor(CodeEditor):
def on_initialize(self, event):
super(RestEditor, self).on_initialize(event)
self.renderOnReturn = self.menuBar.getChecked('menuFormatRenderOnReturn')
self.previewWindow = model.childWindow(self, HtmlPreview)
#self.previewWindow.position = (425, -1)
self.previewWindow.visible = True
self.html = ''
# reST
def on_previewPost_command(self, event):
self.previewWindow.Show()
txt = self.components.document.text
if self.menuBar.getChecked('menuViewSourceIsHtml'):
firstLine = txt[:10].lower()
if firstLine.startswith('<!doctype') or firstLine.startswith('<html'):
# assume full HTML document
html = txt
else:
html = '<html>\n<head>\n</head>\n<body>\n' + txt + '\n</body>\n</html>\n'
else:
# KEA 2004-08-15
# snippet.restify is returning None when there is a reST error
# what's a better way to provide feedback to the user without barfing
# all over the output?
rest = restify(txt)
if rest:
html = '<html>\n<head>\n</head>\n<body>\n' + rest + '\n</body>\n</html>\n'
else:
html = self.previewWindow.components.html.text
# do make sure stylesheets and relative image references can be found
# might need to chdir here
# won't work until there is a document path
curdir = os.getcwd()
self.previewWindow.components.html.text = html
self.html = html
os.chdir(curdir)
def on_document_keyDown(self, event):
if event.keyCode == 13 and self.renderOnReturn:
# since we won't be calling skip, insert a newline manually
self.components.document.CmdKeyExecute(stc.STC_CMD_NEWLINE)
self.on_previewPost_command(None)
else:
event.skip()
def saveHtmlFile(self, path):
try:
f = open(path, 'wb')
try:
f.write(self.html)
finally:
f.close()
except:
pass
def on_saveHtml_command(self, event):
wildcard = "HTML files (*.html)|*.html|All files (*.*)|*.*"
#wildcard = self.resource.strings.saveAsWildcard
if self.documentPath is None:
dir = ''
filename = '*.html'
else:
dir = os.path.dirname(self.documentPath)
filename = os.path.splitext(os.path.basename(self.documentPath))[0] + '.html'
result = dialog.saveFileDialog(None, self.resource.strings.saveAs, dir, filename, wildcard)
if result.accepted:
path = result.paths[0]
self.saveHtmlFile(path)
def on_menuFormatRenderOnReturn_select(self, event):
self.renderOnReturn = self.menuBar.getChecked('menuFormatRenderOnReturn')
def on_doHelpRest_command(self, event):
webbrowser.open('http://docutils.sourceforge.net/rst.html')
def on_doHelpRestQuickReference_command(self, event):
webbrowser.open('http://docutils.sourceforge.net/docs/user/rst/quickref.html')
if __name__ == '__main__':
app = model.Application(RestEditor)
app.MainLoop()
|