]> pere.pagekite.me Git - text-madewithcc.git/blob - fixup-docbook.rb
Fixed fixup-docbook.rb, as changes in Nokogiri rendered it useless
[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').select {|p| p.attributes['id'].value == partid}.each do |part|
25 part.name = tag
26 end
27 end
28
29
30 log 0, 'replace article* with book*'
31 xml.children[0].name = 'book'
32 xml.children[1].name = 'book'
33 xml.css('info').each do |node|
34 node.last_element_child.after(<<'XML')
35 <copyright>
36 <year>2017</year>
37 <holder>Creative Commons</holder>
38 </copyright>
39 XML
40 node.last_element_child.after(<<'XML')
41 <publisher>
42 <publishername>Gunnar Wolf</publishername>
43 <address><city>Mexico City</city></address>
44 </publisher>
45 XML
46 end
47 xml.css('article').each do |node|
48 node.name = 'book'
49 node['lang'] = 'en'
50 end
51
52 log 0, 'change parts to colophon, dedication and chapter'
53 partreplace(xml, 'colophon', 'colophon')
54 partreplace(xml, 'dedication', 'dedication')
55 partreplace(xml, 'foreword', 'preface')
56 partreplace(xml, 'introduction', 'preface')
57 partreplace(xml, 'bibliography', 'chapter')
58 partreplace(xml, 'acknowledgments', 'chapter')
59
60 log 0, 'place part introduction into <partintro>'
61 s = xml.search('part title').select {|tit| tit.content =~ /The Case Studies/}[0]
62
63 if s
64 s.after('<partintro>')
65 p = xml.css('part partintro')[0]
66 s.parent.children.select{|c| c.name == 'para'}.each do |node|
67 node.parent = p
68 end
69 else
70 log 0, 'Adding <partintro> failed!'
71 end
72
73
74 log 0, 'remove empty notes/web links sections'
75 [
76 'Notes',
77 'Web links',
78 'Web link',
79 ].each do |title|
80 xml.search('title').select {|t| t.content == title}.each do |node|
81 p = node.parent
82 node.remove
83 if p.content =~ /^\s*$/
84 p.remove
85 else
86 raise RuntimeError, 'Non-empty «%s» found' % title
87 end
88 end
89 end
90
91 log 0, 'remove title from dedication'
92 xml.css('dedication title')[0].content = ""
93
94 log 0, 'move legal notice to bookinfo'
95 xml.css('book info')[0].last_element_child.after('<legalnotice>')
96 ln = xml.css('book info legalnotice')[0]
97 xml.css('para').each do |para|
98 if para.content =~ /This book is published under a/
99 log 0, 'found legal'
100 para.parent = ln
101 break
102 end
103 end
104
105 log 0, 'replace colophon page with one for this edition'
106 xml.search('colophon para').map {|p| p.remove}
107 s = xml.search('colophon')[0]
108 s.first_element_child.after(<<'XML')
109 <para>Made with Creative Commons</para>
110
111 <para>by Paul Stacey & Sarah Hinchliff Pearson</para>
112
113 <para>© 2017 by the Creative Commons Foundation.</para>
114
115 <para>Published under a Creative Commons Attribution-ShareAlike
116 license (CC BY-SA), version 4.0.</para>
117
118 <para>ISBN: YET-TO-BE-DECIDED (PDF), YET-TO-BE-DECIDED (ePub),
119 YET-TO-BE-DECIDED (Paperback) </para>
120
121 <para>Illustrations by Bryan Mathers,
122 <ulink url="https://bryanmmathers.com/"/>.</para>
123
124 <para>Publisher: Gunnar Wolf.</para>
125
126 <para>
127 <!--space for information about translators-->
128 &nbsp;
129 </para>
130
131 <para>Downloadable e-book available at
132 <ulink url="https://madewith.cc/"/>.</para>
133
134 <para>This book is published under a CC BY-SA license, which means that you
135 can copy, redistribute, remix, transform, and build upon the content for
136 any purpose, even commercially, as long as you give appropriate credit,
137 provide a link to the license, and indicate if changes were made. If you
138 remix, transform, or build upon the material, you must distribute your
139 contributions under the same license as the original. License details:
140 <ulink url="http://creativecommons.org/licenses/by-sa/4.0/"/></para>
141
142 <para>Made With Creative Commons is published with the kind support of
143 Creative Commons and backers of our crowdfunding-campaign on the
144 Kickstarter.com platform.</para>
145
146 <para>This edition of the book is maintained on
147 <ulink url="https://gitlab.com/gunnarwolf/madewithcc-es/"/>, and the
148 translations are maintained on
149 <ulink url="https://hosted.weblate.org/projects/madewithcc/"/>. If
150 you find any error in the book, please let us know via gitlab.</para>
151
152 <para>
153 Classifications:
154 </para>
155
156 <para>
157 (Dewey) 346.048, 347.78
158 </para>
159
160 <para>
161 (UDK) ?
162 </para>
163
164 <para>
165 (US Library of Congress) Z286 O63 S73 2017
166 </para>
167
168 <para>
169 (Melvil) 025.523
170 </para>
171
172 <para>
173 (ACM CRCS) ?
174 </para>
175
176 XML
177
178 log 0, 'remove title from colophon'
179 xml.css('colophon title')[0].content = ""
180
181 log 0, 'change CC logo images to informalfigure'
182 xml.css('figure mediaobject imageobject imagedata[width="40.0%"]').each do |id|
183 f = id.parent.parent.parent
184 f.name = 'informalfigure'
185 end
186
187 log 0, 'assigning IDs to formal figures'
188 seq = 1
189 xml.css('figure').each do |fig|
190 fig['id'] = 'fig-%d' % seq
191 seq = seq + 1
192 end
193
194 # Disabled as dblatex do not understand chapter/chapterinfo/author,
195 # see <URL: https://bugs.debian.org/891183 >.
196 if false
197 log 0, 'migrate chapter author into <chapterinfo> where relevant'
198 xml.css('chapter para').each do |para|
199 if para.content =~ /^\s*((Paul|Sarah Hinchliff) (Stacey|Pearson))\s*$/
200 log 1, 'migrated %s %s' % [$2, $3]
201 para.parent.css('title')[0].before(<<'XML' % [$2, $3])
202 <chapterinfo>
203 <author>
204 <firstname>%s</firstname><surname>%s</surname>
205 </author>
206 </chapterinfo>
207 XML
208 para.remove
209 end
210 end
211 end
212
213 log 0, 'Writing processed file (%s)' % dstfile
214 File.open(dstfile, 'w') {|f| f.write(xml.to_xml())}