Example #1
0
        public Config Parse(string configPath)
        {
            XDocument doc = XDocument.Load(configPath);

            string configDirectory = Path.GetDirectoryName(configPath);

            Config config = new Config();

            config.Culture = doc.Root.GetValue("Culture");
            config.Source =  Path.Combine(configDirectory, doc.Root.GetValue("Source"));
            config.Destination = Path.Combine(configDirectory, doc.Root.GetValue("Destination"));
            config.BaseUrl = doc.Root.GetValue("BaseUrl");

            foreach (var excludePattern in doc.Root.GetChildElements("ExcludePatterns"))
            {
                config.ExcludePatterns.Add(excludePattern.Value);
            }

            foreach (var pageFileNameExtension in doc.Root.GetChildElements("PageFileNameExtensions"))
            {
                config.PageFileNameExtensions.Add(pageFileNameExtension.Value);
            }

            config.ExcerptSeparator = doc.Root.GetValue("ExcerptSeparator");

            return config;
        }
Example #2
0
        internal Site(Config config)
        {
            this.config = config;

            this.Plugins = new PluginManager();
            this.Posts = new List<Post>();
            this.Pages = new List<Page>();
            this.GeneratorPages = new List<Generator>();
        }
Example #3
0
        public SourceFiles Enumerate(Config config)
        {
            SourceFiles sourceFiles = new SourceFiles();

            List<SourceFilePath> pages = new List<SourceFilePath>();
            List<SourceFilePath> staticFiles = new List<SourceFilePath>();
            Scan(pages, staticFiles, config.Source, config.Source, config);

            sourceFiles.Pages = pages;
            sourceFiles.StaticFiles = staticFiles;
            sourceFiles.Posts = FindFilesToProcess(Path.Combine(config.Source, "_Posts"), "*.cshtml", config.ExcludePatterns);
            sourceFiles.Generators = FindFilesToProcess(Path.Combine(config.Source, "_Generators"), "*.cshtml", config.ExcludePatterns);
            sourceFiles.Layouts = FindFilesToProcess(Path.Combine(config.Source, "_Layouts"), "*.cshtml", config.ExcludePatterns);
            sourceFiles.Includes = FindFilesToProcess(Path.Combine(config.Source, "_Includes"), "*.cshtml", config.ExcludePatterns);
            sourceFiles.Plugins = FindFilesToProcess(Path.Combine(config.Source, "_Plugins"), "*.cs", config.ExcludePatterns);

            return sourceFiles;
        }
Example #4
0
        internal override void Run(
            Config config,
            MarkdownTransformer markdownTransformer,
            string relativePath,
            RazorRenderer razorRenderer,
            Site site)
        {
            base.Run(config, markdownTransformer, relativePath, razorRenderer, site);

            if (!string.IsNullOrEmpty(config.ExcerptSeparator))
            {
                if (this.Content.Contains(config.ExcerptSeparator))
                {
                    this.Excerpt = this.Content.Substring(0, this.Content.IndexOf(config.ExcerptSeparator));
                }
                else
                {
                    this.Excerpt = this.Content;
                }
            }
        }
Example #5
0
        private static void Scan(List<SourceFilePath> pages, List<SourceFilePath> staticFiles, string path, string basePath, Config config)
        {
            var excludeFileNames = config.ExcludePatterns.SelectMany(x => Directory.EnumerateFileSystemEntries(path, x, SearchOption.TopDirectoryOnly));
            var fileNames = Directory.EnumerateFileSystemEntries(path, "*", SearchOption.TopDirectoryOnly).Where(x => !excludeFileNames.Contains(x));

            foreach (var fileName in fileNames)
            {
                string file = Path.GetFileName(fileName);

                // Ignore anything that starts with an "_".
                if (file.StartsWith("_"))
                    continue;

                if (Directory.Exists(fileName))
                {
                    Scan(pages, staticFiles, fileName, basePath, config);
                }
                else
                {
                    SourceFilePath sourceFilePath = new SourceFilePath()
                        {
                            AbsolutePath = Path.GetFullPath(fileName),
                            RelativePath = CalculateRelativePath(fileName, basePath)
                        };

                    if (config.PageFileNameExtensions.Contains(Path.GetExtension(file).Substring(1)))
                    {
                        pages.Add(sourceFilePath);
                    }
                    else
                    {
                        staticFiles.Add(sourceFilePath);
                    }
                }
            }
        }
Example #6
0
        public Post RenderPost(Config config, SourceFilePath path, Site site)
        {
            Post post = null;

            try
            {
                post = this.compiler.Compile<Post>(File.ReadAllText(path.AbsolutePath));
                post.Run(config, this.markdownTransformer, path.RelativePath, this, site);
            }
            catch (RazorCompilationException ex)
            {
                throw new GatsbyException(string.Format("Failed while compiling post {0}:\n\t{1}", path.AbsolutePath, string.Join("\n\t", ex.Errors)));
            }

            return post;
        }
