Class | NewsTag |
In: |
lib/webgen/plugins/tags/news.rb
|
Parent: | Tags::DefaultTag |
This plugin creates a ‘news’ tag which can be used to display the contents of a news file.
The news file by default is ‘news.yaml’ and placed in the root of the webgen directory. This can be changed with the ‘filename’ parameter.
The yaml file has the basic format of
date: content date: content
Where the date has the format indicated by the ‘dateFormat’ parmater, which by default is YYYY-MM-DD. The content is formated according to the ‘contentFormat’ parameter and is textile by default. I recommend using the ’|’ version of block text for the content. For example:
2007-03-20: | h2. this is an entry This is some content
When utilzed in a template the ‘news’ tag can optionally take to additional parameters ‘maxEntries’ and ‘maxParagraphs’.
maxEntries: the N most recent entries by date in the news.yaml file to display. maxParagraphs: the content of an entry is truncated to N paragraphs, where a paragraphs ending is defined by "\n\n"
So the following usage of the news tag would disply the first paragraph of the most recent item in the news.yaml file.
{news: {options: {maxEntries: 1, maxParagraphs: 1}}}
While this usage would display all the contents of the news.yaml file sorted in reverse chronological order and displayed fully.
{news: }
# File lib/webgen/plugins/tags/news.rb, line 87 87: def process_tag( tag, chain ) 88: content = StringIO.new 89: begin 90: news_file = param( 'filename' ) 91: filename = File.join( param( 'websiteDir', 'Core/Configuration' ), news_file ) unless filename =~ /^(\/|\w:)/ 92: data = YAML::load( File.read(filename) ) 93: 94: start_date_tag = param('dateTag') 95: end_date_tag = start_date_tag.gsub(/ .*/,'') # in case people put attributes on the tag 96: start_content_tag = param('contentTag') || '' 97: end_content_tag = start_content_tag.gsub(/ .*/,'') # in case people put attributes on the tag 98: 99: format = param('dateFormat') 100: content_handler= @plugin_manager['ContentConverter/Default'].registered_handlers[param('contentFormat')] 101: 102: limit_entries(data).each do |datetime,entry| 103: content.puts "<#{start_date_tag}>#{datetime.strftime(format)}</#{end_date_tag}>" 104: content.print "<#{start_content_tag}>" if start_content_tag.length > 0 105: content.print content_handler.call(entry) 106: content.print "</#{end_content_tag}>" if start_content_tag.length > 0 107: end 108: rescue => boom 109: log(:error) { "Given file <#{filename}> specified in <#{chain.first.node_info[:src]}> does not exist or can't be read" } 110: end 111: content.string 112: end
# File lib/webgen/plugins/tags/news.rb, line 118 118: def limit_entries(data) 119: # convert the entries to something sortable by date 120: time_content_entries = [] 121: data.each_pair do |date,content| 122: case date 123: when String 124: p = ParseDate.parsedate(date) 125: when Date 126: p = [date.year, date.month, date.day] 127: when DateTime 128: p = [date.year, date.month, date.day, date.hour, date.min, date.sec] 129: end 130: time_content_entries << [Time.mktime(*p), content] 131: end 132: 133: # limit the entries if there is an options for it 134: limit = param('options')['maxEntries'] || time_content_entries.size 135: max_paragraphs = param('options')['maxParagraphs'] || nil 136: 137: # we want a descending sort 138: time_content_entries.sort! { |a,b| b[0] <=> a[0] } 139: 140: if max_paragraphs then 141: max_paragraphs = max_paragraphs.to_i 142: time_content_entries[0...limit].collect do |e| 143: e[1] = e[1].split("\n\n")[0...max_paragraphs].join("\n\n") 144: e 145: end 146: else 147: time_content_entries[0...limit] 148: end 149: end