コード例 #1
0
ファイル: PostParser.cs プロジェクト: horsdal/Sandra.Snow
        public static Post GetFileData(FileInfo file, SnowSettings snowSettings)
        {
            var rawPost = File.ReadAllText(file.FullName);
            var fileNameMatches = FileNameRegex.Match(file.Name);
            var rawSettings = string.Empty;

            if (!fileNameMatches.Success)
            {
                file.Name.OutputIfDebug(" - Skipping file: ");
                " - File does not match the format {year}-{month}-{day}-{slug}.(md|markdown)".OutputIfDebug();
                return new Post.MissingPost();
            }

            var result = MarkdownFileParser.ParseDataFromFile(rawPost);
            var settings = ParseSettings(result.Header);

            var year = fileNameMatches.Groups["year"].Value;
            var month = fileNameMatches.Groups["month"].Value;
            var day = fileNameMatches.Groups["day"].Value;
            var slug = fileNameMatches.Groups["slug"].Value.ToUrlSlug();
            var date = DateTime.ParseExact(year + month + day, "yyyyMMdd", CultureInfo.InvariantCulture);

            /// if a 'date' property is found in markdown file header, that date will be used instead of the date in the file name
            if (settings.ContainsKey("date"))
            {
                try
                {
                    date = DateTime.Parse((string)settings["date"]);
                }
                finally
                {
                    /// do nothing, let the current 'date' be as is
                }
            }

            var bodySerialized = Markdown.Transform(result.Body);
            var excerptSerialized = Markdown.Transform(result.Excerpt ?? string.Empty);

            var postHeader = new Post
            {
                FileName = file.Name,
                MarkdownHeader = rawSettings,
                Content = bodySerialized,
                ContentExcerpt = excerptSerialized,
                Settings = settings,
                Year = date.Year,
                Month = date.Month,
                Day = date.Day,
                Date = date,
                Url = slug
            };

            postHeader.SetSnowSettings(snowSettings);
            postHeader.SetHeaderSettings(settings);

            return postHeader;
        }
コード例 #2
0
        public static Post GetFileData(FileInfo file, Browser browser, SnowSettings snowSettings)
        {
            var response = browser.Get("/post/" + HttpUtility.UrlEncodeUnicode(file.Name));
            var rawPost = File.ReadAllText(file.FullName);
            var fileNameMatches = FileNameRegex.Match(file.Name);
            var settings = new Dictionary<string, dynamic>();
            var rawSettings = string.Empty;

            if (!fileNameMatches.Success)
            {
                throw new ApplicationException("File " + file.Name +
                                               " does not match the format {year}-{month}-{day}-{slug}.(md|markdown)");
            }

            //Get the Header info from a Post Markdown File
            //Find the first index of ---
            var startOfSettingsIndex = rawPost.IndexOf("---", StringComparison.InvariantCultureIgnoreCase);
            if (startOfSettingsIndex >= 0)
            {
                //Find the second index of --- after the first
                var endOfSettingsIndex = rawPost.IndexOf("---", startOfSettingsIndex + 3,
                                                         StringComparison.InvariantCultureIgnoreCase);

                //If we find the 2nd index, parse the settings
                //Otherwise we assume there's no header or settings...
                if (endOfSettingsIndex >= 0)
                {
                    rawSettings = rawPost.Substring(startOfSettingsIndex + 3, endOfSettingsIndex - 3);
                    settings = ParseSettings(rawSettings);
                }
            }

            var year = fileNameMatches.Groups["year"].Value;
            var month = fileNameMatches.Groups["month"].Value;
            var day = fileNameMatches.Groups["day"].Value;
            var slug = fileNameMatches.Groups["slug"].Value.ToUrlSlug();
            var date = DateTime.ParseExact(year + month + day, "yyyyMMdd", CultureInfo.InvariantCulture);

            var result = new Post
            {
                FileName = file.Name,
                MarkdownHeader = rawSettings,
                Content = response.Body.AsString(),
                Settings = settings,
                Year = date.Year,
                Month = date.Month,
                Day = date.Day,
                Date = date,
                Url = slug
            };

            result.SetSnowSettings(snowSettings);
            result.SetHeaderSettings(settings);

            return result;
        }
コード例 #3
0
ファイル: PostParser.cs プロジェクト: jessetechie/Sandra.Snow
        public static Post GetFileData(FileInfo file, SnowSettings snowSettings)
        {
            var rawPost = File.ReadAllText(file.FullName);
            var fileNameMatches = FileNameRegex.Match(file.Name);
            var rawSettings = string.Empty;

            if (!fileNameMatches.Success)
            {
                file.Name.OutputIfDebug(" - Skipping file: ");
                " - File does not match the format {year}-{month}-{day}-{slug}.(md|markdown)".OutputIfDebug();
                return new Post.MissingPost();
            }

            var result = MarkdownFileParser.ParseDataFromFile(rawPost);
            var settings = ParseSettings(result.Header);

            var year = fileNameMatches.Groups["year"].Value;
            var month = fileNameMatches.Groups["month"].Value;
            var day = fileNameMatches.Groups["day"].Value;
            var slug = fileNameMatches.Groups["slug"].Value.ToUrlSlug();
            var date = DateTime.ParseExact(year + month + day, "yyyyMMdd", CultureInfo.InvariantCulture);

            var bodySerialized = Markdown.Transform(result.Body);
            var excerptSerialized = Markdown.Transform(result.Excerpt ?? string.Empty);

            var postHeader = new Post
            {
                FileName = file.Name,
                MarkdownHeader = rawSettings,
                Content = bodySerialized,
                ContentExcerpt = excerptSerialized,
                Settings = settings,
                Year = date.Year,
                Month = date.Month,
                Day = date.Day,
                Date = date,
                Url = slug
            };

            postHeader.SetSnowSettings(snowSettings);
            postHeader.SetHeaderSettings(settings);

            return postHeader;
        }
コード例 #4
0
ファイル: PostParser.cs プロジェクト: kunjee17/Sandra.Snow
        public static Post GetFileData(FileInfo file, Browser browser, SnowSettings snowSettings)
        {
            var response = browser.Get("/post/" + HttpUtility.UrlEncodeUnicode(file.Name));
            var rawPost = File.ReadAllText(file.FullName);
            var fileNameMatches = FileNameRegex.Match(file.Name);
            var rawSettings = string.Empty;

            if (!fileNameMatches.Success)
            {
                file.Name.OutputIfDebug(" - Skipping file: ");
                " - File does not match the format {year}-{month}-{day}-{slug}.(md|markdown)".OutputIfDebug();
                return new Post.MissingPost();
            }

            var result = ParseDataFromFile(rawPost);
            var settings = ParseSettings(result.Item1);

            var year = fileNameMatches.Groups["year"].Value;
            var month = fileNameMatches.Groups["month"].Value;
            var day = fileNameMatches.Groups["day"].Value;
            var slug = fileNameMatches.Groups["slug"].Value.ToUrlSlug();
            var date = DateTime.ParseExact(year + month + day, "yyyyMMdd", CultureInfo.InvariantCulture);

            var postHeader = new Post
            {
                FileName = file.Name,
                MarkdownHeader = rawSettings,
                Content = response.Body.AsString(),
                Settings = settings,
                Year = date.Year,
                Month = date.Month,
                Day = date.Day,
                Date = date,
                Url = slug
            };

            postHeader.SetSnowSettings(snowSettings);
            postHeader.SetHeaderSettings(settings);

            return postHeader;
        }