Skip to content

Commit

Permalink
builder removed
Browse files Browse the repository at this point in the history
  • Loading branch information
minad committed Jul 28, 2010
1 parent 7f1be33 commit 5859d6c
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 102 deletions.
91 changes: 38 additions & 53 deletions lib/wiki/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,9 @@ def flash

def flash_messages
if !flash.empty?
builder do
ul do
flash.each do |level, list|
list.each do |msg|
li msg, :class => level
end
end
flash.clear
end
end
li = flash.map {|level, list| list.map {|msg| %{<li class="#{level}">#{escape_html msg}</li>} } }.flatten
flash.clear
"<ul>#{li.join}</ul>"
end
end
end
Expand All @@ -47,38 +40,34 @@ module PageHelper

def pagination(resource, pages, curpage, opts)
if pages > 0
builder do
ul.pagination {
if curpage > 0
li { a '«', :href => resource_path(resource, opts.merge(:curpage => 0)) }
li { a '‹', :href => resource_path(resource, opts.merge(:curpage => curpage - 1)) }
end
min = curpage - 3
max = curpage + 3
if min > 0
min -= max - pages if max > pages
else
max -= min if min < 0
end
max = [max, pages].min
min = [min, 0].max
li '…' if min != 0
(min..max).each do |i|
li {
if i == curpage
a.current i + 1, :href => '#'
else
a i + 1, :href => resource_path(resource, opts.merge(:curpage => i))
end
}
end
li '…' if max != pages
if curpage < pages
li { a '›', :href => resource_path(resource, opts.merge(:curpage => curpage + 1)) }
li { a '»', :href => resource_path(resource, opts.merge(:curpage => pages)) }
end
}
li = []
if curpage > 0
li << %{<a href="#{escape_html resource_path(resource, opts.merge(:curpage => 0))}">«</a>}
li << %{<a href="#{escape_html resource_path(resource, opts.merge(:curpage => curpage - 1))}">‹</a>}
end
min = curpage - 3
max = curpage + 3
if min > 0
min -= max - pages if max > pages
else
max -= min if min < 0
end
max = [max, pages].min
min = [min, 0].max
li '…' if min != 0
(min..max).each do |i|
if i == curpage
li << %{<a class="current" href="#">#{i + 1}</a>}
else
li << %{<a href="#{escape_html resource_path(resource, opts.merge(:curpage => i))}">#{i + 1}</a>}
end
end
li << '…' if max != pages
if curpage < pages
li << %{<a href="#{escape_html resource_path(resource, opts.merge(:curpage => curpage + 1))}">›</a>}
li << %{<a href="#{escape_html resource_path(resource, opts.merge(:curpage => pages))}">»</a>}
end
%{<ul class="pagination">#{li.map {|x| "<li>#{x}</li>"}.join}</ul>}
end
end

Expand Down Expand Up @@ -163,19 +152,15 @@ def date(t)

def breadcrumbs(resource)
path = resource.try(:path) || ''
builder do
li.first.breadcrumb(:class => '' == path ? 'last' : '') {
a Wiki::Config.root_path, :accesskey=>'z', :class => 'root', :href => resource_path(resource, :path => '/')
}
path.split('/').inject('') do |parent,elem|
li.breadcrumb '/'
current = parent/elem
li.breadcrumb(:class => current == path ? 'last' : '') {
a elem, :href => resource_path(resource, :path => current.urlpath)
}
current
end
li = [%{<li class="first breadcrumb#{path.empty? ? ' last' : ''}">
<a accesskey="z" class="root" href="#{escape_html resource_path(resource, :path => '/')}">#{escape_html Wiki::Config.root_path}</a></li>}.unindent]
path.split('/').inject('') do |parent,elem|
current = parent/elem
li << %{<li class="breadcrumb#{current == path ? ' last' : ''}">
<a href="#{escape_html resource_path(resource, :path => current.urlpath)}">#{escape_html elem}</a></li>}.unindent
current
end
li.join('<li class="breadcrumb">/</li>')
end

def resource_path(resource, opts = {})
Expand Down
9 changes: 0 additions & 9 deletions lib/wiki/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,6 @@ def sha256(s)
Digest::SHA256.hexdigest(s)
end

# XML builder (Nokogiri, Markaby, Erector style)
def builder(&block)
doc = Nokogiri::XML::Document.new
root = doc.create_element('root')
doc << root
Nokogiri::XML::Builder.with(root, &block)
root.children.to_html
end

def build_query(params)
params.map do |k, v|
if v.class == Array
Expand Down
15 changes: 4 additions & 11 deletions plugins/misc/redirect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@
class Wiki::Application
hook :layout do |name, doc|
if params[:redirect]
html = builder do
p_ {
text 'Redirected from '
[*params[:redirect]].each do |link|
a link.cleanpath, :href => action_path(link, :edit)
text ' → '
end
text '◉'
}
end
doc.css('#content').children.before(html)
links = [*params[:redirect]].map do |link|
%{<a href="#{escape_html action_path(link, :edit)}">#{escape_html link.cleanpath}</a> → }
end.join
doc.css('#content').children.before("<p>Redirected from #{links} ◉</p>")
end
end

Expand Down
2 changes: 1 addition & 1 deletion plugins/security/acl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def deletable?(user)

# Resource is movable if parent is writable and destination is writable
def movable?(user, destination = nil)
deletable?(user) && (!destination || Tree.find(destination).writable?(user))
deletable?(user) && (!destination || (Resource.find(destination) || Page.new(destination)).writable?(user))
end

private
Expand Down
23 changes: 11 additions & 12 deletions plugins/tag/footnotes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@

Tag.define :references do |context, attrs, content|
footnotes = context.private[:footnotes]
builder do
ol {
footnotes.each do |id, note, refs|
li(:id=>"note#{id}") {
refs.each do |ref|
a.backref '↑', :href => "#ref#{ref}"
end
parent << note
}
end
}
end if footnotes
render :footnotes, :local => {:footnotes => footnotes} if footnotes
end

__END__

@@ footnotes.haml
%ol
- footnotes.each do |id, note, refs|
%li(id="note#{id}")
- refs.each do |ref|
%a.backref(href="#ref#{ref}") ↑
= note
9 changes: 3 additions & 6 deletions plugins/tag/sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
dependencies 'filter/tag'

Tag.define(:sort) do |context, attrs, content|
type = attrs['ordered'].to_s.downcase == 'true' ? :ol : :ul
type = attrs['ordered'].to_s.downcase == 'true' ? 'ol' : 'ul'
order = attrs['descending'].to_s.downcase == 'true' ? -1 : 1
builder do
send(type) do
content.strip.split(/\n/).sort {|a,b| (a <=> b) * order }.each {|x| li(x) }
end
end
%{<#{type}>#{content.strip.split(/\n/).sort {|a,b| (a <=> b) * order }.map {|x| "<li>#{escape_html x}</li>"}.join}</#{type}>}
end

10 changes: 0 additions & 10 deletions test/wiki_util_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@
Wiki::Util.escape_json('a&b<c>').should.equal 'a\u0026b\u003Cc\u003E'
end

it 'should have xml #builder' do
Wiki::Util.builder do
entry {
attribute(:key => 'a') {
text 'text'
}
}
end.should.equal '<entry><attribute key="a">text</attribute></entry>'
end

it 'should have #md5' do
Wiki::Util.md5('test').should.equal '098f6bcd4621d373cade4e832627b4f6'
end
Expand Down

0 comments on commit 5859d6c

Please sign in to comment.