-- thread2html
-- CocoMonar のスレッド記録ファイル *.thread を HTML へ変換する AppleScript アプレット。
property outputHTMLCreatorCode : "Sfri" -- ほかに "CHIM" とか "MSIE" とか。"" にはできません。したいのに。
property outputHTMLFileType : "TEXT"
on run
my xslt((choose file with prompt "Select \"*.thread\" file to convert to HTML : ") as list)
end run
on open srcFiles
xslt(srcFiles)
end open
script theScript
property XSLTNamespace : "http://www.remus.dti.ne.jp/~a-satomi/"
-- トリップの前後についてるゴミタグを除去
on fixTrip(str)
set str to replaceStr(str, "?b>", "")
set res to mgrep str regex "(.*)(◆.+)"
if matchLen of res > 0 then
return item 1 of subMatchStr of res & "" & item 2 of subMatchStr of res & ""
else
return str
end if
end fixTrip
--
→
on fixBR(str)
return replaceStr(str, "
", "
")
end fixBR
-- レス番号へのリンクを修正
on resLinkFix(str)
set res to mgrep str regex "../test/read.cgi/.+?/\\d+/(\\d+).+?target=\"_blank"
if (matchLen of res > 0) then
set leftMatch to ""
set rightMatch to ""
try
set leftMatch to text 1 thru ((matchPos of res) - 1) of str
end try
try
set rightMatch to text ((matchPos of res) + (matchLen of res)) thru end of str
end try
resLinkFix(leftMatch & "#res" & subMatchStr of res & rightMatch)
else
return str
end if
end resLinkFix
-- URL らしき文字列をアンカー化
on urlToAnchor(str)
set res to mgrep str regex "(https?|ftp|ttp|tp|p)://([a-zA-Z][a-zA-Z0-9_\\.\\-]+)(/?[a-zA-Z0-9_;/\\?:@&=\\+\\$,\\-\\.!~\\*\\'()#%]*)"
if (matchLen of res) > 0 then
set leftMatch to ""
set rightMatch to ""
try
set leftMatch to text 1 thru ((matchPos of res) - 1) of str
end try
try
set rightMatch to text ((matchPos of res) + (matchLen of res)) thru end of str
end try
set protocol to item 1 of subMatchStr of res
set domain to item 2 of subMatchStr of res
set pathname to item 3 of subMatchStr of res
if (matchLen of (mgrep protocol regex "https?|ftp") < 0) then set protocol to "http"
set theURL to protocol & "://" & domain & pathname
return leftMatch & "" & theURL & "" & urlToAnchor(rightMatch)
else
return str
end if
end urlToAnchor
-- 文字列 str 中の文字列 match を文字列 replace に置換
on replaceStr(str, match, replace)
set res to mgrep str regex match
if (matchLen of res > 0) then
set leftMatch to ""
set rightMatch to ""
try
set leftMatch to text 1 thru ((matchPos of res) - 1) of str
end try
try
set rightMatch to text ((matchPos of res) + (matchLen of res)) thru end of str
end try
replaceStr(leftMatch & replace & rightMatch, match, replace)
else
return str
end if
end replaceStr
end script
on xslt(xmlFIles)
tell application "Finder"
set cwd to container of (path to me)
set xslFile to (cwd as Unicode text) & "thread2html.xsl"
end tell
repeat with curItem in xmlFIles
tell application "Finder" to set fName to (name of (curItem as alias)) as Unicode text
if fName ends with ".thread" then
set threadName to extract from XML (curItem as alias) ツ
matching "/plist/dict/key[string() = 'Title']" expression "following-sibling::string[1]"
set htmlFile to choose file name with prompt ツ
"Converted HTML file will save as : " default name (threadName as Unicode text) & ".html"
transform XML (curItem as alias) using (xslFile as alias) saving as htmlFile with functions {theScript}
tell application "Finder"
set file type of (htmlFile as alias) to outputHTMLFileType
set creator type of (htmlFile as alias) to outputHTMLCreatorCode
end tell
end if
end repeat
end xslt