-
Notifications
You must be signed in to change notification settings - Fork 3
/
ZtReference.cls
310 lines (249 loc) · 12.7 KB
/
ZtReference.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "ZtReference"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' Class ZtReference.
' It preserves information of a reference entry in the bibliography, i.e. text, Word bookmark, and corresponding citations.
'
' Zotero Tools.
' This software is under Revised ('New') BSD license.
' Copyright © 2019, Olaf Ahrens. All rights reserved.
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' Private variables.
Private pvtConfig As ZtConfig
Private pvtMessageDisplay As ZtIMessageDisplayable
Private pvtBibliography As ZtBibliography
Private pvtRange As Word.Range
Private pvtMatch As ZtRegMatch
Private pvtBookmark As Word.Bookmark
Private pvtCitationGroups() As ZtCitationGroup
Private pvtCitationGroupsCt As Integer
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' Friend procedures and properties.
Friend Function Initialize(ByVal valConfig As ZtConfig, ByVal valMessageDisplay As ZtIMessageDisplayable, ByVal valBibliography As ZtBibliography, _
ByVal valRange As Word.Range) As ZtFMessageType
Dim locResult As ZtFMessageType
Set pvtConfig = valConfig
Set pvtMessageDisplay = valMessageDisplay
Set pvtBibliography = valBibliography
Set pvtRange = valRange.Duplicate
Set pvtMatch = pvtConfig.Final.Reference.WholeRegExp.FirstMatch(pvtRange.Text)
If Not pvtMatch Is Nothing Then
If pvtConfig.User.Macro.Debugging Then
locResult = pvtConfig.Final.Reference.WholeRegExp.Debugging(pvtMatch, pvtRange)
Else
locResult = MessageOk
End If
ElseIf Len(Trim$(pvtRange.Text)) > 0 Then
locResult = pvtMessageDisplay.Show("The reference style you set in ZtConfig.xml seems not to apply to the selected reference.", _
MessageCritical + MessageCancel, _
False, _
pvtRange)
Else
locResult = MessageNo
End If
Initialize = locResult
End Function
Friend Property Get Nr() As Integer
Nr = CInt(pvtMatch.Groups("number")) - 1
End Property
Friend Property Get ScreenTipText() As String
ScreenTipText = Left$(Trim$(pvtMatch.Groups("text")), pvtConfig.Basic.Word.MaxScreenTipTextLength)
End Property
Friend Sub RemoveCitedOnPage()
Dim locToCorrect As Boolean
Dim locRange As Word.Range
With pvtMatch
' Determine whether reference text has to be changed.
If Len(.Groups("backwardLinking")) > 0 Then
locToCorrect = True
ElseIf Len(.Groups("accessedAtEnd")) > 0 And _
.Groups("delimiterBeforeBackwardLinking") <> pvtConfig.User.ReferenceStyle.ReferenceAccessedAtEndSuffix Then
locToCorrect = True
ElseIf Len(.Groups("hyperlinkAtEnd")) > 0 And _
.Groups("delimiterBeforeBackwardLinking") <> pvtConfig.User.ReferenceStyle.ReferenceHyperlinkAtEndSuffix Then
locToCorrect = True
ElseIf .Groups("delimiterBeforeBackwardLinking") <> pvtConfig.User.ReferenceStyle.ReferenceNormalSuffix Then
locToCorrect = True
End If
' If necessary change reference text.
If locToCorrect Then
Set locRange = pvtRange.Duplicate
locRange.MoveStart wdCharacter, _
Len(.Groups("numbering")) + _
Len(.Groups("text")) + _
Len(.Groups("hyperlinkAtEnd")) + _
Len(.Groups("accessedAtEnd"))
If Len(.Groups("accessedAtEnd")) > 0 Then
locRange.Text = pvtConfig.User.ReferenceStyle.ReferenceAccessedAtEndSuffix
ElseIf Len(.Groups("hyperlinkAtEnd")) > 0 Then
locRange.Text = pvtConfig.User.ReferenceStyle.ReferenceHyperlinkAtEndSuffix
Else
locRange.Text = pvtConfig.User.ReferenceStyle.ReferenceNormalSuffix
End If
End If
End With
End Sub
Friend Sub SetBookmark()
Set pvtBookmark = pvtBibliography.MainStory.Document.AddBookmark(pvtConfig.Basic.Macro.ReferenceBookmarkPrefix & _
ZtSubprocedures.AddLeadingZeros(Me.Nr + 1, _
ZtSubprocedures.DigitsCt(pvtBibliography.ReferencesCt)), _
pvtRange)
End Sub
Friend Sub AddCitationGroup(ByVal valCitationGroup As ZtCitationGroup)
ReDim Preserve pvtCitationGroups(pvtCitationGroupsCt)
Set pvtCitationGroups(pvtCitationGroupsCt) = valCitationGroup
pvtCitationGroupsCt = pvtCitationGroupsCt + 1
End Sub
Friend Function SetWebLink(ByRef refAffectedReferencesCt As Integer) As ZtFMessageType
Dim locMatch As ZtRegMatch
Dim locWebLinkRange As Word.Range
Dim locResult As ZtFMessageType
Set locMatch = pvtConfig.Final.Reference.WebLinkRegExp.FirstMatch(pvtRange.Text)
If Not locMatch Is Nothing Then
If pvtConfig.User.Macro.Debugging Then
locResult = pvtConfig.Final.Reference.WebLinkRegExp.Debugging(locMatch, pvtRange)
Else
locResult = MessageOk
End If
If locResult = MessageOk Then
Set locWebLinkRange = pvtRange.Duplicate
With locWebLinkRange
.SetRange .Start + locMatch.FirstIndex, _
.Start + locMatch.FirstIndex + Len(locMatch.Groups("link"))
If .Hyperlinks.Count = 0 Then
If Len(locMatch.Groups("httpLink")) > 0 Then
pvtBibliography.MainStory.Document.AddWebLink locWebLinkRange, _
.Text
ElseIf Len(locMatch.Groups("doiLink")) > 0 Then
pvtBibliography.MainStory.Document.AddWebLink locWebLinkRange, _
pvtConfig.Basic.Macro.DoiResolver & locMatch.Groups("doiLink")
Else
pvtBibliography.MainStory.Document.AddWebLink locWebLinkRange, _
pvtConfig.Basic.Macro.DoiResolver & locMatch.Groups("shortDoiLink")
End If
refAffectedReferencesCt = refAffectedReferencesCt + 1
End If
End With
End If
Else
locResult = MessageOk
End If
SetWebLink = locResult
End Function
Friend Function SetInternalLinking() As ZtFMessageType
Dim locRange As Word.Range
Dim locCitationCtr As Integer
Dim locCitationGroup As ZtCitationGroup
Dim locDeleteRange As Word.Range
Dim locDeleteProcedures As ZtRangeProcedures
Dim locResult As ZtFMessageType
Dim locDeleteCharacters As String
Set locRange = pvtBookmark.Range.Duplicate
Set locDeleteProcedures = New ZtRangeProcedures
locDeleteCharacters = pvtConfig.Basic.Characters.Space & pvtConfig.Final.Reference.ReferenceUniversalSuffixCharacters
' Sort citations by their position in the document. Until now they are sorted by the Word stories they belong to.
pvtSortCitationGroups
With locRange
If pvtCitationGroupsCt > 0 Then
' Correct delimiter and spaces at the end of the range if necessary.
Set locDeleteRange = .Duplicate
With locDeleteRange
.Collapse wdCollapseEnd
locDeleteProcedures.SetRange locDeleteRange
locDeleteProcedures.MoveStartWhile locDeleteCharacters, wdBackward
If Len(.Text) > 0 Then
.Delete
End If
End With
' Insert introducting text.
.InsertAfter pvtConfig.User.BackwardLinkingStyle.DelimiterBefore
If pvtCitationGroupsCt = 1 Then
.InsertAfter pvtConfig.User.BackwardLinkingStyle.PrefixSingular
Else
.InsertAfter pvtConfig.User.BackwardLinkingStyle.PrefixPlural
End If
' Insert backward linking as crossreference which will update automatically, not fixed page numbers.
' Insert extra hyperlinks instead of crossreferences with hyperlinks, for screen tip texts.
.Collapse wdCollapseEnd
.InsertAfter " " ' For protection.
.Collapse wdCollapseStart
For locCitationCtr = 0 To pvtCitationGroupsCt - 1
Set locCitationGroup = pvtCitationGroups(locCitationCtr)
.InsertCrossReference wdRefTypeBookmark, wdPageNumber, locCitationGroup.Bookmark.Name, False, False, False
.MoveEndUntil " ", wdForward
pvtBibliography.MainStory.Document.AddInternalLink locRange, _
locCitationGroup.Bookmark.Name, _
locCitationGroup.Caption.ScreenTipText(.Information(wdActiveEndAdjustedPageNumber))
.Style = pvtBibliography.MainStory.Document.Styles(pvtConfig.User.BackwardLinkingStyle.HyperlinkStyleName)
If locCitationCtr < pvtCitationGroupsCt - 1 Then
.Move wdCharacter, 1
.InsertAfter pvtConfig.User.BackwardLinkingStyle.Delimiter
End If
.Collapse wdCollapseEnd
Next
.Move wdCharacter, 1
.InsertAfter pvtConfig.User.BackwardLinkingStyle.Suffix
.Collapse wdCollapseEnd
.MoveEnd wdCharacter, 1
.Delete ' Delete protection.
locResult = MessageOk
ElseIf pvtBibliography.ShowNoCitationError Then
locResult = pvtMessageDisplay.Show("I couldn't find any citations of the selected reference." & vbNewLine & vbNewLine & _
"You may procede, otherwise cancel.", _
MessageCritical + MessageOkSuppressCancel, _
True, _
locRange)
If locResult = MessageSuppress Then
pvtBibliography.ShowNoCitationError = False
locResult = MessageOk
End If
End If
End With
SetInternalLinking = locResult
End Function
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
' Private procedures and properties.
' Optimized bubblesort from https://de.wikipedia.org/wiki/Bubblesort
Private Sub pvtSortCitationGroups()
Dim locCtr As Integer
Dim locBubbleBorder As Integer
Dim locNewBubbleBorder As Integer
Dim locPageNr0 As Integer
Dim locPageNr1 As Integer
locBubbleBorder = pvtCitationGroupsCt - 1
Do
locNewBubbleBorder = 1
For locCtr = 0 To locBubbleBorder - 1
locPageNr0 = pvtCitationGroups(locCtr).Bookmark.Range.Information(wdActiveEndPageNumber)
locPageNr1 = pvtCitationGroups(locCtr + 1).Bookmark.Range.Information(wdActiveEndPageNumber)
If locPageNr0 >= locPageNr1 Then
If locPageNr0 > locPageNr1 Then
pvtSwapCitationGroups locCtr
locNewBubbleBorder = locCtr
ElseIf pvtCitationGroups(locCtr).CorrectedResultStart > pvtCitationGroups(locCtr + 1).CorrectedResultStart Then
pvtSwapCitationGroups locCtr
locNewBubbleBorder = locCtr
End If
End If
Next
locBubbleBorder = locNewBubbleBorder
Loop While locBubbleBorder > 1
End Sub
Private Sub pvtSwapCitationGroups(ByVal valFirstCitationGroup As Integer)
Dim locTempCitationGroup As ZtCitationGroup
Set locTempCitationGroup = pvtCitationGroups(valFirstCitationGroup)
Set pvtCitationGroups(valFirstCitationGroup) = pvtCitationGroups(valFirstCitationGroup + 1)
Set pvtCitationGroups(valFirstCitationGroup + 1) = locTempCitationGroup
End Sub
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *