-
Notifications
You must be signed in to change notification settings - Fork 0
/
Highlighter.cls
160 lines (144 loc) · 4.73 KB
/
Highlighter.cls
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Highlighter"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Private mDocument As Document
Private mName As String
Private mCodeSections As CodeSections
Private mKeyWordDictionary As KeyWordDictionary
Private mOptions As HighlighterOptions
Private mAllKeyWords As Keywords
Public Property Get CodeSections() As CodeSections
Set CodeSections = mCodeSections
End Property
Public Property Get Name() As String
Name = mName & " in " & Me.Document.Name
End Property
Public Property Get Options() As HighlighterOptions
Set Options = mOptions
End Property
Public Property Get KeyWordDictionary() As KeyWordDictionary
Set KeyWordDictionary = mKeyWordDictionary
End Property
Public Sub AddKeyword(kw As Keyword)
mAllKeyWords.Add kw
End Sub
Public Sub RecreateBuiltInStyles()
mCreateCharacterStyles
mCreateCodeParagraphStyle
End Sub
Private Sub mCreateCharacterStyles()
Dim styleNames As Variant
Dim styleColors As Variant
Dim i As Long
styleNames = Array("Keyword", "DataType", "String", "Comment")
styleColors = Array(wdColorDarkBlue, wdColorOrange, wdColorLightBlue, wdColorGreen)
If mDocument Is Nothing Then
Err.Raise 90815, "Highlighter.CreateCharacterStyles", "No Document Set"
Else
For i = LBound(styleNames) To UBound(styleNames)
CreateCustomCharacterStyle styleNames(i), styleColors(i)
Next i
End If
End Sub
Private Sub mCreateCodeParagraphStyle()
Dim stl As Style
If mDocument Is Nothing Then
Err.Raise 90815, "Highlighter.CreateCodeParagraphStyle", "No Document Set"
Else
Set stl = Nothing
On Error Resume Next
Set stl = mDocument.Styles("Code")
On Error GoTo 0
If stl Is Nothing Then
Set stl = mDocument.Styles.Add("Code", wdStyleTypeParagraph)
End If
With stl
.BaseStyle = mDocument.Styles(wdStyleNormal)
.Font.Name = "Courier New"
.Font.Size = "10"
.NoProofing = True
With stl.ParagraphFormat
.LeftIndent = 10
.RightIndent = 10
.Hyphenation = False
.SpaceBefore = 0
.SpaceAfter = 0
.Shading.BackgroundPatternColor = wdColorGray10
End With
End With
End If
End Sub
Public Sub CreateCustomCharacterStyle(ByVal StyleName As String, ByVal TextColor As WdColor)
Dim stl As Style
Dim i As Long
If mDocument Is Nothing Then
Err.Raise 90817, "Highlighter.CreateCustomCharacterStyle", "No Document Set"
Else
Set stl = Nothing
On Error Resume Next
Set stl = mDocument.Styles(StyleName)
On Error GoTo 0
If stl Is Nothing Then
Set stl = mDocument.Styles.Add(StyleName, wdStyleTypeCharacter)
End If
stl.Font.TextColor = TextColor
End If
End Sub
Public Property Set Document(doc As Document)
Set mDocument = doc
RecreateBuiltInStyles
End Property
Public Property Get Document() As Document
Set Document = mDocument
End Property
Public Sub ParseDocument()
'READINGMARK: Working here
Dim tmpCodeSection As CodeSection
Dim para As Paragraph
Set tmpCodeSection = New CodeSection
tmpCodeSection.Init Me
For Each para In mDocument.StoryRanges(wdMainTextStory).Paragraphs
If para.Style = "Code" Then
tmpCodeSection.AddParagraph para
Else
If tmpCodeSection.ParagraphCount > 0 Then
mCodeSections.Add tmpCodeSection
Set tmpCodeSection = New CodeSection
tmpCodeSection.Init Me
End If
End If
Next para
If tmpCodeSection.ParagraphCount > 0 Then
mCodeSections.Add tmpCodeSection
Set tmpCodeSection = Nothing
End If
'Todo: Start Parsing All Sections
End Sub
Public Sub StyleAllKeywords()
'TODO: Only when Parsed
'TODO: Unstyle all
Dim i As Long
For i = 1 To mAllKeyWords.Count
mAllKeyWords.Item(i).StyleKeyword
Next i
End Sub
Private Sub Class_Initialize()
Set mCodeSections = New CodeSections
mCodeSections.Init Me
If mDocument Is Nothing Then Set mDocument = ThisDocument
mName = "SyntaxHighlighter"
RecreateBuiltInStyles
Set mOptions = New HighlighterOptions
mOptions.Init Me
Set mAllKeyWords = New Keywords
mAllKeyWords.Init Me
Set mKeyWordDictionary = New KeyWordDictionary
mKeyWordDictionary.Init Me
End Sub