Ejemplo n.º 1
0
        protected virtual void HandleSelectorTokens(List <Token> selectorTokens)
        {
            StringBuilder sb = new StringBuilder(this.newText);

            selectorTokens.Sort(delegate(Token t1, Token t2)
            {
                return(t1.Selector == t2.Selector ? t2.Index.CompareTo(t1.Index) : string.Compare(t1.Selector, t2.Selector));
            });

            foreach (Token token in selectorTokens)
            {
                string        strSelector = token.Selector;
                TokenSelector selector    = TokenSelector.ToTokenSelector(this, strSelector);
                selector.Place(sb, token);
            }

            this.newText = sb.ToString();
        }
Ejemplo n.º 2
0
        public virtual string ApplyTemplate(TemplateTag tpl)
        {
            string output;

            if (this.Selector != null && this.Selector.Length > 0)
            {
                TokenSelector selector = TokenSelector.ToTokenSelector(this.Transformer, this.Selector);
                List <string> list     = selector.Matches(this.Transformer.Text, this);

                StringBuilder sb = new StringBuilder();
                foreach (string match in list)
                {
                    if (match != null && match.Length > 0)
                    {
                        sb.Append(tpl.Apply(match));
                    }
                }
                return(sb.ToString());
            }

            output = this.ManagedTokensToString();
            return(tpl.Apply(output != null && output.Length > 0 ? output : this.Value));
        }
Ejemplo n.º 3
0
        public static TokenSelector ToTokenSelector(ITextTransformer parser, string selector)
        {
            if (IsIdSelector(selector))
            {
                if (!parser.Selectors.ContainsKey(selector))
                {
                    throw new Exception(string.Format("Selector '{0}' is not found in the predefined selectors", selector));
                }

                return parser.Selectors[selector];
            }

            selector = TrimTailingSlash(selector);

            TokenSelector tSelector = new TokenSelector();
            Match match = selectorRegex.Match(selector);

            if (!match.Success)
            {
                throw new Exception(string.Format("Selector regex ({0}) is invalid", selector));
            }

            if (match.Groups["position"].Success)
            {
                tSelector.Position = (SelectorPosition)Enum.Parse(typeof(SelectorPosition), match.Groups["position"].Value, true);
            }

            if (match.Groups["selector"].Success)
            {
                string group = match.Groups["selector"].Value;

                if (IsInteger(group))
                {
                    tSelector.GroupIndex = int.Parse(group);
                }
                else
                {
                    tSelector.GroupName = group;
                }
            }

            if (match.Groups["pseudo"].Success)
            {
                tSelector.Pseudo = match.Groups["pseudo"].Value;

                if (match.Groups["pseudoArg"].Success)
                {
                    tSelector.PseudoArg = match.Groups["pseudoArg"].Value;
                }
            }

            if (match.Groups["flags"].Success)
            {
                tSelector.Flags = BuildFlags(match.Groups["flags"].Value);
            }

            string regexStr;

            if (match.Groups["regex1"].Success)
            {
                regexStr = match.Groups["regex1"].Value;
            }
            else if (match.Groups["regex2"].Success)
            {
                regexStr = match.Groups["regex2"].Value;
            }
            else
            {
                throw new Exception("Regex is not found");
            }

            tSelector.Regex = new Regex(regexStr, tSelector.Flags);

            return tSelector;
        }
Ejemplo n.º 4
0
        public static TokenSelector ToTokenSelector(ITextTransformer parser, string selector)
        {
            if (IsIdSelector(selector))
            {
                if (!parser.Selectors.ContainsKey(selector))
                {
                    throw new Exception(string.Format("Selector '{0}' is not found in the predefined selectors", selector));
                }

                return(parser.Selectors[selector]);
            }

            selector = TrimTailingSlash(selector);

            TokenSelector tSelector = new TokenSelector();
            Match         match     = selectorRegex.Match(selector);

            if (!match.Success)
            {
                throw new Exception(string.Format("Selector regex ({0}) is invalid", selector));
            }

            if (match.Groups["position"].Success)
            {
                tSelector.Position = (SelectorPosition)Enum.Parse(typeof(SelectorPosition), match.Groups["position"].Value, true);
            }

            if (match.Groups["selector"].Success)
            {
                string group = match.Groups["selector"].Value;

                if (IsInteger(group))
                {
                    tSelector.GroupIndex = int.Parse(group);
                }
                else
                {
                    tSelector.GroupName = group;
                }
            }

            if (match.Groups["pseudo"].Success)
            {
                tSelector.Pseudo = match.Groups["pseudo"].Value;

                if (match.Groups["pseudoArg"].Success)
                {
                    tSelector.PseudoArg = match.Groups["pseudoArg"].Value;
                }
            }

            if (match.Groups["flags"].Success)
            {
                tSelector.Flags = BuildFlags(match.Groups["flags"].Value);
            }

            string regexStr;

            if (match.Groups["regex1"].Success)
            {
                regexStr = match.Groups["regex1"].Value;
            }
            else if (match.Groups["regex2"].Success)
            {
                regexStr = match.Groups["regex2"].Value;
            }
            else
            {
                throw new Exception("Regex is not found");
            }

            tSelector.Regex = new Regex(regexStr, tSelector.Flags);

            return(tSelector);
        }