From 282515d90798a8e806d55bfdf4cfbcdc207ec366 Mon Sep 17 00:00:00 2001 From: Jacques Distler Date: Sun, 13 Dec 2009 19:25:14 -0600 Subject: [PATCH] Source View Add a Source view. [Based on a suggestion by Andrew Stacey] Fix a well-formedness bug in the list action, due to boneheaded truncation algorithm. [Reported by Roby Bartels] --- app/controllers/wiki_controller.rb | 24 +++++++--- app/views/wiki/page.rhtml | 3 ++ app/views/wiki/source.html.erb | 64 +++++++++++++++++++++++++ test/functional/wiki_controller_test.rb | 7 +++ 4 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 app/views/wiki/source.html.erb diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 31f377ea..95667bae 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -13,7 +13,7 @@ class WikiController < ApplicationController :history, :revision, :atom_with_content, :atom_with_headlines, :if => Proc.new { |c| c.send(:do_caching?) } cache_sweeper :revision_sweeper - layout 'default', :except => [:atom_with_content, :atom_with_headlines, :atom, :tex, :s5, :export_html] + layout 'default', :except => [:atom_with_content, :atom_with_headlines, :atom, :source, :tex, :s5, :export_html] def index if @web_name @@ -357,6 +357,10 @@ class WikiController < ApplicationController end end + def source + #to template + end + def tex if [:markdownMML, :markdownPNG, :markdown].include?(@web.markup) @tex_content = Maruku.new(@page.content).to_latex @@ -385,6 +389,18 @@ class WikiController < ApplicationController end end + def truncate(text, length = 30, truncate_string = '...') + return text if text.length <= length + len = length - truncate_string.length + text.split.inject('') do |t, word| + if t.length + word.length <= len + t << word + ' ' + else + return t.chop + truncate_string + end + end + end + protected def do_caching? @@ -503,11 +519,7 @@ class WikiController < ApplicationController def rss_with_content_allowed? @web.password.nil? or @web.published? end - - def truncate(text, length = 30, truncate_string = '...') - if text.length > length then text[0..(length - 3)] + truncate_string else text end - end - + def filter_spam(content) @@spam_patterns ||= load_spam_patterns @@spam_patterns.each do |pattern| diff --git a/app/views/wiki/page.rhtml b/app/views/wiki/page.rhtml index e276354a..ec5dbce9 100644 --- a/app/views/wiki/page.rhtml +++ b/app/views/wiki/page.rhtml @@ -45,6 +45,9 @@ {:id => 'view_S5'} %> <%- end -%> <%- end -%> + | + <%= link_to 'Source', {:web => @web.address, :action => 'source', :id => @page.name}, + {:id => 'view_source', :rel => 'nofollow' } %> <%= render :partial => 'inbound_links' %> diff --git a/app/views/wiki/source.html.erb b/app/views/wiki/source.html.erb new file mode 100644 index 00000000..952cf56a --- /dev/null +++ b/app/views/wiki/source.html.erb @@ -0,0 +1,64 @@ + + + + + <%- if @page and (@page.name == 'HomePage') -%> + <%= h(@web.name) %> + <%- else @web -%> + <%= @page.plain_name %> in <%= h @web.name %> + <%- end -%> + + + + + + + <%= stylesheet_link_tag 'instiki', :media => 'all' unless @inline_style %> + <%= "" if @style_additions %> + + <%= javascript_include_tag :defaults %> + + + + + +
+ +
+ + + \ No newline at end of file diff --git a/test/functional/wiki_controller_test.rb b/test/functional/wiki_controller_test.rb index 3d0ce78c..30517f77 100644 --- a/test/functional/wiki_controller_test.rb +++ b/test/functional/wiki_controller_test.rb @@ -36,6 +36,13 @@ class WikiControllerTest < ActionController::TestCase set_tex_header end + def test_truncate_page_name + wanted_page_name = 'This is a very, very, very, very, VERY long page name' + evil_page_name = 'This page name has lots of fun & games' + assert_equal 'This is a very, very, very,...', @controller.truncate(WikiWords.separate(wanted_page_name), 35) + assert_equal 'This page name has lots of...', @controller.truncate(WikiWords.separate(evil_page_name)) + end + def test_authenticate set_web_property :password, 'pswd'