Esempio n. 1
0
        public void Process(SiteContext context, bool skipFileOnError = false)
        {
            // Default rendering engine
            if (LightweightMarkupEngine == null)
            {
                LightweightMarkupEngine = new CommonMarkEngine();
            }

            Tracing.Debug("LightweightMarkupEngine: {0}", LightweightMarkupEngine.GetType().Name);

            Context = context;
            PreProcess();

            for (int index = 0; index < context.Posts.Count; index++)
            {
                var p        = context.Posts[index];
                var previous = GetPrevious(context.Posts, index);
                var next     = GetNext(context.Posts, index);
                ProcessFile(context.OutputFolder, p, previous, next, skipFileOnError, p.Filepath);
            }

            for (int index = 0; index < context.Pages.Count; index++)
            {
                var p        = context.Pages[index];
                var previous = GetPrevious(context.Pages, index);
                var next     = GetNext(context.Pages, index);
                ProcessFile(context.OutputFolder, p, previous, next, skipFileOnError);
            }
        }
Esempio n. 2
0
        private void ImportPost(BloggerPost post)
        {
            var header = new
            {
                title      = post.Title,
                date       = post.Published,
                layout     = "post",
                categories = post.Categories,
                tags       = post.Tags
            };

            var yamlHeader  = string.Format("---\r\n{0}---\r\n\r\n", header.ToYaml());
            var postContent = yamlHeader + post.Content;

            string fileName = string.Format(@"{0}-{1}.md", post.Published.ToString("yyyy-MM-dd"), post.Title); //not sure about post name

            foreach (char c in System.IO.Path.GetInvalidFileNameChars())
            {
                fileName = fileName.Replace(c, '_');
            }
            // replace some valid ones too
            fileName = fileName.Replace(' ', '-');
            fileName = fileName.Replace('\u00A0', '-');

            try
            {
                fileSystem.File.WriteAllText(Path.Combine(pathToSite, Path.Combine("_posts", fileName)), postContent);
            }
            catch (Exception e)
            {
                Tracing.Info("Failed to write out {0}", fileName);
                Tracing.Debug(e.Message);
            }
        }
Esempio n. 3
0
        private string RenderContent(string file, string contents)
        {
            string html;

            try
            {
                var contentsWithoutHeader = contents.ExcludeHeader();

                html = Path.GetExtension(file).IsMarkdownFile()
                       ? LightweightMarkupEngine.Convert(contentsWithoutHeader).Trim()
                       : contentsWithoutHeader;

                if (ContentTransformers != null)
                {
                    html = ContentTransformers.Aggregate(html, (current, contentTransformer) => contentTransformer.Transform(current));
                }
            }
            catch (Exception e)
            {
                Tracing.Info("Error ({0}) converting {1}", e.Message, file);
                Tracing.Debug(e.ToString());
                html = String.Format("<p><b>Error converting markdown:</b><br />{0}</p><p>Original content:<br /><pre>{1}</pre></p>", e.Message, contents);
            }
            return(html);
        }
Esempio n. 4
0
        private static void LoadPlugins(ContainerConfiguration configuration, bool safe, string path)
        {
            if (!safe)
            {
                var pluginsPath = Path.Combine(path, "_plugins");

                if (Directory.Exists(pluginsPath))
                {
                    var files = Directory.EnumerateFiles(pluginsPath, "*.dll", SearchOption.AllDirectories);
                    foreach (var file in files)
                    {
                        try
                        {
                            Tracing.Debug("Loading Assembly: " + file);
                            var asm = Assembly.LoadFrom(file);
                            configuration.WithAssembly(asm);
                            Tracing.Debug("Loaded Assembly: " + asm.FullName);
                        }
                        catch (ReflectionTypeLoadException e)
                        {
                            // Cannot load the type
                            Tracing.Error($"Could not load assembly for reason: {e.Message}");
                        }
                        catch (BadImageFormatException e)
                        {
                            // Cannot load the type. It's probably wrong bitness
                            Tracing.Error($"Could not load assembly for reason: {e.Message}");
                        }
                    }

                    AddScriptCs(configuration, pluginsPath);
                }
            }
        }
Esempio n. 5
0
        private static void Main(string[] args)
        {
            Tracing.Logger.SetWriter(Console.Out);
            Tracing.Logger.AddCategory("info");
            Tracing.Logger.AddCategory("error");

            var parameters = BaseParameters.Parse(args, new FileSystem());

            if (parameters.Debug)
            {
                Tracing.Logger.AddCategory("debug");
            }

            var program = new Program();

            Tracing.Info("starting pretzel...");
            Tracing.Debug(string.Format("V{0}", Assembly.GetExecutingAssembly().GetName().Version));

            program.Compose(parameters);

            if (parameters.Help || !args.Any())
            {
                program.ShowHelp(parameters.Options);
                return;
            }

            program.Run(args, parameters);
        }
