Skip to content

Commit

Permalink
Merge pull request #40 from macmillanpublishers/wdv-395-slow-note-cle…
Browse files Browse the repository at this point in the history
…anup

Wdv 395 slow note cleanup
  • Loading branch information
mattretzer authored Jun 30, 2021
2 parents e3f4b7a + 2cd2776 commit b2a5f41
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 67 deletions.
Binary file modified RSuite_Word-template.dotm
Binary file not shown.
Binary file modified StyleTemplate_auto-generate/RSuite.dotx
Binary file not shown.
Binary file modified StyleTemplate_auto-generate/RSuite_NoColor.dotx
Binary file not shown.
14 changes: 5 additions & 9 deletions src/RSuite_Word-template/Clean.bas
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,11 @@ Sub Spaces(MyStoryNo)
' note: replacing with ^p using WithExclude function requires 'vbnewline' instead
Clean_helpers.FindReplaceSimple_WithExclude "^l", vbNewLine, MyStoryNo

'spaces before/after line breaks; run 'trim spaces' instead/beforehand for Note storys:
' as per wdv-354 bug
If MyStoryNo = 2 Or MyStoryNo = 3 Then
Clean_helpers.TrimNoteSpaces (MyStoryNo)
ElseIf MyStoryNo = 1 Then
Clean_helpers.FindReplaceSimple " " + ChrW(13), "^p", MyStoryNo
Clean_helpers.FindReplaceSimple " ^p", "^p", MyStoryNo
End If

' these 2 modified f-and-r's (along with TrimTrailingSpace below (from wdv-387)) help with
' extra para problem surfaced in wdv-354, but are more efficient with notes as per wdv-395
Clean_helpers.TrimLeadingSpace " " + ChrW(13), "^p", MyStoryNo
Clean_helpers.TrimLeadingSpace " ^p", "^p", MyStoryNo

