Beispiel #1
0
        /// <summary>
        /// Replace some tag with a list of sections
        /// </summary>
        /// <param name="sectionName">Name of the section.</param>
        /// <param name="sectionList">The section list.</param>
        /// <param name="separator">The separator.</param>
        public void ReplaceSection(string sectionName, TemplateSectionCollection sectionList, string separator = "")
        {
            // Extraigo la sección que quiero reemplazar
            TemplateSection subSection = ExtractSection(sectionName);

            // Remuevo el content de la sección excepto el tag de inicio.
            _contentBuilder.Remove(subSection.FromContentIndex, subSection.ContentLength + subSection.EndTag.Length);

            // Reemplazo el tag de inicio por el content nuevo
            _contentBuilder.Replace(subSection.BeginTag, sectionList.GetAllContent(separator));
        }
Beispiel #2
0
        /// <summary>
        /// Remove all template tags (with their respective contents).
        /// </summary>
        /// <remarks>
        /// Only calls this methods after all replaces
        /// </remarks>
        public void RemoveInnerSections()
        {
            // Creo la clase para expresiones regulares, el tipo de patron depende del tipo de sección
            Regex regexPatternSection;

            switch (SectionType)
            {
            case TemplateType.ASPX: { regexPatternSection = RegularExpressionsPatterns.RegexTemplateGenericAspxSection; break; }

            case TemplateType.HTML: { regexPatternSection = RegularExpressionsPatterns.RegexTemplateGenericHtmlSection; break; }

            case TemplateType.CS: { regexPatternSection = RegularExpressionsPatterns.RegexTemplateGenericCsSection; break; }

            case TemplateType.SQL: { regexPatternSection = RegularExpressionsPatterns.RegexTemplateGenericSqlSection; break; }

            case TemplateType.CPP: { regexPatternSection = RegularExpressionsPatterns.RegexTemplateGenericCppSection; break; }

            case TemplateType.PHP: { regexPatternSection = RegularExpressionsPatterns.RegexTemplateGenericPhpSection; break; }

            default: { throw new NotSupportedTemplateTypeException(SectionType); }
            }

            // Busco todas las coincidencias del patron en el content de la sección
            MatchCollection matches = regexPatternSection.Matches(Content);

            // Inicializo una lista con las sectionList que contiene el content
            TemplateSectionCollection sectionList = new TemplateSectionCollection();

            foreach (Match match in matches)
            {
                // si es un tag de inicio
                if (match.Value.ToLower().Contains(TemplateTags.SectionBegin))
                {
                    string beginTag     = match.Value;
                    string sectionName  = string.Empty;
                    string variableName = string.Empty;

                    // reviso todos los grupos de la coincidencia, usualmente sería algo asi
                    // 0= <!-- o /*--
                    // 1= begin
                    // 2= section
                    // 3= NOMBRE_SECCION
                    // 4= as
                    // 5= NOMBRE_VARIABLE
                    // 6= --> o */
                    for (int i = 0; i < match.Groups.Count; i++)
                    {
                        Group group = match.Groups[i];

                        // recupero el nombre de la sección, el cual debe venir
                        // después del 'section'
                        if (group.Value.ToLower().Equals(TemplateTags.SectionTag))
                        {
                            sectionName = match.Groups[i + 1].Value;
                        }

                        // recupero el nombre de la variable, el cual debe venir
                        // despés del 'as'
                        else if (group.Value.ToLower().Equals(TemplateTags.SectionAlias))
                        {
                            variableName = match.Groups[i + 1].Value;
                            break;
                        }
                    }

                    // Se encontró una nueva sección, la guardo en la lista
                    TemplateSection seccion = new TemplateSection
                    {
                        BeginTag        = beginTag,
                        SectionVariable = variableName,
                        SectionName     = sectionName
                    };
                    sectionList.AddSection(seccion);
                }

                // o si es un tag de termino
                else if (match.Value.ToLower().Contains(TemplateTags.SectionEnd))
                {
                    string tagFin      = match.Value;
                    string sectionName = string.Empty;

                    // reviso todos los grupos de la coincidencia, debería ser asi
                    // 0= <!-- o /*--
                    // 1= end
                    // 2= section
                    // 3= NOMBRE_SECCION
                    // 4= --> o */
                    for (int i = 0; i < match.Groups.Count; i++)
                    {
                        Group group = match.Groups[i];

                        // recupero el nombre de la sección, el cual debe venir
                        // después del 'section'
                        if (group.Value.ToLower().Equals(TemplateTags.SectionTag))
                        {
                            sectionName = match.Groups[i + 1].Value;
                            break;
                        }
                    }

                    // busco el tag de inicio el cual debería estar en la lista. El tag de inicio debería
                    // haberse ingresado en la lista antes de encontrar el de término. En el caso de que
                    // la encuentre, actualizo la entrada con los datos que faltan.
                    TemplateSection section = sectionList.FindSection(sectionName);

                    if (section != null)
                    {
                        section.EndTag = tagFin;
                    }
                    else
                    {
                        throw new InvalidFormatTagsException(sectionName);
                    }
                }
            }

            // Recorro todas las sectionList que encontré
            foreach (TemplateSection section in sectionList.Sections)
            {
                string cleanText = _contentBuilder.ToString();

                // Verifico que los tags de inicio y término se encuentren en el Content. Esta verificación es para
                // las sectionList que estan contenidas dentro de otras las cuales son eliminadas recursivamente
                if (cleanText.Contains(section.BeginTag) && cleanText.Contains(section.EndTag))
                {
                    // Calculo los indices de inicio y término de los tags (los indices van cambiando a medida que limpio el content)
                    section.BeginTagIndex    = cleanText.IndexOf(section.BeginTag, StringComparison.Ordinal);
                    section.EndTagIndex      = cleanText.IndexOf(section.EndTag, section.BeginTagIndex, StringComparison.Ordinal);
                    section.FromContentIndex = section.BeginTagIndex + section.BeginTag.Length;

                    section.ContentLength = section.EndTagIndex - section.FromContentIndex;

                    // Remuevo la sección del content, desde el primer caracter del tag de inicio hasta el último caracter del tag de término
                    _contentBuilder.Remove(section.BeginTagIndex, section.BeginTag.Length + section.ContentLength + section.EndTag.Length);
                }
            }
        }
Beispiel #3
0
 /// <summary>
 /// Replace some tag with a list of sections
 /// </summary>
 /// <param name="tagName">Tag Name, doesn't need the {} brackets</param>
 /// <param name="sectionList">Section list with all the sections to replace</param>
 /// <param name="isLocalVariable">if set to <c>true</c> [is local variable].</param>
 public void ReplaceTag(string tagName, TemplateSectionCollection sectionList, bool isLocalVariable = true)
 {
     _contentBuilder.Replace(GetReplaceTag(tagName, isLocalVariable), sectionList.GetAllContent());
 }