Exemplo n.º 1
0
 void GenerateContent(ISiteContext context, PageModel model, string path, string segment, string name, List<PageInfo> list, int itemsPerPage)
 {
     int pages = PaginatorInfo.PageCount(itemsPerPage, list.Count);
     for (int page = 0; page < pages; page++)
     {
         GeneratePage(context, model, path, segment, name, list, page, page * itemsPerPage, itemsPerPage);
     }
 }
Exemplo n.º 2
0
        public void Generate(ISiteContext context, PageModel model)
        {
            int itemsPerPage = context.Config.ItemsPerPage;
            if (itemsPerPage <= 0)
            {
                throw new Exception("ItemsPerPage must be a positive number.");
            }

            if (!Directory.Exists(context.TemplatesDir))
            {
                Console.WriteLine("Templates directory not found.");
                return;
            }

            string template;
            string segment;
            Dictionary<string, List<PageInfo>> list;
            if (_type == ArchiveType.Category)
            {
                list = context.Categories;
                template = context.Config.CategoryTemplate;
                segment = context.Config.CategoryDir;
            }
            else // if (_type == ArchiveType.Tag)
            {
                list = context.Tags;
                template = context.Config.TagTemplate;
                segment = context.Config.TagDir;
            }

            string templatePath = Path.Combine(context.TemplatesDir, template);
            if (!File.Exists(templatePath))
            {
                Console.WriteLine("Warning: Template '{0}' not found.", template);
                return;
            }

            _template = new TemplateProcessor<PageModel>(context, templatePath);
            _template.Load();

            string dir = Path.Combine(context.DestinationDir, segment);

            Directory.CreateDirectory(dir);

            foreach (var item in list)
            {
                GenerateContent(context, model, templatePath, segment, item.Key, item.Value, itemsPerPage);
            }
        }
Exemplo n.º 3
0
        public virtual PageTemplate<PageModel> ProcessFile(string src, string dst, string name, PageModel model, StartTemplate<StartModel> startTemplate, Action<string, string> writer)
        {
            string cshtml;
            using (var reader = new StreamReader(src))
            {
                cshtml = reader.ReadToEnd();
            }

            try
            {
                return ProcessRazorTemplate(cshtml, src, dst, name, model, startTemplate, writer);
            }
            catch (Exception ex)
            {
                throw new PageProcessingException("Error processing Razor file.", src, cshtml, ex);
            }
        }
Exemplo n.º 4
0
        protected PageTemplate<PageModel> ProcessRazorTemplate(string cshtml, string path, string dst, string name, PageModel model, StartTemplate<StartModel> startTemplate, Action<string, string> writer)
        {
            // NOTE: On the first pass (scan), we don't have a destination.
            if (!String.IsNullOrWhiteSpace(dst))
                // Set global page depth. Lame but OK since we're single threaded.
                _context.PageDepth = FileUtility.GetDepthFromPath(_context.DestinationDir, dst);
            else
                _context.PageDepth = 0;

            // This model state changes from page to page.
            model.Source = FileUtility.GetRelativePath(_context.SourceDir, path);

            // The page may reference it's own info.
            if (model.PageMap.ContainsKey(model.Source))
                model.PageInfo = model.PageMap[model.Source];
            else
                // Provide a default on the scan pass to obviate null checks.
                model.PageInfo = new PageInfo();

            // Create an instance of the page template for this cshtml.
            if (!_context.PageTemplateService.HasTemplate(name))
                _context.PageTemplateService.Compile(cshtml, typeof(PageModel), name);
            var instance = _context.PageTemplateService.GetTemplate(cshtml, model, name);

            // Apply any _PageStart defaults.
            var pageTemplate = instance as PageTemplate<PageModel>;
            if (pageTemplate != null && startTemplate != null)
            {
                if (startTemplate.ForceLayout ||
                    (String.IsNullOrWhiteSpace(pageTemplate.Layout) &&
                    !String.IsNullOrWhiteSpace(startTemplate.Layout)))
                {
                    pageTemplate.Layout = startTemplate.Layout;
                }
            }

            string result = _context.PageTemplateService.Run(instance);

            if (writer != null)
                writer(dst, result);

            return pageTemplate;
        }
Exemplo n.º 5
0
        public override PageTemplate<PageModel> ProcessFile(string src, string dst, string name, PageModel model, StartTemplate<StartModel> startTemplate, Action<string, string> writer)
        {
            string front;
            string markdown;
            using (var reader = new StreamReader(src))
            {
                ReadMarkdown(reader, out front, out markdown);
            }

            string cshtml = "@{\r\n" + front + "}\r\n" + _markdown.Transform(markdown);

            try
            {
                return ProcessRazorTemplate(cshtml, src, dst, name, model, startTemplate, writer);
            }
            catch (Exception ex)
            {
                throw new PageProcessingException("Error processing Markdown file.", src, cshtml, ex);
            }
        }