Example #7
0
        public List<Generator> RenderPaginator(Config config, SourceFilePath path, Site site)
        {
            List<Generator> pagintors = new List<Generator>();
            RazorTemplateFactory<Generator> factory = null;

            try
            {
                factory = this.compiler.CompileFactory<Generator>(File.ReadAllText(path.AbsolutePath));
            }
            catch (RazorCompilationException ex)
            {
                throw new GatsbyException(string.Format("Failed while compiling paginator {0}:\n\t{1}", path.AbsolutePath, string.Join("\n\t", ex.Errors)));
            }

            int pageNumber = 1;

            while (true)
            {
                var template = factory.Create();
                template.PageNumber = pageNumber;
                template.Run(config, this.markdownTransformer, path.RelativePath, this, site);

                pagintors.Add(template);

                if (template.GeneratorFinished)
                    break;

                pageNumber++;
            }

            return pagintors;
        }
Example #8
0
        private void StartHttpServer(Config config)
        {
            this.httpServer.Start(config.Destination, config.BaseUrl);

            Console.WriteLine("Press any key to stop Http server...");
            Console.ReadKey();
        }
Example #9
0
        private bool BuildSite(Config config)
        {
            try
            {
                this.siteBuilder.Build(config);
            }
            catch (GatsbyException ex)
            {
                this.logger.Error("An error occured while building: {0}", ex.Message);
                return false;
            }
            catch (Exception ex)
            {
                this.logger.Error("An unexpected error occured while building: {0}", ex.Message);
                return false;
            }

            return true;
        }
Example #10
0
        public void Build(Config config)
        {
            SourceFiles sourceFiles = this.sourceFileEnumerator.Enumerate(config);

            if (Directory.Exists(config.Destination))
                Directory.Delete(config.Destination, true);

            Directory.CreateDirectory(config.Destination);

            this.pluginCompiler.RegisterAssemblyResolver();

            try
            {
                Site site = new Site(config);

                foreach (var path in sourceFiles.Plugins)
                {
                    string pluginPath = Path.Combine(config.Destination, Path.GetFileNameWithoutExtension(path.AbsolutePath) + ".dll");
                    this.pluginCompiler.Compile(path, pluginPath, site.Plugins);
                    this.razorRenderer.AddPluginPath(pluginPath);
                }

                this.razorRenderer.LoadIncludes(sourceFiles.Includes);
                this.razorRenderer.LoadLayouts(sourceFiles.Layouts);

                foreach (var path in sourceFiles.Posts)
                {
                    var post = this.razorRenderer.RenderPost(config, path, site);
                    this.SetDefaultsForContent(post);
                    site.Posts.Add(post);
                }

                site.Posts.Sort((x, y) => x.Date.CompareTo(y.Date) * -1);

                foreach (var path in sourceFiles.Pages)
                {
                    var page = this.razorRenderer.RenderPage(config, path, site);
                    this.SetDefaultsForContent(page);
                    site.Pages.Add(page);
                }

                site.Plugins.BeforeGenerators(site);

                foreach (var path in sourceFiles.Generators)
                {
                    var pages = this.razorRenderer.RenderPaginator(config, path, site);

                    foreach (var page in pages)
                        this.SetDefaultsForContent(page);

                    site.GeneratorPages.AddRange(pages);
                }

                site.Plugins.AfterGenerators(site);

                foreach (var post in site.Posts)
                {
                    this.WriteContent(config, post, site);
                }

                foreach (var page in site.Pages)
                {
                    this.WriteContent(config, page, site);
                }

                foreach (var page in site.GeneratorPages)
                {
                    this.WriteContent(config, page, site);
                }

                foreach (var staticFile in sourceFiles.StaticFiles)
                {
                    string destination = Path.Combine(config.Destination, staticFile.RelativePath);

                    string directory = Path.GetDirectoryName(destination);
                    if (!string.IsNullOrEmpty(directory))
                        Directory.CreateDirectory(directory);

                    File.Copy(staticFile.AbsolutePath, destination);
                }

                this.pluginCompiler.DeleteAll();
            }
            finally
            {
                this.pluginCompiler.UnregisterAssemblyResolver();
            }
        }
Example #11
0
        private void WriteContent(Config config, SiteContent page, Site site)
        {
            string content = page.Content;

            if (!string.IsNullOrEmpty(page.Layout))
                content = this.razorRenderer.LayoutContent(page.Layout, content, page, site);

            string destination = Path.Combine(config.Destination, page.Permalink);

            string directory = Path.GetDirectoryName(destination);
            if (!string.IsNullOrEmpty(directory))
                Directory.CreateDirectory(directory);

            File.WriteAllText(destination, content);
        }
Example #12
0
        internal virtual void Run(
            Config config,
            MarkdownTransformer markdownTransformer,
            string relativePath,
            RazorRenderer razorRenderer,
            Site site)
        {
            if (this.Content != null)
                throw new InvalidOperationException("Template has already been run.");

            this.RelativePath = relativePath;
            this.InitGatsbyRazorTemplate(razorRenderer, site);

            try
            {
                this.Content = this.ExecuteTemplate();
            }
            catch (Exception ex)
            {
            }

            if (this.IsMarkdown)
                this.Content = markdownTransformer.Transform(this.Content);
        }