lib/utils.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
module SiteUtils def write_tag_page(dir, tag, count) # Create tag page meta = {} meta[:title] = "Tag: #{tag}" meta[:type] = 'page' meta[:filters_pre] = ['erb', 'redcloth'] meta[:permalink] = tag pl = (count == 1) ? ' is' : 's are' contents = %{\n#{count} item#{pl} tagged with _#{tag}_: <% articles_tagged_with('#{tag}').each do |pg| %>* <span class="<%= pg.attributes[:type] %>_link"> <a href="/articles/<%= pg.attributes[:permalink] %>/"><%= pg.attributes[:title] %></a></span> <% end %> } # Write html page write_item dir/"#{tag}.textile", meta, contents end def write_archive_page(dir, name, count) # Create archive page meta = {} meta[:title] = "Archive: #{name}" meta[:type] = 'page' meta[:filters_pre] = ['erb', 'redcloth'] meta[:permalink] = name.downcase.gsub /\s/, '-' pl = (count == 1) ? ' was' : 's were' contents = %{\n#{count} item#{pl} written in _#{name}_: <% articles_by_month.select{|i| i[0] == "#{name}"}[0][1].each do |pg| %>* <span class="<%= pg.attributes[:type] %>_link"> <a href="/articles/<%= pg.attributes[:permalink] %>/"><%= pg.attributes[:title] %></a></span> <% end %> } # Write file write_item dir/"#{meta[:permalink]}.textile", meta, contents end def write_item(path, meta, contents) path.parent.mkpath (path).open('w+') do |f| f.print "--" f.puts meta.to_yaml f.puts "-----" f.puts contents end end end module TypoUtils extend SiteUtils # Ignored by Nanoc 3 def get_filter(db, fid) filter = db[:text_filters].where("id = ?", fid).get(:name).downcase # Multiple filters are not handled (e.g. markdown smartypants) filter = filter.split(' ')[0] # Prepare metadata case filter when 'textile' then return ['redcloth'], 'textile' when 'markdown' then return ['bluecloth'], 'markdown' when 'bbcode' then return ['bbcode'], 'bbcode' else return [], 'txt' end end def get_tags(keywords=nil) tags = [] if keywords then if keywords.match ',' then tags = keywords.downcase.split(", ") else tags = keywords.downcase.split(" ") end end tags end def get_comments(db, aid) dataset = db[:feedback].where("article_id = ? && state LIKE '%ham%'", aid) comments = [] dataset.each do |c| comment = {} comment[:id] = c[:id] comment[:author] = c[:author] comment[:body] = c[:body].to_s comment[:url] = c[:url] comment[:date] = c[:published_at] comments << comment end comments end def convert_code_blocks(meta, contents) if contents.match /<typo:code/ then # troubles if erb filter is enabled! contents.gsub! /<%/, '<%' contents.gsub! /%>/, '%>' contents.gsub!(/<typo:code lang="([a-zA-Z0-9]+)">/, '<% highlight :\1 do %>') contents.gsub!(/<typo:code>/, '<% highlight :text do %>') contents.gsub!(/<\/typo:code>/, "<% end %>") meta['filters_pre'] = ['erb'].concat meta['filters_pre'] end contents end def write_page(meta, contents, extension) path = (meta['type'] == 'article') ? Pathname.new(Dir.pwd)/"content/articles/" : Pathname.new(Dir.pwd)/"content/" name = "#{meta['permalink']}.#{extension}" write_item path/name, meta, contents end end |