mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Ensure that syntax char formats are unambiguously identifiable
This commit is contained in:
parent
4f4dd87b2a
commit
76d22f8f68
@ -6,6 +6,8 @@ from __future__ import (unicode_literals, division, absolute_import,
|
|||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
|
from PyQt4.Qt import QTextCharFormat
|
||||||
|
|
||||||
from calibre.ebooks.oeb.base import OEB_DOCS, OEB_STYLES
|
from calibre.ebooks.oeb.base import OEB_DOCS, OEB_STYLES
|
||||||
from calibre.ebooks.oeb.polish.container import guess_type
|
from calibre.ebooks.oeb.polish.container import guess_type
|
||||||
|
|
||||||
@ -25,3 +27,11 @@ def editor_from_syntax(syntax, parent=None):
|
|||||||
from calibre.gui2.tweak_book.editor.widget import Editor
|
from calibre.gui2.tweak_book.editor.widget import Editor
|
||||||
return Editor(syntax, parent=parent)
|
return Editor(syntax, parent=parent)
|
||||||
|
|
||||||
|
SYNTAX_PROPERTY = QTextCharFormat.UserProperty
|
||||||
|
|
||||||
|
class SyntaxTextCharFormat(QTextCharFormat):
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
QTextCharFormat.__init__(self, *args, **kwargs)
|
||||||
|
self.setProperty(SYNTAX_PROPERTY, True)
|
||||||
|
|
||||||
|
@ -8,10 +8,9 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
from calibre.gui2.tweak_book.editor import SyntaxTextCharFormat
|
||||||
from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter
|
from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter
|
||||||
|
|
||||||
from PyQt4.Qt import QTextCharFormat
|
|
||||||
|
|
||||||
space_pat = re.compile(r'[ \n\t\r\f]+')
|
space_pat = re.compile(r'[ \n\t\r\f]+')
|
||||||
cdo_pat = re.compile(r'/\*')
|
cdo_pat = re.compile(r'/\*')
|
||||||
sheet_tokens = [(re.compile(k), v, n) for k, v, n in [
|
sheet_tokens = [(re.compile(k), v, n) for k, v, n in [
|
||||||
@ -242,7 +241,7 @@ def create_formats(highlighter):
|
|||||||
'unknown-normal': _('Invalid text'),
|
'unknown-normal': _('Invalid text'),
|
||||||
'unterminated-string': _('Unterminated string'),
|
'unterminated-string': _('Unterminated string'),
|
||||||
}.iteritems():
|
}.iteritems():
|
||||||
f = formats[name] = QTextCharFormat(formats['error'])
|
f = formats[name] = SyntaxTextCharFormat(formats['error'])
|
||||||
f.setToolTip(msg)
|
f.setToolTip(msg)
|
||||||
return formats
|
return formats
|
||||||
|
|
||||||
|
@ -9,8 +9,9 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
|
|||||||
import re
|
import re
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
from PyQt4.Qt import (QTextCharFormat, QFont)
|
from PyQt4.Qt import QFont
|
||||||
|
|
||||||
|
from calibre.gui2.tweak_book.editor import SyntaxTextCharFormat
|
||||||
from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter, run_loop
|
from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter, run_loop
|
||||||
from calibre.gui2.tweak_book.editor.syntax.css import create_formats as create_css_formats, state_map as css_state_map, State as CSSState
|
from calibre.gui2.tweak_book.editor.syntax.css import create_formats as create_css_formats, state_map as css_state_map, State as CSSState
|
||||||
|
|
||||||
@ -109,7 +110,7 @@ def mark_nbsp(state, text, nbsp_format):
|
|||||||
ans = []
|
ans = []
|
||||||
fmt = None
|
fmt = None
|
||||||
if state.bold or state.italic:
|
if state.bold or state.italic:
|
||||||
fmt = QTextCharFormat()
|
fmt = SyntaxTextCharFormat()
|
||||||
if state.bold:
|
if state.bold:
|
||||||
fmt.setFontWeight(QFont.Bold)
|
fmt.setFontWeight(QFont.Bold)
|
||||||
if state.italic:
|
if state.italic:
|
||||||
@ -313,9 +314,9 @@ def create_formats(highlighter):
|
|||||||
'bad-closing': _('A closing tag must contain only the tag name and nothing else'),
|
'bad-closing': _('A closing tag must contain only the tag name and nothing else'),
|
||||||
'no-attr-value': _('Expecting an attribute value'),
|
'no-attr-value': _('Expecting an attribute value'),
|
||||||
}.iteritems():
|
}.iteritems():
|
||||||
f = formats[name] = QTextCharFormat(formats['error'])
|
f = formats[name] = SyntaxTextCharFormat(formats['error'])
|
||||||
f.setToolTip(msg)
|
f.setToolTip(msg)
|
||||||
f = formats['title'] = QTextCharFormat()
|
f = formats['title'] = SyntaxTextCharFormat()
|
||||||
f.setFontWeight(QFont.Bold)
|
f.setFontWeight(QFont.Bold)
|
||||||
return formats
|
return formats
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@ from PyQt4.Qt import (
|
|||||||
QTextEdit, QTextFormat, QWidget, QSize, QPainter, Qt, QRect)
|
QTextEdit, QTextFormat, QWidget, QSize, QPainter, Qt, QRect)
|
||||||
|
|
||||||
from calibre.gui2.tweak_book import tprefs
|
from calibre.gui2.tweak_book import tprefs
|
||||||
|
from calibre.gui2.tweak_book.editor import SYNTAX_PROPERTY
|
||||||
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, XMLHighlighter
|
from calibre.gui2.tweak_book.editor.syntax.html import HTMLHighlighter, XMLHighlighter
|
||||||
@ -211,7 +212,7 @@ class TextEdit(QPlainTextEdit):
|
|||||||
return
|
return
|
||||||
pos = cursor.positionInBlock()
|
pos = cursor.positionInBlock()
|
||||||
for r in cursor.block().layout().additionalFormats():
|
for r in cursor.block().layout().additionalFormats():
|
||||||
if r.start <= pos < r.start + r.length:
|
if r.start <= pos < r.start + r.length and r.format.property(SYNTAX_PROPERTY).toBool():
|
||||||
return r.format
|
return r.format
|
||||||
|
|
||||||
def show_tooltip(self, ev):
|
def show_tooltip(self, ev):
|
||||||
|
@ -8,7 +8,9 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
|
|||||||
|
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
from PyQt4.Qt import (QColor, QTextCharFormat, QBrush, QFont, QApplication, QPalette)
|
from PyQt4.Qt import (QColor, QBrush, QFont, QApplication, QPalette)
|
||||||
|
|
||||||
|
from calibre.gui2.tweak_book.editor import SyntaxTextCharFormat
|
||||||
|
|
||||||
underline_styles = {'single', 'dash', 'dot', 'dash_dot', 'dash_dot_dot', 'wave', 'spell'}
|
underline_styles = {'single', 'dash', 'dot', 'dash_dot', 'dash_dot_dot', 'wave', 'spell'}
|
||||||
|
|
||||||
@ -198,10 +200,10 @@ def u(x):
|
|||||||
if 'Dot' in x:
|
if 'Dot' in x:
|
||||||
return x + 'Line'
|
return x + 'Line'
|
||||||
return x + 'Underline'
|
return x + 'Underline'
|
||||||
underline_styles = {x:getattr(QTextCharFormat, u(x)) for x in underline_styles}
|
underline_styles = {x:getattr(SyntaxTextCharFormat, u(x)) for x in underline_styles}
|
||||||
|
|
||||||
def highlight_to_char_format(h):
|
def highlight_to_char_format(h):
|
||||||
ans = QTextCharFormat()
|
ans = SyntaxTextCharFormat()
|
||||||
if h.bold:
|
if h.bold:
|
||||||
ans.setFontWeight(QFont.Bold)
|
ans.setFontWeight(QFont.Bold)
|
||||||
if h.italic:
|
if h.italic:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user