Exemple #1
0
        /// <summary>
        /// Entry point for epub converter
        /// </summary>
        /// <param name="projInfo">values passed including xhtml and css names</param>
        /// <returns>true if succeeds</returns>
        public bool Export(PublicationInformation projInfo)
        {
	        Common.SetupLocalization();
            if (projInfo == null)
                return false;
            const bool success = true;

            #region Set up progress reporting
#if (TIME_IT)
            DateTime dt1 = DateTime.Now;    // time this thing
#endif
            var myCursor = UseWaitCursor();
            var curdir = Environment.CurrentDirectory;
            var inProcess = SetupProgressReporting(20);
            #endregion Set up progress reporting

            #region Setup
            inProcess.SetStatus("Setup");
            var bookId = Guid.NewGuid(); // NOTE: this creates a new ID each time Pathway is run. 
            PageBreak = InputType.ToLower() == "dictionary" && GetPageBreakStatus(projInfo.SelectedTemplateStyle);
            #region LoadXslts
            var addRevId = LoadAddRevIdXslt();
            var noXmlSpace = LoadNoXmlSpaceXslt();
            var fixEpub = LoadFixEpubXslt();
            #endregion
            #region Create EpubFolder
            if (!Common.Testing)
            {
                CreateEpubFolder(projInfo);
            }
            else
            {
                projInfo.ProjectPath = projInfo.DictionaryPath;
            }

            #endregion

            var preProcessor = new PreExportProcess(projInfo);
            preProcessor.RemoveBrokenImage();

            _isUnixOs = Common.UnixVersionCheck();
            if (_isUnixOs)
            {
                Common.RemoveDTDForLinuxProcess(projInfo.DefaultXhtmlFileWithPath, "epub");
            }

            _isIncludeImage = GetIncludeImageStatus(projInfo.SelectedTemplateStyle);

            _isNoteTargetReferenceExists = Common.NodeExists(projInfo.DefaultXhtmlFileWithPath, "");

            _epubFont = new EpubFont(this);
            _epubManifest = new EpubManifest(this, _epubFont);
            _epubManifest.LoadPropertiesFromSettings();
            LoadOtherFeatures();
            var epubToc = new EpubToc(projInfo.ProjectInputType, TocLevel);
            inProcess.PerformStep();

            #endregion Setup

            #region Xhtml preprocessing
            inProcess.SetStatus("Preprocessing content");

            var glossorywords = WriteGlossaryLink(projInfo);
            GlossaryLinkReferencing(projInfo,glossorywords);

            InsertBeforeAfterInXhtml(projInfo);
            var outputFolder = SetOutputFolderAndCurrentDirectory(projInfo);
            Common.SetProgressBarValue(projInfo.ProgressBar, projInfo.DefaultXhtmlFileWithPath);
            XhtmlPreprocessing(projInfo, preProcessor);
            var langArray = _epubFont.InitializeLangArray(projInfo);
            inProcess.PerformStep();
            #endregion Xhtml preprocessing

            #region Css preprocessing
            inProcess.SetStatus("Preprocessing stylesheet");
            var cssFullPath = CssFullPath(projInfo);
            Common.WriteDefaultLanguages(projInfo, cssFullPath);
            var tempFolder = Path.GetDirectoryName(preProcessor.ProcessedXhtml);
            var mergedCss = MergeAndFilterCss(preProcessor, tempFolder, cssFullPath);
            /* Modify the content in css file for before after css style process */
            preProcessor.InsertPseudoContentProperty(mergedCss, PseudoClass);
            CustomizeCss(mergedCss);
            var niceNameCss = NiceNameCss(projInfo, tempFolder, ref mergedCss);
            var defaultCss = Path.GetFileName(niceNameCss);
            string tempCssFile = mergedCss.Replace(".css", "tmp.css");
            File.Copy(mergedCss, tempCssFile, true);

            Common.SetDefaultCSS(projInfo.DefaultXhtmlFileWithPath, defaultCss);
            Common.SetDefaultCSS(preProcessor.ProcessedXhtml, defaultCss);
            if (!File.Exists(mergedCss))
            {
                File.Copy(tempCssFile, mergedCss, true);
            }

            inProcess.PerformStep();
            #endregion Css preprocessing

            #region Hacks
            XhtmlNamespaceHack(projInfo, preProcessor, langArray);
            Common.ApplyXslt(preProcessor.ProcessedXhtml, noXmlSpace);
            Common.ApplyXslt(preProcessor.ProcessedXhtml, fixEpub);
            inProcess.PerformStep();
            #endregion Hacks

            #region Adding Navigation and Front Matter
            inProcess.SetStatus("Adding Navigation");
            preProcessor.PrepareBookNameAndChapterCount();
            inProcess.PerformStep();

            // insert the front matter items as separate files in the output folder
            inProcess.SetStatus("Adding Front Matter");
            preProcessor.SkipChapterInformation = TocLevel;
            var frontMatter = preProcessor.InsertFrontMatter(tempFolder, false);
            inProcess.PerformStep();
            #endregion Adding Navigation and Front Matter

            #region Add Sections
            inProcess.SetStatus("Add Sections");
            var htmlFiles = new List<string>();
            var splitFiles = new List<string>();
            splitFiles.AddRange(frontMatter);
            SplittingFrontMatter(projInfo, preProcessor, defaultCss, splitFiles);
            SplittingReversal(projInfo, addRevId, langArray, defaultCss, splitFiles);
            AddBooksMoveNotes(inProcess, htmlFiles, splitFiles);
            inProcess.PerformStep();
            #endregion Add Sections

            #region Create structure and add end notes
            inProcess.SetStatus("Creating structure");
            string contentFolder = CreateContentStructure(projInfo, tempFolder);
            inProcess.PerformStep();

            // extract references file if specified
            if (References.Contains("End") && InputType.ToLower().Equals("scripture"))
            {
                inProcess.SetStatus("Creating endnote references file");
                CreateReferencesFile(contentFolder, preProcessor.ProcessedXhtml);
                splitFiles.Add(Common.PathCombine(contentFolder, ReferencesFilename));
            }
            #endregion Create structure and add end notes

            #region Font embedding
            inProcess.SetStatus("Processing fonts");
            if (!FontProcessing(projInfo, langArray, mergedCss, contentFolder))
            {
                // user cancelled the epub conversion - clean up and exit
                Environment.CurrentDirectory = curdir;
                Cursor.Current = myCursor;
                inProcess.Close();
                return false;
            }
            inProcess.PerformStep();
            #endregion Font Embedding

            #region Copy to Epub
            inProcess.SetStatus("Copy contents and styles to Epub");
            CopyStylesAndContentToEpub(mergedCss, defaultCss, htmlFiles, contentFolder);
            inProcess.PerformStep();
            #endregion Copy to Epub

            #region Insert Chapter Links
            inProcess.SetStatus("Insert Chapter Links");
            InsertChapterLinkBelowBookName(contentFolder);
            InsertReferenceLinkInTocFile(contentFolder);
            inProcess.PerformStep();
            #endregion Insert Chapter Links

            #region Process hyperlinks
#if (TIME_IT)
            DateTime dtRefStart = DateTime.Now;
#endif
            inProcess.SetStatus("Processing hyperlinks");
            if (InputType.ToLower() == "scripture" && References.Contains("End"))
            {
                UpdateReferenceHyperlinks(contentFolder, inProcess);
                UpdateReferenceSourcelinks(contentFolder, inProcess);
            }
            FixRelativeHyperlinks(contentFolder);

#if (TIME_IT)
            TimeSpan tsRefTotal = DateTime.Now - dtRefStart;
            Debug.WriteLine("Exportepub: time spent fixing reference hyperlinks: " + tsRefTotal);
#endif
            inProcess.PerformStep();
            #endregion Process hyperlinks

            #region Process images
            inProcess.SetStatus("Processing images");
            ProcessImages(tempFolder, contentFolder);
            inProcess.PerformStep();
            #endregion Process images

            #region Manifest and Table of Contents
            inProcess.SetStatus("Generating .epub TOC and manifest");
            _epubManifest.CreateOpf(projInfo, contentFolder, bookId);
            epubToc.CreateNcx(projInfo, contentFolder, bookId);
            ModifyTOCFile(contentFolder);
            ReplaceEmptyHref(contentFolder);
            if (File.Exists(tempCssFile))
            {
                File.Delete(tempCssFile);
            }
            inProcess.PerformStep();
            #endregion Manifest and Table of Contents

            #region Copy Epub2 package for Epub3
            string epub3Path = projInfo.ProjectPath;
            epub3Path = Common.PathCombine(epub3Path, "Epub3");
            Common.CopyFolderandSubFolder(projInfo.TempOutputFolder, epub3Path, true);
            if (File.Exists(projInfo.DefaultXhtmlFileWithPath))
                File.Copy(projInfo.DefaultXhtmlFileWithPath, Common.PathCombine(epub3Path, Path.GetFileName(projInfo.DefaultXhtmlFileWithPath)), true);

            if (File.Exists(projInfo.DefaultCssFileWithPath))
                File.Copy(projInfo.DefaultCssFileWithPath, Common.PathCombine(epub3Path, Path.GetFileName(projInfo.DefaultCssFileWithPath)), true);

            if (File.Exists(projInfo.DefaultRevCssFileWithPath))
                File.Copy(projInfo.DefaultRevCssFileWithPath, Common.PathCombine(epub3Path, Path.GetFileName(projInfo.DefaultRevCssFileWithPath)), true);

            _exportEpub3 = new Epub3Transformation(this, _epubFont);
            _exportEpub3.Epub3Directory = epub3Path;
            _exportEpub3.Export(projInfo);

            #endregion Copy Epub2 package for Epub3

            #region Packaging for Epub2 and Epub3
            inProcess.SetStatus("Packaging for Epub2");
            if (_isUnixOs)
            {
                AddDtdInXhtml(contentFolder);
            }
            string fileName = CreateFileNameFromTitle(projInfo);
            Compress(projInfo.TempOutputFolder, Common.PathCombine(outputFolder, fileName));
            var outputPathWithFileName = Common.PathCombine(outputFolder, fileName) + ".epub";

            if (!Common.Testing)
            {

                inProcess.SetStatus("Copy html files");
                string htmlFolderPath = Common.PathCombine(Path.GetDirectoryName(epub3Path), "HTML5");
                string oebpsFolderPath = Common.PathCombine(epub3Path, "OEBPS");
                Common.CustomizedFileCopy(oebpsFolderPath, htmlFolderPath, "content.opf, toc.html");
            }

            inProcess.SetStatus("Packaging for Epub3");

            string fileNameV3 = CreateFileNameFromTitle(projInfo);
            string outputPathWithFileNameV3 = null;
            if (epub3Path != null)
            {
                Compress(epub3Path, Common.PathCombine(epub3Path, fileNameV3));
                outputPathWithFileNameV3 = Common.PathCombine(epub3Path, fileNameV3) + ".epub";
            }
#if (TIME_IT)
            TimeSpan tsTotal = DateTime.Now - dt1;
            Debug.WriteLine("Exportepub: time spent in .epub conversion: " + tsTotal);
#endif
            inProcess.PerformStep();
            #endregion Packaging

            #region Validate
            inProcess.SetStatus("Validate");

            #region Option Dialog box
            bool isOutputDilalogNeeded = true;
            if (!Common.Testing)
            {
				string caption = LocalizationManager.GetString("EpubExportTypeDlg.ValidateAndDisplay.Caption", "Export Complete", "");
				if (Utils.MsgBox(LocalizationManager.GetString("Exportepub.ValidateMsgBox", Resources.ExportCallingEpubValidator + "\r\n Do you want to Validate ePub files?"), caption, MessageBoxButtons.YesNo,
                                    MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    ValidateResult(outputPathWithFileName);     // Epub2 ExportType
                    ValidateResult(outputPathWithFileNameV3);   //Epub3 ExportType
                }

                #region Option Dialog box

                if (!Common.Testing && isOutputDilalogNeeded)
                {
                    outputPathWithFileName = RenameEpubFileName(outputPathWithFileName, "epub2");
                    outputPathWithFileNameV3 = RenameEpubFileName(outputPathWithFileNameV3, "epub3");

                    var exportTypeDlg = new EpubExportTypeDlg();
                    exportTypeDlg.ShowDialog();
                    if (exportTypeDlg._exportType == "epub2")
                    {
                        outputFolder = Path.GetDirectoryName(outputPathWithFileName);
                        DisplayOutput(outputFolder, fileName, ref outputPathWithFileName);
                    }
                    else if (exportTypeDlg._exportType == "epub3")
                    {
                        outputFolder = Path.GetDirectoryName(outputPathWithFileNameV3);
                        DisplayOutput(outputFolder, fileName, ref outputPathWithFileNameV3);
                    }
                    else if (exportTypeDlg._exportType == "folder")
                    {
                        if (_isUnixOs)
                        {
                            SubProcess.Run("", "nautilus",  Common.HandleSpaceinLinuxPath(Path.GetDirectoryName(outputFolder)), false);
                        }
                        else
                        {
                            SubProcess.Run(Path.GetDirectoryName(outputFolder), "explorer.exe", Path.GetDirectoryName(outputFolder), false);
                        }
                    }
                }
                #endregion Option Dialog box
            }
            #endregion Option Dialog box

            inProcess.PerformStep();
            #endregion Validate
            
            #region Clean up
            inProcess.SetStatus("Clean up");
            Common.CleanupExportFolder(outputPathWithFileName, ".tmp,.de", "_1", string.Empty);
            if (!Common.Testing)
            {
                Common.CleanupExportFolder(outputPathWithFileNameV3, ".tmp,.de,.zip", "_1", "META-INF,OEBPS");
            }
            inProcess.PerformStep();
            #endregion Clean up

            #region Archive
            inProcess.SetStatus("Archive");
            CreateRAMP(projInfo);
            if (!Common.Testing)
            {
                projInfo.DefaultXhtmlFileWithPath = Common.PathCombine(epub3Path, Path.GetFileName(projInfo.DefaultXhtmlFileWithPath));
                CreateRAMP(projInfo);
            }
            inProcess.PerformStep();
            #endregion Archive

            #region Clean up
            inProcess.SetStatus("Final Clean up");
            Common.CleanupExportFolder(outputPathWithFileName, ".xhtml,.xml,.css", string.Empty, "Test");
            if (!Common.Testing)
            {
                Common.CleanupExportFolder(outputPathWithFileNameV3, ".xhtml,.xml,.css", string.Empty, "Test");
            }
            inProcess.PerformStep();
            #endregion Clean up

            #region Close Reporting
            inProcess.Close();

            Environment.CurrentDirectory = curdir;
            Cursor.Current = myCursor;
            #endregion Close Reporting

            return success;
        }