]> pere.pagekite.me Git - text-madewithcc.git/blob - fixup-docbook-translated.rb
Little little but unavoidable editorial changes
[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, 'Including the Spanish version introduction'
25 if intro = File.open('extra/es/intro_es.xml', 'r').read rescue nil
26 xml.search('preface').last.after( intro )
27 else
28 log 0, ' -!- Introduction text not found or empty'
29 end
30
31 log 0, 'replace images with translated versions'
32 xml.search('imagedata').each do |img|
33 # Included images are in the 'Pictures' directory. Translated images
34 # are in Pictures/[langcode]/. Only translated images need to be
35 # included.
36 orig_img = img.attributes['fileref'].text
37 trans_img = orig_img.gsub(/Pictures/, "Pictures/#{lang}")
38 next if orig_img == trans_img
39
40 if File.exists?(trans_img)
41 log 1, 'Replaced %s by %s' % [orig_img, trans_img]
42 img.set_attribute('fileref', trans_img)
43 end
44 end
45
46 log 0, 'Final editorial requests: Attributions on top, as in other chapters, also for prefaces'
47 xml.search('preface').each do |pref|
48 title = pref.search('title').first
49 attrib = pref.search('blockquote').last
50 attrib_text = attrib.inner_text.gsub(/\s+/,' ')
51
52 # Some formatting issues we need to modify
53 attrib_text.gsub!(/Paul y Sarah/, 'Paul <?latex \textup{>y<?latex } > Sarah')
54 attrib_text.gsub!(/Merkley (CEO.*)/, 'Merkley <citetitle>\1</citetitle>')
55
56 attrib.remove
57 title.after('<blockquote><attribution>%s</attribution></blockquote>' % attrib_text)
58 log 1, 'Moved: %s' % attrib_text
59 end
60
61 log 0, 'Writing processed file'
62 # Unable to figure out API way to replace DOCTYPE
63 data = xml.to_xml()
64 File.open(dstfile, 'w') {|f| f.write(data)}