Class: AutomaticSummary

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/default-helpers.rb

Direct Known Subclasses

AutomaticList, AutomaticRecentChanges

Constant Summary collapse

DEFAULT_SETTINGS =
{
  :max_pages_to_show => nil,
  :description => 'This summary was created automatically',
  :author => 'AutomaticSummary',
  :lines_to_include => 10,
  :sort_pages_by => :created_on, # Could be :revised_on or :score or :name or :name_for_index, or :author
  :reverse_sort => false,
  :remove_deleted_pages => true, # If false will keep references to deleted pages
  :summarise_revisions => false, # If true will list revisions rather than pages
  :merge_revisions_within => false, # If set to a number, repeats with the same author within that many seconds will be merged
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wiki, name, settings = {}, &decision) ⇒ AutomaticSummary



69
70
71
72
73
74
75
# File 'lib/helpers/default-helpers.rb', line 69

def initialize( wiki, name, settings = {}, &decision )
  @wiki, @name, @decision = wiki, name, decision
  @settings = DEFAULT_SETTINGS.merge( settings )
  @summary = FiniteUniqueList.new( @settings[:max_pages_to_show], @settings[:reverse_sort], @settings[:sort_pages_by] )
  add_existing_pages
  start_watching wiki
end

Instance Attribute Details

#decisionObject (readonly)

Returns the value of attribute decision.



67
68
69
# File 'lib/helpers/default-helpers.rb', line 67

def decision
  @decision
end

#nameObject (readonly)

Returns the value of attribute name.



67
68
69
# File 'lib/helpers/default-helpers.rb', line 67

def name
  @name
end

#settingsObject (readonly)

Returns the value of attribute settings.



67
68
69
# File 'lib/helpers/default-helpers.rb', line 67

def settings
  @settings
end

#summaryObject (readonly)

Returns the value of attribute summary.



67
68
69
# File 'lib/helpers/default-helpers.rb', line 67

def summary
  @summary
end

#wikiObject (readonly)

Returns the value of attribute wiki.



67
68
69
# File 'lib/helpers/default-helpers.rb', line 67

def wiki
  @wiki
end

Instance Method Details

#add_existing_pagesObject



102
103
104
105
106
107
108
# File 'lib/helpers/default-helpers.rb', line 102

def add_existing_pages
  if settings[:summarise_revisions]
    scan_revisions_allready_in_wiki
  else 
    scan_pages_allready_in_wiki
  end
end

#check_new(thing) ⇒ Object



89
90
91
92
93
94
# File 'lib/helpers/default-helpers.rb', line 89

def check_new(thing)
  return unless summarise? thing
  remove_previous_revisions thing 
  summary.add thing  
  render_summary_page
end

#confirm_old(thing) ⇒ Object



84
85
86
87
# File 'lib/helpers/default-helpers.rb', line 84

def confirm_old(thing)
  summary.remove(thing) unless summarise?( thing )
  render_summary_page
end

#new_tailObject



174
175
176
# File 'lib/helpers/default-helpers.rb', line 174

def new_tail
  "</automaticsummary>"
end

#new_topObject



164
165
166
167
168
169
170
171
172
# File 'lib/helpers/default-helpers.rb', line 164

def new_top
  (wiki.page(name).empty? ? "" : "#{wiki.page(name).content}\n\n" ) +
"h2. #{name}

p{font-size: x-small;}. #{@settings[:description]}

<automaticsummary warning='DO NOT EDIT between these automatic summary tags, anything you write may be overwritten without warning'>
" 
end

#remove_previous_revisions(revision) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/helpers/default-helpers.rb', line 129

def remove_previous_revisions( revision )
  return unless settings[:summarise_revisions]
  return unless settings[:merge_revisions_within]
  revision.number.downto(0) do |previous_number|
    previous_revision = revision.revisions.at( previous_number )
    break unless previous_revision.author == revision.author
    break unless (revision.revised_on - previous_revision.revised_on) < settings[:merge_revisions_within]
    summary.remove( previous_revision )
  end
end

#render_summaryObject



147
148
149
150
# File 'lib/helpers/default-helpers.rb', line 147

def render_summary
  return "No pages found to summarise" if @summary.empty?
  summary.map { |page| render_summary_of_page(page)  }.to_s
end

#render_summary_of_page(page) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/helpers/default-helpers.rb', line 152

def render_summary_of_page( page )
  page.is_inserted_into(wiki.page( name ))
  content =  "<div class='subpage'>"
  content <<   "[[ #{page.name} ]]<br />\n\n"
  if page.is_a? UploadPage
    content << "[[ insert #{page.name} ]]"
  else
    content << page.content.first_lines( settings[:lines_to_include] ).close_unmatched_html
  end
  content << "\n\np(more). [[(more) => #{page.name}]]\n\n</div>\n"
end

#render_summary_pageObject

These methods relate to how the summary is shown.



142
143
144
145
# File 'lib/helpers/default-helpers.rb', line 142

def render_summary_page
  wiki.page( name ).content =~ /(.*?<automaticsummary.*?>).*?(<\/automaticsummary>.*)/mi
  wiki.revise( name, ($1 || new_top) + "\n\n" + render_summary + "\n\n" + ($2 || new_tail), @settings[:author] )
end

#scan_pages_allready_in_wikiObject



110
111
112
113
114
115
116
# File 'lib/helpers/default-helpers.rb', line 110

def scan_pages_allready_in_wiki   
  wiki.each( settings[:remove_deleted_pages] ) do |name,page| 
    next unless summarise?(page)
    summary.add(page)
  end
  render_summary_page
end

#scan_revisions_allready_in_wikiObject



118
119
120
121
122
123
124
125
126
127
# File 'lib/helpers/default-helpers.rb', line 118

def scan_revisions_allready_in_wiki
  wiki.each( settings[:remove_deleted_pages] ) do |name,page|
    page.revisions.each do |revision| 
      next unless summarise? revision
      remove_previous_revisions( revision )
      summary.add revision
    end
  end
  render_summary_page
end

#start_watching(wiki) ⇒ Object



77
78
79
80
81
82
# File 'lib/helpers/default-helpers.rb', line 77

def start_watching( wiki )
  wiki.watch_for( :page_revised ) do |event,page,revision|
    thing = settings[:summarise_revisions ] ? revision : page
    summary.include?( thing ) ? confirm_old(thing) : check_new(thing)
  end
end

#summarise?(thing) ⇒ Boolean



96
97
98
99
100
# File 'lib/helpers/default-helpers.rb', line 96

def summarise?( thing )
  return false if thing.name == name
  return false if settings[:remove_deleted_pages] && thing.deleted?
  decision.call( thing )
end