Skip to content

Commit

Permalink
Refactoring (move tag-count code into tag generator method)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashes999 committed Jan 27, 2016
1 parent ef58c78 commit 553c328
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
11 changes: 5 additions & 6 deletions Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,19 @@ class Main {
//return result;
});

var tagCounts:Map<String, Int> = new Map<String, Int>();
var tags = new Array<String>();

// Calculate tag counts
for (post in posts) {
for (tag in post.tags) {
if (!tagCounts.exists(tag)) {
tagCounts.set(tag, 0);
if (tags.indexOf(tag) == -1) {
tags.push(tag);
}
tagCounts.set(tag, tagCounts.get(tag) + 1);
}
}

var layoutHtml = new butterfly.LayoutModifier(layoutFile, config).getHtml();
var generator = new butterfly.HtmlGenerator(layoutHtml, posts, pages, tagCounts);
var generator = new butterfly.HtmlGenerator(layoutHtml, posts, pages);
var writer = new butterfly.FileWriter(binDir);

for (post in posts) {
Expand All @@ -82,7 +81,7 @@ class Main {
writer.writePost(page, html);
}

for (tag in tagCounts.keys()) {
for (tag in tags) {
var html = generator.generateTagPageHtml(tag, posts);
writer.write('tag-${tag}.html', html);
}
Expand Down
28 changes: 20 additions & 8 deletions butterfly/HtmlGenerator.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ class HtmlGenerator {
private var layoutHtml:String;
private static inline var COTENT_PLACEHOLDER:String = '<butterfly-content />';
private static inline var PAGES_LINKS_PLACEHOLDER:String = '<butterfly-pages />';
private static inline var TAG_COUNT_PLACEHOLDER:String = '<butterfly-tags />';
private static inline var TAGS_PLACEHOLDER:String = '<butterfly-tags />';
private static inline var COMMENTS_PLACEHOLDER:String = '<butterfly-comments />';
private static inline var DISQUS_HTML_FILE:String = 'templates/disqus.html';
private static inline var DISQUS_PAGE_URL:String = 'PAGE_URL';
private static inline var DISQUS_PAGE_IDENTIFIER = 'PAGE_IDENTIFIER';

private var allContent:Array<butterfly.Post>;

public function new(layoutHtml:String, posts:Array<butterfly.Post>, pages:Array<butterfly.Post>, tagCounts:Map<String, Int>)
public function new(layoutHtml:String, posts:Array<butterfly.Post>, pages:Array<butterfly.Post>)
{
this.layoutHtml = layoutHtml;
if (this.layoutHtml.indexOf(COTENT_PLACEHOLDER) == -1) {
throw "Layout HTML doesn't have the blog post placeholder in it: " + COTENT_PLACEHOLDER;
}

// Pages first so if both a post and page share a title, the page wins.
this.allContent = pages.concat(posts);

var pagesHtml = this.generatePagesLinksHtml(pages);
this.layoutHtml = this.layoutHtml.replace(PAGES_LINKS_PLACEHOLDER, pagesHtml);

var tagCountHtml = this.generateTagCountHtml(tagCounts);
this.layoutHtml = this.layoutHtml.replace(TAG_COUNT_PLACEHOLDER, tagCountHtml);

// Pages first so if both a post and page share a title, the page wins.
this.allContent = pages.concat(posts);
var tagsHtml = this.generateTagsHtml();
this.layoutHtml = this.layoutHtml.replace(TAGS_PLACEHOLDER, tagsHtml);
}

public function generatePostHtml(post:butterfly.Post, config:Dynamic) : String
Expand Down Expand Up @@ -116,8 +116,20 @@ class HtmlGenerator {
return html;
}

private function generateTagCountHtml(tagCounts:Map<String, Int>) : String
private function generateTagsHtml() : String
{
var tagCounts:Map<String, Int> = new Map<String, Int>();

// Calculate tag counts
for (post in this.allContent) {
for (tag in post.tags) {
if (!tagCounts.exists(tag)) {
tagCounts.set(tag, 0);
}
tagCounts.set(tag, tagCounts.get(tag) + 1);
}
}

var tags = sortKeys(tagCounts);
var html = "<ul>";
for (tag in tags) {
Expand Down

0 comments on commit 553c328

Please sign in to comment.