e351883ab3d9d77bde8dac0add3074f8ff5209b3
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

1) #!/bin/bash
2) #
3) # Author: Runa Sandvik, <runa.sandvik@gmail.com>
4) # Google Summer of Code 2009
5) #
6) # This is Free Software (GPLv3)
7) # http://www.gnu.org/licenses/gpl-3.0.txt
8) #
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

9) # This script will convert all of the English wml files in
10) # https://svn.torproject.org/svn/website/trunk/ to pot files, and
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

11) # keep them updated. The script will also convert subdirectories that
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

12) # exist in the website module.
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

13) #
14) # For more information, see the HOWTO and README in
15) # translation/tools/gsoc09.
16) # 
17) 
18) ### start config ###
19) 
20) # Location of the wml files
21) wmldir="$PWD"
22) 
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

23) # Location of the pot files.
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

24) # Assuming that the translation directory is relative to the website
Runa A. Sandvik update file path

Runa A. Sandvik authored 13 years ago

25) podir="`dirname $wmldir`/translation/projects/website/po/templates"
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

26) 
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

27) # Set the copyright holder of the files,
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

28) # for example "The Tor Project, Inc"
29) copyright="The Tor Project, Inc"
30) 
31) # A lot of the wml files have custom tags. These tags have been defined
32) # in website/include/versions.wmi. Tags that people usually forget to close,
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

33) # as well as tags that are not defined in versions.wmi, have been added.
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

34) # See: https://svn.torproject.org/svn/website/trunk/include/versions.wmi
Sebastian Hahn Remove unused version entries

Sebastian Hahn authored 8 years ago

35) customtag=`echo $(cat "$wmldir/include/versions.wmi" | awk '{ printf "<%s> " , $2 }' | sed 's/<>//g') "<svnsandbox> <svnwebsite> <svnprojects> <input> <hr> <br> <img> <gitblob> <video controls> <wiki>"`
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

36) 
37) # We also need to use the nodefault option of po4a; space separated list
38) # of tags that the module should not try to set by default in any
39) # category. For now, we only need the input tag.
40) nodefault='<input>'
41) 
42) # The script can write the name of unprocessed files to a log.
43) # If you want to enable this option, set the logfile here.
Runa A. Sandvik minor update

Runa A. Sandvik authored 13 years ago

44) logfile=""
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

45) 
46) # This is the temp logfile. Leave this line even if you don't want to
47) # log. This will be deleted when the script is done.
48) tmplog="`dirname $wmldir`/tmp.log"
49) 
50) ### end config ###
51) 
52) # Create a lockfile to make sure that only one instance of the script
53) # can run at any time.
54) LOCKFILE=wml2po.lock
55) 
56) if lockfile -! -l 60 -r 3 "$LOCKFILE"; 
57) then
58) 	echo "unable to acquire lock" >2
59) 	exit 1
60) fi
61) 
62) trap "rm -f '$PWD/$LOCKFILE'" exit
63) 
64) # Check if translation/projects/website exist, i.e. has been checked out
65) if [ ! -d $podir ]
66) then
67) 	echo "Have you remembered to check out translation/projects/website?"
68) 	exit 1
69) fi
70) 
71) # If the logfile is set, write the date.
72) if [ $logfile ]
73) then
74) 	echo `date` > $logfile
75) fi
76) 
77) # Create the temp log
78) touch $tmplog
79) 
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

80) # We only need the English wml files, but we do not wish to translate
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

81) # the eff documents.
82) wml=`find $wmldir -regex '^'$wmldir'/.*en/.*\.wml' -type f | grep -v '^'$wmldir'/eff'`
83) 
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

84) # For every English wml, see if the pot needs to be created or updated
85) for file in $wml ; do
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

86) 
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

87) 	# Get the basename of the file we are dealing with
88) 	wmlfile=`basename $file`
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

89) 
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

90) 	# Get the translation priority
91) 	priority=`cat $file | grep "# Translation-Priority" | awk '{print $3}'`
Runa A. Sandvik if the subdirectory does no...

Runa A. Sandvik authored 15 years ago

92) 
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

93) 	# If the file doesn't have a translation-priority, we can assume
94) 	# that it doesn't need to be translated. Skip this file and
95) 	# continue on with the next.
96) 	if [ ! $priority ]
97) 	then
98) 		continue
99) 	fi
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

100) 
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

101) 	# Strip the file for its original extension and add .pot
102) 	pofile="$priority.${wmlfile%%.*}.pot"
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

