Exemplo n.º 1
0
        /// <summary>Attempts to find the named tag, returning the global handler if it's found.</summary>
        /// <param name="tag">The tag to look for.</param>
        /// <returns>The global TagHandler if the tag was found; Null otherwise.</returns>
        public static TagHandler GetHandler(string tag)
        {
            TagHandler handler = null;

            Handlers.TryGetValue(tag, out handler);
            return(handler);
        }
Exemplo n.º 2
0
        /// <summary>Adds a handler to the set.
        /// This is generally done automatically, but you can also add it manually if you wish.</summary>
        /// <param name="handler">The handler to add.</param>
        /// <returns>True if adding it was successful.</returns>
        public static bool AddHandler(TagHandler handler)
        {
            // Grab the extension:
            string extension = handler.TagExtension;

            if (extension != null)
            {
                extension = extension.ToLower() + "-";
            }

            string[] tags = handler.GetTags();

            if (tags == null || tags.Length == 0)
            {
                return(false);
            }

            for (int i = 0; i < tags.Length; i++)
            {
                // Grab the tag name:
                string tagName = tags[i].ToLower();

                if (extension != null)
                {
                    // Drop it in at the start.
                    tagName = extension + tagName;
                }

                TagHandler existingHandler;

                // Does it already exist?
                if (Handlers.TryGetValue(tagName, out existingHandler))
                {
                    // Yes - compare priorities:
                    if (handler.Priority < existingHandler.Priority)
                    {
                        // We're trying to override with a lower priority.
                        continue;
                    }
                    else
                    {
                        // override it!
                        Handlers[tagName] = handler;
                    }
                }
                else
                {
                    // Directly add it in:
                    Handlers.Add(tagName, handler);
                }
            }

            return(true);
        }
Exemplo n.º 3
0
        /// <summary>Attempts to find the tag with the given name.
        /// If it's not found, a default tag which is known to exist can be returned instead.
        /// The handler for the found tag is then instanced and the instance is returned.
        /// For example, tag "h1" with a default of "span".</summary>
        /// <param name="tag">The tag to look for.</param>
        /// <param name="defaultTag">If the given tag is not found, this is used instead.</param>
        /// <returns>An instance of the tag handler for the tag. Throws an error if tag or defaultTag are not found.</returns>
        public static TagHandler GetHandler(string tag, string defaultTag)
        {
            TagHandler globalHandler = GetHandler(tag);

            if (globalHandler == null)
            {
                globalHandler = GetHandler(defaultTag);
            }

            return(globalHandler.GetInstance());
        }
		public override void SetTag(string tag){
			base.SetTag(tag);
			// Var is our default tag handler:
			Handler=TagHandlers.GetHandler("lang-"+Tag,"lang-var");
			if(!SelfClosing){
				SelfClosing=Handler.SelfClosing();
			}
			VariableTag varTag=Handler as VariableTag;
			if(varTag!=null){
				varTag.Element=this;
			}
		}
        public override void SetTag(string tag)
        {
            base.SetTag(tag);
            // Var is our default tag handler:
            Handler = TagHandlers.GetHandler("lang-" + Tag, "lang-var");
            if (!SelfClosing)
            {
                SelfClosing = Handler.SelfClosing();
            }
            VariableTag varTag = Handler as VariableTag;

            if (varTag != null)
            {
                varTag.Element = this;
            }
        }
		/// <summary>Adds a handler to the set.
		/// This is generally done automatically, but you can also add it manually if you wish.</summary>
		/// <param name="handler">The handler to add.</param>
		/// <returns>True if adding it was successful.</returns>
		public static bool AddHandler(TagHandler handler){
			
			// Grab the extension:
			string extension=handler.TagExtension;
			
			if(extension!=null){
				extension=extension.ToLower()+"-";
			}
			
			string[] tags=handler.GetTags();
			
			if(tags==null||tags.Length==0){
				return false;
			}
			
			for(int i=0;i<tags.Length;i++){
				// Grab the tag name:
				string tagName=tags[i].ToLower();
				
				if(extension!=null){
					// Drop it in at the start.
					tagName=extension+tagName;
				}
				
				TagHandler existingHandler;
				
				// Does it already exist?
				if(Handlers.TryGetValue(tagName,out existingHandler)){
					// Yes - compare priorities:
					if(handler.Priority<existingHandler.Priority){
						// We're trying to override with a lower priority.
						continue;
					}else{
						// override it!
						Handlers[tagName]=handler;
					}
					
				}else{
					// Directly add it in:
					Handlers.Add(tagName,handler);
				}
			}
			
			return true;
		}
