doc.rake
4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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