103) 
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

104) 	# Find out what directory the file is in.
105) 	# Also, remove the part of the path that is $wmldir
106) 	indir=`dirname $file`
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

107) 	
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

108) 	# We need to know what one dir up is
109) 	onedirup=`dirname $indir | sed "s#$wmldir/##"`
110) 
111) 	# We need to have the correct, full path to the pot
112) 	# directory for the file we are working on.
113) 	# Also, did the subdirectory exist prior to running this
114) 	# script? If not, create it now and add it to the
115) 	# repository.
116) 	if [ $onedirup = $wmldir ]
117) 	then
118) 		popath="$podir"
119) 	else
120) 
121) 		# We need to know if a subdirectory, such as torbutton,
122) 		# exist in the translation module. If it does not exist,
123) 		# the script will create it in all the directories under
124) 		# translation/projects/website (excluding .svn)
125) 		subdir=`find "$podir" -maxdepth 1 -type d ! -path "$ppodir" ! -path "*\.*"`
126) 
127) 		for dir in $subdir ; do
128) 			if [ ! -d "$podir/$onedirup" ]
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

129) 			then
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

130) 				svn mkdir "$podir/$onedirup"
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

131) 			fi
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

132) 		done
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

133) 
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

134) 		# Set the path
135) 		popath="$podir/$onedirup"
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

136) 
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

137) 	fi
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

138) 		
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

139) 	# Check to see if the pot existed prior to running this
140) 	# script. If it didn't, check if there any files with the same
141) 	# filename, but different priority. If neither of the files
142) 	# exist, create with po4a-gettextize.
143) 	if [ -e "$popath/$pofile" ]
144) 	then
145) 		poexist=1
146) 	elif [ `find $popath -type f -name "*.$filename" | wc -l` -gt "0" ]
147) 	then
148) 		poexist=2
149) 
150) 	# We need to rename the other file
151) 	for file in `find $popath -type f -name "*.$filename"` ; do
152) 		svn mv "$file" "$popath/$pofile"
153) 		echo "$popath/$pofile" > $tmplog
154) 	done
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

155) 
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

156) 	else
157) 		poexist=0
158) 	fi
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

159) 
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

160) 	# If the pot file does not exist, convert it with
161) 	# po4a-gettextize, set the right encoding and charset
162) 	# and the correct copyright.
163) 	if [ $poexist = 0 ]
164) 	then
Runa A. Sandvik updated wml2po.sh to handle...

Runa A. Sandvik authored 13 years ago

165) 		# Do something special for download.wml and its js
166) 		if [ $wmlfile = "download.wml" ] 
167) 		then
168) 			po4a-gettextize -f wml -m "$file" -p "$popath/$pofile" --master-charset utf-8 -o customtag="$customtag" -o nodefault="$nodefault" -o ontagerror="silent"
169) 		else
170) 			# Convert it
171) 			po4a-gettextize -f wml -m "$file" -p "$popath/$pofile" --master-charset utf-8 -o customtag="$customtag" -o nodefault="$nodefault"
172) 		fi
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

173) 
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

174) 		# Check to see if the file exists
175) 		if [ -e "$popath/$pofile" ]
176) 		then
177) 			# We don't want files without
178) 			# content, so check the file first.
179) 			content=`cat "$popath/$pofile" | grep '^#[.]' | wc -l`
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

180) 
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

181) 			# If the file does not have any
182) 			# content, delete it.
183) 			if [ $content = 0 ] 
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

184) 			then
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

185) 				rm -f "$popath/$pofile"
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

186) 				echo "$popath/$pofile" > $tmplog
187) 			else
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

188) 				# Set the right encoding and charset, as well
189) 				# as the correct copyright holder.
190) 				sed -i '0,/ENCODING/ s/ENCODING/8bit/' "$popath/$pofile"
191) 				sed -i '0,/CHARSET/ s/CHARSET/utf-8/' "$popath/$pofile"
192) 				sed -i "0,/Free Software Foundation, Inc/ s/Free Software Foundation, Inc/$copyright/" "$popath/$pofile"
193) 				
194) 				# And add it to the repository
195) 				svn add "$popath/$pofile"
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

196) 				echo "$popath/$pofile" > $tmplog
197) 			fi
Runa A. Sandvik minor fix

Runa A. Sandvik authored 13 years ago

198) 
199) 			# Remove po4a comments from download.wml
200) 			if [ $wmlfile = "download.wml" ]
201) 			then
202) 				sed -i 's/PO4ASHARPEND-->//g' "$popath/$pofile"
203) 			fi
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

