コード例 #1
0
ファイル: Include.cs プロジェクト: zakachun/ThinkComposer
        public override void Render(Context context, IndentationTextWriter result)
        {
            IFileSystem fileSystem = context.Registers["file_system"] as IFileSystem ?? Template.FileSystem;
            string      source     = fileSystem.ReadTemplateFile(context, _templateName);
            Template    partial    = Template.Parse(source);

            string shortenedTemplateName = _templateName.Substring(1, _templateName.Length - 2);
            object variable = context[_variableName ?? shortenedTemplateName];

            context.Stack(() =>
            {
                foreach (var keyValue in _attributes)
                {
                    context[keyValue.Key] = context[keyValue.Value];
                }

                if (variable is IEnumerable)
                {
                    ((IEnumerable)variable).Cast <object>().ToList().ForEach(v =>
                    {
                        context[shortenedTemplateName] = v;
                        partial.Render(result, RenderParameters.FromContext(context));
                    });
                    return;
                }

                context[shortenedTemplateName] = variable;
                partial.Render(result, RenderParameters.FromContext(context));
            });
        }
コード例 #2
0
ファイル: Capture.cs プロジェクト: zakachun/ThinkComposer
 public override void Render(Context context, IndentationTextWriter result)
 {
     using (var temp = IndentationTextWriter.Create())
     {
         base.Render(context, temp);
         context.Scopes.Last()[_to] = temp.ToString();
     }
 }
コード例 #3
0
        public override void Render(Context context, IndentationTextWriter result)
        {
            BlockRenderState blockState = BlockRenderState.Find(context);

            context.Stack(() =>
            {
                context["block"] = new BlockDrop(this, result);
                RenderAll(GetNodeList(blockState), context, result);
            });
        }
コード例 #4
0
ファイル: For.cs プロジェクト: zakachun/ThinkComposer
        public override void Render(Context context, IndentationTextWriter result)
        {
            context.Registers["for"] = context.Registers["for"] ?? new Hash(0);

            object collection = context[_collectionName];

            if (!(collection is IEnumerable))
            {
                return;
            }

            int from = (_attributes.ContainsKey("offset"))
                                ? (_attributes["offset"] == "continue")
                                        ? Convert.ToInt32(context.Registers.Get <Hash>("for")[_name])
                                        : Convert.ToInt32(context[_attributes["offset"]])
                                : 0;

            int?limit = _attributes.ContainsKey("limit") ? context[_attributes["limit"]] as int? : null;
            int?to    = (limit != null) ? (int?)(limit.Value + from) : null;

            List <object> segment = SliceCollectionUsingEach((IEnumerable)collection, from, to);

            if (!segment.Any())
            {
                return;
            }

            if (_reversed)
            {
                segment.Reverse();
            }

            int length = segment.Count;

            // Store our progress through the collection for the continue flag
            context.Registers.Get <Hash>("for")[_name] = from + length;

            context.Stack(() => segment.EachWithIndex((item, index) =>
            {
                context[_variableName] = item;
                context["forloop"]     = Hash.FromAnonymousObject(new
                {
                    name    = _name,
                    length  = length,
                    index   = index + 1,
                    index0  = index,
                    rindex  = length - index,
                    rindex0 = length - index - 1,
                    first   = (index == 0),
                    last    = (index == length - 1)
                });
                RenderAll(NodeList, context, result);
            }));
        }
コード例 #5
0
        public void CallSuper(Context context, IndentationTextWriter result)
        {
            BlockRenderState blockState = BlockRenderState.Find(context);
            Block            parent;

            if (blockState != null &&
                blockState.Parents.TryGetValue(this, out parent) &&
                parent != null)
            {
                parent.Render(context, result);
            }
        }
コード例 #6
0
ファイル: If.cs プロジェクト: zakachun/ThinkComposer
 public override void Render(Context context, IndentationTextWriter result)
 {
     context.Stack(() =>
     {
         foreach (Condition block in Blocks)
         {
             if (block.Evaluate(context))
             {
                 RenderAll(block.Attachment, context, result);
                 return;
             }
         }
         ;
     });
 }
