<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using Famoser.FexCompiler.Enum;
using Famoser.FexCompiler.Models;
using Famoser.FexCompiler.Models.TextRepresentation;
using Famoser.FexCompiler.Models.TextRepresentation.Base;

namespace Famoser.FexCompiler.Helpers
{
    public class LatexHelper
    {
        public static string CreateLatex(Document document)
        {
            var path = PathHelper.GetAssemblyPath("Templates/template_Article.tex");
            var template = File.ReadAllText(path);

            template = template.Replace("TITLE", document.Title);
            template = template.Replace("AUTHOR", document.Author);
            template = template.Replace("GENERATED_AT", DateTime.Now.ToLongDateString());

            var content = ToLatex(document.Content, 0);

            template = template.Replace("CONTENT", content);
            return template;
        }

        private static string ToLatex(List<Content> contentList, int level)
        {
            var res = "";
            var lastWasParagraph = false;
            foreach (var content in contentList)
            {
                if (content is Paragraph)
                {
                    res += ToLatex((Paragraph)content, lastWasParagraph);
                    lastWasParagraph = true;
                }
                else
                {
                    lastWasParagraph = false;
                    if (content is Section)
                    {
                        res += ToLatex((Section)content, level);
                    }
                    else if (content is Code)
                    {
                        res += ToLatex((Code)content);
                    }
                }
            }
            return res;
        }

        private static string ToLatex(Section section, int level)
        {
            var sectionName = "section";
            if (level == 1)
                sectionName = "sub" + sectionName;
            else if (level == 2)
                sectionName = "subsub" + sectionName;
            else if (level == 3)
                sectionName = "paragraph";
            else if (level == 4)
                sectionName = "subparagraph";
            else if (level > 4)
                sectionName = "subsubparagraph";
            var content = "";
            content += "\\" + sectionName + "{" + ToLatex(section.Title, false) + " }\n";
            content += ToLatex(section.Content, level + 1);
            return content;
        }

        private static string ToLatex(Paragraph paragraph, bool hasParagraphBefore)
        {
            var content = "";
            if (paragraph.IsEnumeration)
            {
                content += "\\begin{itemize}\n";
                content += ToLatex(paragraph.LineNodes, "\\item ");
                content += "\\end{itemize}\n";
            }
            else
            {
                if (paragraph.VerticalSpaceBefore && hasParagraphBefore)
                {
                    content += "\\vspace{5pt}";
                }
                content += ToLatex(paragraph.LineNodes, "");
                content += "\n"; //\\* 
            }
            return content;
        }

        private static string ToLatex(List<LineNode> lines, string prefix)
        {
            var content = "";
            foreach (var textNode in lines)
            {
                content += prefix + ToLatex(textNode, true) + "\n";
            }
            return content;
        }

        private static string ToLatex(LineNode line, bool allowNewline)
        {
            var content = ToLatex(line.TextNodes);
            if (allowNewline)
                content += "\n";
            return content;
        }

        private static string ToLatex(TextNode textNode)
        {
            var content = "";
            if (textNode.TextType == TextType.Normal)
            {
                content += EscapeText(textNode.Text);
            }
            else if (textNode.TextType == TextType.Bold)
            {
                content += "\\textbf{" + EscapeText(textNode.Text) + "}";
            }
            content += " ";
            return content;
        }

        private static string ToLatex(List<TextNode> nodes)
        {
            var content = "";
            foreach (var textNode in nodes)
            {
                content += ToLatex(textNode);
            }
            return content;
        }

