]> pere.pagekite.me Git - text-madewithcc.git/blob - fixup.rb
Add more linting rules for es and nb editions.
[text-madewithcc.git] / fixup.rb
1 #!/usr/bin/ruby
2 # coding: utf-8
3 LogLevel=1
4 raise ArgumentError, 'Source/destination files not specified' if ARGV.size != 2
5 srcfile = ARGV[0]
6 dstfile = ARGV[1]
7
8 data=File.open(srcfile).readlines.map {|l| l.chomp!}
9
10 data.map {|lin| lin.gsub!(/\[\]{#anchor-?\d*}/, '')}
11
12 def log(level,what)
13 indicators = %w(! • # -)
14 if level >= LogLevel
15 print indicators[level]
16 else
17 print "\n%s %s\n" % ['*' * (level+1), what]
18 end
19 end
20
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)']
32 ].each do |str|
33 # This should be done more generic, more robust... But before
34 # burning brain cells, verify if it's needed!
35 log 1, str
36 matches=[]
37 data.each_with_index do |lin, idx|
38 if lin == str[0]
39 matches << idx
40 end
41 end
42 if matches.size == 0
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(', ')]
48 end
49 log 2, 'Matches %s: %d - %s' % [str.join(' '), matches.size, matches.join(',')]
50
51 len = str.size
52 at = data.index(str[0])
53 joined = str.reject {|word| word==''}.join(' ')
54 len.times do |offset|
55 if str[offset] != data[at+offset]
56 raise RuntimeError,
57 'Warning: String does not match ("%s" of "%s", offset %d, book at %d)' %
58 [str[offset], joined, offset, at+offset]
59 end
60 end
61
62 data[at] = joined
63 (str.size - 1).times { data.delete_at(at+1) }
64 end
65
66 # Mark up headings: Very artisanal and suboptimal, but should do the
67 # trick.
68 #
69 # For every heading, put here the full string and its heading level,
70 # as follows:
71 #
72 # 1 - Part
73 # 2 - Chapter
74 # 3 - Section
75 # 4 - Subsection
76 #
77 # Try to keep this ordered as it appears within the book, as it will
78 # help us spot omissions and mistakes!
79 log 0, 'Mark up headings'
80 [ [2, 'Foreword'],
81 [2, 'Introduction'],
82 [1, 'Part 1'],
83 [1, 'The Big Picture'],
84 [2, 'The New World of Digital Commons'],
85 [3, 'The Commons, the Market, and the State'],
86 [3, 'The Four Aspects of a Resource'],
87 [4, 'Characteristics'],
88 [4, 'People and processes'],
89 [4, 'Norms and rules'],
90 [4, 'Goals'],
91 [3, 'A Short History of the Commons'],
92 [3, 'The Digital Revolution'],
93 [3, 'The Birth of Creative Commons'],
94 [3, 'The Changing Market'],
95 [3, 'Benefits of the Digital Commons'],
96 [3, 'Our Case Studies'],
97 [3, 'Notes'],
98 [2, 'How to Be Made with Creative Commons'],
99 [3, 'Problem Zero: Getting Discovered'],
100 [4, 'Use CC to grow a larger audience'],
101 [4, 'Use CC to get attribution and name recognition'],
102 [4, 'Use CC-licensed content as a marketing tool'],
103 [4, 'Use CC to enable hands-on engagement with your work'],
104 [4, 'Use CC to differentiate yourself'],
105 [3, 'Making Money'],
106 [4, 'Market-based revenue streams'],
107 [4, 'Providing a custom service to consumers of your work * \[MARKET-BASED\]*'],
108 [4, 'Charging for the physical copy * \[MARKET-BASED\]*'],
109 [4, 'Charging for the in-person version * \[MARKET-BASED\]*'],
110 [4, 'Selling merchandise * \[MARKET-BASED\]*'],
111 [4, 'Charging advertisers or sponsors * \[MARKET-BASED\]*'],
112 [4, 'Charging your content creators * \[MARKET-BASED\]*'],
113 [4, 'Charging a transaction fee * \[MARKET-BASED\]*'],
114 [4, 'Providing a service to your creators* \[MARKET-BASED\]*'],
115 [4, 'Licensing a trademark* \[MARKET-BASED\]*'],
116 [4, 'Reciprocity-based revenue streams'],
117 [4, 'Memberships and individual donations *\[RECIPROCITY-BASED\]*'],
118 [4, 'The pay-what-you-want model *\[RECIPROCITY-BASED\]*'],
119 [4, 'Crowdfunding *\[RECIPROCITY-BASED\]*'],
120 [3, 'Making Human Connections'],
121 [4, 'Be human'],
122 [4, 'Be open and accountable'],
123 [4, 'Design for the good actors'],
124 [4, 'Treat humans like, well, humans'],
125 [4, 'State your principles and stick to them'],
126 [4, 'Build a community'],
127 [4, 'Give more to the commons than you take'],
128 [4, 'Involve people in what you do'],
129 [3, 'Notes'],
130 [2, 'The Creative Commons Licenses'],
131 [1, 'Part 2'],
132 [1, 'The Case Studies'],
133 [2, 'Arduino'],
134 [2, 'Ártica'],
135 [2, 'Blender Institute'],
136 [2, 'Cards Against Humanity'],
137 [2, 'The Conversation'],
138 [2, 'Cory Doctorow'],
139 [2, 'Figshare'],
140 [2, 'Figure.NZ'],
141 [2, 'Knowledge Unlatched'],
142 [2, 'Lumen Learning'],
143 [2, 'Jonathan Mann'],
144 [2, 'Noun Project'],
145 [2, 'Open Data Institute'],
146 [2, 'OpenDesk'],
147 [2, 'OpenStax'],
148 [2, 'Amanda Palmer'],
149 [2, 'PLOS (Public Library of Science)'],
150 [2, 'Rijksmuseum'],
151 [2, 'Shareable'],
152 [2, 'Siyavula'],
153 [2, 'Sparkfun'],
154 [2, 'TeachAIDS'],
155 [2, 'Tribe of Noise'],
156 [2, 'Wikimedia Foundation'],
157 [2, 'Bibliography'],
158 [2, 'Acknowledgments'],
159
160 ].each do |item|
161 log 1, item.join(' -> ')
162 at = data.index {|i| i == item[1]}
163 if at.nil?
164 raise RuntimeError, 'Heading string (level %d) not found: «%s»' % item
165 end
166 data[at] = '%s %s' % ['#' * item[0], data[at]]
167 end
168
169 # We have the explicit strings "Part 1" and "Part 2" as structural
170 # elements — They are to be generated upon book compilation. Nuke
171 # them.
172 data.delete("# Part 1")
173 data.delete("# Part 2")
174
175 # Join erroneously split paragraphs: Write the contents of the line
176 # _preceding_ the unneeded break, the break will be removed.
177 #
178 # I'm noting the line number for each _after_ corrections so it's
179 # easier to find them; please keep them sorted! :-P
180 log 0, 'Join erroneously split paragraphs'
181 ['content and, in turn, spend money and', # 1595
182 'still other', # 1662
183 'content functions as a marketing tool for the paid product or', # 1724
184 'lowest-common-denominator solutions and', #2035
185 'to the values symbolized by', # 2145
186 'the kinds of participative communities that drive open', # 2157
187 'time', # 2220
188 'At a minimum, a CC-', # 2375
189 'easier to trust a', # 2580
190 'free download, the', # 3086
191 'openness to fans remixing the game—give', # 3087
192 'Attribution-', # 3307
193 'both journal publishers and researchers. Figshare now provides', # 3672
194 'get the “network effect”—', # 4002
195 'access to scholarly books. For Frances, the current scholarly-', # 4033
196 'for-', # 4288
197 'sales', # 4410
198 'contributing to the open', # 4438
199 'doesn’t seem like it should be sung about', # 4616
200 'songwriter, and he has found a way to keep it interesting for', # 4624
201 'building trust is the top', # 4793
202 'version', # 6023
203 'license', # 6169
204 'authors and Shuttleworth; Mark remains incredibly proud of this', # 6452
205 'BY-SA and opting in others with collecting societies like', # 7218
206 'Cecilie Maria, Cedric Howe, Cefn Hoile,', # 7796
207 'Braddlee, Drew Spencer, Duncan', # 7839
208 'Elizabeth Holloway, Ellen Buecher, Ellen Kaye-', # 7844
209 'Helen', # 7874
210 ].each do |line|
211 log 1, line
212 at = data.index {|i| i == line}
213 if !at.nil? and data[at+1] == ''
214 data.delete_at(at+1)
215 end
216 end
217
218 log 0, 'Writing processed file'
219 File.open(dstfile, 'w') {|f| f.puts data.join("\n")}