Как заменить содержимое между begin{document} и end{document}

Как заменить содержимое между begin{document} и end{document}. при использовании моего кода весь контент заменяется. В приведенном ниже коде я использую wordOffset, но он не работает

on mouseUp
    put wordOffset("begin{document}",fld "MytextField") into tBegin 
    put wordOffset("end{document}",fld "MytextField") into tEnd
    put the htmlText of field "MytextField" into myHtml
    --put wordOffset("begin{document}",fld "myHtml") into tBegin
    --put wordOffset("begin{document}",fld "myHtml") into tEnd
     set the caseSensitive to true
     put the field SRText into myArrayToBe
     split myArrayToBe by CR
     --enable the field "SRText"
     --put "red,RED" & CR & "green,GREEN" & CR & "blue,BLUE" into myArrayToBe 
     --split myArrayToBe by CR
     put the number of lines of (the keys of myArrayToBe) into myArraylength
     repeat with i = 1 to myArraylength 
        --return i
        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 
           --put (word tBegin to tEnd of fld "MytextField")
           --put replaceText(word tBegin to tEnd of fld "MytextField","searchStr","good") into word tBegin to tEnd of fld "MytextField"
           put wordOffset("begin{document}",fld "MytextField") into tBegin 
           put wordOffset("end{document}",fld "MytextField") into tEnd
           --put holder into  myHtml
           --put replaceText(word tBegin to tEnd of fld "MytextField",searchStr,replaceStr)of fld "MytextField"
          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
    --enable me
    set the htmlText of fld "MytextField" to myHtml

end mouseUp

person Shalu    schedule 09.04.2015    source источник


Ответы (1)


Вам нужно использовать htmlText как для получения, так и для установки текста, иначе ваши смещения отключены (например, если первая строка поля выделена жирным шрифтом, htmlText даст что-то вроде

<b>foo</b><br>begin{document}

тогда как просто «fld «MytextField»» даст

foo
begin{document}

поэтому offset("begin{document}",...) первого дает 16, а второго - 5. Я бы сделал что-то вроде

put the htmlText of fld "MytextField" into theHTML
put offset("begin{document}", theHTML) into tBegin 
put offset("end{document}", theHTML) into tEnd
add length of "begin{document}" to tBegin
put character tBegin to tEnd of theHTML into textToChange

-- now do your search/replace on textToChange, not the field

put textToChange into character tBegin to tEnd of theHTML
set the htmlText of fld "MytextField" to theHTML

Я использую здесь offset(), который дает смещение символов, но wordOffset, как вы его использовали, тоже должен работать, если вы помните, что он дает вам номера слов, поэтому вам придется заменить word tBegin to tEnd of theHTML.

person uliwitness    schedule 09.04.2015
comment
при использовании кода появляется ошибка в строке добавить длину begin{document} в tBegin - person Shalu; 09.04.2015