Search code examples
livecodelivecoding

Replacement only between begin{document} and \end{document}


I have got some of the code from the stack-overflow and it's working, But while changing that code into my requirement(Replacement only between begin{document} and \end{document}) it's not working

this is the code i got

put wordOffset("begin{document}",fld "MytextField") into tBegin
put wordOffset("end{document}",fld "MytextField") into tEnd
put replaceText(word tBegin to tEnd of fld "MytextField","bad","good") into word tBegin to tEnd of fld "MytextField"

I am using the following code. How i convert above code to my requirement.

  on mouseUp 
       put the htmlText of field "MytextField" into myHtml 
       set the caseSensitive to true 
       put the field SRText into myArrayToBe 
       split myArrayToBe by CR 
       put the number of lines of (the keys of myArrayToBe) into myArraylength
       repeat with i = 1 to myArraylength 
          put  myArrayToBe[i] into y
          split y by colon
          put y[1] into searchStr
          put y[2] into replaceStr
          if searchStr is empty then
             put the  0 into m
          else 
          replace searchStr with  "<strike><font bgcolor=" & quote & "yellow" & quote & ">" & searchStr & "</font></strike><font bgcolor=" & quote & "green" & quote & ">" & replaceStr & "</font>" in myHtml
        end if
     end repeat 
     set the htmlText of fld "MytextField" to myHtml 
      end mouseUP

Solution

  • Now I think I understand your problem, try this...

    on mouseUp 
       put the htmlText of field "MytextField" into myHtml
    
       ## First we need to break up myHTML into 3 parts so we can edit the "Document" part...
       -- Find the document "begin" marker and put it's position into the tBegin variable
       put wordOffset("begin{document}",myHtml) into tBegin
    
       -- Find the document "end" marker and put it's position into the tEnd variable
       put wordOffset("end{document}",myHtml) into tEnd 
    
       put word 1 to tBegin of myHtml into tHeader -- Part 1
       put word (tBegin +1) to (tEnd -1) of myHtml into tDocument -- Part 2 = the Document to change
       put word tEnd to -1 of myHtml into tFooter -- Part 3
    
       set the caseSensitive to true 
       put field "SRText" into myArrayToBe 
       split myArrayToBe by CR 
       put the number of lines of (the keys of myArrayToBe) into myArraylength
       repeat with i = 1 to myArraylength 
          put  myArrayToBe[i] into y
          split y by colon
          put y[1] into searchStr
          put y[2] into replaceStr
          if searchStr is empty then
             put the  0 into m
          else 
             replace searchStr with  "<strike><font bgcolor=" & quote & "yellow" & quote & ">" & searchStr & "</font></strike><font bgcolor=" & quote & "green" & quote & ">" & replaceStr & "</font>" in tDocument
          end if
       end repeat 
    
       ## Now we can rebuild the htmlText from the 3 parts and put it back in to the field...
       set the htmlText of fld "MytextField" to tHeader && tDocument && tFooter
    end mouseUp