예제 #1
0
def create_formats(highlighter, add_css=True):
    t = highlighter.theme
    formats = {
        'tag': t['Function'],
        'end_tag': t['Function'],
        'attr': t['Type'],
        'tag_name' : t['Statement'],
        'entity': t['Special'],
        'error': t['Error'],
        'comment': t['Comment'],
        'special': t['Special'],
        'string': t['String'],
        'nsprefix': t['Constant'],
        'preproc': t['PreProc'],
        'nbsp': t['SpecialCharacter'],
    }
    for name, msg in {
            '<': _('An unescaped < is not allowed. Replace it with &lt;'),
            '&': _('An unescaped ampersand is not allowed. Replace it with &amp;'),
            '>': _('An unescaped > is not allowed. Replace it with &gt;'),
            '/': _('/ not allowed except at the end of the tag'),
            '?': _('Unknown character'),
            'bad-closing': _('A closing tag must contain only the tag name and nothing else'),
            'no-attr-value': _('Expecting an attribute value'),
            'only-prefix': _('A tag name cannot end with a colon'),
    }.iteritems():
        f = formats[name] = SyntaxTextCharFormat(formats['error'])
        f.setToolTip(msg)
    f = formats['title'] = SyntaxTextCharFormat()
    f.setFontWeight(QFont.Bold)
    if add_css:
        formats['css_sub_formats'] = create_css_formats(highlighter)
    return formats
예제 #2
0
파일: html.py 프로젝트: alip/calibre
def mark_nbsp(state, text, nbsp_format):
    ans = []
    fmt = None
    if state.bold or state.italic:
        fmt = SyntaxTextCharFormat()
        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
예제 #3
0
파일: css.py 프로젝트: mrmac123/calibre
def create_formats(highlighter):
    theme = highlighter.theme
    formats = {
        'comment': theme['Comment'],
        'error': theme['Error'],
        'string': theme['String'],
        'preproc': theme['PreProc'],
        'keyword': theme['Keyword'],
        'colorname': theme['Constant'],
        'number': theme['Number'],
        'operator': theme['Function'],
        'bracket': theme['Special'],
        'identifier': theme['Identifier'],
        'id_selector': theme['Special'],
        'class_selector': theme['Special'],
        'pseudo_selector': theme['Special'],
        'tag': theme['Identifier'],
    }
    for name, msg in {
        'unknown-normal': _('Invalid text'),
        'unterminated-string': _('Unterminated string'),
    }.iteritems():
        f = formats[name] = SyntaxTextCharFormat(formats['error'])
        f.setToolTip(msg)
    return formats
예제 #4
0
def mark_nbsp(state, text, nbsp_format):
    ans = []
    fmt = None
    if state.is_bold or state.is_italic:
        fmt = SyntaxTextCharFormat()
        if state.is_bold:
            fmt.setFontWeight(QFont.Bold)
        if state.is_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)])
        last = m.end()
    if not ans:
        ans = [(len(text), fmt)]
    return ans
예제 #5
0
def process_text(state, text, nbsp_format, spell_format, user_data):
    ans = []
    fmt = None
    if state.is_bold or state.is_italic:
        fmt = SyntaxTextCharFormat()
        if state.is_bold:
            fmt.setFontWeight(QFont.Bold)
        if state.is_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)])
        last = m.end()
    if not ans:
        ans = [(len(text), fmt)]
    elif last < len(text):
        ans.append((len(text) - last, fmt))

    if tprefs[
            'inline_spell_check'] and state.tags and user_data.tag_ok_for_spell(
                state.tags[-1].name) and hasattr(dictionaries,
                                                 'active_user_dictionaries'):
        split_ans = []
        locale = state.current_lang or dictionaries.default_locale
        sfmt = SyntaxTextCharFormat(spell_format)
        if fmt is not None:
            sfmt.merge(fmt)

        tpos = 0
        for tlen, fmt in ans:
            if fmt is nbsp_format:
                split_ans.append((tlen, fmt))
            else:
                ctext = text[tpos:tpos + tlen]
                ppos = 0
                for start, length in split_into_words_and_positions(
                        ctext, lang=locale.langcode):
                    if start > ppos:
                        split_ans.append((start - ppos, fmt))
                    ppos = start + length
                    recognized = dictionaries.recognized(
                        ctext[start:ppos], locale)
                    if not recognized:
                        wsfmt = SyntaxTextCharFormat(sfmt)
                        wsfmt.setProperty(SPELL_PROPERTY,
                                          (ctext[start:ppos], locale))
                    split_ans.append((length, fmt if recognized else wsfmt))
                if ppos < tlen:
                    split_ans.append((tlen - ppos, fmt))

            tpos += tlen
        ans = split_ans

    return ans
예제 #6
0
파일: html.py 프로젝트: lovesuri/calibre
def process_text(state, text, nbsp_format, spell_format, user_data):
    ans = []
    fmt = None
    if state.is_bold or state.is_italic:
        fmt = SyntaxTextCharFormat()
        if state.is_bold:
            fmt.setFontWeight(QFont.Bold)
        if state.is_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)])
        last = m.end()
    if not ans:
        ans = [(len(text), fmt)]

    if tprefs['inline_spell_check'] and state.tags and user_data.tag_ok_for_spell(state.tags[-1].name):
        split_ans = []
        locale = state.current_lang or dictionaries.default_locale
        sfmt = SyntaxTextCharFormat(spell_format)
        if fmt is not None:
            sfmt.merge(fmt)

        tpos = 0
        for tlen, fmt in ans:
            if fmt is nbsp_format:
                split_ans.append((tlen, fmt))
            else:
                ctext = text[tpos:tpos+tlen]
                ppos = 0
                for start, length in split_into_words_and_positions(ctext, lang=locale.langcode):
                    if start > ppos:
                        split_ans.append((start - ppos, fmt))
                    ppos = start + length
                    recognized = dictionaries.recognized(ctext[start:ppos], locale)
                    if not recognized:
                        wsfmt = SyntaxTextCharFormat(sfmt)
                        wsfmt.setProperty(SPELL_PROPERTY, (ctext[start:ppos], locale))
                    split_ans.append((length, fmt if recognized else wsfmt))
                if ppos == 0:
                    split_ans.append((tlen, fmt))

            tpos += tlen
        ans = split_ans

    return ans
예제 #7
0
파일: themes.py 프로젝트: pgarst/calibre
def highlight_to_char_format(h):
    ans = SyntaxTextCharFormat()
    if h.bold:
        ans.setFontWeight(QFont.Bold)
    if h.italic:
        ans.setFontItalic(True)
    if h.fg is not None:
        ans.setForeground(h.fg)
    if h.bg is not None:
        ans.setBackground(h.bg)
    if h.underline:
        ans.setUnderlineStyle(underline_styles[h.underline])
        if h.underline_color is not None:
            ans.setUnderlineColor(h.underline_color.color())
    return ans
예제 #8
0
파일: themes.py 프로젝트: dusual/calibre
def highlight_to_char_format(h):
    ans = SyntaxTextCharFormat()
    if h.bold:
        ans.setFontWeight(QFont.Bold)
    if h.italic:
        ans.setFontItalic(True)
    if h.fg is not None:
        ans.setForeground(h.fg)
    if h.bg is not None:
        ans.setBackground(h.bg)
    if h.underline is not None:
        ans.setUnderlineStyle(underline_styles[h.underline])
        if h.underline_color is not None:
            ans.setUnderlineColor(h.underline_color.color())
    return ans