Exemple #1
0
        public static FluidValue IncludeFilter(FluidValue input, FilterArguments arguments, TemplateContext context)
        {
            // When a property of a JObject value is accessed, try to look into its properties
            TemplateContext.GlobalMemberAccessStrategy.Register <JObject, object>((source, name) => source[name]);

            // Convert JToken to FluidValue
            FluidValue.SetTypeMapping <JObject>(o => new ObjectValue(o));
            FluidValue.SetTypeMapping <JValue>(o => FluidValue.Create(o.Value));

            var    includePath = GlobalConfiguration.getConfiguration().source + "/" + GlobalConfiguration.getConfiguration().includes_dir + "/" + arguments.At(0).ToStringValue();
            string includeFile = WDHANFile.getFileContents(includePath);

            var includeContext = new TemplateContext();
            var includeJSON    = WDHANFile.parseFrontMatter(includePath);

            if (FluidTemplate.TryParse(includeFile, out var template))
            {
                for (int i = 1; i < arguments.Count - 1; i++)
                {
                    includeContext.SetValue(arguments.At(i).ToStringValue(), arguments.At(i));
                }
                includeContext.SetValue("include", includeJSON);
            }

            return(new StringValue(template.Render(includeContext)));
        }
Exemple #2
0
        public string parseInclude(string includePath, string filePath)
        {
            Console.WriteLine();
            var siteConfig   = GlobalConfiguration.getConfiguration();
            var fileContents = WDHANFile.getFileContents(includePath);

            // Expand layouts, then parse includes

            // When a property of a JObject value is accessed, try to look into its properties
            TemplateContext.GlobalMemberAccessStrategy.Register <JObject, object>((source, name) => source[name]);

            // Convert JToken to FluidValue
            FluidValue.SetTypeMapping <JObject>(o => new ObjectValue(o));
            FluidValue.SetTypeMapping <JValue>(o => FluidValue.Create(o.Value));

            var siteModel = JObject.Parse(File.ReadAllText("./_config.json"));
            var dataSet   = JObject.Parse(File.ReadAllText(siteConfig.source + "/temp/_data.json"));
            var pageModel = WDHANFile.parseFrontMatter(filePath);

            setVariables();
            var includeModel       = JObject.Parse(JsonConvert.SerializeObject(variables));
            var includeFrontmatter = WDHANFile.parseFrontMatter(includePath);

            includeModel.Merge(includeFrontmatter, new JsonMergeSettings
            {
                MergeArrayHandling = MergeArrayHandling.Union
            });

            try
            {
                if (FluidTemplate.TryParse(fileContents, out var template))
                {
                    var context = new TemplateContext();
                    context.CultureInfo = new CultureInfo(siteConfig.culture);

                    siteModel.Merge(dataSet, new JsonMergeSettings {
                        MergeArrayHandling = MergeArrayHandling.Union
                    });
                    context.SetValue("site", siteModel);
                    context.SetValue("page", pageModel);
                    foreach (var collection in siteConfig.collections)
                    {
                        context.SetValue(collection, JObject.Parse(File.ReadAllText(siteConfig.source + "/_" + collection + "/_config.json")));
                    }
                    context.SetValue("include", includeModel);
                    return(template.Render(context));
                }
                else
                {
                    Console.WriteLine("ERROR: Could not parse Liquid context for include " + includePath + ".");
                    return(fileContents);
                }
            }
            catch (ArgumentNullException)
            {
                Console.WriteLine("Include " + includePath + " has no Liquid context to parse.");
                return(fileContents);
            }
        }
