]>
pere.pagekite.me Git - text-madewithcc.git/blob - fixup-docbook.rb
7 raise ArgumentError
, 'Source/destination files not specified' if ARGV.size !
= 2
12 xml
= Nokogiri
::XML(f
)
15 indicators
= %w(! •
# -)
17 print indicators
[level
]
19 print
"\n%s %s\n" % ['*' * (level+
1), what
]
23 def partreplace(xml
, partid
, tag
)
24 xml
.css('part[id=' + partid +
']').each
do |part
|
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')
36 <holder>Creative Commons</holder>
39 node
.last_element_child
.after(<<'XML')
41 <publishername>Instituto de Investigaciones Económicas</publishername>
42 <address><city>Universidad Nacional Autónoma de México</city></address>
46 xml
.css('article').each
do |node
|
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')
59 log
0, 'place part introduction into <partintro>'
60 s
= xml
.xpath("//part/title[text()='The Case Studies']")[0]
62 s
.after('<partintro>')
63 p
= xml
.css('part partintro')[0]
64 s
.parent
.xpath("//part/para").each
do |node
|
70 log
0, 'remove empty notes/web links sections'
76 xml
.xpath("//title[text()='%s']" % title
).each
do |node
|
79 if p
.content
=~
/^\s*$/
82 raise RuntimeError
, 'Non-empty «%s» found' % title
87 log
0, 'remove title from dedication'
88 if ! xml
.css('dedication title').empty
?
89 xml
.css('dedication title')[0].content
= ""
92 log
0, 're-styling dedication, preface and introduction authors'
93 xml
.css('dedication').each
do |ded
|
94 # Take content out of the quote marks, and into the blockquote
95 quote
= ded
.css('quote').first
96 auth
= ded
.css('itemizedlist').first
97 quote_c
= quote
.content
99 # Styling the attribution (including the introducing hyphen) should
100 # be left to the following layers
101 auth_c
.gsub!
(/\s+/, ' ')
102 c
= ded
.css('quote').first
.content
103 quote
.parent
.remove
# remove the '<para>' containing the quote
105 ded
.last_element_child
.after('<blockquote><para>%s</para><attribution>%s</attribution></blockquote>' %
109 %w(preface
#foreword preface#introduction).each do |spec|
110 sect
= xml
.css(spec
)[0]
111 paras
= sect
.css('para emphasis')
112 auth
= '<blockquote><para><attribution>'
113 auth +
= paras
.map
{|p
| p
.content
}.join('</attribution></para><para><attribution>')
114 auth +
= '</attribution></para></blockquote>'
115 sect
.last_element_child
.after(auth
)
116 paras
.each
{|i
| i
.parent
.remove
} # Remove both the 'emphasis' and its parent 'para'
119 auth
= xml
.css('chapter')[0].css('para')[0]
120 auth
.content
=~
/^\s*Paul Stacey\s*$/ or
121 raise RuntimeError
, 'Error finding author name in chapter 1 -- ' + auth
.content
122 auth
.before('<blockquote><attribution>Paul Stacey</attribution></blockquote>')
125 auth
= xml
.css('chapter')[1].css('para')[0]
126 auth
.content
=~
/^\s*Sarah Hinchliff Pearson.*$/ or
127 raise RuntimeError
, 'Error finding author name in chapter 2 -- ' + auth
.content
128 auth
.before('<blockquote><attribution>Sarah Hinchliff Pearson</attribution></blockquote>')
131 log
0, 'move legal notice to bookinfo'
132 xml
.css('book bookinfo')[0].last_element_child
.after('<legalnotice>')
133 ln
= xml
.css('book bookinfo legalnotice')[0]
134 xml
.css('para').each
do |para
|
135 if para
.content
=~
/This book is published under a/
142 log
0, 'replace colophon page with one for this edition'
143 xml
.xpath('//colophon/para').remove
144 s
= xml
.xpath('//colophon')[0]
145 s
.first_element_child
.after(<<'XML')
146 <para>Made with Creative Commons</para>
148 <para>by Paul Stacey & Sarah Hinchliff Pearson</para>
150 <para>© 2017 by the Creative Commons Foundation.</para>
152 <para>Published under a Creative Commons Attribution-ShareAlike
153 license (CC BY-SA), version 4.0.</para>
155 <para>ISBN: YET-TO-BE-DECIDED (PDF), YET-TO-BE-DECIDED (ePub),
156 YET-TO-BE-DECIDED (Paperback) </para>
158 <para>Illustrations by Bryan Mathers,
159 <ulink url="https://bryanmmathers.com/"/>.</para>
161 <para>Publisher: Gunnar Wolf.</para>
164 <!--space for information about translators-->
168 <para>Downloadable e-book available at
169 <ulink url="https://madewith.cc/"/>.</para>
171 <para>This book is published under a CC BY-SA license, which means that you
172 can copy, redistribute, remix, transform, and build upon the content for
173 any purpose, even commercially, as long as you give appropriate credit,
174 provide a link to the license, and indicate if changes were made. If you
175 remix, transform, or build upon the material, you must distribute your
176 contributions under the same license as the original. License details:
177 <ulink url="http://creativecommons.org/licenses/by-sa/4.0/"/></para>
179 <para>Made With Creative Commons is published with the kind support of
180 Creative Commons and backers of our crowdfunding-campaign on the
181 Kickstarter.com platform.</para>
183 <para>This edition of the book is maintained on
184 <ulink url="https://gitlab.com/gunnarwolf/madewithcc-es/"/>, and the
185 translations are maintained on
186 <ulink url="https://hosted.weblate.org/projects/madewithcc/"/>. If
187 you find any error in the book, please let us know via Gitlab or Weblate.</para>
194 (Dewey) 346.048, 347.78
202 (US Library of Congress) Z286 O63 S73 2017
215 log
0, 'remove title from colophon'
216 xml
.css('colophon title')[0].content
= ""
218 log
0, 'change CC logo images to informalfigure'
219 xml
.css('figure mediaobject imageobject imagedata[width="40.0%"]').each
do |id
|
220 f
= id
.parent
.parent
.parent
221 f
.name
= 'informalfigure'
224 log
0, 'assigning IDs to formal figures'
226 xml
.css('figure').each
do |fig
|
227 fig
['id'] = 'fig-%d' % seq
231 log
0, 'Set epigraph line in each case study'
232 xml
.css('blockquote para').select
{|p
| p
.inner_text
=~
/Profile written by/}.each
do |epi
|
236 p
.last_element_child
.after('<attribution>%s</attribution>' % by
)
240 # Disabled as dblatex do not understand chapter/chapterinfo/author,
241 # see <URL: https://bugs.debian.org/891183 >.
243 log
0, 'migrate chapter author into <chapterinfo> where relevant'
244 xml
.css('chapter para').each
do |para
|
245 if para
.content
=~
/^\s*((Paul|Sarah Hinchliff) (Stacey|Pearson))\s*$/
246 log
1, 'migrated %s %s' % [$2, $3]
247 para
.parent
.css('title')[0].before(<<'XML' % [$2, $3])
250 <firstname>%s</firstname><surname>%s</surname>
259 log
0, 'Writing processed file'
260 # Unable to figure out API way to replace DOCTYPE
261 data = xml
.to_xml().gsub!
(/DOCTYPE article/, 'DOCTYPE book')
262 File
.open(dstfile
, 'w') {|f
| f
.write(data)}