Exemplo n.º 6
0
        void GeneratePage(ISiteContext context, PageModel model, string path, string segment, string name, List<PageInfo> list, int page, int skip, int take)
        {
            string format = FileUtility.GetArchiveUrl(context, segment, name, "{0}");
            string first = FileUtility.GetArchiveUrl(context, segment, name);
            string file = FileUtility.GetArchivePath(context, segment, name, false, page);

            // Provide a default list to the template. The template 
            // may present their own ordering using skip/take.
            PageInfo[] pages = list
                .OrderByDescending(p => p.Date)
                .Skip(skip)
                .Take(take)
                .ToArray();

            model.Paginator = new PaginatorInfo(pages, page, list.Count, skip, take, first, format);

            string result = _template.Build(model, file, true, (ctx) =>
                {
                    ctx.ViewBag.ArchiveName = name;
                    ctx.ViewBag.ArchiveType = _type.ToString();
                });
        }
Exemplo n.º 7
0
        void ProcessPages()
        {
            _pageModel = new PageModel();
            _razorProcessor = new RazorProcessor(_context);
            _markdownProcessor = new MarkdownProcessor(_context);
            _markdownProcessor.StartScan();

            Console.WriteLine("Scanning");
            string root = _context.ProjectDir;
            Walk(root, _siteDirName, root, _htmlDirName, false, 0);

            _pageModel = new PageModel(_context);
            _markdownProcessor.StartBuild();

            _pluginManager.PreBuild(_pageModel);

            Console.WriteLine("Building");
            Walk(root, _siteDirName, root, _htmlDirName, true, 0);

            _pluginManager.PostBuild(_pageModel);
        }
Exemplo n.º 8
0
 public void PostBuild(PageModel model)
 {
 }
Exemplo n.º 9
0
 public void PreBuild(PageModel model)
 {
 }
Exemplo n.º 10
0
        public override PageTemplate <PageModel> ProcessFile(string src, string dst, string name, PageModel model, StartTemplate <StartModel> startTemplate, Action <string, string> writer)
        {
            string front;
            string markdown;

            using (var reader = new StreamReader(src))
            {
                ReadMarkdown(reader, out front, out markdown);
            }

            string cshtml = "@{\r\n" + front + "}\r\n" + _markdown.Transform(markdown);

            try
            {
                return(ProcessRazorTemplate(cshtml, src, dst, name, model, startTemplate, writer));
            }
            catch (Exception ex)
            {
                throw new PageProcessingException("Error processing Markdown file.", src, cshtml, ex);
            }
        }
Exemplo n.º 11
0
        protected PageTemplate <PageModel> ProcessRazorTemplate(string cshtml, string path, string dst, string name, PageModel model, StartTemplate <StartModel> startTemplate, Action <string, string> writer)
        {
            // NOTE: On the first pass (scan), we don't have a destination.
            if (!String.IsNullOrWhiteSpace(dst))
            {
                // Set global page depth. Lame but OK since we're single threaded.
                _context.PageDepth = FileUtility.GetDepthFromPath(_context.DestinationDir, dst);
            }
            else
            {
                _context.PageDepth = 0;
            }

            // This model state changes from page to page.
            model.Source = FileUtility.GetRelativePath(_context.SourceDir, path);

            // The page may reference it's own info.
            if (model.PageMap.ContainsKey(model.Source))
            {
                model.PageInfo = model.PageMap[model.Source];
            }
            else
            {
                // Provide a default on the scan pass to obviate null checks.
                model.PageInfo = new PageInfo();
            }

            // Create an instance of the page template for this cshtml.
            if (!_context.PageTemplateService.HasTemplate(name))
            {
                _context.PageTemplateService.Compile(cshtml, typeof(PageModel), name);
            }
            var instance = _context.PageTemplateService.GetTemplate(cshtml, model, name);

            // Apply any _PageStart defaults.
            var pageTemplate = instance as PageTemplate <PageModel>;

            if (pageTemplate != null && startTemplate != null)
            {
                if (startTemplate.ForceLayout ||
                    (String.IsNullOrWhiteSpace(pageTemplate.Layout) &&
                     !String.IsNullOrWhiteSpace(startTemplate.Layout)))
                {
                    pageTemplate.Layout = startTemplate.Layout;
                }
            }

            string result = _context.PageTemplateService.Run(instance);

            if (writer != null)
            {
                writer(dst, result);
            }

            return(pageTemplate);
        }
Exemplo n.º 12
0
        public virtual PageTemplate <PageModel> ProcessFile(string src, string dst, string name, PageModel model, StartTemplate <StartModel> startTemplate, Action <string, string> writer)
        {
            string cshtml;

            using (var reader = new StreamReader(src))
            {
                cshtml = reader.ReadToEnd();
            }

            try
            {
                return(ProcessRazorTemplate(cshtml, src, dst, name, model, startTemplate, writer));
            }
            catch (Exception ex)
            {
                throw new PageProcessingException("Error processing Razor file.", src, cshtml, ex);
            }
        }