/app/models/doclets/pdf_verb_conjugations.rb
Ruby | 260 lines | 209 code | 49 blank | 2 comment | 10 complexity | 6928c03a46e4f8d4d4a6d984686dc565 MD5 | raw file
- # encoding : utf-8
- require 'open-uri'
- module Doclets
- class PdfVerbConjugations
- def initialize(global_verb)
-
-
- @pdf=Prawn::Document.new(:page_layout => :portrait,
- :left_margin => 1.cm, # different
- :right_margin => 1.cm, # units
- :top_margin => 1.08.in, # work
- :bottom_margin => 1.66.cm, # well
- :page_size => "LETTER");
-
- @global_verb=global_verb;
-
-
- @top_y=10.02.in
- @conjugations=@global_verb.conjugated_verbs;
-
- if(@conjugations.length==0) then
- if(@global_verb.conjugations.blank?) then
- mark_page!
- @pdf.text "We're sorry, but this verb has not been conjugated yet."
- elsif(@global_verb.conjugations.length<600) then
- mark_page!
- @pdf.text Nokogiri::HTML.fragment("<basic>"+@global_verb.conjugations+"</basic>").inner_text
- else
- mark_page!
- @pdf.text "This verb is a work in progress"
- end
- else
- @top_y-=0.12.in
- process_conjugations!
-
- end
-
- mark_page! if(@pdf.page_count>1)
-
- sample_sentences!
- end
-
- def mark_page!
- mark_top=10.6.in
- @pdf.canvas do
- @pdf.image(open(URI.escape("http://static.123teachme.com/images/logo.png")),:fit=>[3.cm,2.cm],:at=>[0.3.in,mark_top])
- @pdf.cell(:content=>"<b>Spanish Verb:</b> "+de_acute(@global_verb.verb),:size=>9.5,:borders=>[],:inline_format => true,:at=>[1.6.in,mark_top+0.05.in]);
- @pdf.cell(:content=>"<b>English Translation:</b> "+de_acute(@global_verb.meaning),:size=>9.5,:borders=>[],:inline_format => true,:at=>[1.6.in,mark_top-0.15.in]);
- @pdf.text_box("Copyright © #{Time.now.year} - 123TeachMe.com",:at=>[0.3.in,0.5.in])
- end
- end
-
- def sample_sentences!
- @sentences = TranslatedSentence.find_by_spanish(@global_verb.verb, 17)
- if @sentences.length > 0
- @pdf.start_new_page
- mark_page!
-
- str="";
-
- str+="<u>Sample Translated Sentences Containing <b>'#{@global_verb.verb}'</b> </u>\n";
-
- @sentences.each do |s|
- str+="\n<i>"+s.spanish_sentence+"</i>\n"
- str+=s.english_sentence+"\n"
- end
-
- @pdf.cell(:content=>str,:size=>10,:borders=>[],:inline_format => true,:at=>[0.1.in,@top_y-0.72.in]);
- end
- end
- def de_acute(str)
- str.
- gsub(">",">").
- gsub("<A","<a").
- gsub("</A","</a").
- gsub("B>","b>").
- gsub("I>","i>").
- gsub("é","è").
- gsub("á","à").
- gsub("í","ì").
- gsub("ó","ò").
- gsub("ú","ù").
- gsub("ñ","ñ");
- end
- def process_conjugations!
- mark_page!
-
- if(@global_verb.source=="jehle") then
- jehle_str="This verb table is based upon the work of Professor Fred Jehle of Indiana-Purdue University. The verb tables can be found at <i>http://users.ipfw.edu/JEHLE/VERBLIST.HTM</i>"
-
- @pdf.cell(:content=>jehle_str,:size=>8.5,:background_color=>"e4f2d0",:border_color=>"00bb00",:inline_format => true,:at=>[4.3.in,@top_y+0.07.in],:width=>3.4.in,:align=>:center);
- end
- global_verb=@global_verb;
- conjugated_verbs=@conjugations;
- times_cats={};
- times_cats_order=["Indicative","Subjunctive","Commands","Other Forms"]
- times_cats_order_map=Hash[ times_cats_order.each_with_index.map { |cat,i| [cat, i] } ]
- used_people_map={};
- used_time_map={};
- conjugated_map={}
- tmp_times={};
- tmp_people={};
- conjugated_verbs.each do |cv|
- conjugated_map["#{cv.verb_person_id}_#{cv.verb_time_id}_#{cv.alternate}"]=cv
- tmp_times[cv.verb_time_id]=true;
- tmp_people[cv.verb_person_id]=true;
- end
- tmp_time_ar=times_cats_order.map { [] }
- VerbTime.where('id in ('+tmp_times.keys.join(',')+') AND visible=1').all.each do |vt|
- used_time_map[vt.id]=vt;
- tmp_time_ar[times_cats_order_map[vt.category]].push(vt);
- end
- VerbPerson.where('id in ('+tmp_people.keys.join(',')+')').all.each do |vp|
- used_people_map[vp.id]=vp;
- end
- time_walk=tmp_time_ar.map{ |ar| ar.sort{|v1,v2| v1.id <=> v2.id} }.flatten;
- people_map=Hash[VerbPerson.all.map{ |vp| [vp.id,vp.spanish] }]
- people_mix, english_rows=( used_people_map[9].nil? ?
- [ [ [1,8,16],[2,10],[4],[5],[6],[7] ], 0] :
- [ [ [9],[1,8,16],[2,10],[4],[5],[6],[7] ], 1]);
- people_mix_inverse={};
- people_mix.each_with_index {|aa,idx| aa.each {|ab| people_mix_inverse[ab]=idx } };
- people_headings=people_mix.map {|a| people_map[a[0]] }
- times_map=Hash[ VerbTime.all.map do |vt|
- i={"category"=>vt.category, "name"=>vt.name, "explanation"=>vt.explanation, "id"=>vt.id};
- times_cats[vt.category]||=[];
- times_cats[vt.category]<< i;
- [vt.id,i]
- end ]
- tbl=[];
- cur_tr=[""];
-
- people_headings.each do |heading|
- cur_tr<< "`<b>#{heading}</b>";
- end
- tbl << cur_tr;
- last_cat="";
- time_walk.each do |tki|
- tk=tki.id;
- tense_id=tki.id;
-
- if(last_cat!=tki.category) then
- last_cat=tki.category;
- #<td colspan="<%=people_mix.length+1%>" class="category_heading"><%=tki.category%></td></tr>
- tbl<< ["-<b>#{tki.category}</b>"].concat( people_headings.map {""} );
- end
- cur_tr=[];
- cur_tr<< ".<b>#{tki.name}</b>";
-
- people_mix.each_with_index do |pmix,column_num|
- arr=pmix.map do |person_id|
- [0,1].map do |alt|
- c_obj=conjugated_map["#{person_id}_#{tense_id}_#{alt}"];
- c=c_obj.nil? ? nil : c_obj.conjugation.strip;
- c.blank? ? nil : c;
- end.compact;
- end.compact;
- text=arr.flatten[0..1].join("\n<i>OR</i>\n")
- colspan=(column_num!=english_rows ? 1 : tki.category=="Other Forms" ? people_mix.length-english_rows : 1 );
- if(english_rows==1 && column_num==0) then
- text= "<i>#{text}</i>"
- end
- cur_tr<< (colspan!=1 ? ':' : '')+text;
-
- end
- tbl<< cur_tr
- end
- @pdf.y=@top_y
-
- @pdf.table(
- tbl,
-
- :cell_style => {
- :padding => 2,
- :inline_format => true,
- :size=>8.3,
- :align=>:center,
- :valign=>:center
- }
- ) do |t|
- stopcell=true;
- count=0;
- t.cells.style do |c|
- if(c.content.to_s.length>0) then
- c.content=de_acute(c.content);
- end
- if(count==0) then
- c.borders=[];
- elsif(c.content.to_s.index('-')==0) then
- c.background_color="dddddd"
- c.padding_top=6
- c.borders=[:left,:top,:bottom];
- stopcell=false;
- c.content=c.content[1..-1]
- elsif(c.content.to_s.index('.')==0) then
- c.background_color="fadaa5"
- stopcell=true;
- c.content=c.content[1..-1]
- elsif(c.content.to_s.index("`")==0) then
- c.background_color="ffeeaa"
- c.content=c.content[1..-1]
- elsif(c.content.to_s.index(":")==0) then
- c.content=c.content[1..-1]
- c.borders=[:left,:top,:bottom]
- elsif(c.content.to_s.index("<i>")==0) then
- c.text_color="008080"
- elsif(c.content.to_s.blank?) then
- if((count+1)%(people_headings.length+1)==0) then
- c.borders=[:top,:bottom,:right];
- else
- c.borders=[:top,:bottom];
- end
- if(stopcell==false) then
- c.background_color="dddddd";
- end
- end
- count+=1;
- end
- end
- notes=de_acute(global_verb.notes);
- @pdf.cell(:borders=>[],:inline_format=>true,:content =>"\n"+notes+"\n\n", :size => 8)
-
- end
-
-
-
- def pdf
- @pdf
- end
- end
- end