From a17e899ab18f129ecc6e894e8b2ddc3322aef6f8 Mon Sep 17 00:00:00 2001 From: Harishankar Date: Sun, 28 Nov 2010 17:38:47 +0530 Subject: [PATCH] Code highlighting implemented Syntax highlighting implemented for article editing --- article_dialog.py | 5 ++- article_dialog.ui | 72 ++++++++++++++++++++++++-------------------- highlighter.py | 31 +++++++++++++++++++ main_window.ui | 10 ++++-- ui_article_dialog.py | 19 +++++++----- ui_main_window.py | 7 +++-- 6 files changed, 96 insertions(+), 48 deletions(-) create mode 100644 highlighter.py diff --git a/article_dialog.py b/article_dialog.py index 02c0771..ddd9d18 100644 --- a/article_dialog.py +++ b/article_dialog.py @@ -1,13 +1,16 @@ # BiaWeb Website content manager (c) 2010 V.Harishankar -# Category dialog class +# Article dialog class import PyQt4 import ui_article_dialog +import highlighter class ArticleDialog (PyQt4.QtGui.QDialog, ui_article_dialog.Ui_ArticleDialog): def __init__ (self, parent): PyQt4.QtGui.QDialog.__init__ (self, parent) self.setupUi (self) + # set the code highlighter to the document + self.hltext = highlighter.SimpleHtmlHighlighter (self.content.document ()) # when rejected, confirm first def reject (self): diff --git a/article_dialog.ui b/article_dialog.ui index a4259ef..3d3b726 100644 --- a/article_dialog.ui +++ b/article_dialog.ui @@ -414,19 +414,6 @@ p, li { white-space: pre-wrap; } - - - - - 0 - 210 - - - - Qt::ScrollBarAlwaysOn - - - @@ -543,7 +530,7 @@ p, li { white-space: pre-wrap; } p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'Sans'; font-size:10pt; font-weight:400; font-style:normal;"> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Center para</p> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-style:italic;">Shortcut: Ctrl+C</span></p></body></html> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-style:italic;">Shortcut: Ctrl+O</span></p></body></html> @@ -553,7 +540,7 @@ p, li { white-space: pre-wrap; } :/bia/resources/center.gif:/bia/resources/center.gif - Ctrl+C + Ctrl+O false @@ -616,6 +603,24 @@ p, li { white-space: pre-wrap; } + + + + + 0 + 210 + + + + + Monospace + + + + Qt::ScrollBarAlwaysOn + + + @@ -816,8 +821,8 @@ p, li { white-space: pre-wrap; } onJustify() - 302 - 211 + 332 + 219 98 @@ -832,8 +837,8 @@ p, li { white-space: pre-wrap; } onBullet() - 355 - 199 + 386 + 219 336 @@ -848,8 +853,8 @@ p, li { white-space: pre-wrap; } onNumber() - 387 - 195 + 422 + 219 372 @@ -864,8 +869,8 @@ p, li { white-space: pre-wrap; } onBQuote() - 423 - 203 + 458 + 219 311 @@ -880,8 +885,8 @@ p, li { white-space: pre-wrap; } onCode() - 457 - 203 + 494 + 219 311 @@ -896,8 +901,8 @@ p, li { white-space: pre-wrap; } onTable() - 492 - 203 + 530 + 219 311 @@ -912,8 +917,8 @@ p, li { white-space: pre-wrap; } onHRule() - 532 - 203 + 572 + 219 311 @@ -928,8 +933,8 @@ p, li { white-space: pre-wrap; } onPara() - 566 - 203 + 608 + 219 311 @@ -944,8 +949,8 @@ p, li { white-space: pre-wrap; } onBreak() - 601 - 203 + 644 + 219 311 @@ -972,5 +977,6 @@ p, li { white-space: pre-wrap; } onHRule() onPara() onBreak() + onTextChanged() diff --git a/highlighter.py b/highlighter.py new file mode 100644 index 0000000..95cfa90 --- /dev/null +++ b/highlighter.py @@ -0,0 +1,31 @@ +# BiaWeb Website content manager (c) 2010 V.Harishankar +# HTML highlighter class + +import PyQt4 + +class SimpleHtmlHighlighter (PyQt4.QtGui.QSyntaxHighlighter): + def __init__ (self, document): + PyQt4.QtGui.QSyntaxHighlighter.__init__ (self, document) + + def highlightBlock (self, text): + # define the character highlight formats + charfmt1 = PyQt4.QtGui.QTextCharFormat () + charfmt1.setFontWeight (PyQt4.QtGui.QFont.Bold) + charfmt1.setForeground (PyQt4.QtCore.Qt.blue) + + charfmt2 = PyQt4.QtGui.QTextCharFormat () + charfmt2.setForeground (PyQt4.QtCore.Qt.darkGreen) + + # matching regular expressions + htmltagexps = [ (PyQt4.QtCore.QRegExp ("<[^<>]+>"), charfmt1), + (PyQt4.QtCore.QRegExp ("\"[^\"]+\""), charfmt2) + ] + + # run through the list of regular expressions to highlight + for htmltagexp, charfmt in htmltagexps: + index = htmltagexp.indexIn (text) + while index >= 0: + length = htmltagexp.matchedLength () + # self.setFormat is actual code highlighting + self.setFormat (index, length, charfmt) + index = htmltagexp.indexIn (text, index + length) diff --git a/main_window.ui b/main_window.ui index 3808477..ab98a30 100644 --- a/main_window.ui +++ b/main_window.ui @@ -6,8 +6,8 @@ 0 0 - 633 - 458 + 798 + 517 @@ -88,7 +88,7 @@ 0 0 - 633 + 798 25 @@ -287,6 +287,10 @@ + + categories + articles + diff --git a/ui_article_dialog.py b/ui_article_dialog.py index 7e42cc7..d9dc575 100644 --- a/ui_article_dialog.py +++ b/ui_article_dialog.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'article_dialog.ui' # -# Created: Sun Nov 28 13:59:41 2010 +# Created: Sun Nov 28 17:32:45 2010 # by: PyQt4 UI code generator 4.7.4 # # WARNING! All changes made in this file will be lost! @@ -156,11 +156,6 @@ class Ui_ArticleDialog(object): self.linebreak.setAutoRaise(False) self.linebreak.setObjectName("linebreak") self.gridLayout.addWidget(self.linebreak, 4, 21, 1, 1) - self.content = QtGui.QPlainTextEdit(ArticleDialog) - self.content.setMinimumSize(QtCore.QSize(0, 210)) - self.content.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) - self.content.setObjectName("content") - self.gridLayout.addWidget(self.content, 5, 0, 1, 22) self.label_4 = QtGui.QLabel(ArticleDialog) self.label_4.setObjectName("label_4") self.gridLayout.addWidget(self.label_4, 6, 0, 1, 2) @@ -233,6 +228,14 @@ class Ui_ArticleDialog(object): self.blockquote.setAutoRaise(False) self.blockquote.setObjectName("blockquote") self.gridLayout.addWidget(self.blockquote, 4, 15, 1, 1) + self.content = QtGui.QPlainTextEdit(ArticleDialog) + self.content.setMinimumSize(QtCore.QSize(0, 210)) + font = QtGui.QFont() + font.setFamily("Monospace") + self.content.setFont(font) + self.content.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) + self.content.setObjectName("content") + self.gridLayout.addWidget(self.content, 5, 0, 1, 22) self.retranslateUi(ArticleDialog) QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), ArticleDialog.accept) @@ -392,8 +395,8 @@ class Ui_ArticleDialog(object): "p, li { white-space: pre-wrap; }\n" "\n" "

Center para

\n" -"

Shortcut: Ctrl+C

", None, QtGui.QApplication.UnicodeUTF8)) - self.paracenter.setShortcut(QtGui.QApplication.translate("ArticleDialog", "Ctrl+C", None, QtGui.QApplication.UnicodeUTF8)) +"

Shortcut: Ctrl+O

", None, QtGui.QApplication.UnicodeUTF8)) + self.paracenter.setShortcut(QtGui.QApplication.translate("ArticleDialog", "Ctrl+O", None, QtGui.QApplication.UnicodeUTF8)) self.paraleft.setToolTip(QtGui.QApplication.translate("ArticleDialog", "\n" "