----------------------------------------
-- concatenates 2 text-member into new text member (preserving format and hyperlinks)
----------------------------------------
on concatenateTextMembers (newMem, m1, m2)
newMem.rtf = concatenateRTF (m1.rtf, m2.rtf)
hl = m1.hyperlinks
repeat with h in hl
newMem.char[h[1]..h[2]].hyperlink = m1.char[h[1]..h[2]].hyperlink
end repeat
tOffset = m1.text.length + 1 -- because of automatic linebreak after m1
hl = m2.hyperlinks
repeat with h in hl
newMem.char[tOffset+h[1]..tOffset+h[2]].hyperlink = m2.char[h[1]..h[2]].hyperlink
end repeat
end
----------------------------------------
-- concatenates 2 rtf-docs (given as rtf-text)
----------------------------------------
on concatenateRTF (r1, r2)
od = the itemdelimiter
the itemdelimiter=";"
-- delete ending "}" of doc 1
delete the last char of r1
-- delete rtf-header of doc 2
delete char 1 to 18 of r2
-- separate header and body (separator: first occurence of \pard)
pos = offset("\pard",r1)
h1 = stripCRLF(r1.char[1..pos]) -- header 1
b1 = r1.char[pos+1..r1.length] -- body 1
pos = offset("\pard",r2)
h2 = stripCRLF(r2.char[1..pos]) -- header 2
b2 = r2.char[pos+1..r2.length] -- body 2
-- COLOR-TABLE STUFF
-- extract color-table of doc 1
ac1=offset("{\colortbl", h1) + 10
bc1=offset("}", h1.char[ac1+1..h1.length])
ct1=h1.char[ac1..ac1+bc1-2]
cnt1 = ct1.item.count
-- extract color-table of doc 2
ac2=offset("{\colortbl", h2) + 10
bc2=offset("}", h2.char[ac2+1..h2.length])
ct2=h2.char[ac2..ac2+b2-2]
cnt2 = ct2.item.count
-- create new complete color-table in h1
h1 = h1.char[1..ac1+bc1-2] & ";" & ct2 & h1.char[ac1+bc1-1..h1.length]
-- delete old color-table in h2
delete char ac2 to ac2+bc2-2 of h2
-- replace color-table-references in doc 2 with new indexes
repeat with i = cnt2 down to 1
b2 = str_replace_esc("\cf"&i, "\cf"&(i+cnt1), b2, "\")
end repeat
-- FONT-TABLE STUFF
-- extract font-table of h1
af1=offset("{\fonttbl", h1) + 10
bf1=offset("}}", h1.char[af1+1..h1.length])
ft1=h1.char[af1..af1+bf1-2]
ar1=explode(";}{", ft1)
-- extract font-table of h2
af2=offset("{\fonttbl", h2) + 10
bf2=offset("}}", h2.char[af2+1..h2.length])
ft2=h2.char[af2..af2+bf2-2]
ar2=explode(";}{", ft2)
-- create new complete font-table in h1
fc1=count(ar1)
fc2=count(ar2)
repeat with i = 1 to fc2
f=ar2[i]
delete char 1 to 2 of f
neu="\f"&(fc1+i-1)&f.char[offset("\",f)..f.length]
ar1.add(neu)
end repeat
h1 = h1.char[1..af1+bf1-2] & ";" & implode(";}{", ar1) & h1.char[af1+bf1-1..h1.length] -- ???
-- delete old font-table in h2
delete char af2 to af2+bf2-2 of h2
-- replace font-table-references in doc 2 with new indexes
repeat with i = fc2 down to 1
b2 = str_replace_esc("\f"&i, "\f"&(fc1+i), b2, "\")
end repeat
-- concatenate
ret = h1 & b1 & h2 & b2
return ret
end
----------------------------------------
-- UTILITIES
----------------------------------------
----------------------------------------
-- strip CRLF, if necessary
----------------------------------------
on stripCRLF aString
return str_replace(numtochar(13)&numtochar(10),"",aString)
end
----------------------------------------
--replace in string, only if stringToFind is not escaped, i.e. uneven number
----------------------------------------
on str_replace_esc (stringToFind, stringToInsert, input, esc)
output = ""
findLen = stringToFind.length - 1
repeat while true
currOffset = offset(stringToFind, input)
if currOffset=0 then exit repeat
put input.char [1..currOffset] after output
delete the last char of output
j=1
repeat while (input.char[currOffset-j]=esc) -- "\"
j=j+1
end repeat
if j mod 2 = 1 then
put stringToInsert after output
else
put stringToFind after output
end if
delete input.char [1.. (currOffset + findLen)]
end repeat
put input after output
return output
end
----------------------------------------
--
----------------------------------------
on explode (delim, str)
l=[]
if voidP(str) then return []
dl=length(delim)
repeat while true
pos=offset(delim,str)
if pos=0 then exit repeat
s=str.char[1..pos]
delete the last char of s
l.add(s)
str=str.char[pos+dl..length(str)]
end repeat
if pos=0 then pos=1-dl
l.add(str.char[pos+dl..length(str)])
return l
end
----------------------------------------
--
----------------------------------------
on implode (delim, l)
str=""
repeat with i=1 to l.count
put l[i]&delim after str
end repeat
return str.char[1..(str.length-delim.length)]
end
----------------------------------------
-- replace in string
----------------------------------------
on str_replace stringToFind, stringToInsert, input
output = ""
findLen = stringToFind.length - 1
repeat while true
currOffset = offset(stringToFind, input)
if currOffset=0 then exit repeat
put input.char [1..currOffset] after output
delete the last char of output
put stringToInsert after output
delete input.char [1.. (currOffset + findLen)]
end repeat
put input after output
return output
end