Esempio n. 6
0
        private void AddScriptCs(AggregateCatalog mainCatalog, string pluginsPath)
        {
            var pretzelScriptCsPath = Assembly.GetEntryAssembly().Location.Replace("Pretzel.exe", "Pretzel.ScriptCs.dll");

            if (File.Exists(pretzelScriptCsPath))
            {
                var pretzelScriptcsAssembly = Assembly.LoadFile(pretzelScriptCsPath);
                if (pretzelScriptcsAssembly != null)
                {
                    var factoryType = pretzelScriptcsAssembly.GetType("Pretzel.ScriptCs.ScriptCsCatalogFactory");
                    if (factoryType != null)
                    {
                        var scriptCsCatalogMethod = factoryType.GetMethod("CreateScriptCsCatalog");
                        if (scriptCsCatalogMethod != null)
                        {
                            var catalog = (ComposablePartCatalog)scriptCsCatalogMethod.Invoke(null, new object[] { pluginsPath, new[] { typeof(DotLiquid.Tag), typeof(ITag) } });
                            mainCatalog.Catalogs.Add(catalog);
                        }
                        else
                        {
                            Tracing.Debug("Assembly 'Pretzel.ScriptCs.dll' detected and loaded, type 'Pretzel.ScriptCs.ScriptCsCatalogFactory' found but method 'CreateScriptCsCatalog' not found.");
                        }
                    }
                    {
                        Tracing.Debug("Assembly 'Pretzel.ScriptCs.dll' detected and loaded but type 'Pretzel.ScriptCs.ScriptCsCatalogFactory' not found.");
                    }
                }
                else
                {
                    Tracing.Debug("Assembly 'Pretzel.ScriptCs.dll' detected but not loaded.");
                }
            }
        }
Esempio n. 7
0
        protected override string RenderTemplate(string content, PageContext pageData)
        {
            var serviceConfiguration = new TemplateServiceConfiguration
            {
                TemplateManager          = new IncludesResolver(FileSystem, includesPath),
                BaseTemplateType         = typeof(ExtensibleTemplate <>),
                DisableTempFileLocking   = true,
                CachingProvider          = new DefaultCachingProvider(t => { }),
                ConfigureCompilerBuilder = builder => ModelDirective.Register(builder)
            };

            serviceConfiguration.Activator = new ExtensibleActivator(serviceConfiguration.Activator, Filters, _allTags);

            Engine.Razor = RazorEngineService.Create(serviceConfiguration);

            content = Regex.Replace(content, "<p>(@model .*?)</p>", "$1");

            var pageContent = pageData.Content;

            pageData.Content = pageData.FullContent;

            try
            {
                content          = Engine.Razor.RunCompile(content, pageData.Page.File, typeof(PageContext), pageData);
                pageData.Content = pageContent;
                return(content);
            }
            catch (Exception e)
            {
                Tracing.Error(@"Failed to render template, falling back to direct content");
                Tracing.Debug(e.Message);
                Tracing.Debug(e.StackTrace);
                return(content);
            }
        }
Esempio n. 8
0
        protected override string RenderTemplate(string content, PageContext pageData)
        {
            var serviceConfiguration = new TemplateServiceConfiguration
            {
                TemplateManager  = new IncludesResolver(FileSystem, includesPath),
                BaseTemplateType = typeof(ExtensibleTemplate <>)
            };

            serviceConfiguration.Activator = new ExtensibleActivator(serviceConfiguration.Activator, Filters, Tags);
            Engine.Razor = RazorEngineService.Create(serviceConfiguration);

            content = Regex.Replace(content, "<p>(@model .*?)</p>", "$1");

            try
            {
                return(Engine.Razor.RunCompile(content, pageData.Page.File, typeof(PageContext), pageData));
            }
            catch (Exception e)
            {
                Tracing.Error(@"Failed to render template, falling back to direct content");
                Tracing.Debug(e.Message);
                Tracing.Debug(e.StackTrace);
                return(content);
            }
        }
