mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Start work on CSS syntax highlighter
This commit is contained in:
parent
04745b77b9
commit
ab4f53331e
51
src/calibre/gui2/tweak_book/editor/syntax/css.py
Normal file
51
src/calibre/gui2/tweak_book/editor/syntax/css.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# vim:fileencoding=utf-8
|
||||||
|
from __future__ import (unicode_literals, division, absolute_import,
|
||||||
|
print_function)
|
||||||
|
|
||||||
|
__license__ = 'GPL v3'
|
||||||
|
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
|
from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter
|
||||||
|
|
||||||
|
class State(object):
|
||||||
|
|
||||||
|
NORMAL = 0
|
||||||
|
|
||||||
|
def __init__(self, num):
|
||||||
|
self.parse = num & 0b1111
|
||||||
|
self.blocks = num >> 4
|
||||||
|
|
||||||
|
@property
|
||||||
|
def value(self):
|
||||||
|
return ((self.parse & 0b1111) | (max(0, self.blocks) << 4))
|
||||||
|
|
||||||
|
|
||||||
|
def normal(state, text, i, formats):
|
||||||
|
' The normal state (outside everything) '
|
||||||
|
return [(len(text), None)]
|
||||||
|
|
||||||
|
state_map = {
|
||||||
|
State.NORMAL:normal,
|
||||||
|
}
|
||||||
|
|
||||||
|
class CSSHighlighter(SyntaxHighlighter):
|
||||||
|
|
||||||
|
state_map = state_map
|
||||||
|
state_class = State
|
||||||
|
|
||||||
|
def __init__(self, parent):
|
||||||
|
SyntaxHighlighter.__init__(self, parent)
|
||||||
|
|
||||||
|
def create_formats(self):
|
||||||
|
# t = self.theme
|
||||||
|
self.formats = {
|
||||||
|
}
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
from calibre.gui2.tweak_book.editor.text import launch_editor
|
||||||
|
launch_editor('''\
|
||||||
|
@charset "utf-8";
|
||||||
|
/* A demonstration css sheet */
|
||||||
|
''', path_is_raw=True, syntax='css')
|
||||||
|
|
@ -314,4 +314,4 @@ if __name__ == '__main__':
|
|||||||
<p>Some\xa0words\xa0separated\xa0by\xa0non-breaking\xa0spaces.</p>
|
<p>Some\xa0words\xa0separated\xa0by\xa0non-breaking\xa0spaces.</p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
''')
|
''', path_is_raw=True)
|
||||||
|
@ -17,6 +17,7 @@ from calibre.gui2.tweak_book import tprefs
|
|||||||
from calibre.gui2.tweak_book.editor.themes import THEMES, DEFAULT_THEME, theme_color
|
from calibre.gui2.tweak_book.editor.themes import THEMES, DEFAULT_THEME, theme_color
|
||||||
from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter
|
from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter
|
||||||
from calibre.gui2.tweak_book.editor.syntax.html import HTMLHighlighter
|
from calibre.gui2.tweak_book.editor.syntax.html import HTMLHighlighter
|
||||||
|
from calibre.gui2.tweak_book.editor.syntax.css import CSSHighlighter
|
||||||
|
|
||||||
_dff = None
|
_dff = None
|
||||||
def default_font_family():
|
def default_font_family():
|
||||||
@ -93,7 +94,7 @@ class TextEdit(QPlainTextEdit):
|
|||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
def load_text(self, text, syntax='html'):
|
def load_text(self, text, syntax='html'):
|
||||||
self.highlighter = {'html':HTMLHighlighter}.get(syntax, SyntaxHighlighter)(self)
|
self.highlighter = {'html':HTMLHighlighter, 'css':CSSHighlighter}.get(syntax, SyntaxHighlighter)(self)
|
||||||
self.highlighter.apply_theme(self.theme)
|
self.highlighter.apply_theme(self.theme)
|
||||||
self.highlighter.setDocument(self.document())
|
self.highlighter.setDocument(self.document())
|
||||||
self.setPlainText(text)
|
self.setPlainText(text)
|
||||||
@ -199,49 +200,23 @@ class TextEdit(QPlainTextEdit):
|
|||||||
ev.ignore()
|
ev.ignore()
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
def launch_editor(path_to_edit):
|
def launch_editor(path_to_edit, path_is_raw=False, syntax='html'):
|
||||||
if '<html' in path_to_edit:
|
if path_is_raw:
|
||||||
raw = path_to_edit
|
raw = path_to_edit
|
||||||
else:
|
else:
|
||||||
with open(path_to_edit, 'rb') as f:
|
with open(path_to_edit, 'rb') as f:
|
||||||
raw = f.read().decode('utf-8')
|
raw = f.read().decode('utf-8')
|
||||||
|
ext = path_to_edit.rpartition('.')[-1].lower()
|
||||||
|
if ext in ('html', 'htm', 'xhtml', 'xhtm'):
|
||||||
|
syntax = 'html'
|
||||||
|
elif ext in ('css',):
|
||||||
|
syntax = 'css'
|
||||||
app = QApplication([]) # noqa
|
app = QApplication([]) # noqa
|
||||||
t = TextEdit()
|
t = TextEdit()
|
||||||
t.load_text(raw)
|
t.load_text(raw, syntax=syntax)
|
||||||
d = QDialog()
|
d = QDialog()
|
||||||
d.setLayout(QVBoxLayout())
|
d.setLayout(QVBoxLayout())
|
||||||
d.layout().addWidget(t)
|
d.layout().addWidget(t)
|
||||||
d.exec_()
|
d.exec_()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
launch_editor(
|
|
||||||
textwrap.dedent('''\
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>Page title</title>
|
|
||||||
<style type="text/css">
|
|
||||||
body { color: green; }
|
|
||||||
</style>
|
|
||||||
</head id="1">
|
|
||||||
<body>
|
|
||||||
<!-- The start of the document -->a
|
|
||||||
<h1 class="head" id="one" >A heading</h1>
|
|
||||||
<p> A single &. An proper entity &.
|
|
||||||
A single < and a single >.
|
|
||||||
These cases are perfectly simple and easy to
|
|
||||||
distinguish. In a free hour, when our power of choice is
|
|
||||||
untrammelled and when nothing prevents our being able to do
|
|
||||||
what we like best, every pleasure is to be welcomed and every
|
|
||||||
pain avoided.</p>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
But in certain circumstances and owing to the claims of duty or the obligations
|
|
||||||
of business it will frequently occur that pleasures have to be
|
|
||||||
repudiated and annoyances accepted. The wise man therefore
|
|
||||||
always holds in these matters to this principle of selection:
|
|
||||||
he rejects pleasures to secure other greater pleasures, or else
|
|
||||||
he endures pains.</p>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
'''))
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user