204) 		fi
Runa A. Sandvik convert to po for all langu...

Runa A. Sandvik authored 15 years ago

205) 
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

206) 		# Update the file with po4a-updatepo to make the
207) 		# word wrapping perfect
Runa A. Sandvik updated wml2po.sh to handle...

Runa A. Sandvik authored 13 years ago

208) 		if [ $wmlfile = "download.wml" ]
209) 		then
210) 			po4a-updatepo -f wml -m "$file" -p "$popath/$pofile" --master-charset utf-8 -o customtag="$customtag" -o nodefault="$nodefault" -o ontagerror="silent"
211) 		else
212) 			po4a-updatepo -f wml -m "$file" -p "$popath/$pofile" --master-charset utf-8 -o customtag="$customtag" -o nodefault="$nodefault"
213) 		fi
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

214) 
215) 		# Delete the backup
216) 		rm -f "$popath/$pofile~"
217) 	fi
218) 		
219) 	# If the pot file does exist, calculate the hash first,
220) 	# then update the file, then calculate the hash again.
221) 	if [ $poexist = 1 ]
222) 	then
223) 		# Calculate the hash before we update the file
224) 		before=`grep -vE '^("POT-Creation-Date:|#)' "$popath/$pofile" | md5sum | cut -d " " -f1`
225) 
226) 		# Update the pot file
Runa A. Sandvik updated wml2po.sh to handle...

Runa A. Sandvik authored 13 years ago

227) 		if [ $wmlfile = "download.wml" ]
228) 		then
229) 			po4a-updatepo -f wml -m "$file" -p "$popath/$pofile" --master-charset utf-8 -o customtag="$customtag" -o nodefault="$nodefault" -o ontagerror="silent"
230) 		else
231) 			po4a-updatepo -f wml -m "$file" -p "$popath/$pofile" --master-charset utf-8 -o customtag="$customtag" -o nodefault="$nodefault"
232) 		fi
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

233) 
234) 		# Calculate the new hash
235) 		after=`grep -vE '^("POT-Creation-Date:|#)' "$popath/$pofile" | md5sum | cut -d " " -f1`
236) 
237) 		# Delete the backup
238) 		rm -f "$popath/$pofile~"
239) 
240) 		# Now we need to compare the before and after
241) 		# hash. If they match (i.e. nothing has
242) 		# changed), revert the file.
243) 		if [ $before = $after ]
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

244) 		then
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

245) 			svn revert "$popath/$pofile"
246) 			echo "$popath/$pofile" > $tmplog
247) 		else
248) 			echo "$popath/$pofile" > $tmplog
Runa A. Sandvik minor fix

Runa A. Sandvik authored 13 years ago

249) 
250) 			if [ $wmlfile = "download.wml" ]
251) 			then
252) 				sed -i 's/PO4ASHARPEND-->//g' "$popath/$pofile"
253) 			fi
254) 
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

255) 		fi
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

256) 	fi
257) 
258) 	# If a file with the same name but different priority
259) 	# exist, then rename the file (we have done so already)
260) 	# and update it with po4a-updatepo to make sure
261) 	# everything else is ok.
262) 	if [ $poexist = 2 ]
263) 	then
264) 		# Update the file
Runa A. Sandvik updated wml2po.sh to handle...

Runa A. Sandvik authored 13 years ago

265) 		if [ $wmlfile = "download.wml" ]
266) 		then
267) 			po4a-updatepo -f wml -m "$file" -p "$popath/$pofile" --master-charset utf-8 -o customtag="$customtag" -o nodefault="$nodefault" -o ontagerror="silent"
268) 		else
269) 			po4a-updatepo -f wml -m "$file" -p "$popath/$pofile" --master-charset utf-8 -o customtag="$customtag" -o nodefault="$nodefault"
270) 		fi
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

271) 	fi
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

272) 	
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

273) 	# Write to the logfile
274) 	if [ -e $logfile ]
275) 	then
276) 		if [ `cat $tmplog | grep "$popath/$pofile" | wc -l` -eq "0" ]
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

277) 		then
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

278) 			echo "could not process: " "$file" >> $logfile
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

279) 		fi
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

280) 	fi
Runa A. Sandvik put the scripts in website

Runa A. Sandvik authored 15 years ago

281) 
Runa A. Sandvik create pot files for just t...

Runa A. Sandvik authored 14 years ago

282) 	# Delete the temp log
283) 	rm -f $tmplog