]> pere.pagekite.me Git - text-madewithcc.git/blob - fixup-docbook.rb
Worked on the copyedits for several more pages (reached 2.3.6)
[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.last_element_child.after(<<'XML')
34 <copyright>
35 <year>2017</year>
36 <holder>Creative Commons</holder>
37 </copyright>
38 XML
39 node.last_element_child.after(<<'XML')
40 <publisher>
41 <publishername>Instituto de Investigaciones Económicas</publishername>
42 <address><city>Universidad Nacional Autónoma de México</city></address>
43 </publisher>
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 if ! xml.css('dedication title').empty?
89 xml.css('dedication title')[0].content = ""
90 end
91
92 log 0, 'move legal notice to bookinfo'
93 xml.css('book bookinfo')[0].last_element_child.after('<legalnotice>')
94 ln = xml.css('book bookinfo legalnotice')[0]
95 xml.css('para').each do |para|
96 if para.content =~ /This book is published under a/
97 log 0, 'found legal'
98 para.parent = ln
99 break
100 end
101 end
102
103 log 0, 'replace colophon page with one for this edition'
104 xml.xpath('//colophon/para').remove
105 s = xml.xpath('//colophon')[0]
106 s.first_element_child.after(<<'XML')
107 <para>Made with Creative Commons</para>
108
109 <para>by Paul Stacey & Sarah Hinchliff Pearson</para>
110
111 <para>© 2017 by the Creative Commons Foundation.</para>
112
113 <para>Published under a Creative Commons Attribution-ShareAlike
114 license (CC BY-SA), version 4.0.</para>
115
116 <para>ISBN: YET-TO-BE-DECIDED (PDF), YET-TO-BE-DECIDED (ePub),
117 YET-TO-BE-DECIDED (Paperback) </para>
118
119 <para>Illustrations by Bryan Mathers,
120 <ulink url="https://bryanmmathers.com/"/>.</para>
121
122 <para>Publisher: Gunnar Wolf.</para>
123
124 <para>
125 <!--space for information about translators-->
126 &nbsp;
127 </para>
128
129 <para>Downloadable e-book available at
130 <ulink url="https://madewith.cc/"/>.</para>
131
132 <para>This book is published under a CC BY-SA license, which means that you
133 can copy, redistribute, remix, transform, and build upon the content for
134 any purpose, even commercially, as long as you give appropriate credit,
135 provide a link to the license, and indicate if changes were made. If you
136 remix, transform, or build upon the material, you must distribute your
137 contributions under the same license as the original. License details:
138 <ulink url="http://creativecommons.org/licenses/by-sa/4.0/"/></para>
139
140 <para>Made With Creative Commons is published with the kind support of
141 Creative Commons and backers of our crowdfunding-campaign on the
142 Kickstarter.com platform.</para>
143
144 <para>This edition of the book is maintained on
145 <ulink url="https://gitlab.com/gunnarwolf/madewithcc-es/"/>, and the
146 translations are maintained on
147 <ulink url="https://hosted.weblate.org/projects/madewithcc/"/>. If
148 you find any error in the book, please let us know via Gitlab or Weblate.</para>
149
150 <para>
151 Classifications:
152 </para>
153
154 <para>
155 (Dewey) 346.048, 347.78
156 </para>
157
158 <para>
159 (UDK) ?
160 </para>
161
162 <para>
163 (US Library of Congress) Z286 O63 S73 2017
164 </para>
165
166 <para>
167 (Melvil) 025.523
168 </para>
169
170 <para>
171 (ACM CRCS) ?
172 </para>
173
174 XML
175
176 log 0, 'remove title from colophon'
177 xml.css('colophon title')[0].content = ""
178
179 log 0, 'change CC logo images to informalfigure'
180 xml.css('figure mediaobject imageobject imagedata[width="40.0%"]').each do |id|
181 f = id.parent.parent.parent
182 f.name = 'informalfigure'
183 end
184
185 log 0, 'assigning IDs to formal figures'
186 seq = 1
187 xml.css('figure').each do |fig|
188 fig['id'] = 'fig-%d' % seq
189 seq = seq + 1
190 end
191
192 # Disabled as dblatex do not understand chapter/chapterinfo/author,
193 # see <URL: https://bugs.debian.org/891183 >.
194 if false
195 log 0, 'migrate chapter author into <chapterinfo> where relevant'
196 xml.css('chapter para').each do |para|
197 if para.content =~ /^\s*((Paul|Sarah Hinchliff) (Stacey|Pearson))\s*$/
198 log 1, 'migrated %s %s' % [$2, $3]
199 para.parent.css('title')[0].before(<<'XML' % [$2, $3])
200 <chapterinfo>
201 <author>
202 <firstname>%s</firstname><surname>%s</surname>
203 </author>
204 </chapterinfo>
205 XML
206 para.remove
207 end
208 end
209 end
210
211 log 0, 'Writing processed file'
212 # Unable to figure out API way to replace DOCTYPE
213 data = xml.to_xml().gsub!(/DOCTYPE article/, 'DOCTYPE book')
214 File.open(dstfile, 'w') {|f| f.write(data)}