コード例 #7
0
ファイル: Cycle.cs プロジェクト: zakachun/ThinkComposer
        public override void Render(Context context, IndentationTextWriter result)
        {
            context.Registers["cycle"] = context.Registers["cycle"] ?? new Hash(0);

            context.Stack(() =>
            {
                string key    = context[_name].ToString();
                int iteration = (int)(((Hash)context.Registers["cycle"])[key] ?? 0);
                result.Write(context[_variables[iteration]].ToString());
                ++iteration;
                if (iteration >= _variables.Length)
                {
                    iteration = 0;
                }
                ((Hash)context.Registers["cycle"])[key] = iteration;
            });
        }
コード例 #8
0
ファイル: IfChanged.cs プロジェクト: zakachun/ThinkComposer
        public override void Render(Context context, IndentationTextWriter result)
        {
            context.Stack(() =>
            {
                string tempString;
                using (var temp = IndentationTextWriter.Create())
                {
                    RenderAll(NodeList, context, temp);
                    tempString = temp.ToString();
                }

                if (tempString != (context.Registers["ifchanged"] as string))
                {
                    context.Registers["ifchanged"] = tempString;
                    result.Write(tempString);
                }
            });
        }
コード例 #9
0
ファイル: Extends.cs プロジェクト: zakachun/ThinkComposer
        public override void Render(Context context, IndentationTextWriter result)
        {
            // Get the template or template content and then either copy it (since it will be modified) or parse it
            IFileSystem fileSystem = context.Registers["file_system"] as IFileSystem ?? Template.FileSystem;
            object      file       = fileSystem.ReadTemplateFile(context, _templateName);
            Template    template   = file as Template;

            template = template ?? Template.Parse(file == null ? null : file.ToString());

            List <Block>     parentBlocks   = FindBlocks(template.Root, null);
            List <Block>     orphanedBlocks = ((List <Block>)context.Scopes[0]["extends"]) ?? new List <Block>();
            BlockRenderState blockState     = BlockRenderState.Find(context) ?? new BlockRenderState();

            context.Stack(() =>
            {
                context["blockstate"] = blockState;         // Set or copy the block state down to this scope
                context["extends"]    = new List <Block>(); // Holds Blocks that were not found in the parent
                foreach (Block block in NodeList.OfType <Block>().Concat(orphanedBlocks))
                {
                    Block pb = parentBlocks.Find(b => b.BlockName == block.BlockName);

                    if (pb != null)
                    {
                        Block parent;
                        if (blockState.Parents.TryGetValue(block, out parent))
                        {
                            blockState.Parents[pb] = parent;
                        }
                        pb.AddParent(blockState.Parents, pb.GetNodeList(blockState));
                        blockState.NodeLists[pb] = block.GetNodeList(blockState);
                    }
                    else if (IsExtending(template))
                    {
                        ((List <Block>)context.Scopes[0]["extends"]).Add(block);
                    }
                }
                template.Render(result, RenderParameters.FromContext(context));
            });
        }
コード例 #10
0
 public override void Render(Context context, IndentationTextWriter result)
 {
     context.Stack(() =>
     {
         bool executeElseBlock = true;
         _blocks.ForEach(block =>
         {
             if (block.IsElse)
             {
                 if (executeElseBlock)
                 {
                     RenderAll(block.Attachment, context, result);
                     return;
                 }
             }
             else if (block.Evaluate(context))
             {
                 executeElseBlock = false;
                 RenderAll(block.Attachment, context, result);
             }
         });
     });
 }
コード例 #11
0
        public override void Render(Context context, IndentationTextWriter result)
        {
            context.Stack(() =>
            {
                // First condition is interpreted backwards (if not)
                Condition block = Blocks.First();
                if (!block.Evaluate(context))
                {
                    RenderAll(block.Attachment, context, result);
                    return;
                }

                // After the first condition unless works just like if
                foreach (Condition forEachBlock in Blocks.Skip(1))
                {
                    if (forEachBlock.Evaluate(context))
                    {
                        RenderAll(forEachBlock.Attachment, context, result);
                        return;
                    }
                }
            });
        }
