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]
This commit is contained in:
Jacques Distler 2009-12-13 19:25:14 -06:00
parent c88a1d43dc
commit 282515d907
4 changed files with 92 additions and 6 deletions

View file

@ -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|

View file

@ -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' } %>
</span>
<%= render :partial => 'inbound_links' %>

View file

@ -0,0 +1,64 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
<%- if @page and (@page.name == 'HomePage') -%>
<%= h(@web.name) %>
<%- else @web -%>
<%= @page.plain_name %> in <%= h @web.name %>
<%- end -%>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="robots" content="<%= @robots_metatag_value %>" />
<style type="text/css">
h1#pageName, div.info, .newWikiWord a, a.existingWikiWord, .newWikiWord a:hover, [actiontype="toggle"]:hover, #TextileHelp h3 {
color: #<%= @web ? @web.color : "393" %>;
}
<%= Rails.root.join('public', 'stylesheets', 'instiki.css').read if @inline_style %>
</style>
<%= stylesheet_link_tag 'instiki', :media => 'all' unless @inline_style %>
<%= "<style type='text/css'>#{@style_additions}</style>" if @style_additions %>
<style type="text/css"><!--/*--><![CDATA[/*><!--*/
<%= @web ? @web.additional_style : '' %>
/*]]>*/--></style>
<%= javascript_include_tag :defaults %>
<script type="text/javascript">
<!--//--><![CDATA[//><!--
function updateSize(elt, w, h) {
// adjust to the size of the user's browser area.
// w and h are the original, unadjusted, width and height per row/column
var parentheight = document.viewport.getHeight();
var parentwidth = $('Container').getWidth();
elt.writeAttribute({'cols': Math.floor(parentwidth/w) - 1,
'rows': Math.floor(parentheight/h) - 2 });
elt.setStyle({Width: parentwidth, Height: parentheight});
}
function resizeableTextarea() {
//make the textarea resize to fit available space
$$('textarea#content').each( function(textarea) {
var w = textarea.getWidth()/textarea.getAttribute('cols');
var h = textarea.getStyle('lineHeight').replace(/(\d*)px/, "$1");
Event.observe(window, 'resize', function(){ updateSize(textarea, w, h) });
updateSize(textarea, w, h);
Form.Element.focus(textarea);
});
}
window.onload = function (){
resizeableTextarea();
}
//--><!]]>
</script>
</head>
<body>
<div id="Container">
<textarea id='content' readonly=' readonly' rows='24' cols='60' ><%= h(@page.content.purify) %></textarea>
</div> <!-- Container -->
</body>
</html>

View file

@ -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 &amp; 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'