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