コード例 #1
0
ファイル: MetaDataFiller.cs プロジェクト: richiejp/NHaml
        /// <summary>
        /// Fills the page/control/master metadata using default values and returns the node that contains the main metadata
        /// </summary>
        /// <param name="metadata">The metadata container of the document</param>
        /// <returns>The page declaration metadata container</returns>
        public static MetaNode FillAndGetPageDefinition(Dictionary<string, List<MetaNode>> metadata, TemplateOptions options)
        {
            MetaNode pagedefiniton = null;
            List<MetaNode> data;

            if (metadata.ContainsKey("contentplaceholder"))
            {
                pagedefiniton = new MetaNode("master");
            }
            else
            {
                pagedefiniton = new MetaNode("page");
            }

            if (metadata.TryGetValue("page", out data))
            {
                pagedefiniton = data[0];
            }
            if (metadata.TryGetValue("control", out data))
            {
                pagedefiniton = data[0];
            }
            if (metadata.TryGetValue("master", out data))
            {
                pagedefiniton = data[0];
            }

            if (pagedefiniton.Attributes.Find(x => x.Name == "Language") == null)
            {
                pagedefiniton.Attributes.Add(new AttributeNode("Language") { Value = new TextNode(new TextChunk("C#")) });
            }

            if (pagedefiniton.Attributes.Find(x => x.Name == "AutoEventWireup") == null)
            {
                pagedefiniton.Attributes.Add(new AttributeNode("AutoEventWireup") { Value = new TextNode(new TextChunk("true")) });
            }

            if (!metadata.ContainsKey(pagedefiniton.Name))
            {
                metadata[pagedefiniton.Name] = new List<MetaNode> { pagedefiniton };
            }

            return pagedefiniton;
        }
コード例 #2
0
ファイル: WebFormsVisitor.cs プロジェクト: richiejp/NHaml
 public override void Visit(MetaNode node)
 {
     if (node.Name == "contentplaceholder")
     {
         bool hasChild = node.Child != null;
         WriteText(String.Format("<asp:ContentPlaceHolder ID=\"{0}\" runat=\"server\"", node.Value));
         if (hasChild)
         {
             WriteText(">");
             Visit(node.Child);
             WriteText("</asp:ContentPlaceHolder>");
         }
         else
         {
             WriteText(" />");
         }
     }
     else if (node.Name == "partialcontent")
     {
         string obj = null;
         foreach (var attr in node.Attributes)
         {
             if (attr.Name == "model")
             {
                 obj = ((TextChunk)(((TextNode)attr.Value).Chunks[0])).Text;
             }
         }
         WriteCode(_partialMethod.RenderMethod(node.Value, obj).ToString(), false);
     }
     else if (node.Name == "content")
     {
         WriteText(String.Format("<asp:Content ID=\"{0}\" runat=\"server\">", node.Value));
         if (node.Child!=null)
             Visit(node.Child);
         WriteText(Environment.NewLine);
         WriteIndent();
         WriteText("</asp:Content>");
     }
     else
     {
         base.Visit(node);
     }
 }
