#!ruby # require "erb" def l(filename) "'#{File.read(filename).gsub(/\r?\n/, "\\n").gsub(/'/, "\\\\'")}'" end File.open(ARGV[0], "wb") {|f| f.puts ERB.new(DATA.read).result } puts "javascript:(function(url){var s=document.createElement('script');s.src=url;s.type='text/javascript';document.body.appendChild(s)})('your-url/#{ARGV[0]}');" __END__ var TEMPLATES = { index : { "index.html" : <%=l "index_templates/main_index.txt" %>, "archives.html" : <%=l "index_templates/archives.txt" %>, "styles-site.css" : <%=l "index_templates/styles-site.txt" %>, }, archive : { "エントリー・アーカイブ" : <%=l "archive_templates/individual_entry_archive.txt" %>, "カテゴリー・アーカイブ" : <%=l "archive_templates/category_archive.txt" %>, "日付アーカイブ" : <%=l "archive_templates/date-based_archive.txt" %> }, system : { comment_preview : <%=l "system_templates/comment_preview.txt"%>, comment_pending : <%=l "system_templates/comment_pending.txt"%>, comment_error : <%=l "system_templates/comment_error.txt"%> }, module : { } }; (function () { if (!location.search.match(/mode=list/)) { alert("テンプレートのリストページで実行してください。"); return; } /* 開始 */ var state, log_container; var state_container = $N('div', {style:'background: #000; opacity: 0.8; color: #fff'}, [ state = $N('div', {style:"margin: 1em"}, "開始..."), log_container = $N('div', {style:'font-size: 80%; margin: 2em'}) ]); with (state_container.style) { position = "absolute"; top = "0"; left = "0"; width = "100%"; height = getWindowHeight() + "px"; } document.body.appendChild(state_container); var log = function () { var msg = []; for (var i = 0; i < arguments.length; i++) { msg.push(String(arguments[i])); } msg = msg.join(", "); log_container.appendChild($N("div", {}, msg)); if (console) console.log(arguments) }; var update = function (msg) { state.replaceChild($N("span", {}, msg), state.firstChild); }; /* 準備作業 */ update("準備中...") var path = location.pathname; var blog_id = location.search.match(/blog_id=(\d+)/)[1]; var magic_token = $X("string(//input[@name='magic_token']/@value)"); log(path, blog_id, magic_token); // clean up for (var type in TEMPLATES) { for (var name in TEMPLATES[type]) { if (!TEMPLATES[type][name]) delete TEMPLATES[type][name]; } } /* 既存テンプレートの取得 */ update("テンプレート情報の取得中..."); var index_templates = {}; var index_table_container = document.getElementById('index-selector'); $X('.//tbody/tr', index_table_container).forEach(function (e) { var link = $X('string(.//td[2]/a/@href)', e); var filename = $X('string(.//td[3])', e); log(link, filename); index_templates[filename] = link; }); var archive_templates = {}; var archive_table_container = document.getElementById('archive-selector'); $X('.//tbody/tr', archive_table_container).forEach(function (e) { var link = $X('string(.//td[2]/a/@href)', e); var name = $X('string(.//td[2]/a)', e); if (!link) return; log(link, name); archive_templates[name] = link; }); var system_tempates = {}; var system_table_container = document.getElementById('system-selector'); $X('.//tbody/tr', system_table_container).forEach(function (e) { var link = $X('string(.//td[2]/a/@href)', e); var name = $X('string(.//td[2]/a)', e); if (!link) return; var type = link.match(/&type=([^&]+)/)[1]; log(link, type); system_tempates[type] = [link, name]; }); var module_templates = {}; var module_table_container = document.getElementById('module-selector'); if (module_table_container) { $X('.//tbody/tr', module_table_container).forEach(function (e) { var link = $X('string(.//td[2]/a/@href)', e); var name = $X('string(.//td[2]/a)', e); if (!link) return; log(link, name); module_templates[name] = link; }); } /* インポート反映 */ update("インポート中..."); for (var fn in TEMPLATES.index) { var id = ""; if (index_templates[fn]) { id = index_templates[fn].match(/&id=(\d+)/)[1]; } log("index", blog_id, id); post(path, { id : id, blog_id : blog_id, type : "index", __mode : "save", _type : "template", return_args : ["__mode=view&_type=template&blog_id=", blog_id , "&id=", id].join(""), magic_token : magic_token, rebuild_me : 1, name : fn, outfile : fn, linked_file : '', text : TEMPLATES.index[fn] }); } for (var name in TEMPLATES.archive) { var id = "", type = "archive"; if (archive_templates[name]) { id = archive_templates[name].match(/&id=(\d+)/)[1]; var res = get(archive_templates[name]); type = res.match(/input\s+type="hidden"\s+name="type"\s+value="([^"]+)"/)[1]; } log("archive", blog_id, id ,type); post(path, { id : id, blog_id : blog_id, type : type, __mode : "save", _type : "template", return_args : ["__mode=view&_type=template&blog_id=", blog_id , "&id=", id].join(""), magic_token : magic_token, rebuild_me : 1, name : name, linked_file : '', text : TEMPLATES.archive[name] }); } for (var name in TEMPLATES.system) { if (system_tempates[name]) { var id = system_tempates[name][0].match(/&id=(\d+)/)[1]; var type = name; log(id, type); post(path, { id : id, blog_id : blog_id, type : type, __mode : "save", _type : "template", return_args : ["__mode=view&_type=template&blog_id=", blog_id , "&id=", id].join(""), magic_token : magic_token, rebuild_me : 1, name : system_tempates[name][1], linked_file : '', text : TEMPLATES.system[name] }); } } for (var name in TEMPLATES.module) { var id = ""; if (module_templates[name]) { id = module_templates[name].match(/&id=(\d+)/)[1]; } post(path, { id : id, blog_id : blog_id, type : "custom", __mode : "save", _type : "template", return_args : ["__mode=view&_type=template&blog_id=", blog_id , "&id=", id].join(""), magic_token : magic_token, name : name, linked_file : '', text : TEMPLATES.module[name] }); } alert("インポート完了"); location.reload(); function post(path, query) { var body = []; for (var q in query) { body.push(q, "=", encodeURIComponent(query[q]), "&"); } body = body.join(""); log(body); var xhr = new XMLHttpRequest; xhr.open("POST", path, false); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.send(body); if (xhr.status == 200) { return xhr.responseText; } else { throw ["XMLHttpRequest failed with code", xhr.status].join(''); } } function get(path) { var xhr = new XMLHttpRequest; xhr.open("GET", path, false); xhr.send(null); if (xhr.status == 200) { return xhr.responseText; } else { throw ["XMLHttpRequest failed with code", xhr.status].join(''); } } function $X (exp, context) { if (!context) context = document; var resolver = function (prefix) { var o = document.createNSResolver(context)(prefix); return o ? o : (document.contentType == "text/html") ? "" : "http://www.w3.org/1999/xhtml"; } var exp = document.createExpression(exp, resolver); var result = exp.evaluate(context, XPathResult.ANY_TYPE, null); switch (result.resultType) { case XPathResult.STRING_TYPE : return result.stringValue; case XPathResult.NUMBER_TYPE : return result.numberValue; case XPathResult.BOOLEAN_TYPE: return result.booleanValue; case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: { result = exp.evaluate(context, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); var ret = []; for (var i = 0, len = result.snapshotLength; i < len ; i++) { ret.push(result.snapshotItem(i)); } return ret; } } return null; } function $N (name, attr, childs) { var ret = document.createElement(name); for (k in attr) { if (!attr.hasOwnProperty(k)) continue; v = attr[k]; if (k == "class") { ret.className = v; } else { ret.setAttribute(k, v); } } switch (typeof childs) { case "string": { ret.appendChild(document.createTextNode(childs)); break; } case "object": { for (var i = 0, len = childs.length; i < len; i++) { var child = childs[i]; if (typeof child == "string") { ret.appendChild(document.createTextNode(child)); } else { ret.appendChild(child); } } break; } } return ret; } function getWindowHeight() { // copy from lightbox plus (but codingrule) var ch, sh; if (window.innerHeight) { ch = window.innerHeight; } else if (document.documentElement && document.documentElement.clientHeight) { ch = document.documentElement.clientHeight; } else { ch = document.body.clientHeight; } if (window.innerHeight && window.scrollMaxY) { sh = window.innerHeight + window.scrollMaxY; } else if (document.body.scrollHeight > document.body.offsetHeight) { sh = document.body.scrollHeight; } else { sh = document.body.offsetHeight; } return (ch > sh) ? ch : sh; } })();