]> pere.pagekite.me Git - text-madewithcc.git/blob - fixup-docbook.rb
Fix typo.
[text-madewithcc.git] / fixup-docbook.rb
1 #!/usr/bin/ruby
2 # coding: utf-8
3
4 require 'nokogiri'
5
6 LogLevel=1
7 raise ArgumentError, 'Source/destination files not specified' if ARGV.size != 2
8 srcfile = ARGV[0]
9 dstfile = ARGV[1]
10
11 f=File.open(srcfile)
12 xml = Nokogiri::XML(f)
13
14 def log(level,what)
15 indicators = %w(! • # -)
16 if level >= LogLevel
17 print indicators[level]
18 else
19 print "\n%s %s\n" % ['*' * (level+1), what]
20 end
21 end
22
23 def partreplace(xml, partid, tag)
24 xml.css('part[id=' + partid + ']').each do |part|
25 part.name = tag
26 end
27 end
28
29
30 log 0, 'replace article* with book*'
31 xml.css('articleinfo').each do |node|
32 node.name = 'bookinfo'
33 node.first_element_child.before(<<'XML')
34 <publisher>
35 <publishername>Gunnar Wolf</publishername>
36 <address><city>Mexico City</city></address>
37 </publisher>
38 XML
39 node.first_element_child.before(<<'XML')
40 <copyright>
41 <year>2017</year>
42 <holder>Creative Commons</holder>
43 </copyright>
44 XML
45 end
46 xml.css('article').each do |node|
47 node.name = 'book'
48 node['lang'] = 'en'
49 end
50
51 log 0, 'change parts to colophon, dedication and chapter'
52 partreplace(xml, 'colophon', 'colophon')
53 partreplace(xml, 'dedication', 'dedication')
54 partreplace(xml, 'foreword', 'preface')
55 partreplace(xml, 'introduction', 'preface')
56 partreplace(xml, 'bibliography', 'chapter')
57 partreplace(xml, 'acknowledgments', 'chapter')
58
59 log 0, 'place part introduction into <partintro>'
60 s = xml.xpath("//part/title[text()='The Case Studies']")[0]
61 if s
62 s.after('<partintro>')
63 p = xml.css('part partintro')[0]
64 s.parent.xpath("//part/para").each do |node|
65 node.parent = p
66 end
67 end
68
69
70 log 0, 'remove empty notes/web links sections'
71 [
72 'Notes',
73 'Web links',
74 'Web link',
75 ].each do |title|
76 xml.xpath("//title[text()='%s']" % title).each do |node|
77 p = node.parent
78 node.remove
79 if p.content =~ /^\s*$/
80 p.remove
81 else
82 raise RuntimeError, 'Non-empty «%s» found' % title
83 end
84 end
85 end
86
87 log 0, 'remove title from dedication'
88 xml.css('dedication title')[0].content = ""
89
90 log 0, 'move legal notice to bookinfo'
91 xml.css('book bookinfo')[0].first_element_child.before('<legalnotice>')
92 ln = xml.css('book bookinfo legalnotice')[0]
93 xml.css('para').each do |para|
94 if para.content =~ /This book is published under a/
95 log 0, 'found legal'
96 para.parent = ln
97 break
98 end
99 end
100
101 log 0, 'replace colophon page with one for this edition'
102 xml.xpath('//colophon/para').remove
103 s = xml.xpath('//colophon')[0]
104 s.first_element_child.after(<<'XML')
105 <para>Made With Creative Commons</para>
106
107 <para>by Paul Stacey & Sarah Hinchliff Pearson</para>
108
109 <para>© 2017 by the Creative Commons Foundation.</para>
110
111 <para>Published under a Creative Commons Attribution-ShareAlike
112 license (CC BY-SA), version 4.0.</para>
113
114 <para>ISBN: YET-TO-BE-DECIDED (PDF), YET-TO-BE-DECIDED (ePub),
115 YET-TO-BE-DECIDED (Paperback) </para>
116
117 <para>Illustrations by Bryan Mathers, <ulink url="https://bryanmathers.com/"/></para>
118
119 <para>Publisher: Gunnar Wolf.</para>
120
121 <!-- space for information about translators -->
122 <para>&nbsp;</para>
123
124 <para>Downloadable e-book available at
125 <ulink url="https://madewith.cc/"/></para>
126
127 <para>This book is published under a CC BY-SA license, which means that you
128 can copy, redistribute, remix, transform, and build upon the content for
129 any purpose, even commercially, as long as you give appropriate credit,
130 provide a link to the license, and indicate if changes were made. If you
131 remix, transform, or build upon the material, you must distribute your
132 contributions under the same license as the original. License details:
133 <ulink url="http://creativecommons.org/licenses/by-sa/4.0/"/></para>
134
135 <para>Made With Creative Commons is published with the kind support of
136 Creative Commons and backers of our crowdfunding-campaign on the
137 Kickstarter.com platform.</para>
138
139 XML
140
141 log 0, 'remove title from colophon'
142 xml.css('colophon title')[0].content = ""
143
144 log 0, 'change CC logo images to informalfigure'
145 xml.css('figure mediaobject imageobject imagedata[width="40.0%"]').each do |id|
146 f = id.parent.parent.parent
147 f.name = 'informalfigure'
148 end
149
150 log 0, 'assigning IDs to formal figures'
151 seq = 1
152 xml.css('figure').each do |fig|
153 fig['id'] = 'fig-%d' % seq
154 seq = seq + 1
155 end
156
157 log 0, 'Writing processed file'
158 # Unable to figure out API way to replace DOCTYPE
159 data = xml.to_xml().gsub!(/DOCTYPE article/, 'DOCTYPE book')
160 File.open(dstfile, 'w') {|f| f.write(data)}