Пример #1
0
        public static Page GetFileData(FileInfo file, SnowSettings snowSettings)
        {
            var rawPage         = 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 {slug}.(md|markdown)".OutputIfDebug();
                return(null);
            }

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

            var slug = fileNameMatches.Groups["slug"].Value.ToUrlSlug();

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

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

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

            var pageHeader = new Page
            {
                FileName       = file.Name,
                MarkdownHeader = rawSettings,
                Content        = bodySerialized,
                ContentExcerpt = excerptSerialized,
                Settings       = settings,
                Date           = date,
                Url            = "/" + slug
            };

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

            return(pageHeader);
        }
Пример #2
0
        private static void Main(string[] args)
        {
            StaticConfiguration.DisableErrorTraces = false;

            Console.WriteLine("Sandra.Snow : " + DateTime.Now.ToString("HH:mm:ss") + " : Begin processing");

            try
            {
                var commands = args.Select(x => x.Split('=')).ToDictionary(x => x[0], x => x[1]);

                if (commands.ContainsKey("debug"))
                {
                    DebugHelperExtensions.EnableDebugging();
                }

                if (commands.ContainsKey("vsdebug"))
                {
                    DebugHelperExtensions.WaitForContinue();
                }

                string currentDir;

                if (commands.ContainsKey("config"))
                {
                    currentDir = new FileInfo(commands["config"]).DirectoryName;
                }
                else
                {
                    currentDir = Path.GetDirectoryName(typeof(Program).Assembly.Location);
                }

                currentDir.OutputIfDebug(prefixWith: " - Current directory: ");

                var settings = CreateSettings(currentDir);

                var extensions = new HashSet <string>(new[] { ".md", ".markdown" }, StringComparer.OrdinalIgnoreCase);
                var files      = new DirectoryInfo(settings.Posts).EnumerateFiles()
                                 .Where(x => extensions.Contains(x.Extension));

                SetupOutput(settings);

                StaticPathProvider.Path = settings.CurrentDir;
                SnowViewLocationConventions.Settings = settings;

                var posts = files.Select(x => PostParser.GetFileData(x, settings))
                            .OrderByDescending(x => x.Date)
                            .Where(x => x.Published != Published.Private && !(x is Post.MissingPost))
                            .ToList();

                posts.SetPostUrl(settings);
                posts.UpdatePartsToLatestInSeries();

                TestModule.Posts      = posts;
                TestModule.Drafts     = posts.Where(x => x.Published == Published.Draft).ToList();
                TestModule.Categories = CategoriesPage.Create(posts);
                TestModule.PostsGroupedByYearThenMonth = ArchivePage.Create(posts);
                TestModule.MonthYear = ArchiveMenu.Create(posts);
                TestModule.Settings  = settings;

                var browserComposer = new Browser(with =>
                {
                    with.Module <TestModule>();
                    with.RootPathProvider <StaticPathProvider>();
                    with.ViewEngines(typeof(SuperSimpleViewEngineWrapper), typeof(RazorViewEngine));
                });

                // Compile all Posts
                posts.ForEach(x => ComposeParsedFiles(x, settings.Output, browserComposer));

                // Compile all Drafts
                var drafts = posts.Where(x => x.Published == Published.Draft).ToList();
                drafts.ForEach(x => ComposeDrafts(x, settings.Output, browserComposer));

                // Compile all static files
                foreach (var processFile in settings.ProcessFiles)
                {
                    var success =
                        ProcessFile(processFile, settings, posts, browserComposer);

                    if (!success)
                    {
                        break;
                    }
                }

                foreach (var copyDirectory in settings.CopyDirectories)
                {
                    var sourceDir = (settings.ThemesDir + Path.DirectorySeparatorChar + settings.Theme + Path.DirectorySeparatorChar + copyDirectory);

                    var destinationDir = copyDirectory;

                    if (copyDirectory.Contains(" => "))
                    {
                        var directorySplit = copyDirectory.Split(new[] { " => " }, StringSplitOptions.RemoveEmptyEntries);

                        sourceDir      = directorySplit[0];
                        destinationDir = directorySplit[1];
                    }

                    var source = Path.Combine(settings.CurrentDir, sourceDir);

                    if (!Directory.Exists(source))
                    {
                        source = Path.Combine(settings.CurrentDir, copyDirectory);

                        if (!Directory.Exists(source))
                        {
                            copyDirectory.OutputIfDebug("Unable to find the directory, so we're skipping it: ");
                            continue;
                        }
                    }

                    // If the destination directory is "." copy the folder files to the output folder root
                    var destination = destinationDir == "." ? settings.Output : Path.Combine(settings.Output, destinationDir);

                    new DirectoryInfo(source).Copy(destination, true);
                }

                foreach (var copyFile in settings.CopyFiles)
                {
                    var sourceFile      = (settings.ThemesDir + Path.DirectorySeparatorChar + settings.Theme + Path.DirectorySeparatorChar + copyFile);
                    var source          = Path.Combine(settings.CurrentDir, sourceFile);
                    var destinationFile = copyFile;

                    if (!File.Exists(source))
                    {
                        source = Path.Combine(settings.CurrentDir, copyFile);

                        if (!File.Exists(source))
                        {
                            copyFile.OutputIfDebug("Unable to find the directory, so we're skipping it: ");
                            continue;
                        }
                    }

                    var destination = Path.Combine(settings.Output, destinationFile);

                    File.Copy(source, destination, true);
                }

                Console.WriteLine("Sandra.Snow : " + DateTime.Now.ToString("HH:mm:ss") + " : Finish processing");

                if (commands.ContainsKey("server"))
                {
                    SnowServer.Start(settings);
                }

                if (commands.ContainsKey("debug"))
                {
                    DebugHelperExtensions.WaitForContinue();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.ToString());

                DebugHelperExtensions.WaitForContinue();
            }
        }
