]> pere.pagekite.me Git - text-madewithcc.git/blob - fixup-docbook-translated.rb
Incorporating 2nd reading (Spanish) on chapters 20--22
[text-madewithcc.git] / fixup-docbook-translated.rb
1 #!/usr/bin/ruby
2 # coding: utf-8
3
4 require 'nokogiri'
5
6 LogLevel=1
7 raise ArgumentError, 'Language and source/destination files not specified' if ARGV.size != 3
8 lang = ARGV[0]
9 srcfile = ARGV[1]
10 dstfile = ARGV[2]
11
12 f=File.open(srcfile)
13 xml = Nokogiri::XML(f)
14
15 def log(level,what)
16 indicators = %w(! • # -)
17 if level >= LogLevel
18 print indicators[level]
19 else
20 print "\n%s %s\n" % ['*' * (level+1), what]
21 end
22 end
23
24 log 0, 'replace images with translated versions'
25 xml.search('imagedata').each do |img|
26 # Included images are in the 'Pictures' directory. Translated images
27 # are in Pictures/[langcode]/. Only translated images need to be
28 # included.
29 orig_img = img.attributes['fileref'].text
30 trans_img = orig_img.gsub(/Pictures/, "Pictures/#{lang}")
31 next if orig_img == trans_img
32
33 if File.exists?(trans_img)
34 log 1, 'Replaced %s by %s' % [orig_img, trans_img]
35 img.set_attribute('fileref', trans_img)
36 end
37 end
38
39 log 0, 'Writing processed file'
40 # Unable to figure out API way to replace DOCTYPE
41 data = xml.to_xml()
42 File.open(dstfile, 'w') {|f| f.write(data)}