require 'bitchannel'
module BitChannel
class Syntax
def block_ext__diary(arg)
puts "__diary:#{arg}__"
end
def block_ext__write_diary(arg)
puts "__write_diary:#{arg}__"
end
end
class WikiSpace
def view(name)
ViewPage.new(@config, @repository[name], @repository, true)
end
def diary
DiaryPage.new(@config, @repository)
end
end
class Handler
def handle_diary(req)
@wiki.diary.response
end
end
class WikiPage
def menuitem_diary_enabled?() true end
def diary_rex(user = nil)
/\A\w*#{user}(\d{8})\z/
end
def diary?(user = nil, page_name = nil)
page_name ||= @page.name
page_name =~ diary_rex(user)
end
def diary_page_list(num_entries = nil, user = nil)
num_entries = 5 if 0 == num_entries.to_i
@repository.pages.reject {|page| !diary?(user, page.name) }.sort_by {|page| page.name =~ diary_rex; $1 }.reverse[0, num_entries.to_i]
end
def diary_entries(num_entries = nil, user = nil)
diary_page_list(num_entries, user).map {|page| ViewPage.new(@config, page, @repository) }
end
def diary_body(num_entries = nil, user = nil)
html = []
diary_entries(num_entries, user).each do |entry|
html << %Q|
|
html << entry.__send__(:body)
end
html.join
end
def side_menu_body
compile_page(@repository['SideMenu'].source)
end
end
class ViewPage
def initialize(config, page, repo, top = nil)
super(config, page)
@repository = repo
@top_level = top
@revision = nil
end
def body
page = @page.source
page.gsub!(/\[\[\#comment\]\]/, '') if diary? and !@top_level
html = compile_page(page)
return html if diary?
{
'diary' => Proc.new { diary_body(*($1.to_s).split(/\s+/)) },
'write_diary' => Proc.new { page_name = "Diary#{$1}#{Time.now.strftime("%Y%m%d")}"; %Q|[Write a diary #{page_name}]
| },
}.each do |name, rep|
html.gsub!(/__#{name}:?(.*)__/, &rep)
end
html
end
end
class DiaryPage < WikiPage
def initialize(config, repo)
super config
@repository = repo
@length = 50
@size = 5
end
def last_modified
@repository.last_modified
end
private
def template_id
'diary'
end
def menuitem_diary_enabled?
false
end
end
class HistoryPage
def initialize(config, page)
super config, page
@revision = nil
end
end
class EditPage
def body
@text + ((diary? and @text !~ /\[\[\#comment\]\]/)? "\n\n\n\nComments\n[[#comment]]\n" : '')
end
end
end