]>
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', 'appendix')
57 partreplace(xml
, 'acknowledgments', 'appendix')
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><attribution>%s</attribution><para>%s</para></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 by Paul Stacey and Sarah Hinchliff Pearson</para>
148 <para>© 2017 by the Creative Commons Foundation.</para>
150 <para>Published under a Creative Commons Attribution-ShareAlike
151 license (CC BY-SA), version 4.0.</para>
153 <para>The license means that you can copy, redistribute, remix,
154 transform, and build upon the content for any purpose, even
155 commercially, as long as you give appropriate credit, provide a link
156 to the license, and indicate if changes were made. If you remix,
157 transform, or build upon the material, you must distribute your
158 contributions under the same license as the original. License details:
159 <ulink url="http://creativecommons.org/licenses/by-sa/4.0/"/></para>
161 <para>Illustrations by Bryan Mathers,
162 <ulink url="https://bryanmmathers.com/"/>.</para>
164 <para>Publisher: Gunnar Wolf.</para>
167 <!--space for information about translators-->
171 <para>Made With Creative Commons was originally published with the
172 kind support of Creative Commons and backers of our
173 crowdfunding-campaign on the Kickstarter.com platform.</para>
175 <para>This edition of the book is maintained on
176 <ulink url="https://gitlab.com/gunnarwolf/madewithcc-es/"/>, and the
177 translations are maintained on
178 <ulink url="https://hosted.weblate.org/projects/madewithcc/"/>. If
179 you find any error in the book, please let us know.</para>
181 <para>ISBN: YET-TO-BE-DECIDED (PDF), YET-TO-BE-DECIDED (ePub),
182 YET-TO-BE-DECIDED (Paperback) </para>
184 <para><ulink url="https://madewith.cc/"/></para>
187 (Dewey) 346.048, 347.78
191 (US Library of Congress) Z286 O63 S73 2017
200 log
0, 'remove title from colophon'
201 xml
.css('colophon title')[0].content
= ""
203 log
0, 'change CC logo images to informalfigure'
204 xml
.css('figure mediaobject imageobject imagedata[width="40.0%"]').each
do |id
|
205 f
= id
.parent
.parent
.parent
206 f
.name
= 'informalfigure'
209 log
0, 'assigning IDs to formal figures'
211 xml
.css('figure').each
do |fig
|
212 fig
['id'] = 'fig-%d' % seq
216 log
0, 'Set epigraph line in each case study'
217 xml
.css('blockquote para').select
{|p
| p
.inner_text
=~
/Profile written by/}.each
do |epi
|
221 p
.last_element_child
.after('<attribution>%s</attribution>' % by
)
225 # Disabled as dblatex do not understand chapter/chapterinfo/author,
226 # see <URL: https://bugs.debian.org/891183 >.
228 log
0, 'migrate chapter author into <chapterinfo> where relevant'
229 xml
.css('chapter para').each
do |para
|
230 if para
.content
=~
/^\s*((Paul|Sarah Hinchliff) (Stacey|Pearson))\s*$/
231 log
1, 'migrated %s %s' % [$2, $3]
232 para
.parent
.css('title')[0].before(<<'XML' % [$2, $3])
235 <firstname>%s</firstname><surname>%s</surname>
244 log
0, 'replace "Fig. \#." with docbook figure xref'
245 xml
.css("para").each
do |para
|
246 xml_text
= para
.to_xml(:skip_instruct => true).to_s
247 xml_text
.gsub!
(/Fig\.\s+(\d)\.?/,
248 'Fig. <xref xrefstyle="template:%n" linkend="fig-\\1"/>')
253 log
0, 'Writing processed file'
254 # Unable to figure out API way to replace DOCTYPE
255 data = xml
.to_xml().gsub!
(/DOCTYPE article/, 'DOCTYPE book')
256 File
.open(dstfile
, 'w') {|f
| f
.write(data)}