]>
pere.pagekite.me Git - text-madewithcc.git/blob - fixup.rb
e67f25f2bdf7dca6e3c1694f666faf2b978ea72d
4 raise ArgumentError
, 'Source/destination files not specified' if ARGV.size !
= 2
8 data=File
.open(srcfile
).readlines
.map
{|l
| l
.chomp!
}
10 data.map
{|lin
| lin
.gsub!
(/\[\]{#anchor-?\d*}/, '')}
13 indicators
= %w(! •
# -)
15 print indicators
[level
]
17 print
"\n%s %s\n" % ['*' * (level+
1), what
]
21 # There are several titles that are spread in more than one line. Make
22 # them into translatable sentences.
23 log
0, 'Merging multiline sentences '
24 [ ['Made', '', 'with', '', 'Creative', '', 'Commons'],
25 ['The New','', 'World of', '', 'Digital', '', 'Commons'],
26 ['How', '', 'to Be', '', 'Made with', '', 'Creative', '', 'Commons'],
27 ['Providing a custom service to consumers of your work *', '\[MARKET-BASED\]*'],
28 ['Memberships and individual donations', '*\[RECIPROCITY-BASED\]*'],
29 ['The', '', 'Creative', '', 'Commons', '', 'Licenses'],
30 ['jonathanmann.net and', '', 'jonathanmann.bandcamp.com'],
31 ['PLOS','', '(Public Library of Science)']
33 # This should be done more generic, more robust... But before
34 # burning brain cells, verify if it's needed!
37 data.each_with_index
do |lin
, idx
|
43 raise RuntimeError
, 'Anchor string («%s» for «%s») not found' %
44 [str
[0], str
.reject
{|word
| word
==''}.join(' ')]
45 elsif matches
.size
> 1
46 raise RuntimeError
, 'Anchor string (%s) appears multiple times: %s' %
47 [str
[0], matches
.map
{|i
| i
.to_s
}.join(', ')]
49 log
2, 'Matches %s: %d - %s' % [str
.join(' '), matches
.size
, matches
.join(',')]
52 at
= data.index(str
[0])
53 joined
= str
.reject
{|word
| word
==''}.join(' ')
55 if str
[offset
] !
= data[at+offset
]
57 'Warning: String does not match ("%s" of "%s", offset %d, book at %d)' %
58 [str
[offset
], joined
, offset
, at+offset
]
63 (str
.size
- 1).times
{ data.delete_at(at+
1) }
66 log
0, 'correct emphesis in some titles'
67 data.map
{|lin
| lin
.gsub!
(/ *\* \\\[/, ' *\[')}
69 log
0, 'tag title and author'
70 data.delete_at(1) # Remove unwanted blank line between title and authors
71 data[0].sub!
(/^/, '% ') # title
72 data[1].sub!
(/^/, '% ') # authors
73 data[1].sub!
(/ and /, ';') # authors
75 # Mark up headings: Very artisanal and suboptimal, but should do the
78 # For every heading, put here the full string and its heading level,
86 # Try to keep this ordered as it appears within the book, as it will
87 # help us spot omissions and mistakes!
88 log
0, 'Mark up headings'
92 [1, 'The Big Picture'],
93 [2, 'The New World of Digital Commons'],
94 [3, 'The Commons, the Market, and the State'],
95 [3, 'The Four Aspects of a Resource'],
96 [4, 'Characteristics'],
97 [4, 'People and processes'],
98 [4, 'Norms and rules'],
100 [3, 'A Short History of the Commons'],
101 [3, 'The Digital Revolution'],
102 [3, 'The Birth of Creative Commons'],
103 [3, 'The Changing Market'],
104 [3, 'Benefits of the Digital Commons'],
105 [3, 'Our Case Studies'],
107 [2, 'How to Be Made with Creative Commons'],
108 [3, 'Problem Zero: Getting Discovered'],
109 [4, 'Use CC to grow a larger audience'],
110 [4, 'Use CC to get attribution and name recognition'],
111 [4, 'Use CC-licensed content as a marketing tool'],
112 [4, 'Use CC to enable hands-on engagement with your work'],
113 [4, 'Use CC to differentiate yourself'],
115 [4, 'Market-based revenue streams'],
116 [4, 'Providing a custom service to consumers of your work *\[MARKET-BASED\]*'],
117 [4, 'Charging for the physical copy *\[MARKET-BASED\]*'],
118 [4, 'Charging for the in-person version *\[MARKET-BASED\]*'],
119 [4, 'Selling merchandise *\[MARKET-BASED\]*'],
120 [4, 'Charging advertisers or sponsors *\[MARKET-BASED\]*'],
121 [4, 'Charging your content creators *\[MARKET-BASED\]*'],
122 [4, 'Charging a transaction fee *\[MARKET-BASED\]*'],
123 [4, 'Providing a service to your creators *\[MARKET-BASED\]*'],
124 [4, 'Licensing a trademark *\[MARKET-BASED\]*'],
125 [4, 'Reciprocity-based revenue streams'],
126 [4, 'Memberships and individual donations *\[RECIPROCITY-BASED\]*'],
127 [4, 'The pay-what-you-want model *\[RECIPROCITY-BASED\]*'],
128 [4, 'Crowdfunding *\[RECIPROCITY-BASED\]*'],
129 [3, 'Making Human Connections'],
131 [4, 'Be open and accountable'],
132 [4, 'Design for the good actors'],
133 [4, 'Treat humans like, well, humans'],
134 [4, 'State your principles and stick to them'],
135 [4, 'Build a community'],
136 [4, 'Give more to the commons than you take'],
137 [4, 'Involve people in what you do'],
139 [2, 'The Creative Commons Licenses'],
141 [1, 'The Case Studies'],
144 [2, 'Blender Institute'],
145 [2, 'Cards Against Humanity'],
146 [2, 'The Conversation'],
147 [2, 'Cory Doctorow'],
150 [2, 'Knowledge Unlatched'],
151 [2, 'Lumen Learning'],
152 [2, 'Jonathan Mann'],
154 [2, 'Open Data Institute'],
157 [2, 'Amanda Palmer'],
158 [2, 'PLOS (Public Library of Science)'],
164 [2, 'Tribe of Noise'],
165 [2, 'Wikimedia Foundation'],
167 [1, 'Acknowledgments'],
170 log
1, item
.join(' -> ')
171 at
= data.index
{|i
| i
== item
[1]}
173 raise RuntimeError
, 'Heading string (level %d) not found: «%s»' % item
175 data[at
] = '%s %s' % ['#' * item
[0], data[at
]]
178 # We have the explicit strings "Part 1" and "Part 2" as structural
179 # elements — They are to be generated upon book compilation. Nuke
181 data.delete("# Part 1")
182 data.delete("# Part 2")
184 log
0, 'add heading to colophon page'
185 data.insert(data.index('Made With Creative Commons'), '# Colophon {-}')
187 log
0, 'add dedication as separeate chapter'
188 data.insert(data.index('“I don’t know a whole lot about nonfiction journalism. . .'), '# Dedication {-}')
190 # Join erroneously split paragraphs: Write the contents of the line
191 # _preceding_ the unneeded break, the break will be removed.
193 # I'm noting the line number for each _after_ corrections so it's
194 # easier to find them; please keep them sorted! :-P
195 log
0, 'Join erroneously split paragraphs'
196 ['content and, in turn, spend money and', # 1595
197 'still other', # 1662
198 'content functions as a marketing tool for the paid product or', # 1724
199 'lowest-common-denominator solutions and', #2035
200 'to the values symbolized by', # 2145
201 'the kinds of participative communities that drive open', # 2157
203 'At a minimum, a CC-', # 2375
204 '“Share Your Work” at', # 2508
205 'easier to trust a', # 2580
206 'free download, the', # 3086
207 'openness to fans remixing the game—give', # 3087
208 'Attribution-', # 3307
209 'both journal publishers and researchers. Figshare now provides', # 3672
210 'get the “network effect”—', # 4002
211 'access to scholarly books. For Frances, the current scholarly-', # 4033
214 'contributing to the open', # 4438
215 'doesn’t seem like it should be sung about', # 4616
216 'songwriter, and he has found a way to keep it interesting for', # 4624
217 'building trust is the top', # 4793
220 'authors and Shuttleworth; Mark remains incredibly proud of this', # 6452
221 'BY-SA and opting in others with collecting societies like', # 7218
222 'Cecilie Maria, Cedric Howe, Cefn Hoile,', # 7796
223 'Braddlee, Drew Spencer, Duncan', # 7839
224 'Elizabeth Holloway, Ellen Buecher, Ellen Kaye-', # 7844
228 at
= data.index
{|i
| i
== line
}
229 if !at
.nil? and data[at+
1] == ''
234 log
0, 'Mark quote in dedication as quote with attribute in markdown'
235 start
= "“I don’t know a whole lot about nonfiction journalism. . ."
236 stop
= "- David Foster Wallace"
238 data.each_with_index
do |lin
, idx
|
240 lin
.sub!
(/^- /, "> — ")
241 lin
.sub!
("David Foster Wallace", "*David Foster Wallace*")
253 log
0, 'Identify and mark footnotes/endnodes'
256 data.each_with_index
do |lin
, idx
|
257 if lin
=~
/^## (.+)$/
258 scope
=$1.gsub(" ", "-")
260 # First, mark note reference
261 lin
.sub!
(/([a-z]\.["”]?)(\d+)(\s)/, "\\1[^" + scope +
"-\\2]\\3")
262 lin
.sub!
(/([a-z]\.["”]?)(\d+)$/, "\\1[^" + scope +
"-\\2]")
263 # Next, mark note content, only between /Web Links?|Notes/ and next heading
265 lin
.sub!
(/^(\d+)\. /, "[^" + scope +
"-\\1]: ")
270 if lin
=~
/^(### Notes|Web links?)/
272 # Turn web link line into section header, to make it easier to
273 # find by fixup-docbook.rb.
274 lin
.gsub!
(/^(Web links?)/, "### \\1")
278 log
0, 'Turn indented block after use cases into block quotes'
281 data.each_with_index
do |lin
, idx
|
285 if lin
=~
/^> Profile written by/
289 if lin
=~
/^## Bibliography/
293 if lin
=~
/^## Arduino/
296 if inscope
and lin
=~
/^## /
302 log
0, 'emphesize keywords'
303 data.map
{|lin
| lin
.gsub!
(/^(>\s*)(Revenue model|Interview date|Interviewees?):/, '\\1**\\2**:')}
305 log
0, 'make figure sizes relative to text body width while keeping aspect ratio'
306 data.map
{|lin
| lin
.gsub!
(/width="6.5in"/, 'width="100%"')}
307 data.map
{|lin
| lin
.gsub!
(/width="4.198in"/, 'width="40%"')}
308 data.map
{|lin
| lin
.gsub!
(/width="4.1665in"/, 'width="40%"')}
309 data.map
{|lin
| lin
.gsub!
(/height="[0-9.]+in"/, '')}
311 log
0, 'add figure titles required by Docbook for referable figures'
313 ['10000201000008000000045C30360249076453E6.png', 'Enterprise engagements'],
314 ['10000201000007D0000007D0ACF13F8B71EAF0B9.png', 'Aspects of resource management'],
315 ['10000201000009C40000065D9EC4F530BD4DFBE0.png', 'Different views on resources'],
316 ['10000201000009C4000005153EACBD62F00F6BA9.png', 'Long ago'],
317 ['10000201000009C4000005150F069409C1CC12F0.png', 'State takeover of the commons'],
318 ['10000201000009C400000515F1CAA15B223F6BAF.png', 'Today'],
320 at
= data.index
{|i
| i
.include? fig
[0]}
322 raise RuntimeError
, 'No figure named «%s» found' % fig
[0]
324 data[at
].gsub!
(/!\[\]\(Pictures/, '
327 log 0, 'adding http
:// to all URLs
and turn them into links
'
328 data.map {|lin| lin.gsub!(/(^|\s+)([-a-z0-9\\.]+\.(cc|com|edu|eu|io|is|it|kr|net|nl|nz|org|se))/, '\\1http://\\2')}
329 data.map
{|lin
| lin
.gsub!
(/\b(https?:\/\
/[-a-z0-9\\.]+)(\/[-\\.\
/a-zA-Z0-9#_\?&=,]+[-\/a-zA-Z0-9
#_\?&=,])?/, '[](\\1\\2)')}
331 log
0, 'Writing processed file'
332 File
.open(dstfile
, 'w') {|f
| f
.puts
data.join("\n")}