<commit_before>﻿using FeintTemplateEngine.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace FeintTemplateEngine.Plugins
{
    class HtmlTagPlugin : TemplatePlugin
    {

        public HtmlTagPlugin(TemplateRenderer renderer)
            : base(renderer)
        {
            Priority = TemplatePriority.Low;
            string tagNamePattern = "(?<tag>[a-zA-Z][a-zA-Z0-9_]*)";
            string attributes = "(\\((?<attr>[a-zA-Z0-9_\\-]+([ ]?=[ ]?(\".*?\")))*[ ]*(,[ ]*(?<attr>[a-zA-Z0-9_\\-]+([ ]?=[ ]?(\".*?\")))*[ ]*)*\\))";
            string idPattern = "(#(?<id>[a-zA-Z_][a-zA-Z_\\-09]*))?";
            string classPattern = "(\\.(?<class>[a-zA-Z_][a-zA-Z_\\-09]*))?";
            base.RegularExpressionPatterns.Add("^" + tagNamePattern + idPattern + classPattern + "( (?<text>[^\\(]*.*)|$)$");
            base.RegularExpressionPatterns.Add("^" + tagNamePattern + classPattern + idPattern + "( (?<text>[^\\(]*.*)|$)$");
            base.RegularExpressionPatterns.Add("^" + tagNamePattern + idPattern + classPattern + attributes + "( ?(?<text>[^\\(]*.*)|$)$");
            base.RegularExpressionPatterns.Add("^" + tagNamePattern + classPattern + idPattern + attributes + "( ?(?<text>[^\\(]*.*)|$)$");
            base.RegularExpressionPatterns.Add("^" + tagNamePattern + classPattern + idPattern +attributes+ "?.[w]*$");
            base.RegularExpressionPatterns.Add("^" + tagNamePattern + idPattern + classPattern + attributes + "?.[w]*$");
            
        }

        public override string RenderTag(string line, TemplateReader reader, Dictionary<string, object> parameters, int patternIndex)
        {
            switch (patternIndex)
            {
                case 0:
                    return renderStandardHtmlTag(line, reader, parameters,patternIndex);
                case 1:
                    return renderStandardHtmlTag(line, reader, parameters, patternIndex);
                case 2:
                    return renderTagWithAttributes(line, reader, parameters, patternIndex);
                case 3:
                    return renderTagWithAttributes(line, reader, parameters, patternIndex);
                case 4:
                    return renderTagWithAttributesWithTextBlock(line, reader, parameters, patternIndex);
                case 5:
                    return renderTagWithAttributesWithTextBlock(line, reader, parameters, patternIndex);
            }
            throw new TemplateException("Can't parse html tag.");
        }
        private string renderStandardHtmlTag(string line, TemplateReader reader, Dictionary<string, object> parameters, int patternIndex)
        {
            Match lineMatch = Regex.Match(line.Trim(), RegularExpressionPatterns[patternIndex]);
            String tag = lineMatch.Groups["tag"].Value;
            String text = "";
            int level = reader.Level;
            if (lineMatch.Groups["text"].Success)
            {
                text = lineMatch.Groups["text"].Value;
            }
            StringBuilder renderedBuilder = new StringBuilder();
            renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent));
            renderedBuilder.Append("<");
            renderedBuilder.Append(tag);
            if (lineMatch.Groups["id"].Success)
            {
                renderedBuilder.Append(" id=\"");
                renderedBuilder.Append(lineMatch.Groups["id"].Value);
                renderedBuilder.Append("\"");
            }
            if (lineMatch.Groups["class"].Success)
            {
                renderedBuilder.Append(" class=\"");
                renderedBuilder.Append(lineMatch.Groups["class"].Value);
                renderedBuilder.Append("\"");
            }
            renderedBuilder.Append(">\n");
            if (text.Length > 0)
            {
                renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent + 1));
                renderedBuilder.Append(text);
                renderedBuilder.Append("\n");
            }
            String sourceBlock = reader.ReadBlock();
            renderer.LineIndent++;
            renderedBuilder.Append(renderer.RenderBlock(sourceBlock));
            renderer.LineIndent--;
            renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent));
            renderedBuilder.Append("</");
            renderedBuilder.Append(tag);
            renderedBuilder.Append(">\n");
            return renderedBuilder.ToString();
        }
        private string renderTagWithAttributes(string line, TemplateReader reader, Dictionary<string, object> parameters, int patternIndex)
        {
            Match lineMatch = Regex.Match(line.Trim(), RegularExpressionPatterns[patternIndex]);
            String tag = lineMatch.Groups["tag"].Value;
            String text = "";
            int level = reader.Level;
            if (lineMatch.Groups["text"].Success)
            {
                text = lineMatch.Groups["text"].Value;
            }
            StringBuilder renderedBuilder = new StringBuilder();
            renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent));
            renderedBuilder.Append("<");
            renderedBuilder.Append(tag);
            if (lineMatch.Groups["id"].Success)
            {
                renderedBuilder.Append(" id=\"");
                renderedBuilder.Append(lineMatch.Groups["id"].Value);
                renderedBuilder.Append("\"");
            }
            if (lineMatch.Groups["class"].Success)
            {
                renderedBuilder.Append(" class=\"");
                renderedBuilder.Append(lineMatch.Groups["class"].Value);
                renderedBuilder.Append("\"");
            }
            foreach (Capture cap in lineMatch.Groups["attr"].Captures)
            {
                renderedBuilder.Append(" ");
                renderedBuilder.Append(cap.Value.Trim());
            }
            renderedBuilder.Append(">\n");
            if (text.Length > 0)
            {
                renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent + 1));
                renderedBuilder.Append(text);
                renderedBuilder.Append("\n");
            }
            
            String sourceBlock = reader.ReadBlock();
            renderer.LineIndent++;
            renderedBuilder.Append(renderer.RenderBlock(sourceBlock));
            renderer.LineIndent--;
            renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent));
            renderedBuilder.Append("</");
            renderedBuilder.Append(tag);
            renderedBuilder.Append(">\n");
            return renderedBuilder.ToString();
        }
        private string renderTagWithAttributesWithTextBlock(string line, TemplateReader reader, Dictionary<string, object> parameters, int patternIndex)
        {
            Match lineMatch = Regex.Match(line.Trim(), RegularExpressionPatterns[patternIndex]);
            String tag = lineMatch.Groups["tag"].Value;
            String text = "";
            int level = reader.Level;
            if (lineMatch.Groups["text"].Success)
            {
                text = lineMatch.Groups["text"].Value;
            }
            StringBuilder renderedBuilder = new StringBuilder();
            renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent));
            renderedBuilder.Append("<");
            renderedBuilder.Append(tag);
            if (lineMatch.Groups["id"].Success)
            {
                renderedBuilder.Append(" id=\"");
                renderedBuilder.Append(lineMatch.Groups["id"].Value);
                renderedBuilder.Append("\"");
            }
            if (lineMatch.Groups["class"].Success)
            {
                renderedBuilder.Append(" class=\"");
                renderedBuilder.Append(lineMatch.Groups["class"].Value);
                renderedBuilder.Append("\"");
            }
            foreach (Capture cap in lineMatch.Groups["attr"].Captures)
            {
                renderedBuilder.Append(" ");
                renderedBuilder.Append(cap.Value.Trim());
            }
            renderedBuilder.Append(">\n");
            if (text.Length > 0)
            {
                renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent + 1));
                renderedBuilder.Append(text);
                renderedBuilder.Append("\n");
            }
            renderer.LineIndent++;
            String sourceBlock = reader.ReadBlockWithIndent(renderer.LineIndent);
            renderedBuilder.Append(sourceBlock);
            renderer.LineIndent--;
            renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent));
            renderedBuilder.Append("</");
            renderedBuilder.Append(tag);
            renderedBuilder.Append(">\n");
            return renderedBuilder.ToString();
        }
       
    }
}<commit_msg>div shortcuts, regexp changed<commit_after>﻿using FeintTemplateEngine.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace FeintTemplateEngine.Plugins
{
    class HtmlTagPlugin : TemplatePlugin
    {

        public HtmlTagPlugin(TemplateRenderer renderer)
            : base(renderer)
        {
            Priority = TemplatePriority.Low;
            string tagNamePattern = "(?<tag>[a-zA-Z][a-zA-Z0-9_]*)";
            string attributes = "(\\((?<attr>[a-zA-Z0-9_\\-]+([ ]?=[ ]?(\".*?\")))*[ ]*(,[ ]*(?<attr>[a-zA-Z0-9_\\-]+([ ]?=[ ]?(\".*?\")))*[ ]*)*\\))";
            string idPattern = "(#(?<id>[a-zA-Z_][a-zA-Z_\\-09]*))";
            string classPattern = "(\\.(?<class>[a-zA-Z_][a-zA-Z_\\-09]*))";
            string textPattern = "( (?<text>[^\\(]*.*)|$)";
            string optional = "?";
            base.RegularExpressionPatterns.Add("^" + tagNamePattern + idPattern + optional + classPattern + optional + textPattern + "$");
            base.RegularExpressionPatterns.Add("^" + tagNamePattern + classPattern + optional + idPattern + optional + textPattern + "$");
            base.RegularExpressionPatterns.Add("^" + tagNamePattern + idPattern + optional + classPattern + optional + attributes + textPattern + "$");
            base.RegularExpressionPatterns.Add("^" + tagNamePattern + classPattern + optional + idPattern + optional + attributes + textPattern + "$");
            base.RegularExpressionPatterns.Add("^" + tagNamePattern + classPattern + optional + idPattern + optional + attributes + "?\\.[w]*$");
            base.RegularExpressionPatterns.Add("^" + tagNamePattern + idPattern + optional + classPattern + optional + attributes + "?\\.[w]*$");
            base.RegularExpressionPatterns.Add("^" + idPattern + classPattern + optional + attributes + optional + textPattern + optional + "$");
            base.RegularExpressionPatterns.Add("^" + classPattern + idPattern + optional + attributes + optional + textPattern + optional + "$");


        }

        public override string RenderTag(string line, TemplateReader reader, Dictionary<string, object> parameters, int patternIndex)
        {
            switch (patternIndex)
            {
                case 0:
                    return renderStandardHtmlTag(line, reader, parameters, patternIndex);
                case 1:
                    return renderStandardHtmlTag(line, reader, parameters, patternIndex);
                case 2:
                    return renderTagWithAttributes(line, reader, parameters, patternIndex);
                case 3:
                    return renderTagWithAttributes(line, reader, parameters, patternIndex);
                case 4:
                    return renderTagWithAttributesWithTextBlock(line, reader, parameters, patternIndex);
                case 5:
                    return renderTagWithAttributesWithTextBlock(line, reader, parameters, patternIndex);
                case 6:
                    return renderShortcutDiv(line, reader, parameters, patternIndex);
                case 7:
                    return renderShortcutDiv(line, reader, parameters, patternIndex);
            }
            throw new TemplateException("Can't parse html tag.");
        }
        private string renderStandardHtmlTag(string line, TemplateReader reader, Dictionary<string, object> parameters, int patternIndex)
        {
            Match lineMatch = Regex.Match(line.Trim(), RegularExpressionPatterns[patternIndex]);
            String tag = lineMatch.Groups["tag"].Value;
            String text = "";
            int level = reader.Level;
            if (lineMatch.Groups["text"].Success)
            {
                text = lineMatch.Groups["text"].Value;
            }
            StringBuilder renderedBuilder = new StringBuilder();
            renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent));
            renderedBuilder.Append("<");
            renderedBuilder.Append(tag);
            if (lineMatch.Groups["id"].Success)
            {
                renderedBuilder.Append(" id=\"");
                renderedBuilder.Append(lineMatch.Groups["id"].Value);
                renderedBuilder.Append("\"");
            }
            if (lineMatch.Groups["class"].Success)
            {
                renderedBuilder.Append(" class=\"");
                renderedBuilder.Append(lineMatch.Groups["class"].Value);
                renderedBuilder.Append("\"");
            }
            renderedBuilder.Append(">\n");
            if (text.Length > 0)
            {
                renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent + 1));
                renderedBuilder.Append(text);
                renderedBuilder.Append("\n");
            }
            String sourceBlock = reader.ReadBlock();
            renderer.LineIndent++;
            renderedBuilder.Append(renderer.RenderBlock(sourceBlock));
            renderer.LineIndent--;
            renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent));
            renderedBuilder.Append("</");
            renderedBuilder.Append(tag);
            renderedBuilder.Append(">\n");
            return renderedBuilder.ToString();
        }
        private string renderTagWithAttributes(string line, TemplateReader reader, Dictionary<string, object> parameters, int patternIndex)
        {
            Match lineMatch = Regex.Match(line.Trim(), RegularExpressionPatterns[patternIndex]);
            String tag = lineMatch.Groups["tag"].Value;
            String text = "";
            int level = reader.Level;
            if (lineMatch.Groups["text"].Success)
            {
                text = lineMatch.Groups["text"].Value;
            }
            StringBuilder renderedBuilder = new StringBuilder();
            renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent));
            renderedBuilder.Append("<");
            renderedBuilder.Append(tag);
            if (lineMatch.Groups["id"].Success)
            {
                renderedBuilder.Append(" id=\"");
                renderedBuilder.Append(lineMatch.Groups["id"].Value);
                renderedBuilder.Append("\"");
            }
            if (lineMatch.Groups["class"].Success)
            {
                renderedBuilder.Append(" class=\"");
                renderedBuilder.Append(lineMatch.Groups["class"].Value);
                renderedBuilder.Append("\"");
            }
            foreach (Capture cap in lineMatch.Groups["attr"].Captures)
            {
                renderedBuilder.Append(" ");
                renderedBuilder.Append(cap.Value.Trim());
            }
            renderedBuilder.Append(">\n");
            if (text.Length > 0)
            {
                renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent + 1));
                renderedBuilder.Append(text);
                renderedBuilder.Append("\n");
            }

            String sourceBlock = reader.ReadBlock();
            renderer.LineIndent++;
            renderedBuilder.Append(renderer.RenderBlock(sourceBlock));
            renderer.LineIndent--;
            renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent));
            renderedBuilder.Append("</");
            renderedBuilder.Append(tag);
            renderedBuilder.Append(">\n");
            return renderedBuilder.ToString();
        }
        private string renderTagWithAttributesWithTextBlock(string line, TemplateReader reader, Dictionary<string, object> parameters, int patternIndex)
        {
            Match lineMatch = Regex.Match(line.Trim(), RegularExpressionPatterns[patternIndex]);
            String tag = lineMatch.Groups["tag"].Value;
            String text = "";
            int level = reader.Level;
            if (lineMatch.Groups["text"].Success)
            {
                text = lineMatch.Groups["text"].Value;
            }
            StringBuilder renderedBuilder = new StringBuilder();
            renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent));
            renderedBuilder.Append("<");
            renderedBuilder.Append(tag);
            if (lineMatch.Groups["id"].Success)
            {
                renderedBuilder.Append(" id=\"");
                renderedBuilder.Append(lineMatch.Groups["id"].Value);
                renderedBuilder.Append("\"");
            }
            if (lineMatch.Groups["class"].Success)
            {
                renderedBuilder.Append(" class=\"");
                renderedBuilder.Append(lineMatch.Groups["class"].Value);
                renderedBuilder.Append("\"");
            }
            foreach (Capture cap in lineMatch.Groups["attr"].Captures)
            {
                renderedBuilder.Append(" ");
                renderedBuilder.Append(cap.Value.Trim());
            }
            renderedBuilder.Append(">\n");
            if (text.Length > 0)
            {
                renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent + 1));
                renderedBuilder.Append(text);
                renderedBuilder.Append("\n");
            }
            renderer.LineIndent++;
            String sourceBlock = reader.ReadBlockWithIndent(renderer.LineIndent);
            renderedBuilder.Append(sourceBlock);
            renderer.LineIndent--;
            renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent));
            renderedBuilder.Append("</");
            renderedBuilder.Append(tag);
            renderedBuilder.Append(">\n");
            return renderedBuilder.ToString();
        }

        private string renderShortcutDiv(string line, TemplateReader reader, Dictionary<string, object> parameters, int patternIndex)
        {
            Match lineMatch = Regex.Match(line.Trim(), RegularExpressionPatterns[patternIndex]);
            String tag = "div";
            String text = "";
            int level = reader.Level;
            if (lineMatch.Groups["text"].Success)
            {
                text = lineMatch.Groups["text"].Value;
            }
            StringBuilder renderedBuilder = new StringBuilder();
            renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent));
            renderedBuilder.Append("<");
            renderedBuilder.Append(tag);
            if (lineMatch.Groups["id"].Success)
            {
                renderedBuilder.Append(" id=\"");
                renderedBuilder.Append(lineMatch.Groups["id"].Value);
                renderedBuilder.Append("\"");
            }
            if (lineMatch.Groups["class"].Success)
            {
                renderedBuilder.Append(" class=\"");
                renderedBuilder.Append(lineMatch.Groups["class"].Value);
                renderedBuilder.Append("\"");
            }
            foreach (Capture cap in lineMatch.Groups["attr"].Captures)
            {
                renderedBuilder.Append(" ");
                renderedBuilder.Append(cap.Value.Trim());
            }
            renderedBuilder.Append(">\n");
            if (text.Length > 0)
            {
                renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent + 1));
                renderedBuilder.Append(text);
                renderedBuilder.Append("\n");
            }

            String sourceBlock = reader.ReadBlock();
            renderer.LineIndent++;
            renderedBuilder.Append(renderer.RenderBlock(sourceBlock));
            renderer.LineIndent--;
            renderedBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent));
            renderedBuilder.Append("</");
            renderedBuilder.Append(tag);
            renderedBuilder.Append(">\n");
            return renderedBuilder.ToString();
        }
    }
}
{'fraction_non_alphanumeric': 0.12721907572224073, 'fraction_numerical': 0.0022845176336204844, 'mean_word_length': 2.02200488997555, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 48, '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': '7649423', 'n_tokens_mistral': 5120, 'n_tokens_neox': 4939, 'n_words': 814}