From d565ccc71729145e37296507fbcb90d83305f425 Mon Sep 17 00:00:00 2001
From: Kovid Goyal
Date: Wed, 30 Oct 2013 16:46:47 +0530
Subject: [PATCH] Syntax highlight non-breaking spaces
---
.../gui2/tweak_book/editor/syntax/html.py | 32 +++++++++++++------
1 file changed, 23 insertions(+), 9 deletions(-)
diff --git a/src/calibre/gui2/tweak_book/editor/syntax/html.py b/src/calibre/gui2/tweak_book/editor/syntax/html.py
index 17e7b30b81..7fd7d0fa18 100644
--- a/src/calibre/gui2/tweak_book/editor/syntax/html.py
+++ b/src/calibre/gui2/tweak_book/editor/syntax/html.py
@@ -24,6 +24,7 @@ attribute_name_pat = re.compile(r'''[^%s"'/><=]+''' % space_chars)
self_closing_pat = re.compile(r'/\s*>')
unquoted_val_pat = re.compile(r'''[^%s'"=<>`]+''' % space_chars)
cdata_close_pats = {x:re.compile(r'%s' % x, flags=re.I) for x in cdata_tags}
+nbsp_pat = re.compile('\xa0+')
class State(object):
@@ -72,6 +73,23 @@ def cdata(state, text, i, formats):
num = m.start() - i
return [(num, fmt), (2, formats['end_tag']), (len(m.group()) - 2, formats['tag_name'])]
+def mark_nbsp(state, text, nbsp_format):
+ ans = []
+ fmt = None
+ if state.bold or state.italic:
+ fmt = QTextCharFormat()
+ if state.bold:
+ fmt.setFontWeight(QFont.Bold)
+ if state.italic:
+ fmt.setFontItalic(True)
+ last = 0
+ for m in nbsp_pat.finditer(text):
+ ans.extend([(m.start() - last, fmt), (m.end() - m.start(), nbsp_format)])
+ if not ans:
+ ans = [(len(text), fmt)]
+ return ans
+
+
def normal(state, text, i, formats):
' The normal state in between tags '
ch = text[i]
@@ -115,14 +133,7 @@ def normal(state, text, i, formats):
return [(1, formats['>'])]
t = normal_pat.search(text, i).group()
- fmt = None
- if state.bold or state.italic:
- fmt = QTextCharFormat()
- if state.bold:
- fmt.setFontWeight(QFont.Bold)
- if state.italic:
- fmt.setFontItalic(True)
- return [(len(t), fmt)]
+ return mark_nbsp(state, t, formats['nbsp'])
def opening_tag(state, text, i, formats):
'An opening tag, like '
@@ -257,6 +268,7 @@ class HTMLHighlighter(SyntaxHighlighter):
'string': t['String'],
'nsprefix': t['Constant'],
'preproc': t['PreProc'],
+ 'nbsp': t['CursorLine'],
}
for name, msg in {
'<': _('An unescaped < is not allowed. Replace it with <'),
@@ -314,10 +326,12 @@ if __name__ == '__main__':
A heading that should appear in bold, with an italic word
Some text with inline formatting, that is syntax highlighted. A bold word, and an italic word. \
-Some italic text with a bold italic word in middle.
+Some italic text with a bold-italic word in the middle.
+
+ Some\xa0words\xa0separated\xa0by\xa0non-breaking\xa0spaces.