Last active
August 29, 2015 14:22
-
-
Save harai/f99b399c2e7f512660bc to your computer and use it in GitHub Desktop.
Convert Backlog™ notation to Markdown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Backlog™2Markdown | |
// @namespace https://github.com/harai/ | |
// @description Convert Backlog™ notation to Markdown. | |
// @include https://*.backlog.jp/* | |
// @version 1.0.1 | |
// ==/UserScript== | |
// | |
// Copyright (C) 2015 Akihiro HARAI | |
// Originally created by edvakf | |
// | |
// The JavaScript code in this page is free software: you can | |
// redistribute it and/or modify it under the terms of the GNU | |
// General Public License (GNU GPL) as published by the Free Software | |
// Foundation, either version 3 of the License, or (at your option) | |
// any later version. The code is distributed WITHOUT ANY WARRANTY; | |
// without even the implied warranty of MERCHANTABILITY or FITNESS | |
// FOR A PARTICULAR PURPOSE. See the GNU GPL for more details. | |
// | |
// As additional permission under GNU GPL version 3 section 7, you | |
// may distribute non-source (e.g., minimized or compacted) forms of | |
// that code without the copy of the GNU GPL normally required by | |
// section 4, provided you include this license notice and a URL | |
// through which recipients can access the Corresponding Source. | |
var extendObject=function(a,b){for(property in b)a[property]=b[property];return a},strUtil={times:function(a,b){for(var c="",d=0;d<b;d++)c+=a;return c},_escapeHTML:function(a){a=a.replace(/\&/g,"&");a=a.replace(/</g,"<");a=a.replace(/>/g,">");a=a.replace(/"/g,""");a=a.replace(/\'/g,"'");return a=a.replace(/\\/g,"\")},_escapeInsidePre:function(a){a=a.replace(/\&/g,"&");a=a.replace(/</g,"<");return a=a.replace(/>/g,">")},_unescapeHTML:function(a){a=a.replace(/&/g, | |
"&");a=a.replace(/</g,"<");a=a.replace(/>/g,">");return a=a.replace(/"/g,'"')}},decomposite=function(a,b){var c;return(c=a.exec(b))?{before:b.substr(0,c.index),after:b.substr(c.index+c[0].length),match:c}:{before:b,after:"",match:[]}},Backlog=function(){};Backlog.prototype={parse:function(a){a=new Backlog_Context({text:a});var b=new Backlog_SectionNode;b._new({context:a});b.parse();return a.getResult()}}; | |
var Backlog_Context=function(a){this.self={text:a.text,resultLines:[],prevIsParagraph:null,blanklineCount:0};this.init()}; | |
Backlog_Context.prototype={init:function(){this.setInputText(this.self.text)},hasNext:function(){return null!=this.self.lines&&this.self.lines.length-1>this.self.index},peek:function(){return this.self.lines[this.self.index+1]},next:function(){return this.self.lines[++this.self.index]},setInputText:function(a){this.self.text=a.replace(/\r/g,"");this.self.lines=this.self.text.split("\n").map(function(a){return decomposite(/\s*$/,a).before});this.self.index=-1},getResult:function(){return this.self.resultLines.join("\n")}, | |
getLastPut:function(){return 0==this.self.resultLines.length?null:this.self.resultLines[this.self.resultLines.length-1]},putLine:function(a){this.self.resultLines.push(a)},stackBlankline:function(){this.self.blanklineCount++},processBlanklines:function(a){try{null!==this.self.prevIsParagraph&&(this.self.prevIsParagraph&&a&&0==this.self.blanklineCount||this.putLine(""))}finally{this.self.prevIsParagraph=a,this.self.blanklineCount=0}}};var Backlog_Node=function(){}; | |
Backlog_Node.prototype={pattern:"",_new:function(a){null==a&&(a=[]);this.self={context:a.context}},parse:function(){alert("die")},canParse:function(a){return a.match(this.pattern)}}; | |
var Backlog_LinkNode={replaceLinksInLine:function(a){return a.replace(/\[(https?:\/\/[^\]\s]+?)(?::([^\]\n]*))?\]/g,function(a,c,d){return'<a href="'+strUtil._escapeHTML(c)+'">'+(d?d:c)+"</a>"})}},Backlog_InLine={parsePart:function(a){var b=[{backlog:"'''",md:"*"},{backlog:"''",md:"**"},{backlog:"%%",md:"~~"}],c=function(a,f){if(""===a)return f;var e,g=decomposite(/\\|'''|''|%%/,a);switch(g.match[0]){case "\\":if(0==g.after.length)e="",f.push(g.before+"\\");else{var d=g.after.substr(0,1);switch(d){case "\\":case "'":case "%":e= | |
g.after.substr(1);f.push(g.before+d);break;default:e=g.after,f.push(g.before+"\\")}}break;case b[0].backlog:e=g.after;f.push(g.before);f.push(0);break;case b[1].backlog:e=g.after;f.push(g.before);f.push(1);break;case b[2].backlog:e=g.after;f.push(g.before);f.push(2);break;default:e=g.after,f.push(g.before)}return c(e,f)},d=function(a){a=a.filter(function(a){return""!==a});for(var f="",e=0;e<a.length;e++)"number"!==typeof a[e]?f+=a[e]:-1!==a[e]?f+=b[a[e]].backlog:f.match(/\s$/)||"string"===typeof a[e+ | |
1]&&a[e+1].match(/^\s/)||(f+=" ");return f},h=function(a,f){if(0===a.length)return d(f).trim();var e=a.shift();if("string"===typeof e)return f.push(e),h(a,f);f.push(e);for(var c=f.length-1-1;-1<c;c--)if(f[c]===e){var e=f,l=d(e.slice(c+1,-1)),m=b[e[e.length-1]].md,n=b[e[c]].md;f=e.slice(0,c).concat([-1,n+l+m,-1]);break}return h(a,f)};a=Backlog_LinkNode.replaceInLine(a);a=Backlog_AttachNode.replaceInLine(a);a=c(a,[]);return h(a,[])}},Backlog_QuoteNode=function(){}; | |
Backlog_QuoteNode.prototype=extendObject(new Backlog_Node,{pattern:/^>\s*(.*)$/,parse:function(){for(var a=this.self.context,b;a.hasNext()&&(b=this.canParse(a.peek()));)a.next(),a.putLine("> "+Backlog_InLine.parsePart(b[1],a))}});var Backlog_HnNode=function(){};Backlog_HnNode.prototype=extendObject(new Backlog_Node,{pattern:/^(\*{1,6})\s*(.*)$/,parse:function(a){var b=this.self.context;b.next();var c=strUtil.times("#",a[1].length);b.putLine(c+" "+Backlog_InLine.parsePart(a[2],b))}}); | |
var Backlog_ListNode=function(){};Backlog_ListNode.prototype=extendObject(new Backlog_Node,{pattern:/^([\-\+]+)\s*(.*)$/,parse:function(){for(var a=this.self.context,b;a.hasNext()&&(b=this.canParse(a.peek()));){a.next();var c=strUtil.times(" ",b[1].length-1),d="-"==b[1].substr(0,1)?"* ":"1. ";a.putLine(c+d+Backlog_InLine.parsePart(b[2],a))}}});var Backlog_Quote2Node=function(){}; | |
Backlog_Quote2Node.prototype=extendObject(new Backlog_Node,{pattern:/^\{quote\}$/,endPattern:/^\{\/quote\}$/,parse:function(){var a=this.self.context;for(a.next();a.hasNext();){if(a.peek().match(this.endPattern)){a.next();break}a.putLine("> "+Backlog_InLine.parsePart(a.next(),a))}}});var Backlog_CodeNode=function(){}; | |
Backlog_CodeNode.prototype=extendObject(new Backlog_Node,{pattern:/^\{code(?:\:(\S*))?\}$/,endPattern:/^\{\/code\}$/,parse:function(a){var b=this.self.context;b.next();for(b.putLine("```"+(a[1]||""));b.hasNext();){if(b.peek().match(this.endPattern)){b.next();break}b.putLine(b.next())}b.putLine("```")}});var Backlog_TableNode=function(){}; | |
Backlog_TableNode.prototype=extendObject(new Backlog_Node,{pattern:/^\|(.*)\|h?$/,parse:function(){for(var a=this.self.context,b,c=!0;a.hasNext()&&(b=this.canParse(a.peek()));){a.next();var d=b[1].split("|"),h=d.map(function(a){return" "+Backlog_InLine.parsePart(a.trim(),a)+" "}).join("|");a.putLine("|"+h+"|");c&&(a.putLine("|"+strUtil.times("---|",d.length)),c=!1)}}});var Backlog_ContentsNode=function(){}; | |
Backlog_ContentsNode.prototype=extendObject(new Backlog_Node,{pattern:/^#contents$/,parse:function(){var a=this.self.context;a.next();a.putLine("[toc]")}});var Backlog_ImageNode=function(){};Backlog_ImageNode.prototype=extendObject(new Backlog_Node,{pattern:/^#(?:image|thumbnail)\(([^)]+)\)$/,parse:function(a){var b=this.self.context;b.next();b.putLine("![]["+a[1]+"]")}}); | |
var Backlog_AttachNode={replaceInLine:function(a){return a.replace(/#attach\(([^)]+):(\d+)\)/g,function(a,c,d){return"["+c+"]["+d+"]"})}},Backlog_LinkNode={replaceInLine:function(a){return a.replace(/\[\[((?:[^\]]|\][^\]])+)\]\]/g,function(a,c){var d=decomposite(/[>:]([^:]+:[^:]+)$/,c);return d.match[0]?"["+d.before+"]("+d.match[1]+")":"[["+c+"]]"})}},Backlog_PNode=function(){}; | |
Backlog_PNode.prototype=extendObject(new Backlog_Node,{parse:function(){var a=this.self.context;a.putLine(Backlog_InLine.parsePart(a.next(),a))}});var Backlog_SectionNode=function(){}; | |
Backlog_SectionNode.prototype=extendObject(new Backlog_Node,{childNodes:"hn quote quote2 list code table contents image".split(" "),parse:function(){for(var a=this.self.context,b=this._getChildNodes();a.hasNext();)this._parseWithFoundNode(a.peek(),b)},_getChildNodes:function(a){var b=this.self.context;return(a||this.childNodes).map(function(a){a="Backlog_"+a.charAt(0).toUpperCase()+a.substr(1).toLowerCase()+"Node";a=eval("new "+a+"()");a._new({context:b});return a})},_parseWithFoundNode:function(a, | |
b){var c=this._findNode(a,b);null!==c&&c.node.parse(c.match)},_findNode:function(a,b){for(var c=this.self.context,d=0;d<b.length;d++){var h=b[d],k;if(k=h.canParse(a))return c.processBlanklines(!1),{node:h,match:k}}if(0===a.length)return c.stackBlankline(),c.next(),null;c.processBlanklines(!0);d=new Backlog_PNode;d._new({context:c});return{node:d,match:null}}});global&&(global.Backlog=Backlog,global.Backlog_InLine=Backlog_InLine);var bloggerBacklogMarkup=function(){var a=function(a,b){a.parentNode.insertBefore(b,a)},b=function(a){var b=document.createElement("button");b.classList.add("Btn-gray");var c=document.createTextNode("Markdown\u306b\u5909\u63db");b.appendChild(c);b.style.padding="5px 30px";b.style.margin="0 30px";b.style.float="left";b.id="backlog2md";b.addEventListener("click",function(d){d.preventDefault();d.stopPropagation();a(d);b.disabled=!0;b.replaceChild(document.createTextNode("Markdown\u306b\u5909\u63db\u3057\u307e\u3057\u305f"), | |
c)});return b},c=function(){var a=[],b=[],c=new MutationObserver(function(){a.forEach(function(a){Array.prototype.forEach.call(a.target.querySelectorAll(a.selector),function(c){-1===b.indexOf(c)&&(b.push(c),a.callback(c))})})});return{add:function(b,e,d){a.push({selector:b,callback:d,target:e});e&&c.observe(e,{childList:!0,subtree:!0})}}}(),d=function(){var b=document.getElementById("page.content");return{isShown:function(){return!!b},getValue:function(){return b.value},setValue:function(a){b.value= | |
a},insertButton:function(b){a(document.getElementById("formButtons3"),b)}}}(),h=function(){var b=document.getElementById("issue.description");return{isShown:function(){return!!b},getValue:function(){return b.value},setValue:function(a){b.value=a},insertButton:function(b){a(document.getElementById("setParentIssue"),b)}}}();(function(){c.add(".Comment-editor",document.getElementById("comments"),function(c){var d=c.querySelector("textarea"),g=b(function(){d.value=(new Backlog).parse(d.value);d.dispatchEvent(new KeyboardEvent("keydown", | |
{key:"Shift"}));d.dispatchEvent(new KeyboardEvent("keyup",{key:"Shift"}))});a(c.querySelector("button"),g)})})();var k=[d,h].filter(function(a){return a.isShown()})[0];k&&(d=b(function(){var a=k.getValue(),a=(new Backlog).parse(a);k.setValue(a)}),k.insertButton(d))};bloggerBacklogMarkup(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment