]> pere.pagekite.me Git - text-madewithcc.git/blob - fixup.rb
Translated using Weblate (Norwegian Bokmål)
[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 log 0, 'correct emphesis in some titles'
67 data.map {|lin| lin.gsub!(/ *\* \\\[/, ' *\[')}
68
69 # Mark up headings: Very artisanal and suboptimal, but should do the
70 # trick.
71 #
72 # For every heading, put here the full string and its heading level,
73 # as follows:
74 #
75 # 1 - Part
76 # 2 - Chapter
77 # 3 - Section
78 # 4 - Subsection
79 #
80 # Try to keep this ordered as it appears within the book, as it will
81 # help us spot omissions and mistakes!
82 log 0, 'Mark up headings'
83 [ [2, 'Foreword'],
84 [2, 'Introduction'],
85 [1, 'Part 1'],
86 [1, 'The Big Picture'],
87 [2, 'The New World of Digital Commons'],
88 [3, 'The Commons, the Market, and the State'],
89 [3, 'The Four Aspects of a Resource'],
90 [4, 'Characteristics'],
91 [4, 'People and processes'],
92 [4, 'Norms and rules'],
93 [4, 'Goals'],
94 [3, 'A Short History of the Commons'],
95 [3, 'The Digital Revolution'],
96 [3, 'The Birth of Creative Commons'],
97 [3, 'The Changing Market'],
98 [3, 'Benefits of the Digital Commons'],
99 [3, 'Our Case Studies'],
100 [3, 'Notes'],
101 [2, 'How to Be Made with Creative Commons'],
102 [3, 'Problem Zero: Getting Discovered'],
103 [4, 'Use CC to grow a larger audience'],
104 [4, 'Use CC to get attribution and name recognition'],
105 [4, 'Use CC-licensed content as a marketing tool'],
106 [4, 'Use CC to enable hands-on engagement with your work'],
107 [4, 'Use CC to differentiate yourself'],
108 [3, 'Making Money'],
109 [4, 'Market-based revenue streams'],
110 [4, 'Providing a custom service to consumers of your work *\[MARKET-BASED\]*'],
111 [4, 'Charging for the physical copy *\[MARKET-BASED\]*'],
112 [4, 'Charging for the in-person version *\[MARKET-BASED\]*'],
113 [4, 'Selling merchandise *\[MARKET-BASED\]*'],
114 [4, 'Charging advertisers or sponsors *\[MARKET-BASED\]*'],
115 [4, 'Charging your content creators *\[MARKET-BASED\]*'],
116 [4, 'Charging a transaction fee *\[MARKET-BASED\]*'],
117 [4, 'Providing a service to your creators *\[MARKET-BASED\]*'],
118 [4, 'Licensing a trademark *\[MARKET-BASED\]*'],
119 [4, 'Reciprocity-based revenue streams'],
120 [4, 'Memberships and individual donations *\[RECIPROCITY-BASED\]*'],
121 [4, 'The pay-what-you-want model *\[RECIPROCITY-BASED\]*'],
122 [4, 'Crowdfunding *\[RECIPROCITY-BASED\]*'],
123 [3, 'Making Human Connections'],
124 [4, 'Be human'],
125 [4, 'Be open and accountable'],
126 [4, 'Design for the good actors'],
127 [4, 'Treat humans like, well, humans'],
128 [4, 'State your principles and stick to them'],
129 [4, 'Build a community'],
130 [4, 'Give more to the commons than you take'],
131 [4, 'Involve people in what you do'],
132 [3, 'Notes'],
133 [2, 'The Creative Commons Licenses'],
134 [1, 'Part 2'],
135 [1, 'The Case Studies'],
136 [2, 'Arduino'],
137 [2, 'Ártica'],
138 [2, 'Blender Institute'],
139 [2, 'Cards Against Humanity'],
140 [2, 'The Conversation'],
141 [2, 'Cory Doctorow'],
142 [2, 'Figshare'],
143 [2, 'Figure.NZ'],
144 [2, 'Knowledge Unlatched'],
145 [2, 'Lumen Learning'],
146 [2, 'Jonathan Mann'],
147 [2, 'Noun Project'],
148 [2, 'Open Data Institute'],
149 [2, 'OpenDesk'],
150 [2, 'OpenStax'],
151 [2, 'Amanda Palmer'],
152 [2, 'PLOS (Public Library of Science)'],
153 [2, 'Rijksmuseum'],
154 [2, 'Shareable'],
155 [2, 'Siyavula'],
156 [2, 'SparkFun'],
157 [2, 'TeachAIDS'],
158 [2, 'Tribe of Noise'],
159 [2, 'Wikimedia Foundation'],
160 [2, 'Bibliography'],
161 [2, 'Acknowledgments'],
162
163 ].each do |item|
164 log 1, item.join(' -> ')
165 at = data.index {|i| i == item[1]}
166 if at.nil?
167 raise RuntimeError, 'Heading string (level %d) not found: «%s»' % item
168 end
169 data[at] = '%s %s' % ['#' * item[0], data[at]]
170 end
171
172 # We have the explicit strings "Part 1" and "Part 2" as structural
173 # elements — They are to be generated upon book compilation. Nuke
174 # them.
175 data.delete("# Part 1")
176 data.delete("# Part 2")
177
178 # Join erroneously split paragraphs: Write the contents of the line
179 # _preceding_ the unneeded break, the break will be removed.
180 #
181 # I'm noting the line number for each _after_ corrections so it's
182 # easier to find them; please keep them sorted! :-P
183 log 0, 'Join erroneously split paragraphs'
184 ['content and, in turn, spend money and', # 1595
185 'still other', # 1662
186 'content functions as a marketing tool for the paid product or', # 1724
187 'lowest-common-denominator solutions and', #2035
188 'to the values symbolized by', # 2145
189 'the kinds of participative communities that drive open', # 2157
190 'time', # 2220
191 'At a minimum, a CC-', # 2375
192 'easier to trust a', # 2580
193 'free download, the', # 3086
194 'openness to fans remixing the game—give', # 3087
195 'Attribution-', # 3307
196 'both journal publishers and researchers. Figshare now provides', # 3672
197 'get the “network effect”—', # 4002
198 'access to scholarly books. For Frances, the current scholarly-', # 4033
199 'for-', # 4288
200 'sales', # 4410
201 'contributing to the open', # 4438
202 'doesn’t seem like it should be sung about', # 4616
203 'songwriter, and he has found a way to keep it interesting for', # 4624
204 'building trust is the top', # 4793
205 'version', # 6023
206 'license', # 6169
207 'authors and Shuttleworth; Mark remains incredibly proud of this', # 6452
208 'BY-SA and opting in others with collecting societies like', # 7218
209 'Cecilie Maria, Cedric Howe, Cefn Hoile,', # 7796
210 'Braddlee, Drew Spencer, Duncan', # 7839
211 'Elizabeth Holloway, Ellen Buecher, Ellen Kaye-', # 7844
212 'Helen', # 7874
213 ].each do |line|
214 log 1, line
215 at = data.index {|i| i == line}
216 if !at.nil? and data[at+1] == ''
217 data.delete_at(at+1)
218 end
219 end
220
221 log 0, 'Mark quote in dedication as quote with attribute in markdown'
222 start = "“I don’t know a whole lot about nonfiction journalism. . ."
223 stop = "- David Foster Wallace"
224 quote=false
225 data.each_with_index do |lin, idx|
226 if lin == stop
227 lin.sub!(/^- /, "> &mdash; ")
228 lin.sub!("David Foster Wallace", "*David Foster Wallace*")
229 quote=false
230 break
231 end
232 if lin == start
233 quote=true
234 end
235 if quote
236 lin.sub!(/^/, "> ")
237 end
238 end
239
240 log 0, 'Writing processed file'
241 File.open(dstfile, 'w') {|f| f.puts data.join("\n")}