From 52fe187328dd4f17f7cfab51e2cefd1f6c01966f Mon Sep 17 00:00:00 2001 From: Simon Kornblith Date: Tue, 15 Aug 2006 01:05:20 +0000 Subject: [PATCH] closes #184, support non-ASCII characters in HTML and RTF. since we use the unicode features of RTF 1.5, this requires Word 97 or later on a PC (or presumably Word 98 or later on a Mac) to read. fixes one last strict mode bug --- .../content/scholar/fileInterface.js | 15 ++++- .../content/scholar/ingester/browser.js | 2 + .../chromeFiles/content/scholar/xpcom/cite.js | 58 +++++++++++++++++-- 3 files changed, 67 insertions(+), 8 deletions(-) diff --git a/chrome/chromeFiles/content/scholar/fileInterface.js b/chrome/chromeFiles/content/scholar/fileInterface.js index 332acca167..3ddd6c65f3 100644 --- a/chrome/chromeFiles/content/scholar/fileInterface.js +++ b/chrome/chromeFiles/content/scholar/fileInterface.js @@ -239,19 +239,28 @@ var Scholar_File_Interface = new function() { bibliographyStream.close(); } else if(io.output == "save-as-html") { var fStream = _saveBibliography("HTML"); - if(fStream !== false) { + + if(fStream !== false) { var html = ""; html +='\n'; html +='\n'; html +='\n'; - html +='\n'; + html +='\n'; html +='Bibliography\n'; html +='\n'; html +='\n'; html += bibliography; html +='\n'; html +='\n'; - fStream.write(html, html.length); + + // create UTF-8 output stream + var os = Components.classes["@mozilla.org/intl/converter-output-stream;1"]. + createInstance(Components.interfaces.nsIConverterOutputStream); + os.init(fStream, "UTF-8", 0, "¥"); + + os.writeString(html); + + os.close(); fStream.close(); } } else if(io.output == "save-as-rtf") { diff --git a/chrome/chromeFiles/content/scholar/ingester/browser.js b/chrome/chromeFiles/content/scholar/ingester/browser.js index 7fb330fbcc..787ba1fb6f 100644 --- a/chrome/chromeFiles/content/scholar/ingester/browser.js +++ b/chrome/chromeFiles/content/scholar/ingester/browser.js @@ -357,6 +357,8 @@ Scholar_Ingester_Interface.Progress = new function() { _progressWindow = window.openDialog("chrome://scholar/chrome/ingester/progress.xul", "", "chrome,dialog=no,titlebar=no,popup=yes"); _progressWindow.addEventListener("load", _onWindowLoaded, false); _windowLoading = true; + + return true; } function changeHeadline(headline) { diff --git a/chrome/chromeFiles/content/scholar/xpcom/cite.js b/chrome/chromeFiles/content/scholar/xpcom/cite.js index c79cc3f2bb..f768762724 100644 --- a/chrome/chromeFiles/content/scholar/xpcom/cite.js +++ b/chrome/chromeFiles/content/scholar/xpcom/cite.js @@ -135,7 +135,7 @@ CSL.prototype.createBibliography = function(items, format) { style = "margin-left:0.5in;text-indent:-0.5in;"; } } else if(format == "RTF") { - output += "{\\rtf\\mac\\ansicpg10000{\\fonttbl\\f0\\froman Times New Roman;}{\\colortbl;\\red255\\green255\\blue255;}\\pard\\f0"; + output += "{\\rtf\\ansi{\\fonttbl\\f0\\froman Times New Roman;}{\\colortbl;\\red255\\green255\\blue255;}\\pard\\f0"; if(this._opt.hangingIndent) { output += "\\li720\\fi-720"; } @@ -705,10 +705,47 @@ CSL.prototype._processDate = function(string) { return date; } +/* + * escapes a string for a given format + */ +CSL.prototype._escapeString = function(string, format) { + if(format == "HTML") { + // replace HTML entities + string = string.replace(/&/g, "&"); + string = string.replace(//g, ">"); + + return string; + } else if(format == "RTF") { + var newString = ""; + + // go through and fix up unicode entities + for(i=0; i 127) { // encode unicode + newString += "\\uc0\\u"+charCode.toString()+" "; + } else if(charCode == 92) { // double backslashes + newString += "\\\\"; + } else { + newString += string[i]; + } + } + + return newString; + } else { + return string; + } +} + /* * formats a string according to the cs-format attributes on element */ -CSL.prototype._formatString = function(element, string, format) { +CSL.prototype._formatString = function(element, string, format, dontEscape) { + if(!string) return ""; + if(typeof(string) != "string") { + string = string.toString(); + } + if(element["text-transform"]) { if(element["text-transform"] == "lowercase") { // all lowercase @@ -722,6 +759,10 @@ CSL.prototype._formatString = function(element, string, format) { } } + if(!dontEscape) { + string = this._escapeString(string, format); + } + if(format == "HTML") { var style = ""; @@ -749,12 +790,12 @@ CSL.prototype._formatString = function(element, string, format) { } if(format != "compare" && element.prefix) { - string = element.prefix+string; + string = this._escapeString(element.prefix, format)+string; } if(format != "compare" && element.suffix && (element.suffix.length != 1 || string[string.length-1] != element.suffix)) { // skip if suffix is the same as the last char - string += element.suffix; + string += this._escapeString(element.suffix, format); } return string; @@ -1098,6 +1139,9 @@ CSL.prototype._getFieldValue = function(name, element, item, format, typeName) { return ""; } + // controls whether formatted strings need to be escaped a second time + var dontEscape = true; + if(name == "author") { if(item._csl.subsequentAuthorSubstitute) { // handle subsequent author substitute behavior @@ -1198,10 +1242,12 @@ CSL.prototype._getFieldValue = function(name, element, item, format, typeName) { if(item.edition) { data = item.edition; } + dontEscape = false; } else if(name == "genre") { if(item.type || item.thesisType) { data = (item.type ? item.type : item.thesisType); } + dontEscape = false; } else if(name == "group") { var childData = new Array(); for(var i in element.children) { @@ -1219,6 +1265,7 @@ CSL.prototype._getFieldValue = function(name, element, item, format, typeName) { data = childData.join((element["delimiter"] ? element["delimiter"] : "")); } else if(name == "text") { data = this._getTerm(element["term-name"]); + dontEscape = false; } else if(name == "isbn") { if(item.ISBN) { data = this._formatLocator(null, element, item.ISBN, format); @@ -1229,10 +1276,11 @@ CSL.prototype._getFieldValue = function(name, element, item, format, typeName) { } } else if(name == "number") { data = this._csl.number; + dontEscape = false; } if(data) { - return this._formatString(element, data, format); + return this._formatString(element, data, format, dontEscape); } else if(element.substitute) { // try each substitute element until one returns something for(var i in element.substitute) {