Exemple #1
0
def process_text(state, text, nbsp_format, spell_format, user_data):
    ans = []
    fmt = None
    if state.is_bold or state.is_italic:
        fmt = syntax_text_char_format()
        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 do_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 = QTextCharFormat(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:
                split_ans.extend(
                    check_spelling(text[tpos:tpos + tlen], tlen, fmt, locale,
                                   sfmt, store_locale.enabled))

            tpos += tlen
        ans = split_ans

    return ans
Exemple #2
0
def process_text(state, text, nbsp_format, spell_format, user_data):
    ans = []
    fmt = None
    if state.is_bold or state.is_italic:
        fmt = syntax_text_char_format()
        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 do_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 = QTextCharFormat(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:
                split_ans.extend(
                    check_spelling(text[tpos : tpos + tlen], tlen, fmt, locale, sfmt, store_locale.enabled)
                )

            tpos += tlen
        ans = split_ans

    return ans
Exemple #3
0
    def highlightBlock(self, text):
        words = re.compile('[^_\\W]+', flags=re.UNICODE)

        char_format = QTextCharFormat()
        char_format.setUnderlineColor(Qt.red)
        char_format.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline)

        # collect data for checking
        mark = []
        for match in words.finditer(text):
            final_format = QTextCharFormat()
            final_format.merge(char_format)
            final_format.merge(self.format(match.start()))

            if self._dictionary.word_needs_no_verification(match.group(0)):
                continue

            mark.append((match.group(0).lower(), match.start(),
                         match.end() - match.start(), final_format))

        # word check
        for word, start, length, fmt in mark:
            if not self._dictionary.check_word(word):
                self.setFormat(start, length, fmt)