src/BloomExe/Spreadsheet/SpreadsheetExporter.cs
/// <summary>
/// Output the specified DOM to the specified outputFolder (after deleting any existing content, if
/// permitted...depends on overwrite param and possibly user input).
/// Returns the intermediate spreadsheet object created, and also outputs the path to the xlsx file created.
/// Looks for images in the specified imagesFolderPath (typically the book folder) and copies them to an
/// images subdirectory of the outputFolder.
/// Currently the xlsx file created will have the same name as the outputFolder, typically copied from
/// the input book folder.
/// <returns>the internal spreadsheet, or null if not permitted to overwrite.</returns>
/// </summary>
Output the specified DOM to the specified outputFolder (after deleting any existing content, if
permitted...depends on overwrite param and possibly user input).
Returns the intermediate spreadsheet object created, and also outputs the path to the xlsx file created.
Looks for images in the specified imagesFolderPath (typically the book folder) and copies them to an
images subdirectory of the outputFolder.
Currently the xlsx file created will have the same name as the outputFolder, typically copied from
the input book folder.
the internal spreadsheet, or null if not permitted to overwrite.
Output the specified DOM to the specified outputFolder ( after deleting any existing content if permitted ... depends on overwrite param and possibly user input ) . Returns the intermediate spreadsheet object created and also outputs the path to the xlsx file created . Looks for images in the specified imagesFolderPath ( typically the book folder ) and copies them to an images subdirectory of the outputFolder . Currently the xlsx file created will have the same name as the outputFolder typically copied from the input book folder . the internal spreadsheet or null if not permitted to overwrite .
public InternalSpreadsheet ExportToFolder(HtmlDom dom, string imagesFolderPath, string outputFolder,
			out string outputPath,
			IWebSocketProgress progress = null, OverwriteOptions overwrite = OverwriteOptions.Ask)
		{
			outputPath = Path.Combine(outputFolder,
				Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(outputFolder) + ".xlsx"));
			_outputFolder = outputFolder;
			_outputImageFolder = Path.Combine(_outputFolder, "images");
			try
			{
				if (Directory.Exists(outputFolder))
				{
					if (overwrite == OverwriteOptions.Quit)
					{
						Console.WriteLine($"Output folder ({_outputFolder}) exists. Use --overwrite to overwrite.");
						outputPath = null;
						return null;
					}
					var appearsToBeBloomBookFolder = Directory.EnumerateFiles(outputFolder, "*.htm").Any();
					var msgTemplate = LocalizationManager.GetString("Spreadsheet.Overwrite",
						"You are about to replace the existing folder named {0}");
					var msg = string.Format(msgTemplate, outputFolder);
					var messageBoxButtons = new[]
					{
						new MessageBoxButton() { Text = "Overwrite", Id = "overwrite" },
						new MessageBoxButton() { Text = "Cancel", Id = "cancel", Default = true }
					};
					if (appearsToBeBloomBookFolder)
					{
						if (overwrite == OverwriteOptions.Overwrite)
						{
							Console.WriteLine(
								$"Output folder ({_outputFolder}) exists and appears to be a Bloom book, not a previous export. If you really mean to export there, you'll have to delete the folder first.");
							outputPath = null;
							return null;
						}
						msgTemplate = LocalizationManager.GetString("Spreadsheet.OverwriteBook",
							"The folder named {0} already exists and looks like it might be a Bloom book folder!");
						msg = string.Format(msgTemplate, outputFolder);
						messageBoxButtons = new[] { messageBoxButtons[1] }; 
					}
					if (overwrite == OverwriteOptions.Ask)
					{
						var formToInvokeOn = Application.OpenForms.Cast<Form>().FirstOrDefault(f => f is Shell);
						string result = null;
						formToInvokeOn.Invoke((Action)(() =>
						{
							result = BloomMessageBox.Show(formToInvokeOn, msg, messageBoxButtons,
								MessageBoxIcon.Warning);
						}));
						if (result != "overwrite")
						{
							outputPath = null;
							return null;
						}
					} 
				}
				SIL.IO.RobustIO.DeleteDirectoryAndContents(_outputFolder);
				Directory.CreateDirectory(_outputImageFolder); 
				var spreadsheet = Export(dom, imagesFolderPath, progress);
				spreadsheet.WriteToFile(outputPath, progress);
				return spreadsheet;
			}
			catch (Exception e) when (e is IOException || e is SecurityException || e is UnauthorizedAccessException)
			{
				progress.MessageWithParams("Spreadsheet.WriteFailed", "",
					"Bloom had problems writing files to that location ({0}). Check that you have permission to write there.",
					ProgressKind.Error, _outputFolder);
			}
			outputPath = null;
			return null; 
		}
