summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorroot <root@ndl-server.ndl.kiev.ua>2010-03-26 11:51:13 (GMT)
committerroot <root@ndl-server.ndl.kiev.ua>2010-03-26 11:51:13 (GMT)
commit8cfa874772765e14fa00be565d85c19c5183f00a (patch)
treeee922f3b45fdcf860285c8d1101ad00119ba117e /app
parent5ea4c736edcb50ab8c2c06f18570475fff75a0f6 (diff)
downloadwiki_external_filter-8cfa874772765e14fa00be565d85c19c5183f00a.zip
wiki_external_filter-8cfa874772765e14fa00be565d85c19c5183f00a.tar.gz
wiki_external_filter-8cfa874772765e14fa00be565d85c19c5183f00a.tar.bz2
- Added support for attachments referencing
- Added stderr capturing for better error reports - Added multiple commands support - Added video macro filter based on flowplayer and ffmpeg
Diffstat (limited to 'app')
-rw-r--r--app/controllers/wiki_external_filter_controller.rb4
-rw-r--r--app/helpers/wiki_external_filter_helper.rb50
-rw-r--r--app/views/wiki_external_filter/macro_block.html.erb41
-rw-r--r--app/views/wiki_external_filter/macro_inline.html.erb52
4 files changed, 120 insertions, 27 deletions
diff --git a/app/controllers/wiki_external_filter_controller.rb b/app/controllers/wiki_external_filter_controller.rb
index 23c03dc..94a25bd 100644
--- a/app/controllers/wiki_external_filter_controller.rb
+++ b/app/controllers/wiki_external_filter_controller.rb
@@ -6,12 +6,14 @@ class WikiExternalFilterController < ApplicationController
def filter
name = params[:name]
macro = params[:macro]
+ index = params[:index].to_i
+ filename = params[:filename] ? params[:filename] : name
config = load_config
cache_key = self.construct_cache_key(macro, name)
content = read_fragment cache_key
if (content)
- send_data content, :type => config[macro]['content_type'], :disposition => 'inline'
+ send_data content[index], :type => config[macro]['content_type'], :disposition => 'inline', :filename => filename
else
render_404
end
diff --git a/app/helpers/wiki_external_filter_helper.rb b/app/helpers/wiki_external_filter_helper.rb
index 931b77a..431a93f 100644
--- a/app/helpers/wiki_external_filter_helper.rb
+++ b/app/helpers/wiki_external_filter_helper.rb
@@ -1,4 +1,5 @@
require 'digest/sha2'
+require 'open4'
module WikiExternalFilterHelper
@@ -24,7 +25,7 @@ module WikiExternalFilterHelper
['wiki_external_filter', macro, name].join("/")
end
- def build(text, macro, info)
+ def build(text, attachments, macro, info)
name = Digest::SHA256.hexdigest(text)
result = {}
@@ -49,14 +50,14 @@ module WikiExternalFilterHelper
result[:content_type] = info['content_type']
RAILS_DEFAULT_LOGGER.debug "from cache: #{name}"
else
- result = self.build_forced(text, info)
+ result = self.build_forced(text, attachments, info)
if result[:status]
if expires > 0
write_fragment cache_key, result[:content], :expires_in => expires.seconds
RAILS_DEFAULT_LOGGER.debug "cache saved: #{name}"
end
else
- raise "Error applying external filter: #{result[:content]}"
+ raise "Error applying external filter: stdout is #{result[:content]}, stderr is #{result[:errors]}"
end
end
@@ -66,23 +67,42 @@ module WikiExternalFilterHelper
return result
end
- def build_forced(text, info)
+ def build_forced(text, attachments, info)
+
+ if info['replace_attachments'] and attachments
+ attachments.each do |att|
+ text.gsub!(/#{att.filename.downcase}/i, att.diskfile)
+ end
+ end
result = {}
+ content = []
+ errors = ""
+
+ commands = info['commands']? info['commands'] : [info['command']]
- RAILS_DEFAULT_LOGGER.debug "executing command: #{info['command']}"
+ commands.each do |command|
+ RAILS_DEFAULT_LOGGER.info "executing command: #{command}"
- content = IO.popen(info['command'], 'r+b') { |f|
- f.write info[:prolog] if info.key?(:prolog)
- f.write CGI.unescapeHTML(text)
- f.write info[:epilog] if info.key?(:epilog)
- f.close_write
- f.read
- }
+ c = nil
+ e = nil
- RAILS_DEFAULT_LOGGER.info("child status: sig=#{$?.termsig}, exit=#{$?.exitstatus}")
+ Open4::popen4(command) { |pid, fin, fout, ferr|
+ fin.write info[:prolog] if info.key?(:prolog)
+ fin.write CGI.unescapeHTML(text)
+ fin.write info[:epilog] if info.key?(:epilog)
+ fin.close
+ c, e = [fout.read, ferr.read]
+ }
+
+ RAILS_DEFAULT_LOGGER.debug("child status: sig=#{$?.termsig}, exit=#{$?.exitstatus}")
+
+ content << c
+ errors += e if e
+ end
result[:content] = content
+ result[:errors] = errors
result[:content_type] = info['content_type']
result[:source] = text
result[:status] = $?.exitstatus == 0
@@ -101,12 +121,12 @@ module WikiExternalFilterHelper
end
class Macro
- def initialize(view, source, macro, info)
+ def initialize(view, source, attachments, macro, info)
@view = view
@view.controller.extend(WikiExternalFilterHelper)
source.gsub!(/<br \/>/, "")
source.gsub!(/<\/?p>/, "")
- @result = @view.controller.build(source, macro, info)
+ @result = @view.controller.build(source, attachments, macro, info)
end
def render()
diff --git a/app/views/wiki_external_filter/macro_block.html.erb b/app/views/wiki_external_filter/macro_block.html.erb
index a72ab02..03f3949 100644
--- a/app/views/wiki_external_filter/macro_block.html.erb
+++ b/app/views/wiki_external_filter/macro_block.html.erb
@@ -13,6 +13,15 @@
%>
<div><%= content %></div>
<%
+ when /video\/x\-flv/ then
+ require 'RMagick'
+ image = Magick::Image::from_blob(content[0]).first
+%>
+ <a class='flowplayer-video' href='<%= ActionController::Base.relative_url_root + "/wiki_external_filter/video.flv?name=#{name}" %>' alt='<%= h source %>' style='display:block;width:<%= image.columns %>px;height:<%= image.rows + 24 %>px;background-image:url(<%= url_for(:controller => 'wiki_external_filter', :action => 'filter', :macro => macro, :name => name, :index => 0) %>);background-repeat:no-repeat'><%= image_tag 'play_large.png', :plugin => 'wiki_external_filter', :style => "display:block;position:relative;left:#{image.columns / 2 - 83 / 2}px;top:#{image.rows / 2 - 83 / 2}px" %></a>
+<%
+ @flowplayer_used = true
+%>
+<%
else
%>
<object class='externalfilterinline' name='<%= name %>' data='<%= url_for(:controller => 'wiki_external_filter', :action => 'filter', :macro => macro, :name => name) %>' type='<%= content_type %>' title="<%= h source %>">
@@ -24,6 +33,32 @@
<br/>
<span class='wiki_page'>Goto source: [[<%= wiki_name %>]]</span>
</div>
-<% content_for :header_tags do %>
- <%= stylesheet_link_tag "wiki_external_filter.css", :plugin => "wiki_external_filter", :media => :all %>
-<% end %>
+<%
+ content_for :header_tags do
+%>
+<%
+ if not @stylesheets_included
+ @stylesheets_included = true
+%>
+ <%= stylesheet_link_tag "wiki_external_filter.css", :plugin => "wiki_external_filter", :media => :all %>
+<%
+ end
+%>
+<%
+ if @flowplayer_used
+ if not @flowplayer_scripts_included
+ @flowplayer_scripts_included = true
+%>
+ <%= javascript_include_tag 'flowplayer.min.js', :plugin => 'wiki_external_filter' %>
+ <script language="JavaScript">
+ //<![CDATA[
+ window.onload = function () {
+ flowplayer("a.flowplayer-video", "<%= ActionController::Base.relative_url_root + Engines::RailsExtensions::AssetHelpers.plugin_asset_path('wiki_external_filter', 'javascripts', 'flowplayer.swf') %>", { clip: { bufferLength: 1 } });
+ };
+ //]]>
+ </script>
+<%
+ end
+ end
+ end
+%>
diff --git a/app/views/wiki_external_filter/macro_inline.html.erb b/app/views/wiki_external_filter/macro_inline.html.erb
index 32d1264..8dd3c2f 100644
--- a/app/views/wiki_external_filter/macro_inline.html.erb
+++ b/app/views/wiki_external_filter/macro_inline.html.erb
@@ -2,24 +2,60 @@
case content_type
when /image\// then
%>
- <img class='externalfilterinline' src='<%= url_for(:controller => 'wiki_external_filter', :action => 'filter', :macro => macro, :name => name) %>' alt="<%= h source %>" />
+ <img class='externalfilterinline' src='<%= url_for(:controller => 'wiki_external_filter', :action => 'filter', :macro => macro, :name => name, :index => 0) %>' alt="<%= h source %>" />
<%
when /text\/plain/ then
%>
- <pre class='externalfilterinline'><%= h content %></pre>
+ <pre class='externalfilterinline'><%= h content[0] %></pre>
<%
when /\/.*(x|ht)ml/ then
%>
- <div class='externalfilterinline'><%= content %></div>
+ <div class='externalfilterinline'><%= content[0] %></div>
+<%
+ when /video\/x\-flv/ then
+ require 'RMagick'
+ image = Magick::Image::from_blob(content[0]).first
+%>
+ <a class='flowplayer-video externalfilterinline' href='<%= ActionController::Base.relative_url_root + "/wiki_external_filter/video.flv?name=#{name}" %>' alt='<%= h source %>' style='display:block;width:<%= image.columns %>px;height:<%= image.rows + 24 %>px;background-image:url(<%= url_for(:controller => 'wiki_external_filter', :action => 'filter', :macro => macro, :name => name, :index => 0) %>);background-repeat:no-repeat'><%= image_tag 'play_large.png', :plugin => 'wiki_external_filter', :style => "display:block;position:relative;left:#{image.columns / 2 - 83 / 2}px;top:#{image.rows / 2 - 83 / 2}px" %></a>
+
+<%
+ @flowplayer_used = true
+%>
<%
else
%>
- <object class='externalfilterinline' name='<%= name %>' data='<%= url_for(:controller => 'wiki_external_filter', :action => 'filter', :macro => macro, :name => name) %>' type='<%= content_type %>' title="<%= h source %>">
- <embed name='<%= name %>-2' src='<%= url_for(:controller => 'wiki_external_filter', :action => 'filter', :macro => macro, :name => name) %>' type='<%= content_type %>' title="<%= h source %>" />
+ <object class='externalfilterinline' name='<%= name %>' data='<%= url_for(:controller => 'wiki_external_filter', :action => 'filter', :macro => macro, :name => name, :index => 0) %>' type='<%= content_type %>' title="<%= h source %>">
+ <embed name='<%= name %>-2' src='<%= url_for(:controller => 'wiki_external_filter', :action => 'filter', :macro => macro, :name => name, :index => 0) %>' type='<%= content_type %>' title="<%= h source %>" />
</object>
<%
end
%>
-<% content_for :header_tags do %>
- <%= stylesheet_link_tag "wiki_external_filter.css", :plugin => "wiki_external_filter", :media => :all %>
-<% end %>
+<%
+ content_for :header_tags do
+%>
+<%
+ if not @stylesheets_included
+ @stylesheets_included = true
+%>
+ <%= stylesheet_link_tag "wiki_external_filter.css", :plugin => "wiki_external_filter", :media => :all %>
+<%
+ end
+%>
+<%
+ if @flowplayer_used
+ if not @flowplayer_scripts_included
+ @flowplayer_scripts_included = true
+%>
+ <%= javascript_include_tag 'flowplayer.min.js', :plugin => 'wiki_external_filter' %>
+ <script language="JavaScript">
+ //<![CDATA[
+ window.onload = function () {
+ flowplayer("a.flowplayer-video", "<%= ActionController::Base.relative_url_root + Engines::RailsExtensions::AssetHelpers.plugin_asset_path('wiki_external_filter', 'javascripts', 'flowplayer.swf') %>", { clip: { bufferLength: 1 } });
+ };
+ //]]>
+ </script>
+<%
+ end
+ end
+ end
+%>