flyson
flyso****@users*****
2005年 11月 25日 (金) 00:01:47 JST
Index: bbs2chreader/content/bbs2chreader/test/thread-test.js diff -u bbs2chreader/content/bbs2chreader/test/thread-test.js:1.6 bbs2chreader/content/bbs2chreader/test/thread-test.js:1.7 --- bbs2chreader/content/bbs2chreader/test/thread-test.js:1.6 Wed Nov 23 08:10:27 2005 +++ bbs2chreader/content/bbs2chreader/test/thread-test.js Fri Nov 25 00:01:47 2005 @@ -117,6 +117,8 @@ startThread: function(){ + // HTML ヘッダを送信したら true になる + this._headerResponded = false; // threadURL var threadURLSpec = this._channel.URI.spec.replace(/^bbs2ch:thread-test:/, ""); @@ -130,9 +132,12 @@ this._type = this._bbs2chService.getBoardType(this.threadURL.spec); this.dat = new b2rDat(); - this.dat.init(this.threadURL, this.boardURL, this._type); + this.dat.init(this.threadURL, this.boardURL, this.type); + this.converter = new b2rThreadConverter(); + this.converter.init(this.threadURL, this.boardURL, this.type); + this.requestRespond("<!-- \n"); this.requestRespond("Thread URL : " + this.threadURL.spec + "\n"); this.requestRespond("Board URL : " + this.boardURL.spec + "\n"); this.requestRespond("Type : " + this.type + "\n"); @@ -150,7 +155,7 @@ this.requestRespond(" Last : " + this.optionsLast + "\n"); this.requestRespond(" End : " + this.optionsEnd + "\n"); this.requestRespond(" NoFirst : " + this.optionsNoFirst + "\n"); - this.requestRespond("----- ----- -----\n\n"); + this.requestRespond("-->\n\n"); // 取得済みログの送信 if(this.dat.datFile.exists()){ @@ -175,35 +180,112 @@ }else{ if(!this.optionsNoFirst){ - var firstContent = 1 +"\t: "+ datLines[0] + "\n"; - this.requestRespond(firstContent); + this.requestRespond(this.datLineParse(datLines[0], 1, false) +"\n"); + }else if(this.dat.title){ + this._headerResponded = true; + var header = this.converter.getHeader(this.dat.title); + this.requestRespond(header); + }else{ + this.requestRespond(this.datLineParse(datLines[0], 1, false) +"\n"); } var start = 1; var end = datLines.length; - if(this.optionsLast){ + if(this.optionsLast || this.optionsLast == 0){ start = end - this.optionsLast; if(start < 1) start = 1; } - var content = new Array(); for(var i=start; i<end; i++){ - content.push((i+1) +"\t: "+ datLines[i]); + content.push(this.datLineParse(datLines[i], i+1, false)); + } + this.requestRespond(content.join("\n")); } } - this.requestRespond("\n\n----- ----- -----\n\n"); // オフラインなら終了 if(this._ioService.offline){ this.requestRespond("OFFLINE MODE"); this.requestEnd(); }else{ + var newMark = this.converter.getNewMark() +"\n"; + this.requestRespond(newMark); this.datDownload(); } }, - + + datLineParse: function(aLine, aNumber, aNew){ + if(!aLine) return ""; + + var resArray = aLine.split("<>"); + var resNumber = aNumber; + var resName = "BLOKEN"; + var resMail = ""; + var resDate = "BLOKEN"; + var resID = ""; + var resBeID = ""; + var resMes = ""; + + if(resArray.length > 3){ + resName = resArray[0].replace(/<\/?b>|/g, ""); + resMail = resArray[1]; + resDate = resArray[2]; + resMes = resArray[3]; + } + + // BE:*******-* 形式 を <BE:*******:*> に変換 + var regBeID = /BE: ?(\d+)\-(#{0,5})/; + if(resDate.match(regBeID)){ + resDate = resDate.replace(regBeID, "<BE:$1:" + RegExp.$2.length +">"); + } + + // resDate を DATE、BeID、BeLevel に分割 + if(resDate.match(/(.+)(<BE:\d+:\d+>)/)){ + resDate = RegExp.$1; + resBeID = RegExp.$2; + } + // resDate を DATE と ID に分割 + if(resDate.match(/(.+)ID:(.+)/)){ + resDate = RegExp.$1; + resID = RegExp.$2; + } + + if(resBeID){ + var regBeID2 = /<BE:(\d+):(\d+)>/; + if(resBeID.match(regBeID2)){ + idInfoUrl = "http://be.2ch.net/test/p.php?i=" + RegExp.$1 + + "&u=d:" + this.threadURL.resolve("./") + aNumber; + resBeID = resBeID.replace(regBeID2, String("$1 Lv.$2").link(idInfoUrl)); + } + } + + // JSでは "\" が特殊な意味を持つため、数値文字参照に変換 + resName = resName.replace(/([^\x81-\xfc]|^)\x5C/g,"$1\"); + resMail = resMail.replace(/([^\x81-\xfc]|^)\x5C/g,"$1\"); + // レス番リンク処理 + var regResPointer = /(<a .*?>)?(>>|>)([0-9]{1,4})(\-[0-9]{1,4})?(<\/a>)?/g; + resMes = resMes.replace(regResPointer, '<a href="#res$3" class="resPointer">$2$3$4</a>'); + + // 通常リンク処理 + var regUrlLink = /(h?ttp)(s)?\:([\-_\.\!\~\*\'\(\)a-zA-Z0-9\;\/\?\:\@\&\=\+\$\,\%\#]+)/g; + resMes = resMes.replace(regUrlLink, '<a href="http$2:$3" class="outLink">$1$2:$3</a>'); + + + // スレッドのタイトルが見つかったときは HTML ヘッダを追加して送る + if(!this._headerResponded && resArray[4]){ + this._headerResponded = true; + this.dat.title = resArray[4]; + + var header = this.converter.getHeader(this.dat.title); + this.requestRespond(header); + } + var response = this.converter.getResponse(aNew, aNumber, resName, resMail, resDate, + resID, resBeID, resMes); + return response; + }, + datDownload: function(){ var httpChannel = this._ioService.newChannelFromURI(this.dat.datURL) .QueryInterface(Components.interfaces.nsIHttpChannel); @@ -257,7 +339,7 @@ // DAT を 変換して書き出す for(var i=0; i<datLines.length; i++){ this.dat.lineCount++; - datLines[i] = this.dat.lineCount +"\t: "+ datLines[i]; + datLines[i] = this.datLineParse(datLines[i], this.dat.lineCount, true); } this.requestRespond(datLines.join("\n")); }, @@ -296,6 +378,7 @@ } this._bInputStream.close(); + this.requestRespond("OK"); this.requestEnd(); if(httpStatus == 200 || httpStatus == 206) this.datSave(this._data.join("")); this._data = null; @@ -319,3 +402,69 @@ b2r2chThread.prototype.__proto__ = b2rThread.prototype; + + + +function b2rThreadConverter(){ + +} + +b2rThreadConverter.prototype = { + + init: function(aThreadURL, aBoardURL, aType){ + this._threadURL = aThreadURL; + this._boardURL = aBoardURL; + this._type = aType; + + this._bbs2chService = Components.classes["@mozilla.org/bbs2ch-service;1"] + .getService(Components.interfaces.nsIBbs2chService); + + + this._skinURISpec = this._bbs2chService.prefDefault.getCharPref( + "fls.bbs2chreader.thread_skin_uri"); + this._tmpHeader = this._bbs2chService.readLocalURI(this._skinURISpec + "Header.html"); + this._tmpFooter = this._bbs2chService.readLocalURI(this._skinURISpec + "Footer.html"); + this._tmpRes = this._bbs2chService.readLocalURI(this._skinURISpec + "Res.html"); + this._tmpNewRes = this._bbs2chService.readLocalURI(this._skinURISpec + "NewRes.html"); + this._tmpNewMark = this._bbs2chService.readLocalURI(this._skinURISpec + "NewMark.html"); + + // 基本スキンタグの置換 + this._tmpHeader = this._replaceBaseTag(this._tmpHeader); + this._tmpFooter = this._replaceBaseTag(this._tmpFooter); + this._tmpRes = this._replaceBaseTag(this._tmpRes); + this._tmpNewRes = this._replaceBaseTag(this._tmpNewRes); + this._tmpNewMark = this._replaceBaseTag(this._tmpNewMark); + }, + + /** + * 基本スキンタグの置換 + * @param aString string 置換される文字列 + */ + _replaceBaseTag: function(aString){ + return aString.replace(/<SKINPATH\/>/g, this._skinURISpec) + .replace(/<THREADURL\/>/g, this._threadURL.resolve("./")) + .replace(/<BOARDURL\/>/g, this._boardURL.spec); + }, + + getHeader: function(aTitle){ + return this._tmpHeader.replace(/<THREADNAME\/>/g, aTitle); + }, + + getNewMark: function(){ + return this._tmpNewMark; + }, + + getResponse: function(aNew, aNumber, aName, aMail, aDate, aID, aBeID, aMessage){ + var template = aNew ? this._tmpNewRes : this._tmpRes; + var result = template.replace(/<PLAINNUMBER\/>/g, aNumber) + .replace(/<NUMBER\/>/g, aNumber) + .replace(/<NAME\/>/g, aName) + .replace(/<MAIL\/>/g, aMail) + .replace(/<MAILNAME\/>/g, "") + .replace(/<DATE\/>/g, aDate) + .replace(/<ID\/>/g, aID) + .replace(/<BEID\/>/g, aBeID) + .replace(/<MESSAGE\/>/g, aMessage); + return result; + } +} \ No newline at end of file