Advertisement
Guest User

ZoterZero4

a guest
Sep 27th, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.86 KB | None | 0 0
  1. Function ZoterZeroFieldFix(F) ' Fix a field with ZoterZero
  2. fieldChanged = 0 ' assume nothing changed, unless...
  3. fieldText = F.Code.Text ' get the current code of the field as text
  4. If InStr(fieldText, " ADDIN ZOTERO_ITEM CSL_CITATION") = 1 Then ' make sure this is a Zotero field to modify, starts with right text
  5. myArray = Split(fieldText, " ", 5) ' split that code into parts up to 5
  6. Dim Json As Object ' create a Json object to work with
  7. Set Json = JsonConverter.ParseJson(myArray(4)) ' get the fourth part, which is the Json data
  8. myResult = F.Result.Text ' get the displayed text from the field to work with it below
  9. '' MAIN TEXT REPLACEMENT OF PAGE "0" WITH AUTHOR NAME ONLY
  10. If Json("citationItems").Count = 1 Then ' only works for one citation item
  11. If Json("citationItems")(1)("locator") = "0" Then ' if the page range is set to "0" process for ZoterZero:
  12. If InStr(myResult, "(") = 1 Then ' if it begins with open parentheses...
  13. myResult = Split(myResult, "(", 2) ' split at parentheses, only 2 parts
  14. myResult = myResult(1) ' get the second part (without open parentheses)
  15. myResult = StrReverse(myResult) ' reverse the string so we can work from the other end:
  16. splitChar = " " ' default character to split is a space, occurring between name and year
  17. If InStr(myArray(4), """issued"":") = 0 Then ' if the year is missing, allow year-less name-only cites without parentheses:
  18. splitChar = ":" ' instead split at the colon because there is no year
  19. End If
  20. myResult = Split(myResult, splitChar, 2) ' split at splitChar, up to 2 parts
  21. myResult = myResult(1) ' get the second part (without the actually-last [=year] section)
  22. myResult = StrReverse(myResult) ' reverse back to normal order
  23. fieldChanged = 1 ' report updated below
  24. End If
  25. '' ALTERNATIVE TEXT REPLACEMENT OF PAGE "00" WITH "AUTHOR (YYYY)" FORMAT, **ONLY FOR MOST COMMON SIMPLE SCENARIO**
  26. ''' LIMITATION: ONLY WORKS WITHOUT PAGE NUMBERS DUE TO OBVIOUS CONFLICT WITH "00"...
  27. ElseIf Json("citationItems")(1)("locator") = "00" Then ' if the page range is set to "0" process for ZoterZero:
  28. If InStr(myResult, "(") = 1 And InStr(myArray(4), """issued"":") Then ' if it begins with open parentheses and contains a date...
  29. myResult = Split(myResult, "(", 2) ' split at parentheses, only 2 parts
  30. myResult = myResult(1) ' get the second part (without open parentheses)
  31. myResult = StrReverse(myResult) ' reverse the string so we can work from the other end:
  32. splitChar = " " ' default character to split is a space, occurring between name and year
  33. myResult = Split(myResult, splitChar, 2) ' split at splitChar, up to 2 parts
  34. myResult = myResult(0) & "(" & splitChar & myResult(1) ' recombine with parentheses inserted
  35. myResult = Split(myResult, ":", 2) ' split without pages, up to 2 parts
  36. myResult = ")" & myResult(1) ' recombine without pages
  37. myResult = StrReverse(myResult) ' reverse back to normal order
  38. fieldChanged = 1 ' report updated below
  39. End If
  40. End If
  41. End If
  42. '' SECONDARY TEXT REPLACEMENT FOR DOUBLED PARENTHESES TO REMOVE PARENTHESES
  43. If Left(myResult, 2) = "((" Then ' if it begins with doubled open parentheses...
  44. myResult = Split(myResult, "((", 2) ' split at double parentheses, only 2 parts
  45. myResult = myResult(1) ' get the second part (without open double parentheses)
  46. fieldChanged = 1 ' report updated below
  47. End If
  48. If Right(myResult, 2) = "))" Then ' if it ends with doubled open parentheses...
  49. myResult = StrReverse(myResult) ' reverse the string so we can work from the end:
  50. myResult = Split(myResult, "))", 2) ' split at double parentheses, only 2 parts
  51. myResult = myResult(1) ' get the second part (without close double parentheses)
  52. myResult = StrReverse(myResult) ' reverse the string back to normal:
  53. fieldChanged = 1 ' report updated below
  54. End If
  55. '' CAPITALIZE FIRST LETTER IF NEEDED FOR UNUSUAL CASES LIKE "von" > "Von" SENTENCE INITIALLY
  56. If Left(myResult, 1) = "^" Then ' if it begins with a carrot marker...
  57. myResult = Split(myResult, "^", 2) ' split at carrot, only 2 parts
  58. myResult = myResult(1) ' get the second part (without carrot)
  59. myResultChar = Left(myResult, 1) ' save first character
  60. myResult = Split(myResult, Left(myResult, 1), 2) ' split at first character, only 2 parts
  61. myResult = myResult(1) ' save rest, missing first character
  62. myResult = UCase(myResultChar) & myResult ' combine uppercase version of first character and rest
  63. fieldChanged = 1 ' report updated below
  64. End If
  65. '' UPDATE AND SAVE FIELD IF CHANGED ABOVE:
  66. If fieldChanged = 1 Then ' the field text has been updated as MyResult, let's save it:
  67. ' the following two lines make Zotero think this was the original output, so no warnings!
  68. Json("properties")("plainCitation") = myResult ' set the Json citation data to new label
  69. Json("properties")("formattedCitation") = myResult ' again, other instance
  70. F.Result.Text = myResult ' replace the displayed text with the new text
  71. F.Result.Font.Underline = wdUnderlineNone ' remove dashed underlining from Zotero's delayed update feature if present
  72. myJson = JsonConverter.ConvertToJson(Json) ' collapse Json back to text
  73. F.Code.Text = " " & myArray(1) & " " & myArray(2) & " " & myArray(3) & " " & myJson & " " ' reconstruct field code
  74. ZoterZeroFieldFix = 1 ' updated, return success
  75. End If
  76. End If
  77. End Function
  78.  
  79. Sub ZoterZero()
  80. '
  81. ' ZoterZero main function
  82. '' if selection or text near cursor contains fields, check and fix them
  83. '' else check and fix all fields in document
  84. changeSuccess = 0 ' no fields fixed yet
  85. Selection.Expand Unit:=wdSentence ' expand the selection to at least sentence-level
  86. If Selection.Fields.Count > 0 Then ' if fields are selected...
  87. checkField = Selection.Fields.Count ' get the total number of fields
  88. While checkField > 0 ' check each field
  89. changeSuccess = ZoterZeroFieldFix(Selection.Fields(checkField)) ' check and fix this field
  90. checkField = checkField - 1 ' check the previous field next
  91. Wend
  92. End If
  93. If changeSuccess = 0 Then ' no fields have been updated yet, let's update all fields in document
  94. ' based on http://www.vbaexpress.com/kb/getarticle.php?kb_id=1100
  95. Dim rngStory As Word.Range ' vars for below
  96. Dim lngValidate As Long ' vars for below
  97. Dim oShp As Shape ' vars for below
  98. lngValidate = ActiveDocument.Sections(1).Headers(1).Range.StoryType ' starting point
  99. For Each rngStory In ActiveDocument.StoryRanges 'Iterate through all linked stories
  100. Do
  101. On Error Resume Next
  102. checkField = rngStory.Fields.Count ' get the total number of fields in this section
  103. While checkField > 0 ' check each field
  104. changeSuccess = ZoterZeroFieldFix(rngStory.Fields(checkField)) ' check and fix this field
  105. checkField = checkField - 1 ' check the previous field next
  106. Wend
  107. Select Case rngStory.StoryType
  108. Case 6, 7, 8, 9, 10, 11
  109. If rngStory.ShapeRange.Count > 0 Then
  110. For Each oShp In rngStory.ShapeRange
  111. If oShp.TextFrame.HasText Then
  112. checkField = Shp.TextFrame.TextRange.Fields.Count ' get the total number of fields in this section
  113. While checkField > 0 ' check each field
  114. changeSuccess = ZoterZeroFieldFix(Shp.TextFrame.TextRange.Fields(checkField)) ' check and fix this field
  115. checkField = checkField - 1 ' check the previous field next
  116. Wend
  117. End If
  118. Next
  119. End If
  120. Case Else 'Do Nothing
  121. End Select
  122. On Error GoTo 0
  123. 'Get next linked story (if any)
  124. Set rngStory = rngStory.NextStoryRange ' get ready for next section
  125. Loop Until rngStory Is Nothing ' keep going through until all sections are done
  126. Next
  127. End If
  128. Selection.Collapse ' reset cursor to beginning of section which isn't quite right but close enough
  129. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement