private SiteStaticContentContext CreateStaticContext(string sourceFolder)
        {
            var config     = new Dictionary <string, object>();
            var configPath = Path.Combine(sourceFolder, "config.yml");

            if (this._fileSystem.File.Exists(configPath))
            {
                try
                {
                    config =
                        (Dictionary <string, object>) this._fileSystem.File.ReadAllText(configPath).YamlHeader(true);
                }
                catch (ArgumentException ex)
                {
                    throw new ApplicationException(String.Format("{0}", "config.xml"), ex);
                }
            }

            var context = new SiteStaticContentContext()
            {
                SourceFolder = sourceFolder, Config = config
            };

            return(context);
        }
        public PublishingService(string sourceFolder, ITemplateEngine[] templateEngines)
        {
            this._templateEngines = templateEngines;
            this._fileSystem = new FileSystem();

            // Now lets build the context
            this._context = this.BuildSiteContext(sourceFolder);
        }
        public PublishingService(string sourceFolder, ITemplateEngine[] templateEngines)
        {
            this._templateEngines = templateEngines;
            this._fileSystem      = new FileSystem();

            // Now lets build the context
            this._context = this.BuildSiteContext(sourceFolder);
        }
        /// <summary>
        /// Loads all content items in the certain collection
        /// </summary>
        /// <param name="context"></param>
        /// <param name="collectionFolder"></param>
        /// <returns></returns>
        private IEnumerable <ContentItem> GetCollectionContentItemsInternal(
            SiteStaticContentContext context,
            string collectionFolder)
        {
            var extensions = new HashSet <string>(new[] { ".md", ".markdown", ".html" }, StringComparer.OrdinalIgnoreCase);
            var items      = new List <ContentItem>();

            if (_fileSystem.Directory.Exists(collectionFolder))
            {
                var files = _fileSystem.DirectoryInfo.FromDirectoryName(collectionFolder).GetFiles("*", SearchOption.AllDirectories)
                            .Where(x => extensions.Contains(x.Extension));
                items.AddRange(
                    files.Select(file => CreateContentItem(context, file.FullName))
                    .Where(post => post != null));
            }
            return(items);
        }
        private SiteStaticContentContext CreateStaticContext(string sourceFolder)
        {
            var config     = new Dictionary <string, object>();
            var configPath = Path.Combine(sourceFolder, "config.yml");

            if (this._fileSystem.File.Exists(configPath))
            {
                config =
                    (Dictionary <string, object>) this._fileSystem.File.ReadAllText(configPath).YamlHeader(true);
            }

            var context = new SiteStaticContentContext()
            {
                SourceFolder = sourceFolder, Config = config
            };

            return(context);
        }
        private string EvaluateLink(SiteStaticContentContext context, string path)
        {
            var directory     = Path.GetDirectoryName(path);
            var relativePath  = directory.Replace(context.SourceFolder, string.Empty);
            var fileExtension = Path.GetExtension(path);

            var htmlExtensions = new[] { ".markdown", ".mdown", ".mkdn", ".mkd", ".md", ".textile" };

            if (htmlExtensions.Contains(fileExtension, StringComparer.InvariantCultureIgnoreCase))
            {
                fileExtension = "";
            }

            var link = relativePath.Replace('\\', '/').TrimStart('/') + "/" + this.GetPageTitle(path) + fileExtension;

            if (!link.StartsWith("/"))
            {
                link = "/" + link;
            }
            return(link);
        }
        private ContentItem CreateContentItem(
            SiteStaticContentContext context,
            string path)
        {
            // 1: Read raw contents and meta data. Determine contents format and read it into Contents property, create RawContentItem.
            var rawItem = this.CreateRawItem(path);

            /// if a 'date' property is found in markdown file header, that date will be used instead of the date in the file name
            var date = rawItem.LastWriteTimeUtc;

            if (rawItem.Settings.ContainsKey("date"))
            {
                DateTime.TryParse((string)rawItem.Settings["date"], out date);
            }

            // 2: Use convert engines to get html contents and create ContentItem object
            foreach (var templateEngine in this._templateEngines)
            {
                if (templateEngine.CanProcess(rawItem.ContentType, "html"))
                {
                    var content = templateEngine.Process(rawItem.Content, rawItem.Settings);
                    var page    = new ContentItem {
                        FullContent = content
                    };

                    var relativePath = this.EvaluateLink(context, path);
                    page.SetHeaderSettings(rawItem.Settings);
                    page.Settings = rawItem.Settings;
                    page.Url      = rawItem.Settings.ContainsKey("permalink")
                        ? rawItem.Settings["permalink"]
                        : relativePath;
                    page.FileName = relativePath;
                    page.Date     = date;
                    return(page);
                }
            }

            return(null);
        }
 /// <summary>
 /// Loads all content items in the certain collection
 /// </summary>
 /// <param name="context"></param>
 /// <param name="collectionFolder"></param>
 /// <returns></returns>
 private IEnumerable<ContentItem> GetCollectionContentItemsInternal(
     SiteStaticContentContext context,
     string collectionFolder)
 {
     var extensions = new HashSet<string>(new[] { ".md", ".markdown", ".html" }, StringComparer.OrdinalIgnoreCase);
     var items = new List<ContentItem>();
     if (_fileSystem.Directory.Exists(collectionFolder))
     {
         var files = _fileSystem.DirectoryInfo.FromDirectoryName(collectionFolder).GetFiles("*", SearchOption.AllDirectories)
             .Where(x => extensions.Contains(x.Extension));
         items.AddRange(
             files.Select(file => CreateContentItem(context, file.FullName))
                 .Where(post => post != null).OrderByDescending(x=>x.Date));
     }
     return items;
 }
        private string EvaluateLink(SiteStaticContentContext context, string path)
        {
            var directory = Path.GetDirectoryName(path);
            var relativePath = directory.Replace(context.SourceFolder, string.Empty);
            var fileExtension = Path.GetExtension(path);

            var htmlExtensions = new[] { ".markdown", ".mdown", ".mkdn", ".mkd", ".md", ".textile" };

            if (htmlExtensions.Contains(fileExtension, StringComparer.InvariantCultureIgnoreCase))
            {
                fileExtension = "";
            }

            var link = relativePath.Replace('\\', '/').TrimStart('/') + "/" + this.GetPageTitle(path) + fileExtension;
            if (!link.StartsWith("/"))
            {
                link = "/" + link;
            }
            return link;
        }
        private SiteStaticContentContext CreateStaticContext(string sourceFolder)
        {
            var config = new Dictionary<string, object>();
            var configPath = Path.Combine(sourceFolder, "config.yml");
            if (this._fileSystem.File.Exists(configPath))
            {
                try
                {
                    config =
                    (Dictionary<string, object>)this._fileSystem.File.ReadAllText(configPath).YamlHeader(true);
                }
                catch (ArgumentException ex)
                {
                    throw new ApplicationException(String.Format("{0}", "config.xml"), ex);
                }
                
            }

            var context = new SiteStaticContentContext() { SourceFolder = sourceFolder, Config = config };

            return context;
        }
        private ContentItem CreateContentItem(
            SiteStaticContentContext context,
            string path)
        {
            // 1: Read raw contents and meta data. Determine contents format and read it into Contents property, create RawContentItem.
            var rawItem = this.CreateRawItem(path);

            /// if a 'date' property is found in markdown file header, that date will be used instead of the date in the file name
            var date = rawItem.LastWriteTimeUtc;
            if (rawItem.Settings.ContainsKey("date"))
                DateTime.TryParse((string)rawItem.Settings["date"], out date);

            // 2: Use convert engines to get html contents and create ContentItem object
            foreach (var templateEngine in this._templateEngines)
            {
                if (templateEngine.CanProcess(rawItem.ContentType, "html"))
                {
                    var content = templateEngine.Process(rawItem.Content, rawItem.Settings);
                    var page = new ContentItem { FullContent = content };

                    var relativePath = this.EvaluateLink(context, path);
                    page.SetHeaderSettings(rawItem.Settings);
                    page.Settings = rawItem.Settings;
                    page.Url = rawItem.Settings.ContainsKey("permalink")
                        ? rawItem.Settings["permalink"]
                        : relativePath;
                    page.FileName = relativePath;
                    page.Date = date;
                    return page;
                }
            }

            return null;
        }
        private SiteStaticContentContext CreateStaticContext(string sourceFolder)
        {
            var config = new Dictionary<string, object>();
            var configPath = Path.Combine(sourceFolder, "config.yml");
            if (this._fileSystem.File.Exists(configPath))
            {
                config =
                    (Dictionary<string, object>)this._fileSystem.File.ReadAllText(configPath).YamlHeader(true);
            }

            var context = new SiteStaticContentContext() { SourceFolder = sourceFolder, Config = config };

            return context;
        }