CotEditor+AppleScriptでテキストファイルを縦書きHTMLへ変換する
code:makeEpub.scpt
set headhtml to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE html>
<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\" xml:lang=\"ja\" lang=\"ja\">
<head>
<meta charset=\"utf-8\" />
<link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\" />
<title>サンプル</title>
<style type=\"text/css\">
body{
-ms-writing-mode: tb-rl;
writing-mode: vertical-rl;
margin: 14mm auto;
}
title {
display: none;
string-set: header content();
}
p{
margin: 0px;
}
p:before{content:\" \";
}
@page {
size: A6 portrait;
width: 217pt;
margin: 14mm auto;
}
@page :left {
@top-left {
-epub-writing-mode: vertical-rl;
-ms-writing-mode: tb-rl;
writing-mode: vertical-rl;
content: string(header);
font-size: 7pt;
text-align: left;
}
}
@page :right {
@top-right {
-epub-writing-mode: vertical-rl;
-ms-writing-mode: tb-rl;
writing-mode: vertical-rl;
content: string(header);
font-size: 7pt;
text-align: right;
}
}
</style>
</head>
<body class=\"bodymatter\" epub:type=\"bodymatter\">"
set hoothtml to "</body>
</html>"
-- set bodyhtml to get the clipboard
tell application "CotEditor"
set bodyhtml to contents of front document
set tembody to every paragraph of bodyhtml
end tell
set ihtml to {}
set toclist to {}
set headerID to 0
repeat with i in tembody
if length of i = 0 then
set the end of ihtml to "<p>" & "<br />" & "</p>"
else
if item 1 of i = "○" then
set headerID to headerID + 1
set the end of ihtml to "<h3 id=\"" & headerID & "\">" & i & "</h3>"
set the end of toclist to "<li><a href=\"#" & headerID & "\">" & i & "</a></li>"
else
set the end of ihtml to "<p>" & i & "</p>"
end if
end if
end repeat
set the beginning of ihtml to "<ul>" & toclist & "</ul>"
set newbodyhtml to ihtml as string
set getopenfile to createHtml("sample.html", headhtml & newbodyhtml & hoothtml)
--my replaceText(bodyhtml, "<BR>")
--set aPath to (getopenfile as POSIX file)
--log aPath
tell application "Google Chrome"
open location "file:///Users/Tadanori/Dropbox/text/sample/sample.html"
activate
end tell
on replaceText(theText, replaceStr)
set tmp to AppleScript's text item delimiters
set theList to {}
set theList to every paragraph of theText
set AppleScript's text item delimiters to replaceStr
set theText to theList as string
set AppleScript's text item delimiters to tmp
return theText
end replaceText
on createHtml(fName, aText)
set aTextFile to open for access ("Macintosh HD:Users:Tadanori:Dropbox:text:sample:" & fName) with write permission
--set aEOF to get eof of aTextFile
set eof of aTextFile to 0
try
-- write aText starting at (aEOF + 1) to aTextFile
write aText to aTextFile as «class utf8»
on error aErrorText
display dialog aErrorText
end try
close access aTextFile
return aTextFile
end createHtml
CotEditor+AppleScript