示例#1
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);
            }
        }