Esempio n. 9
0
        private static void AddScriptCs(ContainerConfiguration configuration, string pluginsPath)
        {
            var pretzelScriptCsPath = Path.Combine(new FileInfo(Assembly.GetEntryAssembly().Location).DirectoryName, "Pretzel.ScriptCs.dll");

            if (File.Exists(pretzelScriptCsPath))
            {
                var pretzelScriptcsAssembly = Assembly.LoadFile(pretzelScriptCsPath);
                if (pretzelScriptcsAssembly != null)
                {
                    var factoryType = pretzelScriptcsAssembly.GetType("Pretzel.ScriptCs.ScriptCsCatalogFactory");
                    if (factoryType != null)
                    {
                        var scriptCsCatalogMethod = factoryType.GetMethod("CreateScriptCsCatalog");

                        if (scriptCsCatalogMethod != null)
                        {
                            throw new NotSupportedException($"Currently there is no support for ScriptCS cause the lack of the new 'System.Composition' model.{Environment.NewLine}Please stay tuned.");
                        }
                        //TODO: ScriptCS Support
                        //if (scriptCsCatalogMethod != null)
                        //{
                        //    var catalog = (ComposablePartCatalog)scriptCsCatalogMethod.Invoke(null, new object[]
                        //        {
                        //            pluginsPath,
                        //            new[]
                        //            {
                        //                typeof(DotLiquid.Tag),
                        //                typeof(Logic.Extensibility.ITag),
                        //                typeof(Logic.Templating.Context.SiteContext),
                        //                typeof(IFileSystem),
                        //                typeof(IConfiguration),
                        //            }
                        //        });
                        //    mainCatalog.Catalogs.Add(catalog);
                        //}
                        //else
                        //{
                        //    Tracing.Debug("Assembly 'Pretzel.ScriptCs.dll' detected and loaded, type 'Pretzel.ScriptCs.ScriptCsCatalogFactory' found but method 'CreateScriptCsCatalog' not found.");
                        //}
                    }
                    else
                    {
                        Tracing.Debug("Assembly 'Pretzel.ScriptCs.dll' detected and loaded but type 'Pretzel.ScriptCs.ScriptCsCatalogFactory' not found.");
                    }
                }
                else
                {
                    Tracing.Debug("Assembly 'Pretzel.ScriptCs.dll' detected but not loaded.");
                }
            }
        }
Esempio n. 10
0
        private void AddScriptCs(AggregateCatalog mainCatalog, string pluginsPath)
        {
            var pretzelScriptCsPath = Path.Combine(new FileInfo(Assembly.GetEntryAssembly().Location).DirectoryName, "Pretzel.ScriptCs.dll");

            if (File.Exists(pretzelScriptCsPath))
            {
                var pretzelScriptcsAssembly = Assembly.LoadFile(pretzelScriptCsPath);
                if (pretzelScriptcsAssembly != null)
                {
                    var factoryType = pretzelScriptcsAssembly.GetType("Pretzel.ScriptCs.ScriptCsCatalogFactory");
                    if (factoryType != null)
                    {
                        var scriptCsCatalogMethod = factoryType.GetMethod("CreateScriptCsCatalog");
                        if (scriptCsCatalogMethod != null)
                        {
                            var catalog = (ComposablePartCatalog)scriptCsCatalogMethod.Invoke(null, new object[]
                            {
                                pluginsPath,
                                new[]
                                {
                                    typeof(DotLiquid.Tag),
                                    typeof(Logic.Extensibility.ITag),
                                    typeof(Logic.Templating.Context.SiteContext),
                                    typeof(IFileSystem),
                                    typeof(IConfiguration),
                                }
                            });
                            mainCatalog.Catalogs.Add(catalog);
                        }
                        else
                        {
                            Tracing.Debug("Assembly 'Pretzel.ScriptCs.dll' detected and loaded, type 'Pretzel.ScriptCs.ScriptCsCatalogFactory' found but method 'CreateScriptCsCatalog' not found.");
                        }
                    }
                    else
                    {
                        Tracing.Debug("Assembly 'Pretzel.ScriptCs.dll' detected and loaded but type 'Pretzel.ScriptCs.ScriptCsCatalogFactory' not found.");
                    }
                }
                else
                {
                    Tracing.Debug("Assembly 'Pretzel.ScriptCs.dll' detected but not loaded.");
                }
            }
        }
