doc.rake 4.02 KB
namespace :noosfero do
  namespace :doc do
    input = Dir.glob('doc/noosfero/**/*.textile')
    topics_xhtml = input.map { |item| item.sub('.textile', '.en.xhtml') }
    sections = Dir.glob('doc/noosfero/*').select {|item| File.directory?(item) }
    toc_sections = sections.map {|item| File.join(item, 'toc.en.xhtml')}
    index_sections = sections.map {|item| File.join(item, 'index.en.xhtml')}

    def build_textile(input, output)
      begin
        File.open(output ,'w') do |output_file|
          output_file.write(RedCloth.new(File.read(input)).to_html)
          puts "#{input} -> #{output}"
        end
      rescue Exception => e
        rm_f output
        raise e
      end
    end

    topics_xhtml.each do |target|
      source = target.sub('.en.xhtml', '.textile')
      file target => source do |t|
        build_textile(source, target)
      end
    end

    toc_sections.each do |toc|
      section_topics = Dir.glob(File.dirname(toc) + '/*.textile').map {|item| item.sub('.textile', '.en.xhtml') }.reject {|item| ['index.en.xhtml', 'toc.en.xhtml' ].include?(File.basename(item))}
      file toc => section_topics do |t|
        begin
          File.open(toc, 'w') do |output_file|
            section = File.basename(File.dirname(toc))
            output_file.puts "<!-- THIS FILE IS AUTOGENERATED. DO NOT EDIT -->"
            output_file.puts "<ul>"
            section_topics.each do |item|
              topic = DocTopic.loadfile(item)
              output_file.puts "<li> <a href=\"/doc/#{section}/#{topic.id}\">#{topic.title}</a> </li>"
            end
            output_file.puts "</ul>"
            puts "#{section_topics.join(', ')} -> #{toc}"
          end
        rescue Exception => e
          rm_rf toc
          raise e
        end
      end
    end

    top_level_toc = 'doc/noosfero/toc.en.xhtml'
    file top_level_toc => index_sections do
      begin
        File.open(top_level_toc, 'w') do |output_file|
          output_file.puts "<!-- THIS FILE IS AUTOGENERATED. DO NOT EDIT -->"
          output_file.puts "<ul>"
          index_sections.each do |item|
            section = File.basename(File.dirname(item))
            topic = DocTopic.loadfile(item)
            output_file.puts "<li> <a href=\"/doc/#{section}\">#{topic.title}</a> </li>"
          end
          output_file.puts "</ul>"
        end
      rescue Exception => e
        rm_f top_level_toc
        raise e
      end
    end


    english_xhtml = (topics_xhtml + toc_sections + [top_level_toc])
    task :english => english_xhtml

    po4a_conf = 'tmp/po4a.conf'
    file po4a_conf => english_xhtml do
      begin
        File.open(po4a_conf, 'w') do |file|
          file.puts "[po4a_langs] #{(Noosfero.locales.keys - ['en']).join(' ')}"
          file.puts "[po4a_paths] po/noosfero-doc.pot $lang:po/$lang/noosfero-doc.po"
          english_xhtml.each do |item|
            file.puts "[type: xhtml] #{item} $lang:#{item.sub(/\.en\.xhtml/, '.$lang.xhtml')}"
          end
        end
      rescue Exception => e
        rm_f po4a_conf
        raise e
      end
    end

    task :build => po4a_conf do
      sh "po4a #{po4a_conf}"
    end

    task :clean do
      sh 'rm -f doc/noosfero/*.xhtml'
      sh 'rm -f doc/noosfero/*/*.xhtml'
      rm_f po4a_conf
    end

    task :rebuild => [:clean, :build]

    task :translate => english_xhtml do
      languages = Noosfero.locales.keys - ['en']
      languages.each do |lang|
        po = "po/#{lang}/noosfero-doc.po"
        if File.exists?(po)
          puts "Translating: #{lang}"
          Dir['doc/noosfero/**/*.en.xhtml'].each do |doc|
            target = doc.sub('.en.xhtml', ".#{lang}.xhtml")
            command = "po4a-translate -f xhtml -M utf8 -m #{doc} -p #{po} -L utf8 -l #{target} >/dev/null 2>&1"
            unless system(command)
              puts "Failed in #{lang} translation!"
              puts "Run the command manually to check:"
              puts "$ #{command}"
              raise "Failed."
            end
            print "."
            $stdout.flush
          end
          puts
        end
      end
    end
  end
end