Edit Book: Allow highlighting class names in the HTML differently via Preferences->Editor settings->Color scheme

This commit is contained in:
Kovid Goyal 2016-09-21 07:58:05 +05:30
parent b6aad54e6a
commit 25de034110
2 changed files with 22 additions and 15 deletions

View File

@ -434,7 +434,7 @@ def quoted_val(state, text, i, formats, user_data):
pos = text.find(quote, i) pos = text.find(quote, i)
if pos == -1: if pos == -1:
num = len(text) - i num = len(text) - i
is_link = False is_link = is_class = False
else: else:
num = pos - i + 1 num = pos - i + 1
state.parse = IN_OPENING_TAG state.parse = IN_OPENING_TAG
@ -445,11 +445,14 @@ def quoted_val(state, text, i, formats, user_data):
pass pass
add_attr_data(user_data, ATTR_VALUE, ATTR_END, i + num) add_attr_data(user_data, ATTR_VALUE, ATTR_END, i + num)
is_link = state.attribute_name in LINK_ATTRS is_link = state.attribute_name in LINK_ATTRS
is_class = not is_link and state.attribute_name == 'class'
if is_link: if is_link:
if verify_link(text[i:i+num - 1], user_data.doc_name) is False: if verify_link(text[i:i+num - 1], user_data.doc_name) is False:
return [(num - 1, formats['bad_link']), (1, formats['string'])] return [(num - 1, formats['bad_link']), (1, formats['string'])]
return [(num - 1, formats['link']), (1, formats['string'])] return [(num - 1, formats['link']), (1, formats['string'])]
elif is_class:
return [(num - 1, formats['class_attr']), (1, formats['string'])]
return [(num, formats['string'])] return [(num, formats['string'])]
def closing_tag(state, text, i, formats, user_data): def closing_tag(state, text, i, formats, user_data):
@ -532,6 +535,7 @@ def create_formats(highlighter, add_css=True):
if add_css: if add_css:
formats['css_sub_formats'] = create_css_formats(highlighter) formats['css_sub_formats'] = create_css_formats(highlighter)
formats['spell'].setProperty(SPELL_PROPERTY, True) formats['spell'].setProperty(SPELL_PROPERTY, True)
formats['class_attr'] = syntax_text_char_format(t.get('ClassAttribute', t['String']))
formats['link'] = syntax_text_char_format(t['Link']) formats['link'] = syntax_text_char_format(t['Link'])
formats['link'].setProperty(LINK_PROPERTY, True) formats['link'].setProperty(LINK_PROPERTY, True)
formats['link'].setToolTip(_('Hold down the Ctrl key and click to open this link')) formats['link'].setToolTip(_('Hold down the Ctrl key and click to open this link'))

View File

@ -71,6 +71,7 @@ SOLARIZED = \
Tooltip fg=black bg=ffffed Tooltip fg=black bg=ffffed
Link fg={blue} Link fg={blue}
BadLink fg={cyan} us=wave uc={red} BadLink fg={cyan} us=wave uc={red}
ClassAttribute fg={cyan}
DiffDelete bg={base02} fg={red} DiffDelete bg={base02} fg={red}
DiffInsert bg={base02} fg={green} DiffInsert bg={base02} fg={green}
@ -114,6 +115,7 @@ THEMES = {
SpecialCharacter bg={cursor_loc} SpecialCharacter bg={cursor_loc}
Link fg=cyan Link fg=cyan
BadLink fg={string} us=wave uc=red BadLink fg={string} us=wave uc=red
ClassAttribute fg={string}
DiffDelete bg=341414 fg=642424 DiffDelete bg=341414 fg=642424
DiffInsert bg=143414 fg=246424 DiffInsert bg=143414 fg=246424
@ -162,6 +164,7 @@ THEMES = {
SpellError us=wave uc=magenta SpellError us=wave uc=magenta
Link fg=blue Link fg=blue
BadLink fg={string} us=wave uc=red BadLink fg={string} us=wave uc=red
ClassAttribute fg={string}
DiffDelete bg=rgb(255,180,200) fg=rgb(200,80,110) DiffDelete bg=rgb(255,180,200) fg=rgb(200,80,110)
DiffInsert bg=rgb(180,255,180) fg=rgb(80,210,80) DiffInsert bg=rgb(180,255,180) fg=rgb(80,210,80)
@ -456,23 +459,19 @@ class Property(QWidget):
HELP_TEXT = _('''\ HELP_TEXT = _('''\
<h2>Creating a custom theme</h2> <h2>Creating a custom theme</h2>
<p id="attribute" lang="und">You can create a custom syntax highlighting <p id="attribute" lang="und">You can create a custom syntax highlighting theme, \
theme, with your own colors and font styles. The most important with your own colors and font styles. The most important types of highlighting \
types of highlighting rules are described below. Note that not rules are described below. Note that not every rule supports every kind of \
every rule supports every kind of customization, for example, customization, for example, changing font or underline styles for the \
changing font or underline styles for the <code>Cursor</code> rule <code>Cursor</code> rule does not have any effect as that rule is used only for \
does not have any effect as that rule is used only for the color of the color of the blinking cursor.</p>
the blinking cursor.</p>
<p>As you make changes to your theme on the left, the changes will <p>As you make changes to your theme on the left, the changes will be reflected live in this panel.</p>
be reflected live in this panel.</p>
<p xml:lang="und"> <p xml:lang="und">
{0} {0}
The most important rule. Sets the The most important rule. Sets the foreground and background colors for the \
foreground and background colors for the editor as well as the editor as well as the style of "normal" text, that is, text that does not match any special syntax.
style of "normal" text, that is, text that does not match any
special syntax.
{1} {1}
Defines the colors for text selected by the mouse. Defines the colors for text selected by the mouse.
@ -510,6 +509,9 @@ be reflected live in this panel.</p>
{12} {12}
Comments like <!-- this one --> Comments like <!-- this one -->
{13}
Class attributes, like this <span class="myclass1 myclass2"></span>
</p> </p>
<style type="text/css"> <style type="text/css">
@ -566,7 +568,8 @@ class ThemeEditor(Dialog):
p.load_text(HELP_TEXT.format( p.load_text(HELP_TEXT.format(
*['<b>%s</b>' % x for x in ( *['<b>%s</b>' % x for x in (
'Normal', 'Visual', 'CursorLine', 'LineNr', 'MatchParen', 'Normal', 'Visual', 'CursorLine', 'LineNr', 'MatchParen',
'Function', 'Type', 'Statement', 'Constant', 'SpecialCharacter', 'Error', 'SpellError', 'Comment' 'Function', 'Type', 'Statement', 'Constant', 'SpecialCharacter',
'Error', 'SpellError', 'Comment', 'ClassAttribute'
)] )]
)) ))
p.setMaximumWidth(p.size_hint.width() + 5) p.setMaximumWidth(p.size_hint.width() + 5)