From 7b58475df769176a184fad3a83da15ce58a974f3 Mon Sep 17 00:00:00 2001
From: Simon Kornblith <simon@simonster.com>
Date: Tue, 23 Jun 2009 19:49:56 +0000
Subject: [PATCH] - closes #1512, RTF Scan: Page Numbers - closes #1513, RTF
 Scan: {Bib} Placemarker Flexibility - switches citations from parentheses to
 brackets, to make it easier to distingush between what was reformatted and
 what wasn't - adds a list of example citations to RTF scan dialog

---
 chrome/content/zotero/rtfScan.js             | 47 +++++++++++++++-----
 chrome/content/zotero/rtfScan.xul            | 12 ++++-
 chrome/locale/en-US/zotero/zotero.dtd        |  3 +-
 chrome/locale/en-US/zotero/zotero.properties |  1 +
 4 files changed, 51 insertions(+), 12 deletions(-)

diff --git a/chrome/content/zotero/rtfScan.js b/chrome/content/zotero/rtfScan.js
index 39073b8d52..b0d504a918 100644
--- a/chrome/content/zotero/rtfScan.js
+++ b/chrome/content/zotero/rtfScan.js
@@ -96,7 +96,16 @@ var Zotero_RTFScan = new function() {
 				.createInstance(nsIFilePicker);
 		fp.init(window, Zotero.getString("rtfScan.saveTitle"), nsIFilePicker.modeSave);
 		fp.appendFilter(Zotero.getString("rtfScan.rtf"), "*.rtf");
-		fp.defaultString = "Untitled.rtf";
+		if(inputFile) {
+			var leafName = inputFile.leafName;
+			var dotIndex = leafName.lastIndexOf(".");
+			if(dotIndex != -1) {
+				leafName = leafName.substr(0, dotIndex);
+			}
+			fp.defaultString = leafName+" "+Zotero.getString("rtfScan.scannedFileSuffix")+".rtf";
+		} else {
+			fp.defaultString = "Untitled.rtf";
+		}
 		
 		var rv = fp.show();
 		if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {				
@@ -150,7 +159,7 @@ var Zotero_RTFScan = new function() {
 		const creatorRe = '((?:(?:'+nameRe+', )*'+nameRe+'(?:,? and|,? \\&|,) )?'+nameRe+')(,? et al\\.?)?';
 		// TODO: localize "and" term
 		const creatorSplitRe = /(?:,| *(?:and|\&)) +/g;
-		var citationRe = new RegExp('(\\(|; )('+creatorRe+', (?:"([^"]+)(?:,"|",) )?([0-9]{4})[a-z]?)(?:, ([0-9]+))?(?=[);])|(([A-Z][^ .,;]+)(,? et al\\.?)? (\\(([0-9]{4})[a-z]?\\)))', "gm");
+		var citationRe = new RegExp('(\\\\\\{|; )('+creatorRe+',? (?:"([^"]+)(?:,"|",) )?([0-9]{4})[a-z]?)(?:,(?: pp?\.?)? ([^ )]+))?(?=;|\\\\\\})|(([A-Z][^ .,;]+)(,? et al\\.?)? (\\\\\\{([0-9]{4})[a-z]?\\\\\\}))', "gm");
 		
 		// read through RTF file and display items as they're found
 		// we could read the file in chunks, but unless people start having memory issues, it's
@@ -168,7 +177,7 @@ var Zotero_RTFScan = new function() {
 				var date = m[6];
 				var pages = m[7];
 				var start = citationRe.lastIndex-m[0].length;
-				var end = citationRe.lastIndex+1;
+				var end = citationRe.lastIndex+2;
 			} else {	// suppressed
 				var citationString = m[8];
 				var creators = m[9];
@@ -179,6 +188,7 @@ var Zotero_RTFScan = new function() {
 				var start = citationRe.lastIndex-m[11].length;
 				var end = citationRe.lastIndex;
 			}
+			citationString = citationString.replace("\\{", "{", "g").replace("\\}", "}", "g");
 			var suppressAuthor = !m[2];
 			
 			if(lastCitation && lastCitation.end >= start) {
@@ -564,15 +574,32 @@ var Zotero_RTFScan = new function() {
 		
 		// add bibliography
 		if(style.hasBibliography) {
-			var bibliography = false;
-			contents = contents.replace(BIBLIOGRAPHY_PLACEHOLDER, function() {
-				if(bibliography !== false) {
-					return bibliography;
+			var bibliography = style.formatBibliography(itemSet, "RTF");
+			// cut off initial font formatting 
+			bibliography = bibliography.substr(bibliography.indexOf("\r\n"));
+			// fix line breaks
+			var linebreak = "\r\n";
+			if(contents.indexOf("\r\n") == -1) {
+				bibliography = bibliography.replace("\r\n", "\n", "g");
+				linebreak = "\n";
+			}
+			
+			if(contents.indexOf(BIBLIOGRAPHY_PLACEHOLDER) !== -1) {
+				contents = contents.replace(BIBLIOGRAPHY_PLACEHOLDER, bibliography);
+			} else {
+				// add two newlines before bibliography
+				bibliography = linebreak+"\\"+linebreak+"\\"+bibliography;
+				
+				// add bibliography automatically inside last set of brackets closed
+				const bracketRe = /^\{+/;
+				var m = bracketRe.exec(contents);
+				if(m) {
+					var closeBracketRe = new RegExp("(\\}{"+m[0].length+"}\\s*)$");
+					contents = contents.replace(closeBracketRe, bibliography+"\\1");
 				} else {
-					bibliography = style.formatBibliography(itemSet, "RTF");
-					return bibliography;
+					contents += bibliography;
 				}
-			}, "g");
+			}
 		}
 		
 		Zotero.File.putContents(outputFile, contents);
diff --git a/chrome/content/zotero/rtfScan.xul b/chrome/content/zotero/rtfScan.xul
index 16c16818b1..c4a5b096f3 100644
--- a/chrome/content/zotero/rtfScan.xul
+++ b/chrome/content/zotero/rtfScan.xul
@@ -15,7 +15,17 @@
 	<wizardpage id="intro-page" label="&zotero.rtfScan.introPage.label;"
 	onpageshow="Zotero_RTFScan.introPageShowing()"
 	onpageadvanced="Zotero_RTFScan.introPageAdvanced()">
-		<description width="700">&zotero.rtfScan.introPage.description;</description>
+		<vbox>
+			<description width="700">&zotero.rtfScan.introPage.description;</description>
+			<label value="{Smith, 2009}"/>
+			<label value="Smith {2009}"/>
+			<label value="{Smith et al., 2009}"/>
+			<label value="{John Smith, 2009}"/>
+			<label value="{Smith, 2009, 10-14}"/>
+			<label value="{Smith, &quot;Title,&quot; 2009}"/>
+			<label value="{Jones, 2005; Smith, 2009}"/>
+			<description width="700" style="padding-top:1em">&zotero.rtfScan.introPage.description2;</description>
+		</vbox>
 		<groupbox>
 			<caption label="&zotero.rtfScan.inputFile.label;"/>
 			<hbox align="center">
diff --git a/chrome/locale/en-US/zotero/zotero.dtd b/chrome/locale/en-US/zotero/zotero.dtd
index 699d5a3fa5..62f1029047 100644
--- a/chrome/locale/en-US/zotero/zotero.dtd
+++ b/chrome/locale/en-US/zotero/zotero.dtd
@@ -188,7 +188,8 @@
 <!ENTITY zotero.rtfScan.ambiguousCitations.label			"Ambiguous Citations">
 <!ENTITY zotero.rtfScan.mappedCitations.label				"Mapped Citations">
 <!ENTITY zotero.rtfScan.introPage.label				"Introduction">
-<!ENTITY zotero.rtfScan.introPage.description		"Zotero can automatically extract and reformat citations and insert a bibliography into RTF files. To get started, choose an RTF file below.">
+<!ENTITY zotero.rtfScan.introPage.description		"Zotero can automatically extract and reformat citations and insert a bibliography into RTF files. The RTF Scan feature currently supports citations in variations upon the following formats:">
+<!ENTITY zotero.rtfScan.introPage.description2		"To get started, select an RTF input file and an output file below:">
 <!ENTITY zotero.rtfScan.scanPage.label				"Scanning for Citations">
 <!ENTITY zotero.rtfScan.scanPage.description		"Zotero is scanning your document for citations. Please be patient.">
 <!ENTITY zotero.rtfScan.citationsPage.label				"Verify Cited Items">
diff --git a/chrome/locale/en-US/zotero/zotero.properties b/chrome/locale/en-US/zotero/zotero.properties
index 37e984f3f1..d036199993 100644
--- a/chrome/locale/en-US/zotero/zotero.properties
+++ b/chrome/locale/en-US/zotero/zotero.properties
@@ -539,6 +539,7 @@ rtfScan.scanning.label				= Scanning RTF Document...
 rtfScan.saving.label				= Formatting RTF Document...
 rtfScan.rtf							= Rich Text Format (.rtf)
 rtfScan.saveTitle					= Select a location in which to save the formatted file
+rtfScan.scannedFileSuffix			= (Scanned)
 
 lookup.failure.title				= Lookup Failed
 lookup.failure.description			= Zotero could not find a record for the specified identifier. Please verify the identifier and try again.