public static void Generate(Site site) { LoadData(site); //Read templates var indexTemplate = new IndexTemplate(site); //Clean web target FileManager.Clean(site.WebPath); //Copy static: Done in calling bash script FileManager.Clone(site.StaticPath, site.WebPath); //Generate news indexTemplate["site:news"] = GenerateNews(site); var pageTemplate = new PageTemplate(site); var postTemplate = new PostTemplate(site); //Generate all files GenerateIndex(indexTemplate, site); GenerateFeed(site); foreach (Page p in site.Pages) GeneratePage(site, p, pageTemplate, indexTemplate); foreach (Post p in site.Posts) GeneratePost(site, p, postTemplate, indexTemplate); GenerateNewPostPageTemplate(site); Compressor.CompressDirectoryRecursive(site.WebPath); }
static void GenerateFeed(Site site) { string feedDir = Path.Combine(site.WebPath, "feed"); Directory.CreateDirectory(feedDir); using (TextWriter w = new StreamWriter(Path.Combine(feedDir, ".htaccess"), false, Encoding.ASCII)) { w.WriteLine("DirectoryIndex index.atom"); w.WriteLine("ForceType application/atom+xml"); w.WriteLine("AddType application/atom+xml .atom"); } var feedTemplate = new FeedTemplate(site); var entryTemplate = new FeedEntryTemplate(site); //At least 5 posts and one month Html entries = new Html(); int posts = 0; DateTime expire = DateTime.Now.AddMonths(-1); foreach (Post p in site.Posts) { if (posts > 5 && p.Date < expire && p.LastModified < expire) continue; entries += entryTemplate.Generate(site, p); posts += 1; } feedTemplate.Generate(entries, Path.Combine(feedDir, "index.atom")); }
public Html Generate(Site site, Page page) { var t = new TemplateInstance(this); t.LastModified = page.LastModified; t["title"] = page.Title; t["contents"] = Html.Raw(Generator.Markdown.Transform(page.Content)); return t.ToHtml(); }
public TemplateInstance Create(Site site) { var t = new TemplateInstance(this); t.LastModified = site.LastModified; t["site:path"] = site.UrlPath; t["site:title"] = site.Title; return t; }
public Template(Site site, string filename) { this.site = site; this.filename = filename; string path = Path.Combine(site.TemplatePath, filename); string raw = File.ReadAllText(path, Encoding.UTF8); LastModified = File.GetLastWriteTime(path); int index = 0; while (true) { int tag = raw.IndexOf("<:", index); //Last content if (tag < 0) { if (index < raw.Length) Items.Add(new TemplateItem(Html.Raw(raw.Substring(index)))); //Console.WriteLine("Template Last: " + index + ", " + tag); break; } //Content before tag if (tag > index) { Items.Add(new TemplateItem(Html.Raw(raw.Substring(index, tag - index)))); //Console.WriteLine("Template cont: " + index + ", " + tag); index = tag; } //Tag tag = raw.IndexOf(">", index); if (tag < 0) throw new TemplateFormatException("Expected matching >"); //Console.WriteLine("Template Tag: " + index + ", " + tag); string tagVal = raw.Substring(index + 2, tag - index - 2).Trim().Trim(':').ToLowerInvariant(); //include .md tag contents if (tagVal.EndsWith(".md")) { string mdpath = Path.Combine(site.DataPath, tagVal); string text = File.ReadAllText(mdpath, Encoding.UTF8); Items.Add(new TemplateItem(Html.Raw(Generator.Markdown.Transform(text)))); DateTime fileTime = File.GetLastWriteTime(mdpath); if (LastModified < fileTime) LastModified = fileTime; } else { //Add tag Items.Add(new TemplateItem(tagVal)); } index = tag + 1; } }
public Html Generate(Site site, Post post) { var t = new TemplateInstance(this); t.LastModified = post.LastModified; t["title"] = post.Title; t["author"] = post.Author; t["url"] = site.URL + post.Path; t["updated"] = post.LastModified.ToUniversalTime().ToString("yyyy-MM-dd\\THH:mm:ss\\Z"); t["excerpt"] = Generator.Markdown.Transform(post.ExcerptMarkdown).Trim(); t["content"] = Generator.Markdown.Transform(post.Content).Trim(); return t.ToHtml(); }
public Html Generate(Site site, Post post) { var t = new TemplateInstance(this); t.LastModified = post.LastModified; t["title"] = post.Title; t["gmdate"] = post.Date.ToUniversalTime().ToString("yyyy-MM-dd\\THH:mm:ss\\Z"); t["longdate"] = post.Date.ToString("ddd dd MMM yyyy"); t["monthyear"] = post.Date.ToString("MMM yyyy"); t["updated"] = post.LastModified.ToString("ddd dd MMM yyyy"); t["url"] = site.UrlPath + post.Path; t["excerpt"] = Html.Raw(Generator.Markdown.Transform(post.ExcerptMarkdown)); t["title"] = post.Title; return t.ToHtml(); }
static void GenerateIndex(IndexTemplate template, Site site) { var indexInstance = template.Create(site); if (site.GetPage("index") != null) return; var compactPostTemplate = new CompactPostTemplate(site); indexInstance["title"] = site.Title; indexInstance["tabs"] = GenerateTabs(site, null, null); indexInstance["contents"] = new Html(); foreach (Post p in site.Posts) { indexInstance["contents"] += compactPostTemplate.Generate(site, p); } indexInstance.Write(Path.Combine(site.WebPath, "index.html")); }
public static int Main(string[] args) { try { string path = Environment.CurrentDirectory; if (args.Length > 0) path = Path.GetFullPath(Path.Combine(path, args[0])); Site site = new Site(path); Console.WriteLine("Generating site: " + site.Title); Generator.Generate(site); Console.WriteLine("All done"); return 0; } catch (Exception e) { Console.Error.WriteLine("Error:"); Console.Error.WriteLine(e.Message); Console.Error.WriteLine(e.StackTrace); if (e.InnerException != null) Console.Error.WriteLine(e.InnerException.Message); return -1; } }
static Html GenerateNews(Site site) { Html news = Html.Raw("<ul>"); string lastMonth = ""; foreach (Post p in site.Posts) { string month = p.Date.ToString("MMM yyyy"); if (month != lastMonth) { lastMonth = month; news += Html.Raw("</ul><h3>") + month + Html.Raw("</h3><ul>"); } news += LiTag(site.UrlPath + p.Path, "", p.Title, false, p.LastModified); } news += Html.Raw("</ul>"); return news; }
public PageTemplate(Site site) : base(site, "Page.html") { }
public FeedTemplate(Site site) : base(site, "feed.xml") { }
static void LoadData(Site site) { //Scan for pages string[] pageFiles = Directory.GetFiles(site.DataPath, "*.page", SearchOption.AllDirectories); foreach (string pageFile in pageFiles) { Page p = new Page(site.DataPath, pageFile); //Ignore news pages if (p.HasDate) continue; site.Add(p); //Console.WriteLine("Page: " + p); } site.Pages.Sort(Page.ComparerIndex); //Scan for posts string[] postFiles = Directory.GetFiles(site.DataPath, "*.post", SearchOption.AllDirectories); foreach (string postFile in postFiles) { Post p = new Post(postFile); //Ignore non news pages if (p.Date == DateTime.MinValue) continue; site.Add(p); //Console.WriteLine("News: " + p); } site.Posts.Sort(Post.ComparerLatestFirst); }
static Html GenerateTabs(Site site, Page page, Post post) { //Find out if we have an index page Page indexPage = null; foreach (Page p in site.Pages) { if (p.Name == "index") { indexPage = p; break; } } Html tabs = new Html(); if (indexPage == null) tabs += LiTag(site.UrlPath, "", "News", page == null && post == null, DateTime.MinValue); else tabs += LiTag(site.UrlPath, indexPage.Title, indexPage.LinkTitle, page == indexPage, indexPage.LastModified); foreach (Page p in site.Pages) { if (p.LinkTitle == "") continue; if (p == indexPage) continue; tabs += LiTag(p.LinkUrl ?? site.UrlPath + p.Path, p.Title, p.LinkTitle, p == page, p.LastModified); } return tabs; }
static void GeneratePost(Site site, Post p, PostTemplate postTemplate, IndexTemplate indexTemplate) { string dirPath = Path.Combine(site.WebPath, p.Path); string htmlPath = Path.Combine(dirPath, "index.html"); Directory.CreateDirectory(dirPath); if (Directory.Exists(p.SourceDir)) FileManager.Clone(p.SourceDir, dirPath); var inst = indexTemplate.Create(site); inst["title"] = p.Title; inst["tabs"] = GenerateTabs(site, null, p); inst["contents"] = postTemplate.Generate(site, p); inst.Write(htmlPath); }
public IndexTemplate(Site site) : base(site, "index.html") { }
static void GenerateNewPostPageTemplate(Site site) { string postTemplatePath = Path.Combine(site.DataPath, "new.post-dist"); using (TextWriter w = new StreamWriter(postTemplatePath, false, Encoding.UTF8)) { w.WriteLine("Date: " + DateTime.Now.ToString("yyyy-MM-dd")); w.WriteLine("Author: "); w.WriteLine("Title: "); w.WriteLine(); w.WriteLine("# "); w.WriteLine(); w.WriteLine(); } string pageTemplatePath = Path.Combine(site.DataPath, "new.page-dist"); using (TextWriter w = new StreamWriter(pageTemplatePath, false, Encoding.UTF8)) { w.WriteLine("Title: "); w.WriteLine("#LinkTitle: "); w.WriteLine("#LinkUrl: "); w.WriteLine("Index: "); w.WriteLine(); w.WriteLine("# "); w.WriteLine(); w.WriteLine(); } }
public PostTemplate(Site site) : base(site, "BlogPost.html") { }
public CompactPostTemplate(Site site) : base(site, "BlogPostCompact.html") { }
static void GeneratePage(Site site, Page p, PageTemplate pageTemplate, IndexTemplate indexTemplate) { string dirPath = Path.Combine(site.WebPath, p.Path); if (p.Path == "index/") dirPath = site.WebPath; Directory.CreateDirectory(dirPath); if (Directory.Exists(p.SourceDir)) FileManager.Clone(p.SourceDir, dirPath); var indexInstance = indexTemplate.Create(site); indexInstance["title"] = p.Title; indexInstance["tabs"] = GenerateTabs(site, p, null); indexInstance["contents"] = pageTemplate.Generate(site, p); indexInstance.Write(Path.Combine(dirPath, "index.html")); }
public FeedEntryTemplate(Site site) : base(site, "feed.entry.xml") { }