Exemplo n.º 7
0
        /// <summary>Reads the children for this tag from a lexer.</summary>
        /// <param name="lexer"></param>
        /// <param name="innerElement">True if we're looking for the closing tag of this element to exit.
        /// If its found, this method safely returns. Unbalanced tags will otherwise throw an exception.</param>
        /// <param name="literal">Literal is true if the content should be read 'as is', ignoring any tags.</param>
        protected void ReadContent(MLLexer lexer, bool innerElement, bool literal)
        {
            char          Peek              = lexer.Peek();
            string        variableString    = "";
            bool          readingVariable   = false;
            MLTextElement textElement       = null;
            List <string> variableArguments = null;

            System.Text.StringBuilder builder = new System.Text.StringBuilder();

            while (Peek != StringReader.NULL)
            {
                if (readingVariable)
                {
                    lexer.Read();
                    if (Peek == ';')
                    {
                        readingVariable = false;

                        // First, flush textElement if we're working on one (likely):
                        if (textElement != null)
                        {
                            // Make sure it adds the last word:
                            textElement.DoneWord(true);
                            textElement = null;
                        }

                        if (builder.Length != 0)
                        {
                            // The name is in the builder:
                            variableString = builder.ToString();
                            builder.Length = 0;
                        }

                        // Check if this string (e.g. &WelcomeMessage;) is provided by the variable set:
                        // Generate a new variable element:
                        MLVariableElement varElement = CreateVariableElement();
                        varElement.SetVariableName(variableString);
                        if (variableArguments != null)
                        {
                            varElement.SetArguments(variableArguments.ToArray());
                            variableArguments = null;
                        }
                        varElement.LoadNow(innerElement);
                        variableString = "";
                    }
                    else if (Peek == '(')
                    {
                        // Read runtime argument set. &WelcomeMessage('heya!');
                        variableString = builder.ToString();
                        builder.Length = 0;

                        Peek = lexer.Peek();
                        variableArguments = new List <string>();

                        while (Peek != StringReader.NULL)
                        {
                            if (Peek == ')')
                            {
                                // Add it:
                                variableArguments.Add(builder.ToString());
                                builder.Length = 0;

                                // Read it off:
                                lexer.Read();
                                break;
                            }
                            else if (Peek == ',')
                            {
                                // Done one parameter - onto the next.
                                variableArguments.Add(builder.ToString());
                                builder.Length = 0;
                            }
                            else if (Peek == '"' || Peek == '\'')
                            {
                                // One of our args is a "string".
                                // Use the string reader of the PropertyTextReader to read it.
                                PropertyTextReader.ReadString(lexer, builder);
                                // We don't want to read a char off, so continue here.
                                // Peek the next one:
                                Peek = lexer.Peek();
                                continue;
                            }
                            else if (Peek != ' ')
                            {
                                // Generall numeric args will fall down here.
                                // Disallowing spaces means the set can be spaced out like so: (14, 21)
                                builder.Append(Peek);
                            }
                            // Read off the char:
                            lexer.Read();
                            // Peek the next one:
                            Peek = lexer.Peek();
                        }
                    }
                    else
                    {
                        builder.Append(Peek);
                    }
                }
                else if (!literal && Peek == '<')
                {
                    if (textElement != null)
                    {
                        // Make sure it adds the last word:
                        textElement.DoneWord(true);
                        textElement = null;
                    }
                    // Read off the <.
                    lexer.Read();
                    if (lexer.Peek() == '/')
                    {
                        // Should be the closure (</tag>) for 'this' element.

                        // Read off the /:
                        lexer.Read();

                        char closePeek = lexer.Peek();

                        while (closePeek != '>' && closePeek != StringReader.NULL)
                        {
                            builder.Append(closePeek);
                            lexer.Read();
                            closePeek = lexer.Peek();
                        }

                        string tag = builder.ToString();
                        builder.Length = 0;

                        if (closePeek == '>')
                        {
                            // Read it off:
                            lexer.Read();
                        }

                        if (innerElement && (tag == Tag || tag.ToLower() == Tag))
                        {
                            // Closure for this element read off.
                            // Time to get outta here!
                            return;
                        }
                        else
                        {
                            int charNumber;
                            int line = lexer.GetLineNumber(out charNumber);
                            throw new Exception("Unbalanced tags detected: " + tag + " is closing " + Tag + " at line " + line + ", character " + charNumber + ". " + lexer.ReadLine(line) + ".");
                        }
                    }
                    else
                    {
                        MLElement  tag     = CreateTagElement(lexer);
                        TagHandler handler = tag.GetHandler();
                        if (tag.SelfClosing)
                        {
                            tag.OnChildrenLoaded();
                            handler.OnTagLoaded();
                        }
                        else
                        {
                            handler.OnParseContent(lexer);
                            // Read its kids and possibly its ending tag (the true means we're expecting an ending tag):
                            tag.ReadContent(lexer, true, false);
                            tag.OnChildrenLoaded();
                            handler.OnTagLoaded();
                        }
                    }
                }
                else if (!literal && Peek == '&')
                {
                    // E.g. &gt; &amp; etc.
                    readingVariable = true;
                    // Read off the &:
                    lexer.Read();
                }
                else
                {
                    if (textElement == null)
                    {
                        textElement = CreateTextElement();
                    }
                    textElement.AddCharacter(lexer.Read());
                }

                Peek = lexer.Peek();
            }
            if (textElement != null)
            {
                textElement.DoneWord(true);
            }
        }