|
|
| //라이센스 GPLv2입니다.
| |
|
| |
|
| //테이블 속성을 분리해 내기위한 것들입니다.
| |
| function SplitToList(str){
| |
| var res =[];
| |
| var stack = [];
| |
| var pre_b = -1;
| |
| var pre_o = 0;
| |
| for(var i=0;i<str.length;i++){
| |
| switch(str[i]){
| |
| case "<":
| |
| stack.push({
| |
| list_i :res.length+1,
| |
| i : i
| |
| });
| |
| if(pre_o != i){
| |
| res.push(str.substring(pre_o,i));
| |
| }
| |
|
| |
| pre_o = i+1;
| |
| break;
| |
| case ">":
| |
| if(stack.length == 0){//이전에 <이 없으면
| |
| //>도 그냥 장식이다. 이전에 있던 것들과 substring으로 res에 push해주자
| |
| if(pre_o != i){
| |
| res.push(str.substring(pre_o,i));
| |
| }
| |
| res.push(">");
| |
| pre_o = i+1;
| |
| }
| |
| else{//이전에 <이 있었으면
| |
| //<의 안쪽을 넣어주자.
| |
| res.push(str.substring(stack.pop().i,i+1));
| |
| pre_o = i+1;
| |
| }
| |
| break;
| |
| }
| |
| }
| |
| //만약에 <이 남아 있으면
| |
| //짝이 안남는 불쌍한 것들이다... 원래 위치에 넣어주자.
| |
| var o = null;
| |
| while(null != (o = stack.pop())){
| |
| res.splice(o.list_i,0,"<");
| |
| }
| |
| if(pre_o != str.length){
| |
| res.push(str.substring(pre_o,str.length));
| |
| }
| |
| return res;
| |
| }
| |
| function ConvertToMediaWikiRef(str){
| |
| var res =[];
| |
| var stack = [];
| |
| var pre_b = -1;
| |
| var pre_o = 0;
| |
| for(var i=0;i<str.length;i++){
| |
| if(str.substr(i,2) == "[*"){
| |
| stack.push({
| |
| list_i :res.length+1,
| |
| i : i
| |
| });
| |
| if(pre_o != i)res.push(str.substring(pre_o,i));
| |
| pre_o = i+1;
| |
| }
| |
| else if(str[i] == "]")
| |
| {
| |
| if(str[i+1] != "]"){ //앞 뒤에 ]이 있으면 이건 링크 태그다.
| |
| if(stack.length == 0){//이전에 [*이 없으면
| |
| //]도 그냥 장식이다. 이전에 있던 것들과 substring으로 res에 push해주자
| |
| if(pre_o != i)res.push(str.substring(pre_o,i) + "]");
| |
| pre_o = i+1;
| |
| }
| |
| else{//이전에 <이 있었으면
| |
|
| |
| //<의 안쪽을 넣어주자.
| |
| res.push(str.substring(stack.pop().i,i+1));
| |
| pre_o = i+1;
| |
| }
| |
| }
| |
| else{
| |
| i++;
| |
| }
| |
| }
| |
| }
| |
| //만약에 <이 남아 있으면
| |
| //짝이 안남는 불쌍한 것들이다... 원래 위치에 넣어주자.
| |
| var o = null;
| |
| while(null != (o = stack.pop())){
| |
| res.splice(o.list_i,0,"[*");
| |
| }
| |
| if(pre_o != str.length){
| |
| res.push(str.substring(pre_o,str.length));
| |
| }
| |
|
| |
| for(var i = 0 ; i < res.length ; i++){
| |
| var it = res[i];
| |
| if(it.startsWith("[*") && it.endsWith("]")){
| |
| res[i] = "<ref>" + it.substring(2,it.length-1) + "</ref>";
| |
| }
| |
| }
| |
| return res.join("");
| |
| }
| |
| function ConvertToMediaWikiTable(str){
| |
| var res = [];var tablemark = null;var markStartIdx = 0;
| |
| while(-1 != (markStartIdx = str.indexOf("||"))){
| |
| if(markStartIdx != 0){
| |
| if(str[markStartIdx-1] != "\n"){
| |
| res.push(str.substring(0,markStartIdx+2));
| |
| str = str.substring(markStartIdx+2,str.length);
| |
| continue;
| |
| }
| |
| }
| |
| res.push(str.substring(0,markStartIdx));
| |
| str = str.substring(markStartIdx,str.length);
| |
| tablemark = [];
| |
| var tablestyle=[];
| |
| while(1){
| |
| var newLineIdx = str.indexOf("\n")+1;
| |
| if(str.substr(newLineIdx,2) != "||") break;
| |
| str = str.substring(newLineIdx,str.length);
| |
| var lineEndIdx = str.indexOf("\n");
| |
| if(lineEndIdx==-1)lineEndIdx = str.length;
| |
| var line = str.substring(0,lineEndIdx).split("||");
| |
| str = str.substring(lineEndIdx,str.length);
| |
| line.splice(0,1);
| |
| if(line[line.length - 1].trim().length == 0){
| |
| line.splice(line.length-1,1);
| |
| }
| |
|
| |
| var colspan = 0;
| |
| var rowspan= "";
| |
| var d = [];
| |
| for(var i=0; i < line.length ; i++){
| |
| var c=line[i];
| |
| line[i] = "";
| |
| if(c.length==0)colspan++;
| |
| else{
| |
| var b=0;
| |
| var is_h = 0;
| |
| var style = [];
| |
| var l = SplitToList(c);
| |
| c = [];
| |
|
| |
| for(var j = 0 ; j < l.length ; j++)
| |
| {
| |
| var a=l[j];
| |
| if(a[0] == "<" && a[a.length-1] == ">"){
| |
| a = a.substring(1,a.length-1);
| |
| var v = a.substring(1,a.length);
| |
|
| |
| if(a[0]=="-"){
| |
| colspan = v-1;
| |
| }
| |
| else if(a[0]=="|"){
| |
| rowspan="rowspan=\""+v+"\" ";
| |
| b=1;
| |
| }
| |
| else if(a[0]=="("){
| |
| style.push("text-align:left");
| |
| }
| |
| else if(a[0]==")"){
| |
| style.push("text-align:right");
| |
| }
| |
| else if(a[0]==":"){
| |
| style.push("text-align:center");
| |
| }
| |
| else if(a=="h")is_h=1;
| |
| else if(a.startsWith("width")){
| |
| style.push("width:" + a.substring(a.indexOf("=")+1,a.length));
| |
| }
| |
| else if(a.startsWith("height")){
| |
| style.push("height:" + a.substring(a.indexOf("=")+1,a.length));
| |
| }
| |
| else if(a.startsWith("bgcolor")){
| |
| style.push("background-color:" + a.substring(a.indexOf("=")+1,a.length));
| |
| }
| |
| else if(a.startsWith("style")){
| |
| style.push(a.substring(a.indexOf("=")+1,a.length-1));
| |
| }
| |
| else if(a.startsWith("#")){
| |
| style.push("background-color:"+a);
| |
| }
| |
| else if(a.startsWith("table")){
| |
| var a=a.substring(6,a.length);
| |
| v=a.substring(a.indexOf("=")+1,a.length);
| |
| if(a.startsWith("bgcolor"))tablestyle.push("background-color:"+v);
| |
| else if(a.startsWith("align"))tablestyle.push("float:"+v);
| |
| else if(a.startsWith("width"))tablestyle.push("width:"+v);
| |
| else if(a.startsWith("style"))tablestyle.push(v.substring(0,v.length-1));
| |
| }
| |
| else c.push(l[j]);
| |
|
| |
| }
| |
| else c.push(l[j]);
| |
|
| |
| }
| |
| c = c.join("");
| |
| if(c.length == 0){
| |
| colspan++;
| |
| continue;
| |
| }
| |
| if(is_h==0){
| |
| tablemark.push("\n|");
| |
| }
| |
| else{
| |
| tablemark.push("\n!");
| |
| }
| |
| tablemark.push(rowspan);
| |
| if(style.length!=0){
| |
| b=1;
| |
| var t = "style=\"";
| |
| t+=style.join(";");
| |
| t+='" ';
| |
| tablemark.push(t);
| |
| }
| |
| if(colspan!=0){
| |
| tablemark.push("colspan=\"" + (colspan +1) + "\" ");
| |
| b=1;
| |
| }
| |
| if(b)tablemark+="|";
| |
| c = ConvertToMediaWikiRef(c);
| |
| tablemark.push(c.replace("[BR]","<br>").replace("[br]","<br>"));
| |
| colspan=0;
| |
| }
| |
| }
| |
| tablemark.push("\n|-");
| |
| }
| |
| res.push("\n{|class=\"wikitable\" style=\"");
| |
| if(tablestyle.length!=0){
| |
| b=1;
| |
| res.push(tablestyle.join(";"));
| |
| }
| |
| res.push("\"\n|-");
| |
| res.push(tablemark.join(""));
| |
| res.push("\n|}");
| |
| }
| |
| res.push(str);
| |
| return res.join("");
| |
| }
| |
|
| |
| $(document).ready(function(){
| |
| $("#wpSave").click(function()
| |
| {
| |
| $("#wpTextbox1").val(ConvertToMediaWikiTable($("#wpTextbox1").val()));
| |
| });
| |
| });
| |