From 99128676cb9c08a38524692ca30b162ef711c711 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 9 Jul 2014 18:32:12 +0530 Subject: [PATCH] Also highlight URLs in CSS files and make them Ctrl-clickable --- src/calibre/gui2/tweak_book/boss.py | 7 +++++-- .../gui2/tweak_book/editor/syntax/css.py | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index 9e2e00597d..ddcd747b64 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -963,7 +963,6 @@ class Boss(QObject): raise self.apply_container_update_to_gui() - @in_thread_job def link_clicked(self, name, anchor): if not name: return @@ -979,8 +978,12 @@ class Boss(QObject): ' the Table of Contents, you may' ' need to refresh it by right-clicking and choosing "Refresh".') % name, show=True) syntax = syntax_from_mime(name, mt) + if not syntax: + return error_dialog( + self.gui, _('Unsupported file format'), + _('Editing files of type %s is not supported' % mt), show=True) editor = self.edit_file(name, syntax) - if anchor: + if anchor and editor is not None: editor.go_to_anchor(anchor) @in_thread_job diff --git a/src/calibre/gui2/tweak_book/editor/syntax/css.py b/src/calibre/gui2/tweak_book/editor/syntax/css.py index e40ec2de53..7f9e79e678 100644 --- a/src/calibre/gui2/tweak_book/editor/syntax/css.py +++ b/src/calibre/gui2/tweak_book/editor/syntax/css.py @@ -10,7 +10,7 @@ import re from PyQt4.Qt import QTextBlockUserData -from calibre.gui2.tweak_book.editor import syntax_text_char_format +from calibre.gui2.tweak_book.editor import syntax_text_char_format, LINK_PROPERTY from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter space_pat = re.compile(r'[ \n\t\r\f]+') @@ -24,8 +24,10 @@ sheet_tokens = [(re.compile(k), v, n) for k, v, n in [ (r'[~\^\*!%&\[\]\(\)<>\|+=@:;,./?-]', 'operator', 'operator'), ]] +URL_TOKEN = 'url' + content_tokens = [(re.compile(k), v, n) for k, v, n in [ - (r'url\(.*?\)', 'string', 'url'), + (r'url\(.*?\)', 'string', URL_TOKEN), (r'@\S+', 'preproc', 'at-rule'), (r'(azimuth|background-attachment|background-color|' r'background-image|background-position|background-repeat|' @@ -211,6 +213,14 @@ def content(state, text, i, formats, user_data): for token, fmt, name in content_tokens: m = token.match(text, i) if m is not None: + if name is URL_TOKEN: + url = m.group() + prefix, main, suffix = url[:4], url[4:-1], url[-1] + if len(main) > 1 and main[0] in ('"', "'") and main[0] == main[-1]: + prefix += main[0] + suffix = main[-1] + suffix + main = main[1:-1] + return [(len(prefix), formats[fmt]), (len(main), formats['link']), (len(suffix), formats[fmt])] return [(len(m.group()), formats[fmt])] return [(len(text) - i, formats['unknown-normal'])] @@ -262,6 +272,7 @@ def create_formats(highlighter): 'class_selector': theme['Special'], 'pseudo_selector': theme['Special'], 'tag': theme['Identifier'], + 'link': theme['Link'], } for name, msg in { 'unknown-normal': _('Invalid text'), @@ -269,6 +280,8 @@ def create_formats(highlighter): }.iteritems(): f = formats[name] = syntax_text_char_format(formats['error']) f.setToolTip(msg) + formats['link'].setToolTip(_('Hold down the Ctrl key and click to open this link')) + formats['link'].setProperty(LINK_PROPERTY, True) return formats