Changeset 1251
- Timestamp:
- 2008-11-12 21:05:07 (2 months ago)
- Files:
-
- trunk/app/helpers/application_helper.rb (modified) (2 diffs)
- trunk/app/models/virtual_class.rb (modified) (1 diff)
- trunk/config/environment.rb (modified) (2 diffs)
- trunk/lib/base_additions.rb (modified) (1 diff)
- trunk/lib/core_ext/patcher.rb (modified) (2 diffs)
- trunk/lib/has_relations.rb (modified) (2 diffs)
- trunk/lib/parser/lib/rules/zazen.rb (modified) (1 diff)
- trunk/lib/parser/lib/rules/zena.rb (modified) (1 diff)
- trunk/lib/parser/test/parser/zazen.yml (modified) (1 diff)
- trunk/lib/use_find_helpers.rb (added)
- trunk/lib/use_zafu.rb (added)
- trunk/test/helpers/testhelp.rb (modified) (1 diff)
- trunk/vendor/plugins/math (added)
- trunk/vendor/plugins/math/patch (added)
- trunk/vendor/plugins/math/patch/application_helper.rb (added)
- trunk/vendor/plugins/zena_captcha/patch/application_helper.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/app/helpers/application_helper.rb
r1243 r1251 5 5 module ApplicationHelper 6 6 include Zena::Acts::Secure 7 @@_asset_methods = {} 8 9 # define an asset method ('key' => method_name). 10 def self.asset_method(opts) 11 opts.each do |k,v| 12 @@_asset_methods[k] = v 13 end 14 end 7 15 8 16 def dom_id(node) … … 349 357 # Parse the text in the given context (used by zazen) 350 358 def make_asset(opts) 351 asset_type = opts[:asset_type] 352 content = opts[:content] 353 node = opts[:node] 354 case asset_type 355 when 'math' 356 if !(content =~ /^\s*\\begin\{(align|equation|itemize|equation)/) 357 pre = '\[' 358 post = '\]' 359 else 360 pre = post = '' 361 end 362 363 # FIXME: SECURITY LateX filtering: is this enough ? 364 if content =~ /\\input/ 365 return "<span class='math_preview'>#{content.gsub(/(\\input\w*)/, "<span style=\'color:red;\'>#{$1}</span> (not supported)")}</span>" 366 end 367 368 if opts[:output] == 'latex' 369 "#{pre}#{content}#{post}" 370 elsif ENABLE_MATH 371 # Create PNG image 372 # 1. get image path 373 math_id = Digest::SHA1.hexdigest(content)[0..4] 374 filename = math_id + '.png' 375 filepath = node.asset_path(filename) 376 unless File.exist?(filepath) 377 if opts[:preview] 378 # do not render image during preview 379 tag = content =~ /\n/ ? 'pre' : 'span' 380 return "<#{tag} class='math_preview'>#{content}</#{tag}>" 381 else 382 # create image 383 FileUtils::mkpath(File.dirname(filepath)) unless File.exist?(File.dirname(filepath)) 384 begin 385 tempf = Tempfile.new(filename) # TODO: do we need to close this file ? 386 base = tempf.path 387 latex_template = %q{ 388 \documentclass[10pt]{article} 389 \usepackage[utf8]{inputenc} 390 \usepackage{amssymb} 391 392 \usepackage{amsmath} 393 \usepackage{amsfonts} 394 \usepackage{ulem} % strikethrough (\sout{...}) 395 \usepackage{hyperref} % links 396 397 398 % shortcuts 399 \DeclareMathOperator*{\argmin}{arg\,min} 400 \newcommand{\ve}[1]{\boldsymbol{#1}} 401 \newcommand{\ma}[1]{\boldsymbol{#1}} 402 \newenvironment{m}{\begin{bmatrix}}{\end{bmatrix}} 403 404 \pagestyle{empty} 405 \begin{document} 406 407 } 408 409 File.open("#{base}.tex", 'wb') do |f| 410 f.syswrite(latex_template) 411 f.syswrite(pre) 412 f.syswrite(content) 413 f.syswrite(post) 414 f.syswrite("\n\\end{document}\n") 415 end 416 417 system("cd #{File.dirname(tempf.path)}; latex -interaction=batchmode #{"#{base}.tex".inspect} &> '#{base}.err'") 418 if !File.exists?("#{base}.dvi") 419 Node.logger.error(File.read("#{base}.err")) 420 system("cp '#{RAILS_ROOT}/public/world.png' #{filepath.inspect}") 421 else 422 system("dvips #{tempf.path}.dvi -E -o #{base}.ps &> '#{base}.err'") #|| Node.logger.error(File.read("#{base}.err")) 423 system("convert -units PixelsPerInch -density 150 -matte -fuzz '10%' -transparent '#ffffff' #{base}.ps #{filepath.inspect} &> '#{base}.err'") #|| Node.logger.error(File.read("#{base}.err")) 424 end 425 426 ensure 427 system("rm -rf #{tempf.path.inspect} #{(tempf.path + '.*').inspect}") 428 end 429 end 430 end 431 "<span class='math'><img src='#{zen_path(node, :asset => math_id, :format => 'png')}'/></span>" 432 else 433 # Math not supported 434 "[#{asset_type}]#{content}[/#{asset_type}]" 435 end 359 asset_tag = opts[:asset_tag] 360 if asset_method = @@_asset_methods[asset_tag] 361 self.send(asset_method, opts) 436 362 else 437 363 # Unknown tag. Ignore 438 "[#{asset_t ype}]#{content}[/#{asset_type}]"364 "[#{asset_tag}]#{opts[:content]}[/#{asset_tag}]" 439 365 end 440 366 end trunk/app/models/virtual_class.rb
r1110 r1251 2 2 belongs_to :create_group, :class_name => 'Group', :foreign_key => 'create_group_id' 3 3 validate :valid_virtual_class 4 include Zena::Relations:: AllRelationsFinder4 include Zena::Relations::ClassMethods 5 5 6 6 def to_s trunk/config/environment.rb
r1239 r1251 28 28 end 29 29 30 30 config.load_paths += Dir["#{RAILS_ROOT}/vendor/plugins/**/models"] 31 32 31 33 # Force all environments to use the same logger level 32 34 # (by default production uses :info, the others :debug) … … 90 92 91 93 require File.join(lib_path, 'base_additions') 94 require File.join(lib_path, 'use_find_helpers') 95 require File.join(lib_path, 'use_zafu') 92 96 require File.join(lib_path, 'node_query') 93 97 require File.join(lib_path, 'comment_query') trunk/lib/base_additions.rb
r1081 r1251 1 class ActiveRecord::Base2 class << self3 4 def fetch_ids(sql, id_attr='id')5 unless sql =~ /SELECT/i6 sql = "SELECT `#{id_attr}` FROM #{self.table_name} WHERE #{sql}"7 end8 connection.select_all(sql, "#{name} Load").map! do |record|9 record[id_attr.to_s]10 end11 end12 13 def fetch_list(sql, *attr_list)14 unless sql =~ /SELECT/i15 sql = "SELECT #{attr_list.map {|a| "`#{a}`"}.join(', ')} FROM #{self.table_name} WHERE #{sql}"16 end17 connection.select_all(sql, "#{name} Load").map! do |record|18 Hash[*(attr_list.map {|attr| [attr, record[attr.to_s]] }.flatten)]19 end20 end21 22 def next_zip(site_id)23 res = connection.update "UPDATE zips SET zip=@zip:=zip+1 WHERE site_id = '#{site_id}'"24 if res == 025 # error26 raise Zena::BadConfiguration, "no zip entry for (#{site_id})"27 end28 rows = connection.execute "SELECT @zip"29 rows.fetch_row[0].to_i30 end31 32 def fetch_attribute(attribute, sql)33 unless sql =~ /SELECT/i34 sql = "SELECT `#{attribute}` FROM #{table_name} WHERE #{sql}"35 end36 row = connection.execute(sql).fetch_row37 row ? row[0] : nil38 end39 end40 end41 42 1 module Zena 43 2 # This exception occurs when we have configuration problems. trunk/lib/core_ext/patcher.rb
r1061 r1251 1 def load_patches_from_plugins 2 file_name = caller[0].split('/').last.split(':').first 1 def foreach_plugin(&block) 3 2 plugins_folder = File.join(RAILS_ROOT, 'vendor', 'plugins') 4 3 Dir.foreach(plugins_folder) do |plugin| 5 4 next if plugin =~ /\A\./ 6 patch_file = File.join(plugins_folder, plugin, 'patch', file_name) 5 block.call(File.join(plugins_folder, plugin)) 6 end 7 end 8 9 def load_patches_from_plugins 10 file_name = caller[0].split('/').last.split(':').first 11 foreach_plugin do |plugin_path| 12 patch_file = File.join(plugin_path, 'patch', file_name) 7 13 if File.exist?(patch_file) 8 14 load patch_file … … 10 16 end 11 17 end 18 19 def load_models_from_plugins 20 foreach_plugin do |plugin_path| 21 Dir.foreach(File.join(plugins_path, 'models')) do |model_name| 22 load File.join(plugins_path, 'models', model_name) 23 end 24 end 25 end trunk/lib/has_relations.rb
r1250 r1251 4 4 # this is called when the module is included into the 'base' module 5 5 def self.included(base) 6 # add all methods from the module "AddActsAsMethod" to the 'base' module 7 base.extend Zena::Relations::ClassMethods 6 base.extend Zena::Relations::TriggerClassMethod 7 end 8 end 9 10 module TriggerClassMethod 11 def has_relations 12 validate :relations_valid 13 after_save :update_relations 14 after_destroy :destroy_links 15 16 class_eval <<-END 17 include Zena::Relations::InstanceMethods 18 class << self 19 include Zena::Relations::ClassMethods 20 end 21 22 def relation_base_class 23 #{self} 24 end 25 END 8 26 end 9 27 end 10 28 11 module AllRelationsFinder29 module ClassMethods 12 30 # All relations related to the current class/virtual_class with its ancestors. 13 31 def all_relations(start=nil) … … 27 45 end 28 46 end 29 end30 31 module ClassMethods32 def has_relations33 validate :relations_valid34 after_save :update_relations35 after_destroy :destroy_links36 37 class_eval <<-END38 include Zena::Relations::InstanceMethods39 def relation_base_class40 #{self}41 end42 END43 end44 include Zena::Relations::AllRelationsFinder45 47 end 46 48 trunk/lib/parser/lib/rules/zazen.rb
r1236 r1251 275 275 eat $& 276 276 # [math]....[/math] (we do not use <math> to avoid confusion with mathml) 277 store @helper.make_asset(:asset_t ype=> $1, :content => $2, :node => @options[:node], :preview => @context[:preview], :output => @context[:output])277 store @helper.make_asset(:asset_tag => $1, :content => $2, :node => @options[:node], :preview => @context[:preview], :output => @context[:output]) 278 278 end 279 279 else trunk/lib/parser/lib/rules/zena.rb
r1250 r1251 39 39 40 40 require 'yaml' 41 begin42 # FIXME: zafu_readable should belong to core_ext.43 class ActiveRecord::Base44 @@_zafu_readable ||= {} # defined for each class45 @@_safe_attribute ||= {} # defined for each class46 @@_zafu_context ||= {} # defined for each class (list of methods to change contexts)47 @@_zafu_readable_attributes ||= {} # full list with inherited attributes48 @@_safe_attribute_list ||= {} # full list with inherited attributes49 @@_zafu_known_contexts ||= {} # full list with inherited attributes50 51 def self.zafu_readable(*list)52 @@_zafu_readable[self] ||= []53 @@_zafu_readable[self] = (@@_zafu_readable[self] + list.map{|l| l.to_s}).uniq54 end55 56 def self.safe_attribute(*list)57 @@_safe_attribute[self] ||= []58 @@_safe_attribute[self] = (@@_safe_attribute[self] + list.map{|l| l.to_s}).uniq59 end60 61 def self.zafu_context(hash)62 @@_zafu_context[self] ||= {}63 @@_zafu_context[self].merge!(hash.stringify_keys)64 end65 66 def self.zafu_readable_attributes67 @@_zafu_readable_attributes[self] ||= if superclass == ActiveRecord::Base68 @@_zafu_readable[self] || []69 else70 (superclass.zafu_readable_attributes + (@@_zafu_readable[self] || [])).uniq.sort71 end72 end73 74 def self.safe_attribute_list75 @@_safe_attribute_list[self] ||= if superclass == ActiveRecord::Base76 @@_safe_attribute[self] || []77 else78 (superclass.safe_attribute_list + (@@_safe_attribute[self] || [])).uniq.sort79 end80 end81 82 def self.zafu_known_contexts83 @@_zafu_known_contexts[self] ||= begin84 res = {}85 if superclass == ActiveRecord::Base86 @@_zafu_context[self] || {}87 else88 superclass.zafu_known_contexts.merge(@@_zafu_context[self] || {})89 end.each do |k,v|90 if v.kind_of?(Hash)91 res[k] = v.merge(:node_class => parse_class(v[:node_class]))92 else93 res[k] = {:node_class => parse_class(v)}94 end95 end96 res97 end98 end99 100 def self.parse_class(klass)101 if klass.kind_of?(Array)102 if klass[0].kind_of?(String)103 [Module::const_get(klass[0])]104 else105 klass106 end107 else108 if klass.kind_of?(String)109 Module::const_get(klass)110 else111 klass112 end113 end114 end115 116 def self.safe_attribute?(sym)117 column_names.include?(sym) || zafu_readable?(sym) || safe_attribute_list.include?(sym.to_s)118 end119 120 def self.zafu_readable?(sym)121 if sym.to_s =~ /(.*)_zips?$/122 return true if self.ancestors.include?(Node) && RelationProxy.find_by_role($1.singularize)123 end124 self.zafu_readable_attributes.include?(sym.to_s)125 end126 127 def zafu_read(sym)128 return "'#{sym}' not readable" unless self.class.zafu_readable?(sym)129 self.send(sym)130 end131 end132 rescue NameError133 puts "Testing out of Rails, ActiveRecord uninitialized."134 end135 41 136 42 module Zena trunk/lib/parser/test/parser/zazen.yml
r1248 r1251 209 209 make_asset: 210 210 src: "my email: [email]bob@example.com[/email]." 211 res: "<p>my email: [make_asset asset_t ype:|email| content:|bob@example.com| output:|html|].</p>"211 res: "<p>my email: [make_asset asset_tag:|email| content:|bob@example.com| output:|html|].</p>" trunk/test/helpers/testhelp.rb
r1250 r1251 2 2 require File.join(File.dirname(__FILE__), '..','..','lib','yaml_test') 3 3 begin 4 #require 'turn'5 #Test::Unit::UI::Console::TestRunner.use_progressbar = true4 require 'turn' 5 Test::Unit::UI::Console::TestRunner.use_progressbar = true 6 6 rescue MissingSourceFile 7 7 # ignore missing 'turn' testing gem trunk/vendor/plugins/zena_captcha/patch/application_helper.rb
r1250 r1251 2 2 module ApplicationHelper 3 3 include ReCaptcha::ViewHelper 4 asset_method 'email' => :email_asset 4 5 5 6 # FIXME: remove when rails 2.0. … … 23 24 end 24 25 25 def make_asset(opts) 26 asset_type = opts[:asset_type] 27 content = opts[:content] 28 if asset_type == 'email' && current_site.d_mail_hide_priv && current_site.d_mail_hide_pub 26 def email_asset(opts) 27 content = opts[:content] 28 if current_site.d_mail_hide_priv && current_site.d_mail_hide_pub 29 29 mail_hide(content, :mh_priv => current_site.d_mail_hide_priv, :mh_pub => current_site.d_mail_hide_pub) 30 30 else 31 super31 "<a href='mailto:#{content}'>#{content}</a>" 32 32 end 33 33 end