コード例 #12
0
        private static void WriteElement(Element element, IndentationTextWriter writer)
        {
            writer.Write("<");
            WriteTypeDeclaration(element.TypeDeclaration, writer);
            ArrayList list = new ArrayList();
            ArrayList list2 = new ArrayList();
            Property property = null;
            foreach (Property property2 in element.Properties)
            {
                switch (property2.PropertyType)
                {
                    case PropertyType.Value:
                    case PropertyType.Declaration:
                    case PropertyType.Namespace:
                        {
                            list.Add(property2);
                            continue;
                        }
                    case PropertyType.Content:
                        {
                            property = property2;
                            continue;
                        }
                    case PropertyType.List:
                    case PropertyType.Dictionary:
                        {
                            list2.Add(property2);
                            continue;
                        }
                    case PropertyType.Complex:
                        {
                            if (!IsExtension(property2.Value))
                            {
                                break;
                            }
                            list.Add(property2);
                            continue;
                        }
                    default:
                        {
                            continue;
                        }
                }
                list2.Add(property2);
            }
            foreach (Property property3 in list)
            {
                writer.Write(" ");
                WritePropertyDeclaration(property3.PropertyDeclaration, element.TypeDeclaration, writer);
                writer.Write("=");
                writer.Write("\"");
                switch (property3.PropertyType)
                {
                    case PropertyType.Value:
                    case PropertyType.Declaration:
                    case PropertyType.Namespace:
                        writer.Write(property3.Value.ToString());
                        break;

                    case PropertyType.Complex:
                        WriteComplexElement((Element)property3.Value, writer);
                        break;

                    default:
                        throw new NotSupportedException();
                }
                writer.Write("\"");
            }
            if ((property != null) || (list2.Count > 0))
            {
                writer.Write(">");
                if ((list2.Count > 0) || (property.Value is IList))
                {
                    writer.WriteLine();
                }
                writer.Indentation++;
                foreach (Property property4 in list2)
                {
                    writer.Write("<");
                    WriteTypeDeclaration(element.TypeDeclaration, writer);
                    writer.Write(".");
                    WritePropertyDeclaration(property4.PropertyDeclaration, element.TypeDeclaration, writer);
                    writer.Write(">");
                    writer.WriteLine();
                    writer.Indentation++;
                    WritePropertyValue(property4, writer);
                    writer.Indentation--;
                    writer.Write("</");
                    WriteTypeDeclaration(element.TypeDeclaration, writer);
                    writer.Write(".");
                    WritePropertyDeclaration(property4.PropertyDeclaration, element.TypeDeclaration, writer);
                    writer.Write(">");
                    writer.WriteLine();
                }
                if (property != null)
                {
                    WritePropertyValue(property, writer);
                }
                writer.Indentation--;
                writer.Write("</");
                WriteTypeDeclaration(element.TypeDeclaration, writer);
                writer.Write(">");
            }
            else
            {
                writer.Write(" />");
            }
            writer.WriteLine();
        }
コード例 #13
0
 public BlockDrop(Block block, IndentationTextWriter result)
 {
     _block  = block;
     _result = result;
 }
コード例 #14
0
        public override void Render(Context context, IndentationTextWriter result)
        {
            object coll = context[_collectionName];

            if (!(coll is IEnumerable))
            {
                return;
            }
            IEnumerable <object> collection = ((IEnumerable)coll).Cast <object>();

            if (_attributes.ContainsKey("offset"))
            {
                int offset = Convert.ToInt32(_attributes["offset"]);
                collection = collection.Skip(offset);
            }

            if (_attributes.ContainsKey("limit"))
            {
                int limit = Convert.ToInt32(_attributes["limit"]);
                collection = collection.Take(limit);
            }

            collection = collection.ToList();
            int length = collection.Count();

            int cols = Convert.ToInt32(context[_attributes["cols"]]);

            int row = 1;
            int col = 0;

            result.WriteLine("<tr class=\"row1\">");
            context.Stack(() => collection.EachWithIndex((item, index) =>
            {
                context[_variableName]  = item;
                context["tablerowloop"] = Hash.FromAnonymousObject(new
                {
                    length    = length,
                    index     = index + 1,
                    index0    = index,
                    col       = col + 1,
                    col0      = col,
                    rindex    = length - index,
                    rindex0   = length - index - 1,
                    first     = (index == 0),
                    last      = (index == length - 1),
                    col_first = (col == 0),
                    col_last  = (col == cols - 1)
                });

                ++col;

                using (var temp = IndentationTextWriter.Create())
                {
                    RenderAll(NodeList, context, temp);
                    result.Write("<td class=\"col{0}\">{1}</td>", col, temp.ToString());
                }

                if (col == cols && index != length - 1)
                {
                    col = 0;
                    ++row;
                    result.WriteLine("</tr>");
                    result.Write("<tr class=\"row{0}\">", row);
                }
            }));
            result.WriteLine("</tr>");
        }
