wiki_page_categories.patch

Sven Schwyn, 2009-04-30 13:21

Download (8.69 KB)

View differences:

redmine/app/controllers/wiki_controller.rb 2009-04-30 13:18:51 +0200
156 156
      export = render_to_string :action => 'export_multiple', :layout => false
157 157
      send_data(export, :type => 'text/html', :filename => "wiki.html")
158 158
      return      
159
    when 'category'
160
      @category = params[:category].gsub(/_/, ' ')
161
      @pages = @wiki.find_pages_in_category(@category)
159 162
    else
160 163
      # requested special page doesn't exist, redirect to default page
161 164
      redirect_to :action => 'index', :id => @project, :page => nil and return
redmine/app/helpers/application_helper.rb 2009-04-30 13:13:41 +0200
320 320

  
321 321
    project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
322 322

  
323
    # Wiki category links
324
    #
325
    # Examples:
326
    #   [[category:mycategory]]
327
    #   [[category:mycategory|mytext]]
328
    # wiki links can refer other project wikis, using project name or identifier:
329
    #   [[myproject:category:mycategory]]
330
    #   [[myproject:category:mycategory|mytext]]
331
    text = text.gsub(/(!)?(\[\[(?:(.+?):)?category:(.+?)(?:\|(.+?))?\]\])/) do |m|
332
      esc, all, link_project, category, title = $1, $2, $3, $4, $5
333
      if esc.nil?
334
        if link_project.nil?
335
          link_project = project
336
        else
337
          link_project = Project.find_by_name(link_project) || Project.find_by_identifier(link_project)
338
        end
339
        if link_project && link_project.wiki
340
          link_to((title || category), :only_path => only_path, :controller => 'wiki', :id => project, :action => 'special', :page => 'category', :category => Wiki.titleize(category))
341
        else
342
          title || category
343
        end
344
      else
345
        '!' + all
346
      end
347
    end
348

  
323 349
    # Wiki links
324 350
    #
325 351
    # Examples:
redmine/app/models/wiki.rb 2009-04-30 13:19:23 +0200
42 42
    end
43 43
    page
44 44
  end
45

  
46
  # find pages which belong to a certain category
47
  def find_pages_in_category(category)
48
    pages.find :all, :conditions => [ "LOWER(categories) LIKE LOWER(?)", "%|#{category.downcase}|%"], :order=> 'title'
49
  end
45 50
  
46 51
  # Finds a page by title
47 52
  # The given string can be of one of the forms: "title" or "project:title"
......
62 67
    end
63 68
  end
64 69
  
70
  def self.upcase_first(title)
71
    title = (title.slice(0..0).upcase + (title.slice(1..-1) || '')) if title
72
  end
73

  
65 74
  # turn a string into a valid page title
66 75
  def self.titleize(title)
67 76
    # replace spaces with _ and remove unwanted caracters
68 77
    title = title.gsub(/\s+/, '_').delete(',./?;|:') if title
69 78
    # upcase the first letter
70
    title = (title.slice(0..0).upcase + (title.slice(1..-1) || '')) if title
79
    title = self.upcase_first(title)
71 80
    title
72 81
  end  
73 82
end
redmine/app/models/wiki_content.rb 2009-04-30 12:23:33 +0200
22 22
  belongs_to :page, :class_name => 'WikiPage', :foreign_key => 'page_id'
23 23
  belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
24 24
  validates_presence_of :text
25

  
26
  def after_save
27
    page.categories = categories.join('|')
28
    page.categories = '|' + page.categories + '|' if !categories.empty? 
29
    page.save!
30
  end
31

  
32
  def categories
33
    if text.match(/\{\{categor(?:y|ies)\((.+?)\)\}\}/)
34
      $1.split(/\s*,\s*/).map { |c| Wiki.upcase_first(c) }
35
    else
36
      []
37
    end
38
  end
25 39
  
26 40
  acts_as_versioned
27 41
  class Version
redmine/app/views/wiki/_sidebar.rhtml 2009-04-30 12:30:47 +0200
3 3
<%= link_to l(:field_start_page), {:action => 'index', :page => nil} %><br />
4 4
<%= link_to l(:label_index_by_title), {:action => 'special', :page => 'Page_index'} %><br />
5 5
<%= link_to l(:label_index_by_date), {:action => 'special', :page => 'Date_index'} %><br />
6

  
7
<% if wiki_categories = @page.categories.sub(/^\|(.*)\|$/, '\1').split('|') rescue nil %>
8
  <% if (!wiki_categories.empty?) %>