        private static string EscapeText(string text)
        {
            /*
             * & %  # _ { } ~ ^ \
             * */
            var replaces = new Dictionary<string, string>()
            {
                {"\\", "\\textbackslash "},
                {"→", "->" },
                {"∙", "*" },
                {"α", "\\textalpha " },
                {"β", "\\textbeta " },
                {"σ", "\\textsigma " },
                {"~", "\\textasciitilde "},
                {"^", "\\textasciicircum "},
                {">", "\\textgreater "},
                {"<", "\\textless "},
                {"*", "\\textasteriskcentered "},
                {"|", "\\textbar "},
                {"{", "\\textbraceleft "},
                {"}", "\\textbraceright "},
                {"$", "\\textdollar "},
                {"—", "\\textemdash "},<commit_msg>paragraph newline fix<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using Famoser.FexCompiler.Enum;
using Famoser.FexCompiler.Models;
using Famoser.FexCompiler.Models.TextRepresentation;
using Famoser.FexCompiler.Models.TextRepresentation.Base;

namespace Famoser.FexCompiler.Helpers
{
    public class LatexHelper
    {
        public static string CreateLatex(Document document)
        {
            var path = PathHelper.GetAssemblyPath("Templates/template_Article.tex");
            var template = File.ReadAllText(path);

            template = template.Replace("TITLE", document.Title);
            template = template.Replace("AUTHOR", document.Author);
            template = template.Replace("GENERATED_AT", DateTime.Now.ToLongDateString());

            var content = ToLatex(document.Content, 0);

            template = template.Replace("CONTENT", content);
            return template;
        }

        private static string ToLatex(List<Content> contentList, int level)
        {
            var res = "";
            var lastWasParagraph = false;
            foreach (var content in contentList)
            {
                if (content is Paragraph)
                {
                    res += ToLatex((Paragraph)content, lastWasParagraph);
                    lastWasParagraph = true;
                }
                else
                {
                    lastWasParagraph = false;
                    if (content is Section)
                    {
                        res += ToLatex((Section)content, level);
                    }
                    else if (content is Code)
                    {
                        res += ToLatex((Code)content);
                    }
                }
            }
            return res;
        }

        private static string ToLatex(Section section, int level)
        {
            var sectionName = "section";
            if (level == 1)
                sectionName = "sub" + sectionName;
            else if (level == 2)
                sectionName = "subsub" + sectionName;
            else if (level == 3)
                sectionName = "paragraph";
            else if (level == 4)
                sectionName = "subparagraph";
            else if (level > 4)
                sectionName = "subsubparagraph";
            var content = "";
            content += "\\" + sectionName + "{" + ToLatex(section.Title, false) + " }\n";
            if (level >= 3)
            {
                //force line break after paragraph
                content += "\\mbox{}\\\\\\vspace{3pt}";
            }
            content += ToLatex(section.Content, level + 1);
            return content;
        }

        private static string ToLatex(Paragraph paragraph, bool hasParagraphBefore)
        {
            var content = "";
            if (paragraph.IsEnumeration)
            {
                content += "\\begin{itemize}\n";
                content += ToLatex(paragraph.LineNodes, "\\item ");
                content += "\\end{itemize}\n";
            }
            else
            {
                if (paragraph.VerticalSpaceBefore && hasParagraphBefore)
                {
                    content += "\\vspace{5pt}";
                }
                content += ToLatex(paragraph.LineNodes, "");
                content += "\n"; //\\* 
            }
            return content;
        }

        private static string ToLatex(List<LineNode> lines, string prefix)
        {
            var content = "";
            foreach (var textNode in lines)
            {
                content += prefix + ToLatex(textNode, true) + "\n";
            }
            return content;
        }

        private static string ToLatex(LineNode line, bool allowNewline)
        {
            var content = ToLatex(line.TextNodes);
            if (allowNewline)
                content += "\n";
            return content;
        }

        private static string ToLatex(TextNode textNode)
        {
            var content = "";
            if (textNode.TextType == TextType.Normal)
            {
                content += EscapeText(textNode.Text);
            }
            else if (textNode.TextType == TextType.Bold)
            {
                content += "\\textbf{" + EscapeText(textNode.Text) + "}";
            }
            content += " ";
            return content;
        }

        private static string ToLatex(List<TextNode> nodes)
        {
            var content = "";
            foreach (var textNode in nodes)
            {
                content += ToLatex(textNode);
            }
            return content;
        }

        private static string EscapeText(string text)
        {
            /*
             * & %  # _ { } ~ ^ \
             * */
            var replaces = new Dictionary<string, string>()
            {
                {"\\", "\\textbackslash "},
                {"→", "->" },
                {"∙", "*" },
                {"α", "\\textalpha " },
                {"β", "\\textbeta " },
                {"σ", "\\textsigma " },
                {"~", "\\textasciitilde "},
                {"^", "\\textasciicircum "},
                {">", "\\textgreater "},
                {"<", "\\textless "},
                {"*", "\\textasteriskcentered  "},
                {"|", "\\textbar "},
                {"{", "\\textbraceleft "},
                {"}", "\\textbraceright "},
                {"$", "\\textdollar "},
                {"—", "\\textemdash "},
{'fraction_non_alphanumeric': 0.11955317577548005, 'fraction_numerical': 0.0016617429837518464, 'mean_word_length': 1.2400744416873448, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7577504', 'n_tokens_mistral': 2842, 'n_tokens_neox': 2617, 'n_words': 576}