コード例 #15
0
 public override void Render(Context context, IndentationTextWriter result)
 {
 }
コード例 #16
0
ファイル: Inject.cs プロジェクト: zakachun/ThinkComposer
        public override void Render(Context context, IndentationTextWriter result)
        {
            IFileSystem fileSystem          = context.Registers["file_system"] as IFileSystem ?? Template.FileSystem;
            Template    partial             = null;
            var         workingTemplateName = _templateName;

            var Trace = new System.Diagnostics.StackTrace(false);

            if (Trace.FrameCount > MaxNestedStackCalls)
            {
                throw new UsageAnomaly("Nesting too deep (possible infinite recursion loop).");
            }

            partial = GetRegisteredSubTemplate(_templateName);

            if (partial == null)
            {
                string source = fileSystem.ReadTemplateFile(context, _templateName);
                partial             = Template.Parse(source);
                workingTemplateName = _templateName.Substring(1, _templateName.Length - 2); // old: shortenedTemplateName
            }

            object variable = context[_variableName ?? workingTemplateName];

            variable = (variable is DropProxy ? ((DropProxy)variable).GetObject() : variable);

            var Parameters = new RenderParameters();

            Parameters.LocalVariables = DotLiquid.Hash.FromAnonymousObject(variable);
            Parameters.RethrowErrors  = true;

            var PreviousRecursionLevel = RecursionLevel;

            if (_modifierName != "keepindent")
            {
                if (_modifierName == "noindent")
                {
                    RecursionLevel = 0;
                }
                else
                {
                    RecursionLevel++;
                }

                result.IndentationDepth = RecursionLevel;
            }

            result.IsAtLineStart = true;

            partial.Render(result, Parameters);

            if (_modifierName != "keepindent")
            {
                if (_modifierName == "noindent")
                {
                    RecursionLevel = PreviousRecursionLevel;
                }
                else
                {
                    RecursionLevel--;
                }

                result.IndentationDepth = RecursionLevel;
            }

            /*- old 'include' tag code
             * context.Stack(() =>
             * {
             *  foreach (var keyValue in _attributes)
             *      context[keyValue.Key] = context[keyValue.Value];
             *
             *  if (variable is IEnumerable)
             *  {
             *      ((IEnumerable) variable).Cast<object>().ToList().ForEach(v =>
             *      {
             *          context[workingTemplateName] = v;
             *          partial.Render(result, RenderParameters.FromContext(context));
             *      });
             *      return;
             *  }
             *
             *  context[workingTemplateName] = variable;
             *  partial.Render(result, RenderParameters.FromContext(context));
             * }); */
        }
コード例 #17
0
ファイル: Assign.cs プロジェクト: zakachun/ThinkComposer
 public override void Render(Context context, IndentationTextWriter result)
 {
     context.Scopes.Last()[_to] = _from.Render(context);
 }
コード例 #18
0
		private static void WritePropertyValue(Property property, IndentationTextWriter writer)
		{
			object value = property.Value;

			if (value is IList)
			{
				IList elements = (IList)value;

				if ((property.PropertyDeclaration != null) && (property.PropertyDeclaration.Name == "Resources") && (elements.Count == 1) && (elements[0] is Element))
				{
					Element element = (Element) elements[0];
					if ((element.TypeDeclaration.Name == "ResourceDictionary") && (element.TypeDeclaration.Namespace == "System.Windows") && (element.TypeDeclaration.Assembly == "PresentationFramework") && (element.Arguments.Count == 0) && (element.Properties.Count == 1) && (element.Properties[0].PropertyType == PropertyType.Content))
					{
						WritePropertyValue(element.Properties[0], writer);
						return;
					}
				}

				foreach (object child in elements)
				{
					if (child is string)
					{
						writer.Write((string)child);
					}
					else if (child is Element)
					{
						WriteElement((Element)child, writer);
					}
					else
					{
						throw new NotSupportedException();
					}
				}
			}
			else if (value is string)
			{
				string text = (string)value;
				writer.Write(text);
			}
			else if (value is Element)
			{
				Element element = (Element)value;
				WriteElement(element, writer);
			}
			else
			{
				throw new NotSupportedException();
			}
		}
