예제 #1
0
        private ParsedTag ParseTag(string token)
        {
            ParsedTag t = new ParsedTag();

            t.TagName = ParseTagName(token);
            ParseTagAttributes(t, token);
            return(t);
        }
예제 #2
0
 private void ProcessTag(ParsedTag tag, Queue <string> contents, StringBuilder sb)
 {
     if (Handlers.ContainsKey(tag.TagName))
     {
         ITagHandler handler = Handlers[tag.TagName];
         if (handler != null)
         {
             string contentsFlat = string.Empty;
             foreach (string s in contents)
             {
                 contentsFlat += s;
             }
             sb.Append(handler.Process(this.MTApp, Handlers, tag, contentsFlat));
         }
     }
 }
예제 #3
0
        private void ParseTagAttributes(ParsedTag t, string input)
        {
            string tagname = ParseTagName(input);
            string temp    = input.TrimStart('<');

            temp = temp.TrimEnd('>');
            temp = temp.TrimEnd('/');
            //Trim off tag name
            temp = temp.Substring(tagname.Length, temp.Length - (tagname.Length));
            temp = temp.Trim();

            bool isParsingAttribute      = false;
            bool isParsingAttributeValue = false;

            TempAttr currentAttribute = null;

            // loop through all characters, splitting of attributes
            char[] characters = temp.ToCharArray();
            for (int i = 0; i < temp.Length; i++)
            {
                char current = temp[i];

                if (isParsingAttribute)
                {
                    if (isParsingAttributeValue)
                    {
                        // append the current character
                        currentAttribute.Value += current;

                        // check to see if we're done with the attribute
                        if (currentAttribute.Value.Length >= 2)
                        {
                            if (currentAttribute.Value.EndsWith("\""))
                            {
                                isParsingAttributeValue = false;
                                isParsingAttribute      = false;

                                currentAttribute.Value = currentAttribute.Value.TrimStart('"');
                                currentAttribute.Value = currentAttribute.Value.TrimEnd('"');

                                // Add or overwrite value
                                t.SetSafeAttribute(currentAttribute.Name, currentAttribute.Value);

                                currentAttribute = null;
                            }
                        }
                    }
                    else
                    {
                        // we're not parsing the value yet so check for "="
                        if (current == '=')
                        {
                            // skip this charater but enable attribute value parsing;
                            isParsingAttributeValue = true;
                        }
                        else
                        {
                            currentAttribute.Name += current;
                        }
                    }
                }
                else
                {
                    // not parsing right now, check to see if we need to start
                    if (!char.IsWhiteSpace(current))
                    {
                        // not white space so let's start our attribute name
                        currentAttribute   = new TempAttr(current.ToString(), "");
                        isParsingAttribute = true;
                    }
                }
            }
        }
예제 #4
0
        private void ProcessTemplate(Queue <string> tokens, StringBuilder sb)
        {
            string tagStarter = "<";

            bool           parsingTag = false;
            Queue <string> subqueue   = new Queue <string>();
            string         startToken = string.Empty;

            while (tokens.Count > 0)
            {
                string currentToken = tokens.Dequeue();

                if (parsingTag)
                {
                    if (IsClosingTag(currentToken, ParseTagName(startToken)))
                    {
                        // Yes, this tag closed the starting tag
                        ParsedTag t = ParseTag(startToken);
                        ProcessTag(t, subqueue, sb);

                        // reset everything since the tag is parsed below
                        parsingTag = false;
                        startToken = "";
                        subqueue   = new Queue <string>();
                    }
                    else
                    {
                        // Nope, no closing yet, just enqueue the token
                        subqueue.Enqueue(currentToken);
                    }
                }
                else
                {
                    // we're not parsing, check for a tag start
                    if (currentToken.StartsWith(tagStarter))
                    {
                        if (IsAcceptedTag(currentToken))
                        {
                            parsingTag = true;

                            if (IsSelfClosed(currentToken))
                            {
                                // Tag is self closed, just parse it
                                ParsedTag t2 = ParseTag(currentToken);
                                ProcessTag(t2, subqueue, sb);

                                parsingTag = false;
                            }
                            else
                            {
                                // store the starting token for later parsing
                                startToken = currentToken;
                            }
                        }
                        else
                        {
                            // not an accepted tag, just dump the sucker
                            sb.Append(currentToken);
                        }
                    }
                    else
                    {
                        // not starting a tag, just dump the output
                        sb.Append(currentToken);
                    }
                }
            }


            if (parsingTag)
            {
                if (startToken.Length > 0)
                {
                    sb.Append(startToken);
                }
            }

            // if the subque has items in it because we didn't find a closing tag, dump them
            if (subqueue.Count > 0)
            {
                while (subqueue.Count > 0)
                {
                    string subqueuetoken = subqueue.Dequeue();
                    sb.Append(subqueuetoken);
                }
            }
        }