Feature #22923 » 0001-Add-ODT-export-for-wiki-pages.patch
| Gemfile | ||
|---|---|---|
|
gem "actionpack-xml_parser"
|
||
|
gem "roadie-rails"
|
||
|
gem "mimemagic"
|
||
|
gem "html2odt", "~> 0.3.3"
|
||
|
# Request at least nokogiri 1.6.7.2 because of security advisories
|
||
|
gem "nokogiri", ">= 1.6.7.2"
|
||
|
# Request at least rails-html-sanitizer 1.0.3 because of security advisories
|
||
|
# Request at least rails-html-sanitizer 1.0.3 because of security advisories
|
||
|
gem "rails-html-sanitizer", ">= 1.0.3"
|
||
|
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
|
||
| app/controllers/wiki_controller.rb | ||
|---|---|---|
|
export = render_to_string :action => 'export', :layout => false
|
||
|
send_data(export, :type => 'text/html', :filename => "#{@page.title}.html")
|
||
|
return
|
||
|
elsif params[:format] == 'odt'
|
||
|
send_file_headers! :type => 'application/vnd.oasis.opendocument.text',
|
||
|
:filename => "#{@page.title}.odt"
|
||
|
render :inline => "<%= raw wiki_page_to_odt(@page, @project) %>"
|
||
|
return
|
||
|
elsif params[:format] == 'txt'
|
||
|
send_data(@content.text, :type => 'text/plain', :filename => "#{@page.title}.txt")
|
||
|
return
|
||
| ... | ... | |
|
format.pdf {
|
||
|
send_file_headers! :type => 'application/pdf', :filename => "#{@project.identifier}.pdf"
|
||
|
}
|
||
|
format.odt {
|
||
|
send_file_headers! :type => 'application/vnd.oasis.opendocument.text',
|
||
|
:filename => "#{@project.identifier}.odt"
|
||
|
render :inline => "<%= raw wiki_pages_to_odt(@pages, @project) %>"
|
||
|
}
|
||
|
end
|
||
|
end
|
||
| app/helpers/wiki_helper.rb | ||
|---|---|---|
|
module WikiHelper
|
||
|
include Redmine::Export::PDF::WikiPdfHelper
|
||
|
include Redmine::Export::ODT::WikiOdtHelper
|
||
|
def wiki_page_options_for_select(pages, selected = nil, parent = nil, level = 0)
|
||
|
pages = pages.group_by(&:parent) unless pages.is_a?(Hash)
|
||
| app/views/wiki/index.html.erb | ||
|---|---|---|
|
<% if User.current.allowed_to?(:export_wiki_pages, @project) %>
|
||
|
<%= f.link_to('PDF', :url => {:action => 'export', :format => 'pdf'}) %>
|
||
|
<%= f.link_to('HTML', :url => {:action => 'export'}) %>
|
||
|
<%= f.link_to('ODT', :url => {:action => 'export', :format => 'odt'}) %>
|
||
|
<% end %>
|
||
|
<% end %>
|
||
|
<% end %>
|
||
| app/views/wiki/show.html.erb | ||
|---|---|---|
|
<%= "#{l(:label_version)} #{@content.version}/#{@page.content.version}" %>
|
||
|
<%= '('.html_safe + link_to(l(:label_diff), :controller => 'wiki', :action => 'diff',
|
||
|
:id => @page.title, :project_id => @page.project,
|
||
|
:version => @content.version) + ')'.html_safe if @content.previous %> -
|
||
|
:version => @content.version) + ')'.html_safe if @content.previous %> -
|
||
|
<%= link_to((l(:label_next) + " \xc2\xbb"), :action => 'show',
|
||
|
:id => @page.title, :project_id => @page.project,
|
||
|
:version => @content.next.version) + " - " if @content.next %>
|
||
| ... | ... | |
|
<% other_formats_links do |f| %>
|
||
|
<%= f.link_to 'PDF', :url => {:id => @page.title, :version => params[:version]} %>
|
||
|
<%= f.link_to 'HTML', :url => {:id => @page.title, :version => params[:version]} %>
|
||
|
<%= f.link_to 'ODT', :url => {:id => @page.title, :version => params[:version]} %>
|
||
|
<%= f.link_to 'TXT', :url => {:id => @page.title, :version => params[:version]} %>
|
||
|
<% end if User.current.allowed_to?(:export_wiki_pages, @project) %>
|
||
| config/initializers/20-mime_types.rb | ||
|---|---|---|
|
Mime::SET << Mime::CSV unless Mime::SET.include?(Mime::CSV)
|
||
|
Mime::Type.register "application/vnd.oasis.opendocument.text", :odt
|
||
| lib/redmine/export/odt/wiki_odt_helper.rb | ||
|---|---|---|
|
module Redmine
|
||
|
module Export
|
||
|
module ODT
|
||
|
module WikiOdtHelper
|
||
|
def wiki_pages_to_odt(pages, project)
|
||
|
doc = Html2Odt::Document.new
|
||
|
doc.image_location_mapping = image_location_mapping_proc(pages.map(&:attachments).flatten)
|
||
|
doc.html = cleanup_html(html_for_page_hierarchy(pages.group_by(&:parent_id)))
|
||
|
doc.base_uri = project_wiki_index_url(project)
|
||
|
doc.title = project.name
|
||
|
doc.author = User.current.name
|
||
|
doc.data
|
||
|
end
|
||
|
def wiki_page_to_odt(page, project)
|
||
|
doc = Html2Odt::Document.new
|
||
|
doc.image_location_mapping = image_location_mapping_proc(page.attachments)
|
||
|
doc.base_uri = project_wiki_page_url(project, page)
|
||
|
doc.author = User.current.name
|
||
|
doc.title = "#{project.name} - #{page.title}"
|
||
|
doc.html = cleanup_html(html_for_page_hierarchy(nil => [page]))
|
||
|
doc.data
|
||
|
end
|
||
|
protected
|
||
|
def html_for_page_hierarchy(pages, node = nil, level = 0)
|
||
|
return "" if pages[node].blank?
|
||
|
html = ""
|
||
|
pages[node].each do |page|
|
||
|
html += "<hr/>\n" unless level == 0 && page == pages[node].first
|
||
|
html += textilizable(page.content, :text,
|
||
|
:only_path => false,
|
||
|
:edit_section_links => false,
|
||
|
:headings => false)
|
||
|
html += html_for_page_hierarchy(pages, page.id, level + 1)
|
||
|
end
|
||
|
html
|
||
|
end
|
||
|
def cleanup_html(html)
|
||
|
# Strip {{toc}} tags
|
||
|
#
|
||
|
# The links generated within the toc-pseudo-macro will not work inside
|
||
|
# the ODT, so let's remove it..
|
||
|
html = html.gsub(/<p>\{\{([<>]?)toc.*?\}\}<\/p>/i, '')
|
||
|
# Cleanup {{collapse}} macro output
|
||
|
#
|
||
|
# The collapse macro is generating the following (simplified) markup:
|
||
|
#
|
||
|
# <p>
|
||
|
# <a class="collapsible collapsed">Open link</a>
|
||
|
# <a class="collapsible">Close link</a>
|
||
|
# <div class="collapsed-text">Content</div>
|
||
|
# </p>
|
||
|
#
|
||
|
# An HTML parser (like Nokogiri or any browser) will create the
|
||
|
# following DOM
|
||
|
#
|
||
|
# <p>
|
||
|
# <a class="collapsible collapsed">Open link</a>
|
||
|
# <a class="collapsible">Close link</a>
|
||
|
# </p>
|
||
|
# <div class="collapsed-text">Content</div>
|
||
|
# <p/>
|
||
|
#
|
||
|
# So we're trying to remove the first p, containing the links, and
|
||
|
# we're replacing the div.collapsed-text with its content. The
|
||
|
# remaining p is difficult to target (or does not seem to be created
|
||
|
# in Nokogiri), so we're leaving that one alone.
|
||
|
#
|
||
|
# In a previous version, we were only removing the links themselves,
|
||
|
# but this lead to errors in html2odt's own HTML cleanup (elements,
|
||
|
# that needed fixing were not found).
|
||
|
doc = Nokogiri::HTML::DocumentFragment.parse(html)
|
||
|
doc.css(".collapsible.collapsed").each do |collapsed_links|
|
||
|
collapsed_links.parent.remove
|
||
|
end
|
||
|
doc.css(".collapsed-text").each do |collapsed_text|
|
||
|
collapsed_text.replace collapsed_text.children
|
||
|
end
|
||
|
html = doc.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML)
|
||
|
html
|
||
|
end
|
||
|
def image_location_mapping_proc(attachments)
|
||
|
lambda do |src|
|
||
|
# See if src maps to local URL which happens to be an attachment of
|
||
|
# this wiki page
|
||
|
if src =~ /\Ahttps?:\/\//
|
||
|
# Ignore URLs pointing to other hosts
|
||
|
src_uri = URI.parse(src) rescue nil
|
||
|
next unless src_uri
|
||
|
src_base_url = "#{src_uri.scheme}://#{src_uri.host}"
|
||
|
if src_uri.default_port != src_uri.port
|
||
|
src_base_url += ":#{src_uri.port}"
|
||
|
end
|
||
|
next if request.base_url != src_base_url
|
||
|
else
|
||
|
# Ignore URLs with protocols != http(s)
|
||
|
next if src.include? "://"
|
||
|
end
|
||
|
# Include public images
|
||
|
#
|
||
|
public_path = File.join(Rails.public_path, src)
|
||
|
# but make sure, that we're not vulnerable to directory traversal attacks
|
||
|
#
|
||
|
# File.realpath accesses file system and raises Errno::ENOENT if file
|
||
|
# does not exist
|
||
|
valid_path = File.realpath(public_path).starts_with?(Rails.public_path.to_s) rescue false
|
||
|
next public_path if valid_path and File.readable?(public_path)
|
||
|
# Include attached images
|
||
|
#
|
||
|
path = Rails.application.routes.recognize_path(src) rescue nil
|
||
|
next if path.blank?
|
||
|
next if path[:controller] != "attachments"
|
||
|
next if path[:id].blank?
|
||
|
attachment = attachments.find { |a| a.to_param == path[:id] }
|
||
|
if path[:action] == "thumbnail" and path[:size].present?
|
||
|
return attachment.thumbnail(size: path[:size])
|
||
|
end
|
||
|
if path[:action] == "download"
|
||
|
return attachment.diskfile
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
- « Previous
- 1
- …
- 3
- 4
- 5
- Next »