9
    <h3><%= l((wiki_categories.length==1)?(:label_wiki_category):(:label_wiki_category_plural)) %></h3>
10
    <% wiki_categories.sort.each do |category| %>
11
      <%= link_to category, {:action => 'special', :page => 'category', :category => Wiki.titleize(category)} %><br />
12
    <% end %>
13
  <% end %>
14
<% end %>
redmine/app/views/wiki/special_category.rhtml 2009-04-29 21:34:30 +0200
1
<h2><%= l(:label_wiki_category) %>: <%= @category %></h2>
2

  
3
<% if @pages.empty? %>
4
  <p class="nodata"><%= l(:label_no_data) %></p>
5
<% end %>
6

  
7
<% content_for :sidebar do %>
8
  <%= render :partial => 'sidebar' %>
9
<% end %>
10

  
11
<% unless @pages.empty? %>
12
  <p>
13
    <ul>
14
      <% @pages.each do |page| %>
15
        <% if page.title[0,1].upcase != (group rescue nil) %>
16
          </ul><h4><%= group = page.title[0,1].upcase %></h4><ul>
17
        <% end %>
18
        <li><%= link_to page.pretty_title, {:action => 'index', :page => page.title} %></li>
19
      <% end %>
20
    </ul>
21
  </p>
22
<% end %>
23

  
redmine/config/routes.rb 2009-04-29 23:47:27 +0200
15 15
  map.signin 'login', :controller => 'account', :action => 'login'
16 16
  map.signout 'logout', :controller => 'account', :action => 'logout'
17 17
  
18
  map.connect 'wiki/:id/category/:category', :controller => 'wiki', :page => 'category', :action => 'special', :category => nil
18 19
  map.connect 'wiki/:id/:page/:action', :controller => 'wiki', :page => nil
19 20
  map.connect 'roles/workflow/:id/:role_id/:tracker_id', :controller => 'roles', :action => 'workflow'
20 21
  map.connect 'help/:ctrl/:page', :controller => 'help'
redmine/db/migrate/102_add_wiki_pages_categories.rb 2009-04-19 19:40:12 +0200
1
class AddWikiPagesCategories < ActiveRecord::Migration
2
  def self.up
3
    add_column :wiki_pages, :categories, :string
4
  end
5

  
6
  def self.down
7
    remove_column :wiki_pages, :categories
8
  end
9
end
redmine/lang/en.yml 2009-04-29 19:01:43 +0200
484 484
label_wiki_edit_plural: Wiki edits
485 485
label_wiki_page: Wiki page
486 486
label_wiki_page_plural: Wiki pages
487
label_wiki_category: Category
488
label_wiki_category_plural: Categories
487 489
label_index_by_title: Index by title
488 490
label_index_by_date: Index by date
489 491
label_current_version: Current version
redmine/lib/redmine/wiki_formatting/macros.rb 2009-04-30 12:28:39 +0200
116 116
        @included_wiki_pages.pop
117 117
        out
118 118
      end
119

  
120
      desc "Add the current wiki page to a category.\nExample: !{{category(Help)}}"
121
      macro :category do |obj, args|
122
        ''   # categories are listed in the sidebar
123
      end
124

  
125
      desc "Add the current wiki page to several categories.\nExample: !{{categories(Help, Snippets)}}"
126
      macro :categories do |obj, args|
127
        ''   # categories are listed in the sidebar
128
      end
129

  
130
      desc "Display a list of pages which belong to the specified category.\nExample: !{{pages_in_category(Help)}}"
131
      macro :pages_in_category do |obj, args|
132
        if !args.first.nil?
133
          content_tag :ul, :class => :pages_in_category  do
134
            @wiki.find_pages_in_category(args.first).map do |p|
135
              content_tag :li do
136
                link_to p.pretty_title, {:action => 'index', :page => p.title}
137
              end 
138
            end
139
          end
140
        end
141
      end
119 142
    end
120 143
  end
121 144
end