|
| 1 | +require 'pathname' |
| 2 | + |
| 3 | +class AutoHelper |
| 4 | + attr_reader :root, :site, :tags, :authors |
| 5 | + |
| 6 | + def initialize(root, site) |
| 7 | + @root = Pathname(root) |
| 8 | + @site = site |
| 9 | + @tags = [] |
| 10 | + @authors = {} |
| 11 | + end |
| 12 | + |
| 13 | + def parse_tags(item) |
| 14 | + item.attributes[:tags].each { |tag| @tags << tag unless @tags.include?(tag) } |
| 15 | + end |
| 16 | + |
| 17 | + def parse_author(item) |
| 18 | + authors[item.attributes[:author]] = item.attributes[:author_full] |
| 19 | + end |
| 20 | + |
| 21 | + def parse_site |
| 22 | + site.items.each do |item| |
| 23 | + parse_tags (item) if item.attributes[:tags] |
| 24 | + parse_author(item) if item.attributes[:author] |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + def write_file(file, content) |
| 29 | + file.open('w') { |file| file.write(content) } |
| 30 | + end |
| 31 | + |
| 32 | + def render_layout(layout_name, mapping = {}) |
| 33 | + layout = @site.layouts.find { |l| l.identifier == layout_name.cleaned_identifier } |
| 34 | + filter = Nanoc::Filter.named('erb').new(mapping) |
| 35 | + filter.run(layout.raw_content) |
| 36 | + end |
| 37 | + |
| 38 | + def create_tag_page(tag_page, tag) |
| 39 | + write_file(tag_page, render_layout("templates/tag_page", tag: tag)) |
| 40 | + end |
| 41 | + |
| 42 | + def create_tag_feed(feed_page, tag) |
| 43 | + #TODO |
| 44 | + end |
| 45 | + |
| 46 | + def ensure_tag_page(tags_dir, tag) |
| 47 | + tag_page = tags_dir + "#{tag}.haml" |
| 48 | + feed_page = tags_dir + "#{tag}_feed.haml" |
| 49 | + |
| 50 | + create_tag_page(tag_page, tag) unless tag_page.exist? |
| 51 | + create_tag_feed(feed_page, tag) unless feed_page.exist? |
| 52 | + end |
| 53 | + |
| 54 | + def ensure_tag_pages |
| 55 | + tags_dir = root + 'content' + 'tags' |
| 56 | + tags_dir.mkpath |
| 57 | + |
| 58 | + tags.each do |tag| |
| 59 | + ensure_tag_page(tags_dir, tag) |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + def create_author_page(author_page, author, author_full) |
| 64 | + write_file(author_page, render_layout("templates/author_page", author: author, author_full: author_full)) |
| 65 | + end |
| 66 | + |
| 67 | + def create_author_feed(feed_page, author, author_full) |
| 68 | + #TODO |
| 69 | + end |
| 70 | + |
| 71 | + def ensure_author_page(authors_dir, author, author_full) |
| 72 | + author_page = authors_dir + "#{author}.haml" |
| 73 | + feed_page = authors_dir + "#{author}_feed.haml" |
| 74 | + |
| 75 | + create_author_page(author_page, author, author_full) unless author_page.exist? |
| 76 | + create_author_feed(feed_page, author, author_full) unless feed_page.exist? |
| 77 | + end |
| 78 | + |
| 79 | + def ensure_author_pages |
| 80 | + authors_dir = root + 'content' + 'authors' |
| 81 | + authors_dir.mkpath |
| 82 | + |
| 83 | + authors.each_pair do |author, author_full| |
| 84 | + ensure_author_page(authors_dir, author, author_full) |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + def auto |
| 89 | + parse_site |
| 90 | + ensure_tag_pages |
| 91 | + ensure_author_pages |
| 92 | + end |
| 93 | +end |
0 commit comments