Пример #3
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Sandra.Snow : " + DateTime.Now.ToString("HH:mm:ss") + " : Begin processing");

            try
            {
                var commands = args.Select(x => x.Split('=')).ToDictionary(x => x[0], x => x[1]);

                if (commands.ContainsKey("debug"))
                {
                    DebugHelperExtensions.EnableDebugging();
                }

                string currentDir;

                if (commands.ContainsKey("config"))
                {
                    currentDir = new FileInfo(commands["config"]).DirectoryName;
                }
                else
                {
                    currentDir = Path.GetDirectoryName(typeof(Program).Assembly.Location);
                }

                currentDir.OutputIfDebug(prefixWith: "current directory: ");

                var settings = CreateSettings(currentDir);

                var extensions = new HashSet <string>(new[] { ".md", ".markdown" }, StringComparer.OrdinalIgnoreCase);
                var files      = new DirectoryInfo(settings.Posts).EnumerateFiles()
                                 .Where(x => extensions.Contains(x.Extension));

                SetupOutput(settings);

                StaticPathProvider.Path = settings.CurrentDir;
                SnowViewLocationConventions.Settings = settings;

                var browserParser = new Browser(with =>
                {
                    with.Module <TestModule>();
                    with.RootPathProvider <StaticPathProvider>();
                    with.ViewEngine <CustomMarkDownViewEngine>();
                });

                var posts = files.Select(x => PostParser.GetFileData(x, browserParser, settings))
                            .OrderByDescending(x => x.Date)
                            .ToList();

                posts.SetPostUrl(settings);
                posts.UpdatePartsToLatestInSeries();

                var categories = (from c in posts.SelectMany(x => x.Categories)
                                  group c by c
                                  into g
                                  select new Category
                {
                    Name = g.Key,
                    Count = g.Count()
                }).OrderBy(cat => cat.Name).ToList();

                TestModule.Posts      = posts;
                TestModule.Categories = categories;
                TestModule.PostsGroupedByYearThenMonth = GroupStuff(posts);
                TestModule.MonthYear = GroupMonthYearArchive(posts);
                TestModule.Settings  = settings;

                var browserComposer = new Browser(with =>
                {
                    with.Module <TestModule>();
                    with.RootPathProvider <StaticPathProvider>();
                    with.ViewEngines(typeof(SuperSimpleViewEngineWrapper), typeof(RazorViewEngine));
                });

                // Compile all Posts
                posts.ForEach(x => ComposeParsedFiles(x, settings.Output, browserComposer));

                // Compile all static files
                settings.ProcessFiles.ForEach(x => ProcessFiles(x, settings, posts, browserComposer));

                foreach (var copyDirectory in settings.CopyDirectories)
                {
                    var sourceDir      = copyDirectory;
                    var destinationDir = copyDirectory;

                    if (copyDirectory.Contains(" => "))
                    {
                        var directorySplit = copyDirectory.Split(new[] { " => " }, StringSplitOptions.RemoveEmptyEntries);

                        sourceDir      = directorySplit[0];
                        destinationDir = directorySplit[1];
                    }

                    var source      = Path.Combine(settings.CurrentDir, sourceDir);
                    var destination = Path.Combine(settings.Output, destinationDir);
                    new DirectoryInfo(source).Copy(destination, true);
                }

                if (commands.ContainsKey("debug"))
                {
                    DebugHelperExtensions.WaitForContinue();
                }

                Console.WriteLine("Sandra.Snow : " + DateTime.Now.ToString("HH:mm:ss") + " : Finish processing");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                DebugHelperExtensions.WaitForContinue();
            }
        }