public InternalSpreadsheet ExportToFolder ( HtmlDom dom , string imagesFolderPath , string outputFolder , out string outputPath , IWebSocketProgress progress = null , OverwriteOptions overwrite = OverwriteOptions . Ask ) { outputPath = Path . Combine ( outputFolder , Path . Combine ( outputFolder , Path . GetFileNameWithoutExtension ( outputFolder ) + " " ) ) ; _outputFolder = outputFolder ; _outputImageFolder = Path . Combine ( _outputFolder , " " ) ; try { if ( Directory . Exists ( outputFolder ) ) { if ( overwrite == OverwriteOptions . Quit ) { Console . WriteLine ( $" Output folder ( { _outputFolder } ) exists. Use --overwrite to overwrite. " ) ; outputPath = null ; return null ; } var appearsToBeBloomBookFolder = Directory . EnumerateFiles ( outputFolder , " " ) . Any ( ) ; var msgTemplate = LocalizationManager . GetString ( " " , " " ) ; var msg = string . Format ( msgTemplate , outputFolder ) ; var messageBoxButtons = new [ ] { new MessageBoxButton ( ) { Text = " " , Id = " " } , new MessageBoxButton ( ) { Text = " " , Id = " " , Default = true } } ; if ( appearsToBeBloomBookFolder ) { if ( overwrite == OverwriteOptions . Overwrite ) { Console . WriteLine ( $" Output folder ( { _outputFolder } ) exists and appears to be a Bloom book, not a previous export. If you really mean to export there, you'll have to delete the folder first. " ) ; outputPath = null ; return null ; } msgTemplate = LocalizationManager . GetString ( " " , " " ) ; msg = string . Format ( msgTemplate , outputFolder ) ; messageBoxButtons = new [ ] { messageBoxButtons [ 1 ] } ; } if ( overwrite == OverwriteOptions . Ask ) { var formToInvokeOn = Application . OpenForms . Cast < Form > ( ) . FirstOrDefault ( f => f is Shell ) ; string result = null ; formToInvokeOn . Invoke ( ( Action ) ( ( ) => { result = BloomMessageBox . Show ( formToInvokeOn , msg , messageBoxButtons , MessageBoxIcon . Warning ) ; } ) ) ; if ( result != " " ) { outputPath = null ; return null ; } } } SIL . IO . RobustIO . DeleteDirectoryAndContents ( _outputFolder ) ; Directory . CreateDirectory ( _outputImageFolder ) ; var spreadsheet = Export ( dom , imagesFolderPath , progress ) ; spreadsheet . WriteToFile ( outputPath , progress ) ; return spreadsheet ; } catch ( Exception e ) when ( e is IOException || e is SecurityException || e is UnauthorizedAccessException ) { progress . MessageWithParams ( " " , " " , " " , ProgressKind . Error , _outputFolder ) ; } outputPath = null ; return null ; }
Output the specified DOM to the specified outputFolder (after deleting any existing content, if
permitted...depends on overwrite param and possibly user input).
Output the specified DOM to the specified outputFolder ( after deleting any existing content if permitted ... depends on overwrite param and possibly user input ) .
// I'm assuming someone working with a command-line can cope with English. // Don't think it's worth cluttering the XLF with this. // Assume we can't UI in this mode. But we absolutely must not overwrite the book folder! // So quit anyway. // only cancel // if it's not Ask, at this point it must be Overwrite, so go ahead. // In case there's a previous export, get rid of it. // also (re-)creates its parent, outputFolder // some error occurred and was caught
dom
HtmlDom
imagesFolderPath
string
outputFolder
string
string
out
progress
IWebSocketProgress
overwrite
OverwriteOptions