Skip to content

HighlightRule

Sub classes

Name Children Inherits
Rule
prettyqt.syntaxhighlighters.markdownhighlighter
  • Link
  • Image
  • Heading1
  • Heading2
  • Heading3
  • Heading4
  • Heading5
  • Heading6
  • Emphasis
  • Strong
  • Code
  • ...
Rule
prettyqt.syntaxhighlighters.pythonhighlighter
  • Keyword
  • Operator
  • Bracket
  • Self
  • String
  • Def
  • Class
  • Comment
  • Number
Rule
prettyqt.syntaxhighlighters.yamlhighlighter
  • Bool
  • Decimal
  • Rule2
  • Rule3
  • ListMember
  • DocumentStart
  • Brackets
  • Rule7
  • Rule8
  • DocType
  • Xml
  • ...

🛈 DocStrings

A highlighting rule, configured via class attributes of subclasses.

Source code in prettyqt/syntaxhighlighters/highlightrule.py
class HighlightRule:
    """A highlighting rule, configured via class attributes of subclasses."""

    regex: ClassVar[str | list[str]] = ""
    color: ClassVar[datatypes.ColorType] = "black"
    italic: ClassVar[bool] = False
    bold: ClassVar[bool] = False
    minimal: ClassVar[bool] = False
    font_size: ClassVar[float | None] = None
    nth: ClassVar[int] = 0
    compiled: ClassVar[re.Pattern | list[re.Pattern] | None] = None
    fmt: ClassVar[gui.TextCharFormat]

    def __init_subclass__(cls):
        super().__init_subclass__()
        if isinstance(cls.regex, str):
            cls.compiled = re.compile(cls.regex)
            # cls.compiled.setMinimal(True)
        else:
            cls.compiled = [re.compile(r) for r in cls.regex]
        cls.fmt = cls.get_format()

    @classmethod
    def get_format(cls) -> gui.TextCharFormat:
        fmt = gui.TextCharFormat()
        fmt.setFontItalic(cls.italic)
        fmt.set_foreground_color(cls.color)
        if cls.font_size:
            fmt.setFontPointSize(cls.font_size)
        if cls.bold:
            fmt.set_font_weight("bold")
        return fmt

:fa-brands-github: Show source on GitHub