Web/Templating/TemplateParser.cs

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;

using ComLib;


namespace ComLib.Web.Templating
{
    /// <summary>
    /// Parse to parse html/xml like tags in text.
    /// CAUTION!!! 
    /// This is a non-strict html light-weight implementation, instead of having a full lexical parser for html.
    /// By "non-strict" it does NOT take into account whether the parsed tags are nested inside an html comment or xml cdata
    /// <widget id="2" />
    /// </summary>
    public class TemplateParser
    {
        private string _text;
        private string _tagPrefix;
        private List<Tag> _tags;
        

        /// <summary>
        /// Parser for the tags.
        /// </summary>
        /// <param name="tagname"></param>
        public TemplateParser(string tagPrefix)
        {
            _tagPrefix = tagPrefix;
        }


        /// <summary>
        /// Parses the text and obtains a collection of the tags to parse.
        /// </summary>
        /// <param name="text"></param>
        public IList<Tag> Parse(string text)
        {
            _text = text;
            _tags = new List<Tag>();
            // \<pch\:(?<tag>[\w]+)\s{1}\s*(?<name>[\w]+)=\"(?<val>[\w]+)\"\s*/\>
            string pattern = "\\<" + _tagPrefix + "\\:(?<tag>[\\w]+)\\s{1}\\s*(?<name>[\\w]+)=\\\"(?<val>[\\w]+)\\\"\\s*/\\>";
            var matches = Regex.Matches(_text, pattern, RegexOptions.IgnoreCase);

            if (matches != null && matches.Count > 0)
            {
                foreach (Match match in matches)
                {
                    Tag tag = new Tag();
                    tag.Attributes = new OrderedDictionary();
                    tag.Position = match.Index;
                    tag.Length = match.Length;
                    Group taggroup = match.Groups["tag"];
                    if (tag != null)
                    {
                        tag.Name = taggroup.Value;
后面还有 20 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。