forked from tweecode/twine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweestyler.py
134 lines (99 loc) · 4.86 KB
/
tweestyler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from tweelexer import TweeLexer
import wx, wx.stc
class TweeStyler(TweeLexer):
"""Applies syntax highlighting for Twee syntax in a wx.StyledTextCtrl.
This needs to be passed the control it will be lexing, so it can
look up the body text as needed.
"""
def __init__(self, control, frame):
self.ctrl = control
self.frame = frame
self.app = frame.app
self.ctrl.Bind(wx.stc.EVT_STC_STYLENEEDED, lambda event: self.lex())
self.initStyles()
def initStyles(self):
"""
Initialize style definitions. This is automatically invoked when
the constructor is called, but should be called any time font
preferences are changed.
"""
bodyFont = wx.Font(self.app.config.ReadInt('windowedFontSize'), wx.MODERN, wx.NORMAL, \
wx.NORMAL, False, self.app.config.Read('windowedFontFace'))
monoFont = wx.Font(self.app.config.ReadInt('monospaceFontSize'), wx.MODERN, wx.NORMAL, \
wx.NORMAL, False, self.app.config.Read('monospaceFontFace'))
self.ctrl.StyleSetFont(wx.stc.STC_STYLE_DEFAULT, bodyFont)
self.ctrl.StyleClearAll()
# Styles 1-8 are BOLD, ITALIC, UNDERLINE, and bitwise combinations thereof
for i in range(0,8):
self.ctrl.StyleSetFont(i, bodyFont)
if (i & 1):
self.ctrl.StyleSetBold(i, True)
if (i & 2):
self.ctrl.StyleSetItalic(i, True)
if (i & 4):
self.ctrl.StyleSetUnderline(i, True)
for i in self.STYLE_CONSTANTS:
self.ctrl.StyleSetFont(i, bodyFont)
self.ctrl.StyleSetBold(self.GOOD_LINK, True)
self.ctrl.StyleSetForeground(self.GOOD_LINK, self.GOOD_LINK_COLOR)
self.ctrl.StyleSetBold(self.BAD_LINK, True)
self.ctrl.StyleSetForeground(self.BAD_LINK, self.BAD_LINK_COLOR)
self.ctrl.StyleSetBold(self.BAD_MACRO, True)
self.ctrl.StyleSetForeground(self.BAD_MACRO, self.BAD_LINK_COLOR)
self.ctrl.StyleSetBold(self.STORYINCLUDE_LINK, True)
self.ctrl.StyleSetForeground(self.STORYINCLUDE_LINK, self.STORYINCLUDE_COLOR)
self.ctrl.StyleSetForeground(self.MARKUP, self.MARKUP_COLOR)
self.ctrl.StyleSetForeground(self.INLINE_STYLE, self.MARKUP_COLOR)
self.ctrl.StyleSetBold(self.BAD_INLINE_STYLE, True)
self.ctrl.StyleSetForeground(self.BAD_INLINE_STYLE, self.BAD_LINK_COLOR)
self.ctrl.StyleSetBold(self.HTML, True)
self.ctrl.StyleSetForeground(self.HTML, self.HTML_COLOR)
self.ctrl.StyleSetForeground(self.HTML_BLOCK, self.HTML_COLOR)
self.ctrl.StyleSetBold(self.MACRO, True)
self.ctrl.StyleSetForeground(self.MACRO, self.MACRO_COLOR)
self.ctrl.StyleSetItalic(self.COMMENT, True)
self.ctrl.StyleSetForeground(self.COMMENT, self.COMMENT_COLOR)
self.ctrl.StyleSetForeground(self.SILENT, self.COMMENT_COLOR)
self.ctrl.StyleSetFont(self.MONO, monoFont)
self.ctrl.StyleSetBold(self.EXTERNAL, True)
self.ctrl.StyleSetForeground(self.EXTERNAL, self.EXTERNAL_COLOR)
self.ctrl.StyleSetBold(self.IMAGE, True)
self.ctrl.StyleSetForeground(self.IMAGE, self.IMAGE_COLOR)
self.ctrl.StyleSetBold(self.PARAM, True)
self.ctrl.StyleSetForeground(self.PARAM, self.PARAM_COLOR)
self.ctrl.StyleSetBold(self.PARAM_VAR, True)
self.ctrl.StyleSetForeground(self.PARAM_VAR, self.PARAM_VAR_COLOR)
self.ctrl.StyleSetBold(self.PARAM_STR, True)
self.ctrl.StyleSetForeground(self.PARAM_STR, self.PARAM_STR_COLOR)
self.ctrl.StyleSetBold(self.PARAM_NUM, True)
self.ctrl.StyleSetForeground(self.PARAM_NUM, self.PARAM_NUM_COLOR)
self.ctrl.StyleSetBold(self.PARAM_BOOL, True)
self.ctrl.StyleSetForeground(self.PARAM_BOOL, self.PARAM_BOOL_COLOR)
def getText(self):
return self.ctrl.GetTextUTF8()
def passageExists(self, title):
return (self.frame.widget.parent.passageExists(title, False))
def includedPassageExists(self, title):
return (self.frame.widget.parent.includedPassageExists(title))
def applyStyle(self, start, end, style):
self.ctrl.StartStyling(start, self.TEXT_STYLES)
self.ctrl.SetStyling(end, style)
def getHeader(self):
return self.frame.getHeader()
# style colors
GOOD_LINK_COLOR = '#3333cc'
EXTERNAL_COLOR = '#337acc'
STORYINCLUDE_COLOR = '#906fe2'
BAD_LINK_COLOR = '#cc3333'
MARKUP_COLOR = '#008200'
MACRO_COLOR = '#a94286'
COMMENT_COLOR = '#868686'
IMAGE_COLOR = '#088A85'
HTML_COLOR = '#4d4d9d'
# param colours
PARAM_COLOR = '#7f456a'
PARAM_VAR_COLOR = '#005682'
PARAM_BOOL_COLOR = '#626262'
PARAM_STR_COLOR = '#008282'
PARAM_NUM_COLOR = '#A15000'
TEXT_STYLES = 31 # mask for StartStyling() to indicate we're only changing text styles