[1c330e]: retext.py Maximize Restore History

Download this file

retext.py    393 lines (363 with data), 15.7 kB

  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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt4.Qt import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import markdown
md = markdown.Markdown()
app_name = "ReText"
app_version = "0.3.2 alpha"
class HtmlHighlighter(QSyntaxHighlighter):
def __init__(self, parent):
QSyntaxHighlighter.__init__(self, parent)
def highlightBlock(self, text):
charFormat = QTextCharFormat()
patterns = ("<[^>]*>", "&[^;]*;", "\"[^\"]*\"", "<!--[^-->]*-->")
foregrounds = [Qt.darkMagenta, Qt.darkCyan, Qt.darkYellow, Qt.gray]
for i in range(len(patterns)):
expression = QRegExp(patterns[i])
index = expression.indexIn(text)
if i == 3:
charFormat.setFontWeight(QFont.Normal)
else:
charFormat.setFontWeight(QFont.Bold)
charFormat.setForeground(foregrounds[i]);
while (index >= 0):
length = expression.matchedLength()
self.setFormat(index, length, charFormat)
index = expression.indexIn(text, index + length)
class ReTextWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.resize(800, 600)
screen = QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)
self.setWindowTitle(self.tr('New document') + '[*] ' + QChar(0x2014) + ' ' + app_name)
self.setWindowIcon(QIcon.fromTheme('accessories-text-editor'))
self.centralwidget = QWidget(self)
self.verticalLayout = QVBoxLayout(self.centralwidget)
self.previewBox = QTextEdit(self.centralwidget)
self.previewBox.setVisible(False)
self.previewBox.setReadOnly(True)
self.verticalLayout.addWidget(self.previewBox)
self.editBox = QTextEdit(self.centralwidget)
self.editBox.setAcceptRichText(False)
monofont = QFont()
monofont.setFamily('monospace')
self.editBox.setFont(monofont)
self.verticalLayout.addWidget(self.editBox)
self.setCentralWidget(self.centralwidget)
self.syntaxHighlighter = HtmlHighlighter(self.editBox.document())
self.toolBar = QToolBar(self.tr('File toolbar'), self)
self.addToolBar(Qt.TopToolBarArea, self.toolBar)
self.editBar = QToolBar(self.tr('Edit toolbar'), self)
self.addToolBar(Qt.TopToolBarArea, self.editBar)
self.actionNew = QAction(QIcon.fromTheme('document-new'), self.tr('New'), self)
self.actionNew.setShortcut(QKeySequence.New)
self.connect(self.actionNew, SIGNAL('triggered()'), self.createNew)
self.actionOpen = QAction(QIcon.fromTheme('document-open'), self.tr('Open'), self)
self.actionOpen.setShortcut(QKeySequence.Open)
self.connect(self.actionOpen, SIGNAL('triggered()'), self.openFile)
self.actionSave = QAction(QIcon.fromTheme('document-save'), self.tr('Save'), self)
self.actionSave.setEnabled(False)
self.actionSave.setShortcut(QKeySequence.Save)
self.connect(self.editBox.document(), SIGNAL('modificationChanged(bool)'), self.modificationChanged)
self.connect(self.actionSave, SIGNAL('triggered()'), self.saveFile)
self.actionSaveAs = QAction(QIcon.fromTheme('document-save-as'), self.tr('Save as'), self)
self.actionSaveAs.setShortcut(QKeySequence.SaveAs)
self.connect(self.actionSaveAs, SIGNAL('triggered()'), self.saveFileAs)
self.actionPrint = QAction(QIcon.fromTheme('document-print'), self.tr('Print'), self)
self.actionPrint.setShortcut(QKeySequence.Print)
self.connect(self.actionPrint, SIGNAL('triggered()'), self.printFile)
self.actionPreview = QAction(QIcon.fromTheme('x-office-document'), self.tr('Preview'), self)
self.actionPreview.setCheckable(True)
self.connect(self.actionPreview, SIGNAL('triggered(bool)'), self.preview)
self.actionPerfectHtml = QAction(QIcon.fromTheme('text-html'), 'HTML', self)
self.connect(self.actionPerfectHtml, SIGNAL('triggered()'), self.saveFilePerfect)
self.actionPdf = QAction(QIcon.fromTheme('application-pdf'), 'PDF', self)
self.connect(self.actionPdf, SIGNAL('triggered()'), self.savePdf)
self.actionQuit = QAction(QIcon.fromTheme('application-exit'), self.tr('Quit'), self)
self.actionQuit.setShortcut(QKeySequence.Quit)
self.actionQuit.setMenuRole(QAction.QuitRole)
self.connect(self.actionQuit, SIGNAL('triggered()'), qApp, SLOT('quit()'))
self.actionUndo = QAction(QIcon.fromTheme('edit-undo'), self.tr('Undo'), self)
self.actionUndo.setShortcut(QKeySequence.Undo)
self.actionRedo = QAction(QIcon.fromTheme('edit-redo'), self.tr('Redo'), self)
self.actionRedo.setShortcut(QKeySequence.Redo)
self.connect(self.actionUndo, SIGNAL('triggered()'), self.editBox, SLOT('undo()'))
self.connect(self.actionRedo, SIGNAL('triggered()'), self.editBox, SLOT('redo()'))
self.actionUndo.setEnabled(False)
self.actionRedo.setEnabled(False)
self.connect(self.editBox.document(), SIGNAL('undoAvailable(bool)'), self.actionUndo, SLOT('setEnabled(bool)'))
self.connect(self.editBox.document(), SIGNAL('redoAvailable(bool)'), self.actionRedo, SLOT('setEnabled(bool)'))
self.actionCopy = QAction(QIcon.fromTheme('edit-copy'), self.tr('Copy'), self)
self.actionCopy.setShortcut(QKeySequence.Copy)
self.actionCopy.setEnabled(False)
self.actionCut = QAction(QIcon.fromTheme('edit-cut'), self.tr('Cut'), self)
self.actionCut.setShortcut(QKeySequence.Cut)
self.actionCut.setEnabled(False)
self.actionPaste = QAction(QIcon.fromTheme('edit-paste'), self.tr('Paste'), self)
self.actionPaste.setShortcut(QKeySequence.Paste)
self.connect(self.actionCut, SIGNAL('triggered()'), self.editBox, SLOT('cut()'))
self.connect(self.actionCopy, SIGNAL('triggered()'), self.editBox, SLOT('copy()'))
self.connect(self.actionPaste, SIGNAL('triggered()'), self.editBox, SLOT('paste()'))
self.connect(qApp.clipboard(), SIGNAL('dataChanged()'), self.clipboardDataChanged)
self.clipboardDataChanged()
self.actionAutoFormatting = QAction(self.tr('Auto-formatting'), self)
self.actionAutoFormatting.setCheckable(True)
self.actionAutoFormatting.setChecked(True)
self.connect(self.actionAutoFormatting, SIGNAL('triggered(bool)'), self.enableAutoFormatting)
self.actionRecentFiles = QAction(QIcon.fromTheme('document-open-recent'), self.tr('Open recent'), self)
self.connect(self.actionRecentFiles, SIGNAL('triggered()'), self.openRecent)
self.actionAbout = QAction(QIcon.fromTheme('help-about'), self.tr('About %1').arg(app_name), self)
self.actionAbout.setMenuRole(QAction.AboutRole)
self.connect(self.actionAbout, SIGNAL('triggered()'), self.aboutDialog)
self.actionAboutQt = QAction(self.tr('About Qt'), self)
self.actionAboutQt.setMenuRole(QAction.AboutQtRole)
self.connect(self.actionAboutQt, SIGNAL('triggered()'), qApp, SLOT('aboutQt()'))
self.usefulTags = ('a', 'center', 'i', 'img', 's', 'span', 'table', 'td', 'tr', 'u')
self.usefulChars = ('laquo', 'minus', 'mdash', 'nbsp', 'ndash', 'raquo')
self.tagsBox = QComboBox(self.editBar)
self.tagsBox.addItem(self.tr('Tags'))
self.tagsBox.addItems(self.usefulTags)
self.connect(self.tagsBox, SIGNAL('activated(int)'), self.insertTag)
self.symbolBox = QComboBox(self.editBar)
self.symbolBox.addItem(self.tr('Symbols'))
self.symbolBox.addItems(self.usefulChars)
self.connect(self.symbolBox, SIGNAL('activated(int)'), self.insertSymbol)
self.menubar = QMenuBar(self)
self.menubar.setGeometry(QRect(0, 0, 800, 25))
self.setMenuBar(self.menubar)
self.menuFile = self.menubar.addMenu(self.tr('File'))
self.menuEdit = self.menubar.addMenu(self.tr('Edit'))
self.menuHelp = self.menubar.addMenu(self.tr('Help'))
self.menuFile.addAction(self.actionNew)
self.menuFile.addAction(self.actionOpen)
self.menuFile.addAction(self.actionRecentFiles)
self.menuFile.addSeparator()
self.menuFile.addAction(self.actionSave)
self.menuFile.addAction(self.actionSaveAs)
self.menuFile.addSeparator()
self.menuExport = self.menuFile.addMenu(self.tr('Export'))
self.menuExport.addAction(self.actionPerfectHtml)
self.menuExport.addAction(self.actionPdf)
self.menuFile.addAction(self.actionPrint)
self.menuFile.addSeparator()
self.menuFile.addAction(self.actionQuit)
self.menuEdit.addAction(self.actionUndo)
self.menuEdit.addAction(self.actionRedo)
self.menuEdit.addSeparator()
self.menuEdit.addAction(self.actionCut)
self.menuEdit.addAction(self.actionCopy)
self.menuEdit.addAction(self.actionPaste)
self.menuEdit.addSeparator()
self.menuEdit.addAction(self.actionAutoFormatting)
self.menuEdit.addAction(self.actionPreview)
self.menuHelp.addAction(self.actionAbout)
self.menuHelp.addAction(self.actionAboutQt)
self.menubar.addMenu(self.menuFile)
self.menubar.addMenu(self.menuEdit)
self.menubar.addMenu(self.menuHelp)
self.toolBar.addAction(self.actionOpen)
self.toolBar.addAction(self.actionSave)
self.toolBar.addAction(self.actionPrint)
self.toolBar.addSeparator()
self.toolBar.addAction(self.actionPreview)
self.editBar.addAction(self.actionUndo)
self.editBar.addAction(self.actionRedo)
self.editBar.addSeparator()
self.editBar.addAction(self.actionCut)
self.editBar.addAction(self.actionCopy)
self.editBar.addAction(self.actionPaste)
self.editBar.addWidget(self.tagsBox)
self.editBar.addWidget(self.symbolBox)
self.useAutoFormatting = True
self.fileName = None
def preview(self, viewmode):
self.editBar.setEnabled(not viewmode)
self.editBox.setVisible(not viewmode)
self.previewBox.setVisible(viewmode)
if viewmode:
self.previewBox.setHtml(self.parseText())
def setCurrentFile(self):
curFile = self.fileName
self.setWindowFilePath(curFile);
settings = QSettings()
files = settings.value("recentFileList").toStringList()
files.removeAll(self.fileName)
files.prepend(self.fileName)
while len(files) > 10:
files.removeLast()
settings.setValue("recentFileList", files)
def createNew(self):
if self.maybeSave():
self.fileName = ""
self.editBox.clear()
self.actionPreview.setChecked(False)
self.setWindowTitle(self.tr('New document') + '[*] ' + QChar(0x2014) + ' ' + app_name)
self.setWindowFilePath("")
self.editBox.document().setModified(False)
self.modificationChanged(False)
self.preview(False)
def openRecent(self):
settings = QSettings()
files = settings.value("recentFileList").toStringList()
for i in files:
if not QFile.exists(i):
files.removeAll(i)
settings.setValue("recentFileList", files)
(item, ok) = QInputDialog.getItem(self, app_name, self.tr("Open recent"), files, 0, False)
if ok and not item.isEmpty():
self.fileName = item
self.openFileMain()
def openFile(self):
if self.maybeSave():
self.fileName = QFileDialog.getOpenFileName(self, self.tr("Open file"), "", \
self.tr("ReText files (*.re *.mdml *.txt)")+";;"+self.tr("All files (*)"))
self.openFileMain()
def openFileMain(self):
if QFile.exists(self.fileName):
openfile = QFile(self.fileName)
openfile.open(QIODevice.ReadOnly)
openstream = QTextStream(openfile)
html = openstream.readAll()
openfile.close()
self.actionPreview.setChecked(False)
self.editBox.setPlainText(html)
self.editBox.document().setModified(False)
self.modificationChanged(False)
self.preview(False)
if QFileInfo(self.fileName).suffix().startsWith("htm"):
self.useAutoFormatting = False
self.actionAutoFormatting.setChecked(False)
self.setWindowTitle("")
self.setWindowFilePath(self.fileName)
self.setCurrentFile()
def saveFile(self):
self.saveFileMain(False)
def saveFileAs(self):
self.saveFileMain(True)
def saveFileMain(self, dlg):
if (not self.fileName) or dlg:
self.fileName = QFileDialog.getSaveFileName(self, self.tr("Save file"), "", self.tr("ReText files (*.re *.mdml *.txt)"))
if self.fileName:
if QFileInfo(self.fileName).suffix().isEmpty():
self.fileName.append(".re")
savefile = QFile(self.fileName)
savefile.open(QIODevice.WriteOnly)
savestream = QTextStream(savefile)
savestream.__lshift__(self.editBox.toPlainText())
savefile.close()
self.editBox.document().setModified(False)
self.setCurrentFile()
def saveFilePerfect(self):
if not self.fileName:
self.fileName = QFileDialog.getSaveFileName(self, self.tr("Save file"), "", self.tr("HTML files (*.html *.htm)"))
if self.fileName:
if QFileInfo(self.fileName).suffix().isEmpty():
self.fileName.append(".html")
td = QTextDocument()
td.setHtml(self.parseText())
writer = QTextDocumentWriter(self.fileName)
writer.write(td)
def savePdf(self):
fileName = QFileDialog.getSaveFileName(self, self.tr("Export document to PDF"), "", self.tr("PDF files (*.pdf)"));
if fileName:
if QFileInfo(fileName).suffix().isEmpty():
fileName.append(".pdf")
printer = QPrinter(QPrinter.HighResolution)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName(fileName)
printer.setCreator(app_name+" "+app_version)
td = QTextDocument()
td.setHtml(self.parseText())
td.print_(printer)
def printFile(self):
printer = QPrinter(QPrinter.HighResolution)
printer.setCreator(app_name+" "+app_version)
dlg = QPrintDialog(printer, self)
dlg.setWindowTitle(self.tr("Print Document"))
if (dlg.exec_() == QDialog.Accepted):
td = QTextDocument()
td.setHtml(self.parseText())
td.print_(printer)
def modificationChanged(self, changed):
self.actionSave.setEnabled(changed)
self.setWindowModified(changed)
def clipboardDataChanged(self):
self.actionPaste.setEnabled(qApp.clipboard().mimeData().hasText())
def insertTag(self, num):
if num:
ut = self.usefulTags[num-1]
hc = not ut in ('img', 'td', 'tr')
arg = ''
if ut == 'a':
arg = ' href=""'
if ut == 'img':
arg = ' src=""'
if ut == 'img':
arg = ' style=""'
tc = self.editBox.textCursor()
if hc:
toinsert = '<'+ut+arg+'>'+tc.selectedText()+'</'+ut+'>'
tc.removeSelectedText
tc.insertText(toinsert)
else:
tc.insertText('<'+ut+arg+'>'+tc.selectedText())
self.tagsBox.setCurrentIndex(0)
def insertSymbol(self, num):
if num:
self.editBox.insertPlainText('&'+self.usefulChars[num-1]+';')
self.symbolBox.setCurrentIndex(0)
def maybeSave(self):
if not self.editBox.document().isModified():
return True
ret = QMessageBox.warning(self, app_name, self.tr("The document has been modified.\nDo you want to save your changes?"), \
QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel)
if ret == QMessageBox.Save:
self.saveFileMain(False)
return True
elif ret == QMessageBox.Cancel:
return False
return True
def closeEvent(self, closeevent):
if self.maybeSave():
closeevent.accept()
else:
closeevent.ignore()
def aboutDialog(self):
QMessageBox.about(self, self.tr('About %1').arg(app_name), self.tr('This is <b>%1</b>, version %2<br>Author: Dmitry Shachnev, 2011').arg(app_name, app_version))
def enableAutoFormatting(self, yes):
self.useAutoFormatting = yes
def parseText(self):
htmltext = self.editBox.toPlainText()
if self.useAutoFormatting:
toinsert = md.convert(unicode(htmltext))
else:
toinsert = htmltext
return toinsert
def main(fileName):
app = QApplication(sys.argv)
app.setOrganizationName("ReText project")
app.setApplicationName("ReText")
RtTranslator = QTranslator()
if not RtTranslator.load("retext_"+QLocale.system().name()):
if sys.platform == "linux2":
RtTranslator.load("retext_"+QLocale.system().name(), "/usr/lib/retext")
QtTranslator = QTranslator()
QtTranslator.load("qt_"+QLocale.system().name(), QLibraryInfo.location(QLibraryInfo.TranslationsPath))
app.installTranslator(RtTranslator)
app.installTranslator(QtTranslator)
window = ReTextWindow()
if QFile.exists(fileName):
window.fileName = fileName
window.openFileMain()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
if len(sys.argv) > 1:
main(sys.argv[1])
else:
main("")