<s> import re <EOL> from PyQt4 . QtGui import QSyntaxHighlighter , QColor , QFont , QTextCharFormat <EOL> from PyQt4 . QtCore import Qt <EOL> from . mdx_strkundr import DEL_RE , INS_RE , STRONG_RE , EMPH_RE <EOL> from . mikibook import Mikibook <EOL> class MikiHighlighter ( QSyntaxHighlighter ) : <EOL> WORDS = r'<STR_LIT>' <EOL> def __init__ ( self , parent = None , scale_font_sizes = True ) : <EOL> super ( MikiHighlighter , self ) . __init__ ( parent ) <EOL> baseFontSize = Mikibook . settings . value ( '<STR_LIT>' , type = int , defaultValue = <NUM_LIT:12> ) <EOL> baseFontFam = Mikibook . settings . value ( '<STR_LIT>' , defaultValue = None ) <EOL> NUM = <NUM_LIT:16> <EOL> self . patterns = [ ] <EOL> regexp = [ <NUM_LIT:0> ] * NUM <EOL> font = [ <NUM_LIT:0> ] * NUM <EOL> color = [ <NUM_LIT:0> ] * NUM <EOL> color_settings = Mikibook . highlighterColors ( ) <EOL> regexp [ <NUM_LIT:0> ] = r'<STR_LIT>' <EOL> font [ <NUM_LIT:0> ] = QFont ( baseFontFam , baseFontSize , - <NUM_LIT:1> ) <EOL> color [ <NUM_LIT:0> ] = QColor ( color_settings [ <NUM_LIT:0> ] ) <EOL> regexp [ <NUM_LIT:1> ] = '<STR_LIT>' <EOL> color [ <NUM_LIT:1> ] = QColor ( color_settings [ <NUM_LIT:1> ] ) <EOL> if scale_font_sizes : <EOL> font [ <NUM_LIT:1> ] = QFont ( baseFontFam , <NUM_LIT:2> * baseFontSize , QFont . Bold ) <EOL> else : <EOL> font [ <NUM_LIT:1> ] = QFont ( baseFontFam , baseFontSize , QFont . Bold ) <EOL> regexp [ <NUM_LIT:2> ] = '<STR_LIT>' <EOL> color [ <NUM_LIT:2> ] = QColor ( color_settings [ <NUM_LIT:2> ] ) <EOL> if scale_font_sizes : <EOL> font [ <NUM_LIT:2> ] = QFont ( baseFontFam , <NUM_LIT> / <NUM_LIT:3> * baseFontSize , QFont . Bold ) <EOL> else : <EOL> font [ <NUM_LIT:2> ] = QFont ( baseFontFam , baseFontSize , QFont . Bold ) <EOL> regexp [ <NUM_LIT:3> ] = '<STR_LIT>' <EOL> color [ <NUM_LIT:3> ] = QColor ( color_settings [ <NUM_LIT:3> ] ) <EOL> if scale_font_sizes : <EOL> font [ <NUM_LIT:3> ] = QFont ( baseFontFam , <NUM_LIT> / <NUM_LIT:3> * baseFontSize , QFont . Bold ) <EOL> else : <EOL> font [ <NUM_LIT:3> ] = QFont ( baseFontFam , baseFontSize , QFont . Bold ) <EOL> regexp [ <NUM_LIT:4> ] = '<STR_LIT>' <EOL> color [ <NUM_LIT:4> ] = QColor ( color_settings [ <NUM_LIT:4> ] ) <EOL> font [ <NUM_LIT:4> ] = QFont ( baseFontFam , baseFontSize , QFont . Bold ) <EOL> regexp [ <NUM_LIT:5> ] = '<STR_LIT>' <EOL> color [ <NUM_LIT:5> ] = QColor ( color_settings [ <NUM_LIT:5> ] ) <EOL> font [ <NUM_LIT:5> ] = QFont ( baseFontFam , baseFontSize , - <NUM_LIT:1> ) <EOL> regexp [ <NUM_LIT:6> ] = '<STR_LIT>' <EOL> color [ <NUM_LIT:6> ] = QColor ( color_settings [ <NUM_LIT:6> ] ) <EOL> font [ <NUM_LIT:6> ] = QFont ( baseFontFam , baseFontSize , - <NUM_LIT:1> ) <EOL> regexp [ <NUM_LIT:7> ] = DEL_RE <EOL> color [ <NUM_LIT:7> ] = QColor ( color_settings [ <NUM_LIT:7> ] ) <EOL> font [ <NUM_LIT:7> ] = QFont ( baseFontFam , baseFontSize , - <NUM_LIT:1> ) <EOL> regexp [ <NUM_LIT:8> ] = INS_RE <EOL> font [ <NUM_LIT:8> ] = QFont ( baseFontFam , baseFontSize , - <NUM_LIT:1> ) <EOL> font [ <NUM_LIT:8> ] . setUnderline ( True ) <EOL> regexp [ <NUM_LIT:9> ] = STRONG_RE <EOL> color [ <NUM_LIT:9> ] = QColor ( color_settings [ <NUM_LIT:9> ] ) <EOL> font [ <NUM_LIT:9> ] = QFont ( baseFontFam , baseFontSize , QFont . Bold ) <EOL> regexp [ <NUM_LIT:10> ] = EMPH_RE <EOL> color [ <NUM_LIT:10> ] = QColor ( color_settings [ <NUM_LIT:10> ] ) <EOL> font [ <NUM_LIT:10> ] = QFont ( baseFontFam , baseFontSize , - <NUM_LIT:1> , True ) <EOL> regexp [ <NUM_LIT:11> ] = ( r'<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> font [ <NUM_LIT:11> ] = QFont ( baseFontFam , baseFontSize , - <NUM_LIT:1> , True ) <EOL> font [ <NUM_LIT:11> ] . setUnderline ( True ) <EOL> regexp [ <NUM_LIT:12> ] = r'<STR_LIT>' <EOL> color [ <NUM_LIT:12> ] = QColor ( color_settings [ <NUM_LIT:12> ] ) <EOL> font [ <NUM_LIT:12> ] = QFont ( baseFontFam , baseFontSize , - <NUM_LIT:1> ) <EOL> regexp [ <NUM_LIT> ] = r'<STR_LIT>' <EOL> color [ <NUM_LIT> ] = QColor ( color_settings [ <NUM_LIT> ] ) <EOL> font [ <NUM_LIT> ] = QFont ( baseFontFam , baseFontSize , - <NUM_LIT:1> ) <EOL> regexp [ <NUM_LIT> ] = '<STR_LIT>' <EOL> color [ <NUM_LIT> ] = QColor ( color_settings [ <NUM_LIT> ] ) <EOL> font [ <NUM_LIT> ] = QFont ( baseFontFam , baseFontSize , QFont . Bold ) <EOL> regexp [ <NUM_LIT:15> ] = r'<STR_LIT>' <EOL> color [ <NUM_LIT:15> ] = QColor ( color_settings [ <NUM_LIT:15> ] ) <EOL> font [ <NUM_LIT:15> ] = QFont ( baseFontFam , baseFontSize , QFont . Bold ) <EOL> for i in range ( NUM ) : <EOL> p = re . compile ( regexp [ i ] ) <EOL> f = QTextCharFormat ( ) <EOL> if font [ i ] != <NUM_LIT:0> : <EOL> f . setFont ( font [ i ] ) <EOL> if color [ i ] != <NUM_LIT:0> : <EOL> f . setForeground ( color [ i ] ) <EOL> self . patterns . append ( ( p , f ) ) <EOL> self . speller = parent . speller <EOL> fenced_font = QFont ( baseFontFam , baseFontSize , - <NUM_LIT:1> ) <EOL> self . fenced_block = re . compile ( r"<STR_LIT>" ) <EOL> self . fenced_format = QTextCharFormat ( ) <EOL> self . fenced_format . setFont ( fenced_font ) <EOL> math_font = QFont ( baseFontFam , baseFontSize , - <NUM_LIT:1> ) <EOL> self . math_block = re . compile ( r"<STR_LIT>" ) <EOL> self . math_format = QTextCharFormat ( ) <EOL> self . math_format . setFont ( math_font ) <EOL> self . settext_h1 = re . compile ( '<STR_LIT>' ) <EOL> self . settext_h2 = re . compile ( '<STR_LIT>' ) <EOL> def highlightSpellcheck ( self , text ) : <EOL> for word_object in re . finditer ( self . WORDS , str ( text ) ) : <EOL> if not word_object . group ( ) : <EOL> continue <EOL> if self . speller and not self . speller . check ( word_object . group ( ) ) : <EOL> current_format = self . format ( word_object . start ( ) ) <EOL> current_format . setUnderlineColor ( Qt . red ) <EOL> current_format . setUnderlineStyle ( QTextCharFormat . SpellCheckUnderline ) <EOL> self . setFormat ( word_object . start ( ) , <EOL> word_object . end ( ) - word_object . start ( ) , current_format ) <EOL> def highlightBlock ( self , text ) : <EOL> for i in range ( <NUM_LIT:0> , len ( self . patterns ) ) : <EOL> p = self . patterns [ i ] <EOL> for match in p [ <NUM_LIT:0> ] . finditer ( text ) : <EOL> self . setFormat ( <EOL> match . start ( ) , match . end ( ) - match . start ( ) , p [ <NUM_LIT:1> ] ) <EOL> if text == '<STR_LIT>' and self . currentBlock ( ) . next ( ) . text ( ) != '<STR_LIT>' : <EOL> self . setCurrentBlockState ( self . previousBlockState ( ) ) <EOL> elif self . previousBlockState ( ) == <NUM_LIT:3> : <EOL> self . setFormat ( <NUM_LIT:0> , len ( text ) , self . patterns [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> self . setCurrentBlockState ( <NUM_LIT:0> ) <EOL> elif self . previousBlockState ( ) == <NUM_LIT:4> : <EOL> self . setFormat ( <NUM_LIT:0> , len ( text ) , self . patterns [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] ) <EOL> self . setCurrentBlockState ( <NUM_LIT:0> ) <EOL> else : <EOL> m = self . fenced_block . match ( text ) <EOL> m2 = self . math_block . match ( text ) <EOL> self . setCurrentBlockState ( <NUM_LIT:0> ) <EOL> if self . previousBlockState ( ) not in ( <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> if m : <EOL> self . setCurrentBlockState ( <NUM_LIT:1> ) <EOL> elif m2 and not ( text [ m2 . start ( ) : m2 . end ( ) ] . endswith ( "<STR_LIT>" ) and not text == "<STR_LIT>" ) : <EOL> self . setCurrentBlockState ( <NUM_LIT:2> ) <EOL> else : <EOL> if self . settext_h1 . match ( self . currentBlock ( ) . next ( ) . text ( ) ) and text != '<STR_LIT>' : <EOL> self . setFormat ( <NUM_LIT:0> , len ( text ) , self . patterns [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> self . setCurrentBlockState ( <NUM_LIT:3> ) <EOL> elif self . settext_h2 . match ( self . currentBlock ( ) . next ( ) . text ( ) ) and text != '<STR_LIT>' : <EOL> self . setFormat ( <NUM_LIT:0> , len ( text ) , self . patterns [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] ) <EOL> self . setCurrentBlockState ( <NUM_LIT:4> ) <EOL> elif self . previousBlockState ( ) == <NUM_LIT:1> : <EOL> if m : <EOL> self . setCurrentBlockState ( <NUM_LIT:0> ) <EOL> else : <EOL> self . setCurrentBlockState ( <NUM_LIT:1> ) <EOL> self . setFormat ( <NUM_LIT:0> , len ( text ) , self . fenced_format ) <EOL> elif self . previousBlockState ( ) == <NUM_LIT:2> : <EOL> if m2 : <EOL> self . setCurrentBlockState ( <NUM_LIT:0> ) <EOL> else : <EOL> self . setCurrentBlockState ( <NUM_LIT:2> ) <EOL> self . setFormat ( <NUM_LIT:0> , len ( text ) , self . math_format ) <EOL> self . highlightSpellcheck ( text ) </s>
