Example #1
0
        private void PreprocessOneFile(string inputPath, List <string> tablePaths, string outputPath)
        {
            DateTime fileDate;

            fileDate = File.GetLastWriteTimeUtc(inputPath);
            if (fileDate > sourceDate)
            {
                sourceDate = fileDate;
                projectOptions.SourceFileDate = sourceDate;
            }
            string input;
            // Read file into input
            // Instead of asking the user what the character encoding is, we guess that it is either
            // Windows 1252 or UTF-8, and choose which one of those based on the assumed presence of
            // surrogates in UTF-8, unless there is a byte-order mark.
            Encoding enc = fileHelper.IdentifyFileCharset(inputPath);
            // MessageBox.Show(inputPath + " is encoded as " + enc.ToString());
            StreamReader reader = new StreamReader(inputPath, enc /* Encoding.GetEncoding(globe.globe.projectOptions.InputEncoding) */);

            input = reader.ReadToEnd() + "\0";
            reader.Close();

            // Copy input into buffer
            byte[] inputBytes = Encoding.UTF8.GetBytes(input); // replaced by previous output for subsequent iterations.
            int    cbyteInput = inputBytes.Length;             // replaced by output length for subsequent iterations

            byte[] outBuffer = inputBytes;                     // in case 0 iterations
            int    nOutLen   = inputBytes.Length;

            foreach (string tp in tablePaths)
            {
                if (tp.EndsWith(".process"))
                {
                    // This will become a switch if we get more processes
                    if (tp == kFootnotesProcess)
                    {
                        FixMultipleFootnotes(ref inputBytes, ref cbyteInput);
                        outBuffer = inputBytes; // in case last pass
                        nOutLen   = cbyteInput; // in case last pass.
                    }
                    else
                    {
                        WriteLine("ERROR: Process " + tp + " not known.");
                        return;
                    }
                    continue;
                }
                string tablePath = Path.Combine(inputProjectDirectory, tp);
                if (!File.Exists(tablePath))
                {
                    tablePath = Path.Combine(inputDirectory, tp);
                }
                if (!File.Exists(tablePath))
                {
                    tablePath = SFConverter.FindAuxFile(tp);
                }
                if (File.Exists(tablePath))
                {
                    if (tablePath.EndsWith(".re"))
                    {
                        // Apply a regular expression substitution
                        string       temp        = Encoding.UTF8.GetString(inputBytes, 0, cbyteInput - 1); // leave out final null
                        StreamReader tableReader = new StreamReader(tablePath, Encoding.UTF8);
                        fileDate = File.GetLastWriteTimeUtc(tablePath);
                        if (fileDate > sourceDate)
                        {
                            sourceDate = fileDate;
                            projectOptions.SourceFileDate = sourceDate;
                        }
                        while (!tableReader.EndOfStream)
                        {
                            string source = tableReader.ReadLine();
                            if (source.Trim().Length == 0)
                            {
                                continue;
                            }

                            char delim = source[0];
                            if (delim == ':')
                            {
                                temp = DoRangeReplacement(source, temp);
                            }
                            else
                            {
                                string[] parts       = source.Split(new char[] { delim });
                                string   pattern     = parts[1];               // parts[0] is the empty string before the first delimiter
                                string   replacement = parts[2];
                                replacement = replacement.Replace("$r", "\r"); // Allow $r in replacement to become a true cr
                                replacement = replacement.Replace("$n", "\n"); // Allow (literal) $n in replacement to become a true newline
                                temp        = System.Text.RegularExpressions.Regex.Replace(temp, pattern, replacement);
                            }
                        }
                        tableReader.Close();
                        temp       = temp + "\0";
                        outBuffer  = Encoding.UTF8.GetBytes(temp);
                        inputBytes = outBuffer;
                        cbyteInput = nOutLen = inputBytes.Length;
                    }
                }
                else
                {
                    MessageBox.Show("Can't find preprocessing file " + tp, "Error in preprocessing file list");
                }
            }

            // Convert the output back to a file
            Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); // make sure it exists.
            StreamWriter output = new StreamWriter(outputPath, false, Encoding.UTF8);

            // Make sure no trailing nulls get written to file.
            while (nOutLen > 0 && outBuffer[nOutLen - 1] == 0)
            {
                nOutLen--;
            }
            string outputString = Encoding.UTF8.GetString(outBuffer, 0, nOutLen);

            output.Write(outputString);
            output.Close();

            // Eradicate depricated PUA characters
            fileHelper.revisePua(outputPath);
        }