コード例 #3
0
ファイル: CodeDomVisitor.cs プロジェクト: richiejp/NHaml
        public override void Visit(MetaNode node)
        {
            PopString();
            if (node.Name == "contentplaceholder")
            {
                bool hasChild = node.Child != null;
                var childInvoke = new CodeMethodInvokeExpression
                {
                    Method = new CodeMethodReferenceExpression
                    {
                        MethodName = "RunContent",
                        TargetObject = new CodePropertyReferenceExpression
                        {
                            PropertyName = "Child",
                            TargetObject = new CodeThisReferenceExpression()
                        }
                    }
                };
                childInvoke.Parameters.Add(new CodeVariableReferenceExpression { VariableName = _writerName });
                childInvoke.Parameters.Add(new CodePrimitiveExpression { Value = node.Value });

                if (hasChild)
                {
                    var baseInvoke = new CodeMethodInvokeExpression
                    {
                        Method = new CodeMethodReferenceExpression
                        {
                            MethodName = "RunContent",
                            TargetObject = new CodeThisReferenceExpression()
                        }
                    };
                    baseInvoke.Parameters.Add(new CodeVariableReferenceExpression { VariableName = _writerName });
                    baseInvoke.Parameters.Add(new CodePrimitiveExpression { Value = node.Value });

                    var ifStatement = new CodeConditionStatement(
                            new CodeMethodInvokeExpression(
                                new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "Child"),
                                "ContainsContent",
                                new CodePrimitiveExpression(node.Value)
                            )
                        );
                    ifStatement.TrueStatements.Add(childInvoke);
                    ifStatement.FalseStatements.Add(baseInvoke);

                    var safeIfInvoke = new CodeConditionStatement(
                        new CodeBinaryOperatorExpression(
                            new CodePropertyReferenceExpression(new CodeThisReferenceExpression(),"Child"),
                            CodeBinaryOperatorType.IdentityEquality,
                            new CodePrimitiveExpression(null)
                        ),
                        new CodeStatement[] {
                            new CodeThrowExceptionStatement(
                                new CodeObjectCreateExpression(
                                    new CodeTypeReference(typeof(InvalidOperationException)),
                                    new CodePrimitiveExpression("Child is missing")
                                )
                            )},
                        new CodeStatement[] { ifStatement }
                    );

                    _actualCode.Statements.Add(safeIfInvoke);
                    WriteText(System.Environment.NewLine);

                    string oldMethod = "Main";
                    foreach (var pair in _methods)
                    {
                        if (pair.Value == _actualCode)
                            oldMethod = pair.Key;
                    }
                    _actualCode = CreateNewMethod(node.Value);
                    if (node.Child != null)
                        VisitAndIdentAlways(node.Child,true);
                    PopString();
                    _actualCode = _methods[oldMethod];
                }
                else
                {
                    var safeChildInvoke = new CodeConditionStatement(
                        new CodeBinaryOperatorExpression(
                            new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "Child"),
                            CodeBinaryOperatorType.IdentityEquality,
                            new CodePrimitiveExpression(null)
                        ),
                        new CodeStatement[] {
                            new CodeThrowExceptionStatement(
                                new CodeObjectCreateExpression(
                                    new CodeTypeReference(typeof(InvalidOperationException)),
                                    new CodePrimitiveExpression("Child is missing")
                                )
                            )},
                        new CodeStatement[] { new CodeExpressionStatement(childInvoke) }
                    );
                    _actualCode.Statements.Add(safeChildInvoke);
                    WriteText(System.Environment.NewLine);
                }
            }
            else if (node.Name == "partialcontent")
            {
                string obj = null;
                foreach (var attr in node.Attributes)
                {
                    if (attr.Name == "model")
                    {
                        obj = ((TextChunk)(((TextNode)attr.Value).Chunks[0])).Text;
                    }
                }
                RenderPartial(node.Value,obj);
            }
            else if (node.Name == "content")
            {
                string oldMethod = "Main";
                foreach (var pair in _methods) {
                    if (pair.Value == _actualCode)
                        oldMethod = pair.Key;
                }
                _actualCode = CreateNewMethod(node.Value);
                if (node.Child != null)
                    VisitAndIdentAlways(node.Child,true);
                PopString();
                _actualCode = _methods[oldMethod];
            }
            else
            {
                base.Visit(node);
            }
        }
コード例 #4
0
ファイル: NodeVisitorBase.cs プロジェクト: richiejp/NHaml
 public virtual void Visit(MetaNode node)
 {
     if (node.Child != null)
         Visit(node.Child);
 }
コード例 #5
0
ファイル: MetaMarkupRule.cs プロジェクト: richiejp/NHaml
        public override AstNode Process(ParserReader parser)
        {
            var reader = parser.Input;
            var attributeParser = new AttributeParser(parser);
            var baseIndent = parser.Indent;

            bool isPartial = false;

            if (!reader.Read())
                return null;

            string name;
            if (reader.CurrentChar == '_')
            {
                reader.Skip("_");
                name = "contentplaceholder";
                isPartial = true;
            }
            else
            {
                reader.Skip("@");
                name = reader.ReadName();
                isPartial = false;
            }
            var node = new MetaNode(name);

            reader.SkipWhiteSpaces();

            if (isPartial && (reader.CurrentChar != null))
            {
                node.Name = "partialcontent";
                node.Value = reader.ReadWhile(c => !Char.IsWhiteSpace(c) && c != '(' && c != '{');
            }
            else
            {
                node.Value = "Main";
            }

            while (!reader.IsEndOfStream)
            {
                switch (reader.CurrentChar)
                {
                    case '=':
                        {
                            if (!isPartial)
                            {
                                reader.Skip("=");
                                reader.SkipWhiteSpaces();
                                switch (reader.CurrentChar)
                                {
                                    case '\'':
                                        reader.Skip("'");
                                        node.Value = reader.ReadWhile(c => c != '\'');
                                        reader.Skip("'");
                                        break;
                                    case '"':
                                        reader.Skip("\"");
                                        node.Value = reader.ReadWhile(c => c != '"');
                                        reader.Skip("\"");
                                        break;
                                    default:
                                        node.Value = reader.ReadWhile(c => !Char.IsWhiteSpace(c) && c != '(' && c != '{');
                                        break;
                                }
                            }
                            else
                            {
                                throw new ParserException(reader, "'=' is not allowed in partial definitions");
                            }
                            break;
                        }
                    case '(':
                        {
                            node.Attributes.AddRange(attributeParser.ParseHtmlStyle(false));
                            break;
                        }
                    case '{':
                        {
                            node.Attributes.AddRange(attributeParser.ParseRubyStyle(false));
                            break;
                        }
                    default:
                        {
                            var index = reader.Index;
                            var text = reader.ReadToEndMultiLine();
                            node.Child = parser.ParseText(text.TrimStart(), index);
                            break;
                        }
                }
                reader.SkipWhiteSpaces();
            }

            if (reader.NextLine != null && reader.NextLine.Indent > reader.CurrentLine.Indent)
            {
                node.Child = parser.ParseChildren(baseIndent, node.Child);
            }

            node.EndInfo = reader.SourceInfo;
            if (!_node.Metadata.ContainsKey(name)) _node.Metadata[name] = new List<MetaNode>();
            _node.Metadata[name].Add(node);
            return node;
        }