Esempio n. 11
0
            public void Configuration(IAppBuilder app)
            {
                app.Run(context =>
                {
                    var path = context.Request.Path.Value;

                    Tracing.Debug(path);

                    if (!Content.IsAvailable(path))
                    {
                        var path404 = "/404.html";
                        context.Response.StatusCode = 404;

                        if (Content.IsAvailable(path404))
                        {
                            context.Response.ContentType = path404.MimeType();
                            return(context.Response.WriteAsync(Content.GetContent(path404)));
                        }

                        context.Response.ContentType = path.MimeType();
                        return(context.Response.WriteAsync("Page not found: " + path));
                    }

                    if (path.MimeType().IsBinaryMime())
                    {
                        context.Response.ContentType = path.MimeType();
                        var fileContents             = Content.GetBinaryContent(path);
                        context.Response.Headers["Content-Range"]  = string.Format("bytes 0-{0}", fileContents.Length - 1);
                        context.Response.Headers["Content-Length"] = fileContents.Length.ToString(CultureInfo.InvariantCulture);
                        return(context.Response.WriteAsync(fileContents));
                    }

                    if (Content.IsDirectory(path) && !path.EndsWith("/"))
                    {
                        // if path is a directory without trailing slash, redirects to the same url with a trailing slash
                        context.Response.StatusCode          = 301;
                        context.Response.Headers["location"] = string.Format("http://localhost:{0}{1}/", context.Request.LocalPort, path);
                        return(Task.Delay(0));
                    }

                    context.Response.ContentType = path.MimeType();
                    return(context.Response.WriteAsync(Content.GetContent(path)));
                });
            }
Esempio n. 12
0
        private static void Main(string[] args)
        {
            var parameters = BaseParameters.Parse(args, new FileSystem());

            InitializeTrace(parameters.Debug);

            var program = new Program();

            Tracing.Info("starting pretzel...");
            Tracing.Debug("V{0}", Assembly.GetExecutingAssembly().GetName().Version);

            program.Compose(parameters);

            if (parameters.Help || !args.Any())
            {
                program.ShowHelp(parameters.Options);
                return;
            }

            program.Run(parameters);
        }
Esempio n. 13
0
        private string RenderContent(string file, string contents, IDictionary <string, object> header)
        {
            string html;

            try
            {
                var contentsWithoutHeader = contents.ExcludeHeader();
                html = string.Equals(Path.GetExtension(file), ".md", StringComparison.InvariantCultureIgnoreCase)
                       ? Markdown.Transform(contentsWithoutHeader)
                       : contentsWithoutHeader;

                html = contentTransformers.Aggregate(html, (current, contentTransformer) => contentTransformer.Transform(current));
            }
            catch (Exception e)
            {
                Tracing.Info(String.Format("Error ({0}) converting {1}", e.Message, file));
                Tracing.Debug(e.ToString());
                html = String.Format("<p><b>Error converting markdown</b></p><pre>{0}</pre>", contents);
            }
            return(html);
        }
Esempio n. 14
0
        private string RenderContent(string file, string contents, IDictionary <string, object> header)
        {
            string html;

            try
            {
                var contentsWithoutHeader = contents.ExcludeHeader();

                html = Path.GetExtension(file).IsMarkdownFile()
                       ? CommonMark.CommonMarkConverter.Convert(contentsWithoutHeader).Trim()
                       : contentsWithoutHeader;

                html = contentTransformers.Aggregate(html, (current, contentTransformer) => contentTransformer.Transform(current));
            }
            catch (Exception e)
            {
                Tracing.Info(String.Format("Error ({0}) converting {1}", e.Message, file));
                Tracing.Debug(e.ToString());
                html = String.Format("<p><b>Error converting markdown</b></p><pre>{0}</pre>", contents);
            }
            return(html);
        }
Esempio n. 15
0
        private static async Task <int> Main(string[] args)
        {
            Tracing.SetTrace(ConsoleTrace.Write);

            args = PatchSourcePath(args);

            var parseResult = ParseArguments(args);

            if (parseResult.hasError)
            {
                return(-1);
            }

            try
            {
                InitializeTrace(parseResult.debug);
                Tracing.Info("starting pretzel...");
                Tracing.Debug("V{0}", Assembly.GetExecutingAssembly().GetName().Version);

                using (var host = Compose(parseResult.debug, parseResult.safe, parseResult.source))
                {
                    var program = new Program();
                    host.SatisfyImports(program);
                    var result = await program.Run(GlobalOptions, args);

                    WaitForClose();
                    return(result);
                }
            }
            catch (Exception ex)
            {
                Tracing.Error(ex.Message);
                WaitForClose();
                return(-1);
            }
        }