Example #2
0
        public void WriteTheModule()
        {
            string about;   // licenseHtml or contents of about.inc in input project directory

            if (String.IsNullOrEmpty(globe.projectOptions.fcbhId))
            {
                return;
            }
            int i;

            if ((globe.projectOptions.languageId.Length < 3) ||
                (globe.projectOptions.translationId.Length < 3))
            {
                return;
            }
            string UsfxPath         = Path.Combine(globe.outputProjectDirectory, "usfx");
            string browserBiblePath = Path.Combine(globe.outputProjectDirectory, "browserBible");
            string browserBibleCssFileName;

            // Using latin.css for ALL projects marked as having a Latin script, regardless of exclusive use of common characters or not per Ken Bitgood 20 April 2016.
            // Reversed 15 Dec 2018 due to problems with NASB display.
            if (globe.projectOptions.commonChars)
            {
                browserBibleCssFileName = "latin.css";
            }
            else
            {
                browserBibleCssFileName = globe.projectOptions.customCssFileName;
            }
            Utils.EnsureDirectory(browserBiblePath);
            if (String.IsNullOrEmpty(globe.projectOptions.fcbhId))
            {
                MessageBox.Show("Missing FCBHID for " + globe.projectOptions.translationId);
                globe.projectOptions.fcbhId = globe.projectOptions.translationId;
            }



            if (!Directory.Exists(UsfxPath))
            {
                MessageBox.Show(UsfxPath + " not found!", "ERROR");
                return;
            }
            if (Directory.Exists(browserBiblePath))
            {   // Delete any old files in this directory.
                Utils.DeleteDirectory(browserBiblePath);
            }
            Utils.EnsureDirectory(browserBiblePath);
            string browserBibleCss = Path.Combine(browserBiblePath, browserBibleCssFileName);

            Utils.DeleteFile(browserBibleCss);
            // Always get the browser Bible CSS file from BibleConv/browserBiblecss/ with the same file name as the current custom CSS file name used for simple HTML.
            string browserBiblecssDir = Path.Combine(globe.dataRootDir, "browserBiblecss");
            string specialCss         = Path.Combine(browserBiblecssDir, browserBibleCssFileName);

            if (File.Exists(specialCss))
            {
                File.Copy(specialCss, browserBibleCss);
            }
            string fallbackCss = Path.Combine(browserBiblecssDir, "fallback.css");

            if (File.Exists(fallbackCss))
            {
                File.Copy(fallbackCss, Path.Combine(browserBiblePath, "fallback.css"));
            }
            string aboutFile = Path.Combine(globe.inputProjectDirectory, "about.inc");

            if (File.Exists(aboutFile))
            {
                StreamReader sr = new StreamReader(aboutFile);
                about = globe.expandPercentEscapes(sr.ReadToEnd());
                sr.Close();
            }
            else
            {
                about = globe.copyrightPermissionsStatement();
            }



            string sqlDir = Path.Combine(globe.outputProjectDirectory, "sql");

            Utils.EnsureDirectory(sqlDir);
            usfx2BrowserBible toBrowserBible;

            toBrowserBible = new usfx2BrowserBible();
            toBrowserBible.projectOptions   = globe.projectOptions;
            toBrowserBible.projectOutputDir = globe.outputProjectDirectory;
            toBrowserBible.stripPictures    = true;
            DateTime srcDate = globe.sourceDate;   // Bring local to avoid potential exception in marshall-by-reference class

            toBrowserBible.indexDateStamp = "This module was generated by <a href='http://eBible.org'>eBible.org</a> on " + DateTime.UtcNow.ToString("d MMM yyyy") +
                                            " from source files dated " + srcDate.ToString("d MMM yyyy" + ".");

            if (!String.IsNullOrEmpty(certified))
            {
                StreamReader sr   = new StreamReader("/home/kahunapule/sync/doc/Electronic Scripture Publishing/ebible_certified_sm.b64");
                string       cert = sr.ReadToEnd();
                sr.Close();
                File.Copy(certified, Path.Combine(browserBiblePath, "eBible.org_certified.jpg"));
                toBrowserBible.indexDateStamp = String.Format("{0}<br /><a href='http://eBible.org/certified/' target='_blank'><img src='data:image/png;base64,{1}'>",
                                                              toBrowserBible.indexDateStamp, cert);
            }
            toBrowserBible.CrossRefToFilePrefixMap = globe.projectOptions.CrossRefToFilePrefixMap;
            string usfxFilePath = Path.Combine(UsfxPath, "usfx.xml");
            string orderFile    = Path.Combine(globe.inputProjectDirectory, "bookorder.txt");

            if (!File.Exists(orderFile))
            {
                orderFile = SFConverter.FindAuxFile("bookorder.txt");
            }
            StringBuilder localNumbers = new StringBuilder("\"numbers\":[");

            for (i = 0; i < 150; i++)
            {
                localNumbers.Append("\"" + fileHelper.LocalizeDigits(i.ToString()) + "\",");
            }
            localNumbers.Append("\"" + fileHelper.LocalizeDigits("150") + "\"],");
            toBrowserBible.country     = globe.projectOptions.country;
            toBrowserBible.countryCode = globe.projectOptions.countryCode;
            toBrowserBible.bookInfo.ReadPublicationOrder(orderFile);
            toBrowserBible.MergeXref(Path.Combine(globe.inputProjectDirectory, "xref.xml"));
            toBrowserBible.sourceLink               = globe.expandPercentEscapes("<a href=\"http://%h/%t\">%v</a>");
            toBrowserBible.textDirection            = globe.projectOptions.textDir;
            toBrowserBible.languageNameInEnglish    = globe.projectOptions.languageNameInEnglish;
            toBrowserBible.languageNameInVernacular = globe.projectOptions.languageName;
            toBrowserBible.traditionalAbbreviation  = globe.projectOptions.translationTraditionalAbbreviation;
            toBrowserBible.englishDescription       = globe.projectOptions.EnglishDescription;
            toBrowserBible.customCssName            = browserBibleCssFileName;
            toBrowserBible.numbers     = localNumbers.ToString();
            toBrowserBible.fcbhAudioNt = globe.projectOptions.fcbhAudioNT;
            toBrowserBible.fcbhAudioOt = globe.projectOptions.fcbhAudioOT;
            toBrowserBible.fcbhDramaNt = globe.projectOptions.fcbhDramaNT;
            toBrowserBible.fcbhDramaOt = globe.projectOptions.fcbhDramaOT;
            toBrowserBible.fcbhPortion = globe.projectOptions.fcbhAudioPortion;
            toBrowserBible.coverName   = Path.GetFileName(globe.preferredCover);
            string coverPath = Path.Combine(browserBiblePath, toBrowserBible.coverName);

            File.Copy(globe.preferredCover, coverPath, true);
            string covertnpng = Path.Combine(browserBiblePath, "covertn.png");
            string covertnb64 = Path.Combine(browserBiblePath, "covertn.b64");

            fileHelper.RunCommand(String.Format("shrinkcover {0} {1} {2}", coverPath, covertnpng, covertnb64));
            toBrowserBible.b64CoverName = covertnb64;
            if ((globe.er != null) && (globe.er.countries != null))
            {
                toBrowserBible.countries = globe.er.countries;
            }
            if (globe.projectOptions.commonChars)
            {
                toBrowserBible.preferredFont = "latin";
            }
            else
            {
                toBrowserBible.preferredFont = globe.projectOptions.fontFamily;
            }
            toBrowserBible.shortTitle  = globe.projectOptions.shortTitle;
            toBrowserBible.fcbhId      = globe.projectOptions.fcbhId;
            toBrowserBible.dialectCode = globe.projectOptions.languageId + globe.projectOptions.dialect + globe.projectOptions.script;
            toBrowserBible.script      = globe.projectOptions.script;
            string sqlTranslationId = globe.projectOptions.translationId.Replace("-", "_");

            toBrowserBible.sqlTableName = sqlTranslationId + "_isvl";
            toBrowserBible.sqlFileName  = Path.Combine(Path.Combine(globe.outputProjectDirectory, "sql"), globe.projectOptions.translationId + "_isvl.sql");
            toBrowserBible.langCodes    = globe.languageCodes;
            toBrowserBible.xrefCall.SetMarkers(globe.projectOptions.xrefCallers);
            toBrowserBible.footNoteCall.SetMarkers(globe.projectOptions.footNoteCallers);
            toBrowserBible.redistributable = globe.projectOptions.redistributable;
            toBrowserBible.projectInputDir = globe.inputProjectDirectory;
            toBrowserBible.ConvertUsfxToHtml(usfxFilePath, browserBiblePath,
                                             globe.projectOptions.vernacularTitle,
                                             globe.projectOptions.languageId,
                                             globe.projectOptions.translationId,
                                             globe.projectOptions.chapterLabel,
                                             globe.projectOptions.psalmLabel,
                                             "<a href='copyright.htm'>" + usfxToHtmlConverter.EscapeHtml(globe.shortCopyrightMessage) + "</a>",
                                             globe.expandPercentEscapes(globe.projectOptions.homeLink),
                                             globe.expandPercentEscapes(globe.projectOptions.footerHtml),
                                             globe.expandPercentEscapes(globe.projectOptions.indexHtml),
                                             about,
                                             globe.projectOptions.ignoreExtras,
                                             globe.projectOptions.goText);
            ci = new CreateIndex();
            ci.MakeJsonIndex(Path.Combine(Path.Combine(globe.outputProjectDirectory, "search"), "verseText.xml"), Path.Combine(browserBiblePath, "index"),
                             Path.Combine(sqlDir, sqlTranslationId + "_conc.sql"));
            ci.MakeLemmaIndex(Path.Combine(Path.Combine(globe.outputProjectDirectory, "search"), "verseText.lemma"), Path.Combine(browserBiblePath, "indexlemma"));
            string fontsDir = Path.Combine(browserBiblePath, "fonts");

            fileHelper.EnsureDirectory(fontsDir);
            string fontSource = Path.Combine(globe.dataRootDir, "fonts");
            string fontName   = globe.projectOptions.fontFamily.ToLower().Replace(' ', '_');

            fileHelper.CopyFile(Path.Combine(fontSource, fontName + ".ttf"), Path.Combine(fontsDir, fontName + ".ttf"));
            fileHelper.CopyFile(Path.Combine(fontSource, fontName + ".woff"), Path.Combine(fontsDir, fontName + ".woff"));
            fileHelper.CopyFile(Path.Combine(fontSource, fontName + ".eot"), Path.Combine(fontsDir, fontName + ".eot"));
            Utils.DeleteFile(covertnpng);
            Utils.DeleteFile(covertnb64);
            Logit.CloseFile();
            if (Logit.loggedError)
            {
                globe.projectOptions.lastRunResult = false;
            }
            if (Logit.loggedWarning)
            {
                globe.projectOptions.warningsFound = true;
            }
        }