' these 2 f-and-r's get special attention because they span 2 paras.
Clean_helpers.TrimTrailingSpace_WithExclude ChrW(13) + " ", vbNewLine, MyStoryNo
Clean_helpers.TrimTrailingSpace_WithExclude "^p ", vbNewLine, MyStoryNo
Expand Down
136 changes: 80 additions & 56 deletions src/RSuite_Word-template/Clean_helpers.bas
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,57 @@ Public Function FindReplaceSimple(ByVal sFind As String, ByVal sReplace As Strin

End Function

Public Function FindReplaceSimpleWthRng(myRng As Range, ByVal sFind As String, ByVal sReplace As String)

'Public Function FindReplaceSimpleWthRng(myRng As Range, ByVal sFind As String, ByVal sReplace As String)
'
' Call ClearSearch
'
' With myRng.Find
' .Text = sFind
' .Replacement.Text = sReplace
' .Execute Replace:=wdReplaceAll, Forward:=True
' End With
'
'End Function

Public Function TrimLeadingSpace(ByVal sFind As String, ByVal sReplace As String, Optional storyNumber As Variant = 1)
' This is a mirror of the function below (sans exclude):
' to trim leading spaces from paras within a note, instead of running an F&R inside each note as done previously.
Dim Rg As Range
Set Rg = ActiveDocument.StoryRanges(storyNumber)

Call ClearSearch

With myRng.Find
.Text = sFind
.Replacement.Text = sReplace
.Execute Replace:=wdReplaceAll, Forward:=True
End With

With Rg.Find
.Text = sFind
While .Execute
' skipping space removal for blank notes
If storyNumber = 3 Then
If Rg.Endnotes.Count = 1 Then
If Rg.Endnotes(1).Range.Text <> "" Then
If Rg.Characters.First = " " Then
Rg.Characters.First = ""
Rg.Collapse wdCollapseEnd
End If
End If
End If
ElseIf storyNumber = 2 Then
If Rg.Footnotes.Count = 1 Then
If Rg.Footnotes(1).Range.Text <> "" Then
If Rg.Characters.First = " " Then
Rg.Characters.First = ""
Rg.Collapse wdCollapseEnd
End If
End If
End If
ElseIf Rg.Characters.First = " " Then
Rg.Characters.First = ""
Rg.Collapse wdCollapseEnd
End If
Wend
End With

End Function

Public Function TrimTrailingSpace_WithExclude(ByVal sFind As String, ByVal sReplace As String, Optional storyNumber As Variant = 1)
' When we used straight up f&r for this, if range spanned 2 differently styled paras it would restyle para 1 to match 2.
' By just trimming the space, without replacing the ^p, we leave the first para alone entirely.
Expand Down Expand Up @@ -258,54 +298,38 @@ Public Function AtStartOfDocument() As Boolean
End Select
End Function

Public Function TrimNoteSpaces(storyNumber As Variant)
Dim oRng As Range
' Can optionally fully trim note whitespace with commented whiles & Wends below,
' But this is not necessary since previous f&r's are trimming multispaces to 1
' 4/2/20 - adding ' ^p' cleanup here, b/c it still creates extra ^ps with blank notes.
' Here it's easy to finely tune our replacements since we are handling note ranges already

If storyNumber = 2 Then 'footnotes
For Each note In ActiveDocument.Footnotes
Set oRng = note.Range
With oRng
If .Characters.Last = " " Then 'While
.Characters.Last = ""
End If 'Wend
If .Characters.First = " " Then 'While
.Characters.First = ""
End If 'Wend
End With
' strip trailing space from non-note ending paras:
' we skip empty notes due to false positives, and undeletable note-ending ^p's
' they can result in extra ^p's being inserted.
If oRng.Text <> "" Then
FindReplaceSimpleWthRng oRng, " " + ChrW(13), "^p"
FindReplaceSimpleWthRng oRng, " ^p", "^p"
End If
Next note
ElseIf storyNumber = 3 Then 'endnotes
For Each note In ActiveDocument.Endnotes
Set oRng = note.Range
With oRng
If .Characters.Last = " " Then 'While
.Characters.Last = ""
End If 'Wend
If .Characters.First = " " Then 'While
.Characters.First = ""
End If 'Wend
End With
' strip trailing space from non-note ending paras:
' we skip empty notes due to false positives, and undeletable note-ending ^p's
' they can result in extra ^p's being inserted.
If oRng.Text <> "" Then
FindReplaceSimpleWthRng oRng, " " + ChrW(13), "^p"
FindReplaceSimpleWthRng oRng, " ^p", "^p"
End If
Next note
End If

End Function
'Public Function TrimNoteSpaces(storyNumber As Variant)
'Dim oRng As Range
'' Can optionally fully trim note whitespace with commented whiles & Wends below,
'' But this is not necessary since previous f&r's are trimming multispaces to 1
'
'If storyNumber = 2 Then 'footnotes
' For Each note In ActiveDocument.Footnotes
' Set oRng = note.Range
' With oRng
' If .Characters.Last = " " Then 'While
' .Characters.Last = ""
' End If 'Wend
' If .Characters.First = " " Then 'While
' .Characters.First = ""
' End If 'Wend
' End With
' Next note
'ElseIf storyNumber = 3 Then 'endnotes
' For Each note In ActiveDocument.Endnotes
' Set oRng = note.Range
' With oRng
' If .Characters.Last = " " Then 'While
' .Characters.Last = ""
' End If 'Wend
' If .Characters.First = " " Then 'While
' .Characters.First = ""
' End If 'Wend
' End With
' Next note
'End If
'
'End Function
Function fnoteRefText()
Dim fnote As Footnote
For Each fnote In ActiveDocument.Footnotes
Expand Down
2 changes: 1 addition & 1 deletion src/RSuite_Word-template/TestModule5_cleanup_tables.bas
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ Private Sub TestTables_wdv387spaces() 'TODO Rename test
Dim testTableIndex As Long
On Error GoTo TestFail
'Arrange:
Const C_PROC_NAME = "TestTables_trimNoteSpaces" '<-- name of this test procedure
Const C_PROC_NAME = "TestTables_wdv387spaces" '<-- name of this test procedure
'MyStoryNo = 1 '<< override test_init here as needed: use 1 for Main body of docx: use 2 for footnotes, 3 for endnotes
expected_str = vbCr + "cat" + vbCr
testTableIndex = 16
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.4.2
6.4.3

0 comments on commit b2a5f41

Please sign in to comment.