]> pere.pagekite.me Git - text-madewithcc.git/blob - fixup-docbook-translated.rb
Move translated image selection into po file.
[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, 'Final editorial requests: Attributions on top, as in other chapters, also for prefaces'
32 xml.search('preface').each do |pref|
33 title = pref.search('title').first
34 attrib = pref.search('blockquote').last
35 attrib_text = attrib.inner_text.gsub(/\s+/,' ')
36
37 # Some formatting issues we need to modify
38 attrib_text.gsub!(/Paul y Sarah/, 'Paul <?latex \textup{>y<?latex } > Sarah')
39 attrib_text.gsub!(/Merkley (CEO.*)/, 'Merkley <citetitle>\1</citetitle>')
40 attrib_text.gsub!(/Wolf (Ins.*)/, 'Wolf <citetitle>\1</citetitle>')
41
42 attrib.remove
43 title.after('<blockquote><attribution>%s</attribution></blockquote>' % attrib_text)
44 log 1, 'Moved: %s' % attrib_text
45 end
46
47 log 0, 'Replace legal info by the information assembled by our publisher'
48 if legal = File.open('extra/es/legal_info.xml', 'r').read rescue nil
49 xml.search('colophon').first.inner_html = legal
50 end
51
52 log 0, 'Add a colophon at the end'
53 # Doing this the dirty, non-DocBooky way: I'm adding a large <?latex
54 # ... ?> section to the last appendix. This should probably go in the
55 # <colophon> section, but it's already used.
56 #
57 # A cleaner way would be to replace the <xsl:param
58 # name="latex.enddocument"> section in pdf_es.xsl — But it gets
59 # trickier :-Þ
60 if colophon = File.open('extra/es/colophon.tex', 'r').read rescue nil
61 xml.search('appendix').last.search('para').last.after('<?latex %s ?>' % colophon)
62 end
63
64
65 log 0, 'Writing processed file'
66 # Unable to figure out API way to replace DOCTYPE
67 data = xml.to_xml()
68 File.open(dstfile, 'w') {|f| f.write(data)}