mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Also highlight URLs in CSS files and make them Ctrl-clickable
This commit is contained in:
parent
7ab719cfcd
commit
99128676cb
@ -963,7 +963,6 @@ class Boss(QObject):
|
|||||||
raise
|
raise
|
||||||
self.apply_container_update_to_gui()
|
self.apply_container_update_to_gui()
|
||||||
|
|
||||||
@in_thread_job
|
|
||||||
def link_clicked(self, name, anchor):
|
def link_clicked(self, name, anchor):
|
||||||
if not name:
|
if not name:
|
||||||
return
|
return
|
||||||
@ -979,8 +978,12 @@ class Boss(QObject):
|
|||||||
' the Table of Contents, you may'
|
' the Table of Contents, you may'
|
||||||
' need to refresh it by right-clicking and choosing "Refresh".') % name, show=True)
|
' need to refresh it by right-clicking and choosing "Refresh".') % name, show=True)
|
||||||
syntax = syntax_from_mime(name, mt)
|
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)
|
editor = self.edit_file(name, syntax)
|
||||||
if anchor:
|
if anchor and editor is not None:
|
||||||
editor.go_to_anchor(anchor)
|
editor.go_to_anchor(anchor)
|
||||||
|
|
||||||
@in_thread_job
|
@in_thread_job
|
||||||
|
@ -10,7 +10,7 @@ import re
|
|||||||
|
|
||||||
from PyQt4.Qt import QTextBlockUserData
|
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
|
from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter
|
||||||
|
|
||||||
space_pat = re.compile(r'[ \n\t\r\f]+')
|
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'),
|
(r'[~\^\*!%&\[\]\(\)<>\|+=@:;,./?-]', 'operator', 'operator'),
|
||||||
]]
|
]]
|
||||||
|
|
||||||
|
URL_TOKEN = 'url'
|
||||||
|
|
||||||
content_tokens = [(re.compile(k), v, n) for k, v, n in [
|
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'@\S+', 'preproc', 'at-rule'),
|
||||||
(r'(azimuth|background-attachment|background-color|'
|
(r'(azimuth|background-attachment|background-color|'
|
||||||
r'background-image|background-position|background-repeat|'
|
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:
|
for token, fmt, name in content_tokens:
|
||||||
m = token.match(text, i)
|
m = token.match(text, i)
|
||||||
if m is not None:
|
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(m.group()), formats[fmt])]
|
||||||
|
|
||||||
return [(len(text) - i, formats['unknown-normal'])]
|
return [(len(text) - i, formats['unknown-normal'])]
|
||||||
@ -262,6 +272,7 @@ def create_formats(highlighter):
|
|||||||
'class_selector': theme['Special'],
|
'class_selector': theme['Special'],
|
||||||
'pseudo_selector': theme['Special'],
|
'pseudo_selector': theme['Special'],
|
||||||
'tag': theme['Identifier'],
|
'tag': theme['Identifier'],
|
||||||
|
'link': theme['Link'],
|
||||||
}
|
}
|
||||||
for name, msg in {
|
for name, msg in {
|
||||||
'unknown-normal': _('Invalid text'),
|
'unknown-normal': _('Invalid text'),
|
||||||
@ -269,6 +280,8 @@ def create_formats(highlighter):
|
|||||||
}.iteritems():
|
}.iteritems():
|
||||||
f = formats[name] = syntax_text_char_format(formats['error'])
|
f = formats[name] = syntax_text_char_format(formats['error'])
|
||||||
f.setToolTip(msg)
|
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
|
return formats
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user