コード例 #19
0
		private static void WriteElement(Element element, IndentationTextWriter writer)
		{
			writer.Write("<");
			WriteTypeDeclaration(element.TypeDeclaration, writer);

			ArrayList attributes = new ArrayList();
			ArrayList properties = new ArrayList();
			Property contentProperty = null;
			foreach (Property property in element.Properties)
			{
				switch (property.PropertyType)
				{
					case PropertyType.List:
					case PropertyType.Dictionary:
						properties.Add(property);
						break;

					case PropertyType.Namespace:
					case PropertyType.Value:
					case PropertyType.Declaration:
						attributes.Add(property);
						break;

					case PropertyType.Complex:
						if (IsExtension(property.Value))
						{
							attributes.Add(property);
						}
						else
						{
							properties.Add(property);
						}
						break;

					case PropertyType.Content:
						contentProperty = property;
						break;
				}
			}

			foreach (Property property in attributes)
			{
				writer.Write(" ");
				WritePropertyDeclaration(property.PropertyDeclaration, element.TypeDeclaration, writer);
				writer.Write("=");
				writer.Write("\"");

				switch (property.PropertyType)
				{
					case PropertyType.Complex:
						WriteComplexElement((Element)property.Value, writer);
						break;

					case PropertyType.Namespace:
					case PropertyType.Declaration:
					case PropertyType.Value:
						writer.Write(property.Value.ToString());
						break;

					default:
						throw new NotSupportedException();
				}


				writer.Write("\"");
			}

			if ((contentProperty != null) || (properties.Count > 0))
			{
				writer.Write(">");

				if ((properties.Count > 0) || (contentProperty.Value is IList))
				{
					writer.WriteLine();
				}

				writer.Indentation++;

				foreach (Property property in properties)
				{
					writer.Write("<");
					WriteTypeDeclaration(element.TypeDeclaration, writer);
					writer.Write(".");
					WritePropertyDeclaration(property.PropertyDeclaration, element.TypeDeclaration, writer);
					writer.Write(">");
					writer.WriteLine();
					writer.Indentation++;
					WritePropertyValue(property, writer);
					writer.Indentation--;
					writer.Write("</");
					WriteTypeDeclaration(element.TypeDeclaration, writer);
					writer.Write(".");
					WritePropertyDeclaration(property.PropertyDeclaration, element.TypeDeclaration, writer);
					writer.Write(">");
					writer.WriteLine();
				}

				if (contentProperty != null)
				{
					WritePropertyValue(contentProperty, writer);
				}

				writer.Indentation--;

				writer.Write("</");
				WriteTypeDeclaration(element.TypeDeclaration, writer);
				writer.Write(">");
			}
			else
			{
				writer.Write(" />");
			}

			writer.WriteLine();
		}
コード例 #20
0
		public override string ToString()
		{
			using (StringWriter stringWriter = new StringWriter())
			{
				using (IndentationTextWriter indentationTextWriter = new IndentationTextWriter(stringWriter))
				{
					WriteElement(this.rootElement, indentationTextWriter);
				}

				return stringWriter.ToString();
			}
		}
コード例 #21
0
 private static void WritePropertyValue(Property property, IndentationTextWriter writer)
 {
     object obj2 = property.Value;
     if (obj2 is IList)
     {
         IList list = (IList)obj2;
         if (((property.PropertyDeclaration != null) && (property.PropertyDeclaration.Name == "Resources")) && ((list.Count == 1) && (list[0] is Element)))
         {
             Element element = (Element)list[0];
             if ((((element.TypeDeclaration.Name == "ResourceDictionary") && (element.TypeDeclaration.Namespace == "System.Windows")) && ((element.TypeDeclaration.Assembly == "PresentationFramework") && (element.Arguments.Count == 0))) && ((element.Properties.Count == 1) && (element.Properties[0].PropertyType == PropertyType.Content)))
             {
                 WritePropertyValue(element.Properties[0], writer);
                 return;
             }
         }
         foreach (object obj3 in list)
         {
             if (!(obj3 is string))
             {
                 if (!(obj3 is Element))
                 {
                     throw new NotSupportedException();
                 }
                 WriteElement((Element)obj3, writer);
             }
             else
             {
                 writer.Write((string)obj3);
                 continue;
             }
         }
     }
     else if (obj2 is string)
     {
         string str = (string)obj2;
         writer.Write(str);
     }
     else
     {
         if (!(obj2 is Element))
         {
             throw new NotSupportedException();
         }
         Element element2 = (Element)obj2;
         WriteElement(element2, writer);
     }
 }