Exemple #3
0
        public string parseInclude(string includePath, JObject pageModel, string collectionName, string filePath)
        {
            setVariables();
            var siteConfig = GlobalConfiguration.getConfiguration();

            // When a property of a JObject value is accessed, try to look into its properties
            TemplateContext.GlobalMemberAccessStrategy.Register <JObject, object>((source, name) => source[name]);

            // Convert JToken to FluidValue
            FluidValue.SetTypeMapping <JObject>(o => new ObjectValue(o));
            FluidValue.SetTypeMapping <JValue>(o => FluidValue.Create(o.Value));

            string includeFile = WDHANFile.getFileContents(includePath);

            var context          = new TemplateContext();
            var givenModel       = JObject.Parse(JsonConvert.SerializeObject(variables));
            var frontmatterModel = WDHANFile.parseFrontMatter(includePath);

            givenModel.Merge(frontmatterModel, new JsonMergeSettings
            {
                MergeArrayHandling = MergeArrayHandling.Union
            });

            var siteModel = JObject.Parse(File.ReadAllText("./_config.json"));
            Dictionary <string, object> collectionConfig = JsonConvert.DeserializeObject <Dictionary <string, object> >(File.ReadAllText(siteConfig.source + "/_" + collectionName + "/_config.json"));
            var collectionModel = JObject.Parse(File.ReadAllText(siteConfig.source + "/_" + collectionName + "/_config.json"));
            var collectionPosts = JObject.Parse(File.ReadAllText(siteConfig.source + "/temp/_" + collectionName + "/_entries.json"));
            var dataSet         = JObject.Parse(File.ReadAllText(siteConfig.source + "/temp/_data.json"));

            try
            {
                JObject pageObjectModel = Page.getPage(Page.getDefinedPage(new Page {
                    frontmatter = pageModel, content = WDHANFile.getFileContents(filePath), path = filePath
                }));
                pageModel.Merge(pageObjectModel, new JsonMergeSettings
                {
                    MergeArrayHandling = MergeArrayHandling.Union
                });
            }
            catch
            {
            }

            siteModel.Merge(dataSet, new JsonMergeSettings
            {
                MergeArrayHandling = MergeArrayHandling.Union
            });

            Console.WriteLine("INCFILE: \n" + includeFile);

            try
            {
                if (FluidTemplate.TryParse(includeFile, out var template))
                {
                    context.SetValue("include", givenModel);
                    context.SetValue("site", siteModel);
                    context.SetValue("page", pageModel);
                    collectionModel.Merge(collectionPosts, new JsonMergeSettings
                    {
                        MergeArrayHandling = MergeArrayHandling.Union
                    });
                    context.SetValue(collectionName, collectionModel);
                    return(template.Render(context));
                }
                else
                {
                    Console.WriteLine("ERROR: Could not parse Liquid context.");
                    return(includeFile);
                }
            }
            catch (ArgumentNullException)
            {
                Console.WriteLine("No Liquid context to parse.");
                return(includeFile);
            }
        }
Exemple #4
0
        public static string parseRaw(string filePath)
        {
            Console.WriteLine("parseRaw - " + filePath);

            var siteConfig   = GlobalConfiguration.getConfiguration();
            var fileContents = WDHANFile.getFileContents(filePath);

            fileContents = Include.evalInclude(filePath); // Expand includes (must happen after layouts are retreived, as layouts can have includes)

            // When a property of a JObject value is accessed, try to look into its properties
            TemplateContext.GlobalMemberAccessStrategy.Register <JObject, object>((source, name) => source[name]);

            // Convert JToken to FluidValue
            FluidValue.SetTypeMapping <JObject>(o => new ObjectValue(o));
            FluidValue.SetTypeMapping <JValue>(o => FluidValue.Create(o.Value));


            var siteModel = JObject.Parse(File.ReadAllText("./_config.json"));
            var dataSet   = JObject.Parse(File.ReadAllText(siteConfig.source + "/temp/_data.json"));
            var pageModel = WDHANFile.parseFrontMatter(filePath);

            Console.WriteLine("fileContents!!!" + filePath + ":\n" + fileContents);

            try
            {
                if (FluidTemplate.TryParse(fileContents, out var template))
                {
                    var context = new TemplateContext();
                    context.CultureInfo = new CultureInfo(siteConfig.culture);

                    siteModel.Merge(dataSet, new JsonMergeSettings {
                        MergeArrayHandling = MergeArrayHandling.Union
                    });
                    context.SetValue("site", siteModel);
                    context.SetValue("page", pageModel);

                    foreach (var collection in siteConfig.collections)
                    {
                        if (File.Exists(siteConfig.source + "/temp/_" + collection + "/_entries.json"))
                        {
                            var collectionModel = JObject.Parse(File.ReadAllText(siteConfig.source + "/_" + collection + "/_config.json"));
                            collectionModel.Merge(JObject.Parse(File.ReadAllText(siteConfig.source + "/temp/_" + collection + "/_entries.json")), new JsonMergeSettings {
                                MergeArrayHandling = MergeArrayHandling.Union
                            });
                            context.SetValue(collection, collectionModel);
                        }
                    }
                    Console.WriteLine("DONE!!! - " + filePath + " \n" + template.Render(context));
                    return(template.Render(context));
                }
                else
                {
                    Console.WriteLine("ERROR: Could not parse Liquid context for file " + filePath + ".");
                    Console.WriteLine("TryParse error:\n" + fileContents);
                    return(fileContents);
                }
            }
            catch (ArgumentNullException ex)
            {
                Console.WriteLine("File " + filePath + " has no Liquid context to parse.\n" + ex.ToString());
                return(fileContents);
            }
        }