Exemple #1
0
        private IEnumerable <(bool success, string imgHtmlOrError, string originalHtml)> GenerateImages(
            LaTeXTag tag,
            MatchCollection matches)
        {
            List <(bool, string, string)> ret = new List <(bool, string, string)>();

            foreach (Match match in matches)
            {
                string originalHtml = match.Groups[0].Value;
                string latexCode    = match.Groups[1].Value;

                try
                {
                    latexCode = LaTeXUtils.PlainText(latexCode);

                    var(success, imgHtmlOrError) = LaTeXUtils.GenerateDviFile(Config,
                                                                              tag,
                                                                              latexCode);

                    if (success == false)
                    {
                        ret.Add((false, imgHtmlOrError, originalHtml));
                        continue;
                    }

                    (success, imgHtmlOrError) = LaTeXUtils.GenerateImgFile(Config);

                    if (success && string.IsNullOrWhiteSpace(imgHtmlOrError))
                    {
                        ret.Add((false,
                                 "An unknown error occured, make sure your TeX installation has all the required packages, "
                                 + "or set it to install missing packages on-the-fly",
                                 originalHtml));
                    }

                    imgHtmlOrError = GenerateImgHtml(imgHtmlOrError,
                                                     tag.SurroundTexWith(latexCode));

                    ret.Add((success, imgHtmlOrError, originalHtml));
                }
                catch (Exception ex)
                {
                    ret.Add((false, ex.Message, originalHtml));
                }
            }

            return(ret);
        }
        public static (bool success, string pathOrError) GenerateDviFile(LaTeXCfg config,
                                                                         LaTeXTag tag,
                                                                         string latexContent)
        {
            if (File.Exists(LaTeXConst.Paths.TempTexFilePath))
            {
                File.Delete(LaTeXConst.Paths.TempTexFilePath);
            }

            if (File.Exists(LaTeXConst.Paths.TempDviFilePath))
            {
                File.Delete(LaTeXConst.Paths.TempDviFilePath);
            }

            latexContent = tag.LaTeXBegin + latexContent + tag.LaTeXEnd;

            File.WriteAllText(LaTeXConst.Paths.TempTexFilePath,
                              latexContent);
            var(success, output) = Execute(config.DviGenerationCmd, config.ExecutionTimeout);

            return(success, success ? LaTeXConst.Paths.TempDviFilePath : output);
        }
        public static (bool success, string pathOrError) GenerateDviFile(LaTeXCfg config,
                                                                         LaTeXTag tag,
                                                                         string latexContent)
        {
            // Make sure to delete existing .tex, .dvi or .log file to start with a clean slate
            if (File.Exists(LaTeXConst.Paths.TempTexFilePath))
            {
                File.Delete(LaTeXConst.Paths.TempTexFilePath);
            }

            if (File.Exists(LaTeXConst.Paths.TempDviFilePath))
            {
                File.Delete(LaTeXConst.Paths.TempDviFilePath);
            }

            if (File.Exists(LaTeXConst.Paths.TexErrorLog))
            {
                File.Delete(LaTeXConst.Paths.TexErrorLog);
            }

            // Build .tex file content and write it
            latexContent = tag.LaTeXBegin + latexContent + tag.LaTeXEnd;

            File.WriteAllText(LaTeXConst.Paths.TempTexFilePath, latexContent);

            // Log for debugging purpose
            LogTo.Verbose(
                "Wrote TeX file {TempTexFilePath}:\r\n\r\n-------------------------------------------------\r\n{LatexContent}\r\n-------------------------------------------------\r\n",
                LaTeXConst.Paths.TempTexFilePath, latexContent);

            // Build the .dvi from the .tex file using dvi2png or another processor depending on user's config
            var(success, output) = Execute(config.DviGenerationCmd, config.ExecutionTimeout);

            // Log for debugging purpose
            LogTeXLogs();

            return(success, success ? LaTeXConst.Paths.TempDviFilePath : output);
        }