X-Git-Url: https://pere.pagekite.me/gitweb/text-madewithcc.git/blobdiff_plain/e6abcdf8f98e6df2314791b2d29fb92081bf55f2..b60ae2dffcb9dda4b7710acbf51ee66a4f5ab93d:/fixup-docbook-translated.rb diff --git a/fixup-docbook-translated.rb b/fixup-docbook-translated.rb new file mode 100644 index 0000000..57958d4 --- /dev/null +++ b/fixup-docbook-translated.rb @@ -0,0 +1,42 @@ +#!/usr/bin/ruby +# coding: utf-8 + +require 'nokogiri' + +LogLevel=1 +raise ArgumentError, 'Language and source/destination files not specified' if ARGV.size != 3 +lang = ARGV[0] +srcfile = ARGV[1] +dstfile = ARGV[2] + +f=File.open(srcfile) +xml = Nokogiri::XML(f) + +def log(level,what) + indicators = %w(! • # -) + if level >= LogLevel + print indicators[level] + else + print "\n%s %s\n" % ['*' * (level+1), what] + end +end + +log 0, 'replace images with translated versions' +xml.search('imagedata').each do |img| + # Included images are in the 'Pictures' directory. Translated images + # are in Pictures/[langcode]/. Only translated images need to be + # included. + orig_img = img.attributes['fileref'].text + trans_img = orig_img.gsub(/Pictures/, "Pictures/#{lang}") + next if orig_img == trans_img + + if File.exists?(trans_img) + log 1, 'Replaced %s by %s' % [orig_img, trans_img] + img.set_attribute('fileref', trans_img) + end +end + +log 0, 'Writing processed file' +# Unable to figure out API way to replace DOCTYPE +data = xml.to_xml() +File.open(dstfile, 'w') {|f| f.write(data)}