Esempio n. 16
0
        private Page CreatePage(SiteContext context, IDictionary <string, object> config, string file, bool isPost)
        {
            try
            {
                if (pageCache.ContainsKey(file))
                {
                    return(pageCache[file]);
                }
                var contents = SafeReadContents(file);
                var header   = contents.YamlHeader();
                var content  = RenderContent(file, contents, header);

                if (header.ContainsKey("published") && header["published"].ToString().ToLower() == "false")
                {
                    return(null);
                }

                var page = new Page
                {
                    Title    = header.ContainsKey("title") ? header["title"].ToString() : "this is a post",
                    Date     = header.ContainsKey("date") ? DateTime.Parse(header["date"].ToString()) : file.Datestamp(),
                    Content  = content,
                    Filepath = isPost ? GetPathWithTimestamp(context.OutputFolder, file) : GetFilePathForPage(context, file),
                    File     = file,
                    Bag      = header,
                };

                // resolve categories and tags
                if (isPost)
                {
                    page.Categories = ResolveCategories(context, header, page);

                    if (header.ContainsKey("tags"))
                    {
                        page.Tags = header["tags"] as IEnumerable <string>;
                    }
                }

                // resolve permalink
                if (header.ContainsKey("permalink"))
                {
                    page.Url = linkHelper.EvaluatePermalink(header["permalink"].ToString(), page);
                }
                else if (isPost && config.ContainsKey("permalink"))
                {
                    page.Url = linkHelper.EvaluatePermalink(config["permalink"].ToString(), page);
                }
                else
                {
                    page.Url = linkHelper.EvaluateLink(context, page);
                }

                // resolve id
                page.Id = page.Url.Replace(".html", string.Empty).Replace("index", string.Empty);

                // ensure the date is accessible in the hash
                if (!page.Bag.ContainsKey("date"))
                {
                    page.Bag["date"] = page.Date;
                }

                // The GetDirectoryPage method is reentrant, we need a cache to stop a stack overflow :)
                pageCache.Add(file, page);
                page.DirectoryPages = GetDirectoryPages(context, config, Path.GetDirectoryName(file), isPost).ToList();

                return(page);
            }
            catch (Exception e)
            {
                Tracing.Info(String.Format("Failed to build post from File: {0}", file));
                Tracing.Info(e.Message);
                Tracing.Debug(e.ToString());
            }

            return(null);
        }
Esempio n. 17
0
        private Page CreatePage(SiteContext context, IConfiguration config, string file, bool isPost)
        {
            try
            {
                if (pageCache.ContainsKey(file))
                {
                    return(pageCache[file]);
                }
                var content = SafeReadContents(file);

                var relativePath   = MapToOutputPath(context, file);
                var scopedDefaults = context.Config.Defaults.ForScope(relativePath);

                var header = scopedDefaults.Merge(content.YamlHeader());

                if (header.ContainsKey("published") && header["published"].ToString().ToLower(CultureInfo.InvariantCulture) == "false")
                {
                    return(null);
                }

                var page = new Page
                {
                    Title    = header.ContainsKey("title") ? header["title"].ToString() : "this is a post",
                    Date     = header.ContainsKey("date") ? DateTime.Parse(header["date"].ToString()) : file.Datestamp(fileSystem),
                    Content  = content,
                    Filepath = isPost ? GetPathWithTimestamp(context.OutputFolder, file) : GetFilePathForPage(context, file),
                    File     = file,
                    Bag      = header,
                };

                // resolve categories and tags
                if (isPost)
                {
                    page.Categories = ResolveCategories(context, header, page);

                    if (header.ContainsKey("tags"))
                    {
                        page.Tags = header["tags"] as IEnumerable <string>;
                    }
                }

                // resolve permalink
                if (header.ContainsKey("permalink"))
                {
                    page.Url = linkHelper.EvaluatePermalink(header["permalink"].ToString(), page);
                }
                else if (isPost && config.ContainsKey("permalink"))
                {
                    page.Url = linkHelper.EvaluatePermalink(config["permalink"].ToString(), page);
                }
                else
                {
                    page.Url = linkHelper.EvaluateLink(context, page);
                }

                // resolve id
                page.Id = page.Url.Replace(".html", string.Empty).Replace("index", string.Empty);

                // always write date back to Bag as DateTime
                page.Bag["date"] = page.Date;

                // The GetDirectoryPage method is reentrant, we need a cache to stop a stack overflow :)
                pageCache.Add(file, page);
                page.DirectoryPages = GetDirectoryPages(context, config, Path.GetDirectoryName(file), isPost).ToList();

                return(page);
            }
            catch (Exception e)
            {
                Tracing.Info("Failed to build post from File: {0}", file);
                Tracing.Info(e.Message);
                Tracing.Debug(e.ToString());
            }

            return(null);
        }