-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlite-actors.rb
executable file
·443 lines (379 loc) · 10.8 KB
/
highlite-actors.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/usr/local/bin/ruby
# = highlite-actors.rb
#
# Author:: Dirk Meyer
# Copyright:: Copyright (c) 2020-2023 Dirk Meyer
# License:: Distributes under the same terms as Ruby
#
require 'cgi'
require 'yaml'
require 'json'
require 'fileutils'
$: << '.'
# wiki config file
CONFIG_FILE = 'wiki-config.yml'.freeze
# list of patterns with different color
COLOR_CONFIG_FILE = 'colors.wiki'.freeze
# input for actors to wiki lines
WIKI_ACTORS = 'wiki-actors.json'.freeze
# input html
INPUT_HTML = 'all.html'.freeze
# old input html
CHANGED_HTML = 'all.html.orig'.freeze
# output directory
ACTORS_DIR = 'actors'.freeze
# output index for pdf
ACTORS_PDF_INDEX = 'actors.wiki'.freeze
# output index for html
ACTORS_HTML_INDEX = 'actors-html.wiki'.freeze
# regular expression for matching names
# U+0430, 0xD0 0xB0, Cyrillic Small Letter A
MATCH_NAME = "[A-Za-z0-9\u0430_'-]+".freeze
# css highlite with color
# HIGHLITE = ' style="background-color:#FFFF00;"'.freeze
HIGHLITE = ' class="highlite"'.freeze
# css changed highlite with color
NEWHIGHLITE = ' class="newhighlite"'.freeze
# css highlite with color
CHANGED = ' class="changed"'.freeze
$debug = 0
@ignore_changed = true
# read wiki paths
def read_yaml( filename, default = {} )
config = default
return config unless File.exist?( filename )
config.merge!( YAML.load_file( filename ) )
end
# read patters for color replacements from given file
def read_colors( filename )
@colors = {}
return unless File.exist?( filename )
File.read( filename ).split( "\n" ).each do |line|
next if line =~ /^#/
next if line =~ /^<[\/]*file/
next unless line.include?( '|' )
script, pattern, color = line.split( '|', 3 )
@colors[ script ] = {} unless @colors.key?( script )
@colors[ script ][ pattern ] = color
end
end
# check if downloaded file has changed
def downloaded?( filename, buffer )
return false unless File.exist?( filename )
old = File.read( filename )
return true if buffer == old
false
end
# write buffer to file
def file_put_contents( filename, buffer, mode = 'w+' )
return if downloaded?( filename, buffer )
File.open( filename, mode ) do |f|
f.write( buffer )
f.close
end
end
# add line with prefixed tag
def add_tag_line( line, tag )
@out << tag
@out << line
end
# add normal line
def add_line( line )
add_tag_line( line, '<p>' )
end
# save actor HTML file
def save_actor( actor )
# U+2019, 0xe2 0x80 0x99, RIGHT SINGLE QUOTATION MARK
actor2 = actor.downcase.delete( "'\u2019" )
actor2.delete!( ' #' )
filename = "#{ACTORS_DIR}/#{actor2}.html"
@seen_output[ filename ] = true
file_put_contents( filename, @out )
end
# get list of roles from wiki text
def parse_role_name( text )
rest = text.gsub( / *\[[^\]]*\]/, '' )
rest.gsub!( ''', "'" )
rest.gsub!( /^The /, '' )
rest.gsub!( /^A /, '' )
list = []
while /^#{MATCH_NAME}( and |, *)/ =~ rest
name, rest = rest.split( / and |, */, 2 )
name.sub!( /'s$/, '' )
list.push( name )
end
# Scene Sketches
name = rest.split( ' ', 2 )[ 0 ]
case name
when /^#{MATCH_NAME}'s:*$/
name.sub!( /'s$/, '' )
list.push( name.sub( ':', '' ) )
when /^#{MATCH_NAME}:*$/
list.push( name.sub( ':', '' ) )
when /^#{MATCH_NAME} *=*$/
# ignore group
else
warn "Error in Role: '#{name}', #{text}"
end
# p [ 'parse_role_name', list, text ]
list
end
# show some debug info
def debug( scene, actor, pattern, line )
return if $debug.zero?
return unless actor == 'Eisfuchs'
p [ scene, actor, pattern ]
p line
puts
end
# match the patterns for an actor anywhere in the line
def match_pattern( scene, actor, line )
return false unless @wiki_highlite[ scene ].key?( actor )
@wiki_highlite[ scene ][ actor ].each do |pattern|
next unless /[^a-z0-9]#{pattern}[^a-z0-9]/i =~ line
debug( scene, actor, pattern, line )
return true
end
false
end
# match the patterns for an actor in dialog lines
def match_list( scene, actor, pattern, line2 )
return false if line2 == ''
return false if line2 =~ /<a href=/
list = parse_role_name( line2 )
list.each do |role|
next unless /#{pattern}/i =~ role
debug( scene, actor, pattern, line2 )
return true
end
false
end
# search wiki text for actor in the line
def match_pattern2( scene, actor, line )
return nil unless @wiki_highlite[ scene ].key?( actor )
@wiki_highlite[ scene ][ actor ].each do |pattern|
# item
if pattern[ 0 ] == pattern[ 0 ].downcase
# next unless /[^a-z0-9]#{pattern}[^a-z0-9]/i =~ line
next unless /[^a-z0-9]#{pattern} *</i =~ line
debug( scene, actor, pattern, line )
return pattern
end
case line
when /%(ATT)%/
if /[^a-z0-9]#{pattern}[^a-z0-9]/i =~ line
debug( scene, actor, pattern, line )
return pattern
end
if /[^a-z0-9]#{actor}[^a-z0-9]/i =~ line
debug( scene, actor, pattern, line )
return pattern
end
next
when /%(SND)%/
if /[^a-z0-9]#{pattern}[^a-z0-9].*:/i =~ line
debug( scene, actor, pattern, line )
return pattern
end
next
when /%(HND|FOG|SPT)%/, /Setting:/
if /[^a-z0-9]#{actor}[^a-z0-9]/i =~ line
debug( scene, actor, pattern, line )
return pattern
end
next
when /%ACT%/
line2 = line.sub( /\n*.*%ACT%[^>]*> */, '' )
next unless match_list( scene, actor, pattern, line2 )
return pattern
end
line2 = line.sub( /^\n/, '' )
next if /^###/ =~ line2
return pattern if match_list( scene, actor, pattern, line2 )
end
nil
end
# find out the color for highlite
def find_highlite_color( actor, pattern )
return HIGHLITE unless @colors.key?( actor )
return HIGHLITE unless @colors[ actor ].key?( pattern )
" class=\"#{@colors[ actor ][ pattern ]}\""
end
# modify line when an actor was found
def parse_patterns( scene, actor, line )
return '' unless @wiki_highlite[ scene ].key?( actor )
pattern = match_pattern2( scene, actor, line )
return '' if pattern.nil?
find_highlite_color( actor, pattern )
end
# modify line where script changed
def parse_changed( scene, line )
return '' if @ignore_changed
return '' if @changed[ scene ].key?( line )
CHANGED
end
# decide what to highlite
def highlite_patterns( scene, actor, line )
changed = parse_changed( scene, line )
highlite = parse_patterns( scene, actor, line )
if changed == ''
if highlite == ''
add_line( line )
return
end
add_tag_line( line, "<p#{highlite}>" )
return
end
if highlite == ''
add_tag_line( line, "<p#{changed}>" )
return
end
add_tag_line( line, "<p#{NEWHIGHLITE}>" )
end
# @changed[ scene ][ line ]
# modify table row when an actor was found
def table_line( scene, actor, line )
sections = line.split( '<table' )
line2 = sections.first
highlite_patterns( scene, actor, line2 )
sections[ 1 .. ].each do |table|
rows = table.split( '<tr' )
add_tag_line( rows.first, '<table' )
rows[ 1 .. ].each do |row|
if /[^a-z0-9]#{actor}[^a-z0-9]/i =~ row
add_tag_line( row, "<tr#{HIGHLITE}" )
next
end
if actor =~ /'/
pattern2 = actor.gsub( "'", ''' )
if /[^a-z0-9]#{pattern2}[^a-z0-9]/i =~ row
add_tag_line( row, "<tr#{HIGHLITE}" )
next
end
end
add_tag_line( row, '<tr' )
end
end
end
# read old script foir changes
def read_changed
@changed = {}
scene = nil
lines = File.read( CHANGED_HTML ).split( '<p>' )
lines.each do |line|
case line
when /class="plugin_include_content /
scene = line.slice( /"plugin_include_content [^ {]+/ )
scene = scene.split( ':' ).last.delete( '"' )
scene << '.wiki'
next
end
next if scene.nil?
@changed[ scene ] = {} unless @changed.key?( scene )
@changed[ scene ][ line ] = true
end
end
# generate HTML output for an actor
def build_output( actor )
scene = nil
@out = ''
@linenumber = 0
version = File.stat( INPUT_HTML ).mtime.strftime( '%Y-%m-%d %H:%M' )
lines = File.read( INPUT_HTML ).split( '<p>' )
lines.each do |line|
@linenumber += 1
case line
when /<title>/
add_tag_line( line.sub( ':all', ":#{actor}:#{version}" ), '' )
next
when /<!-- TOC START -->/
add_tag_line( line, '' )
next
when /class="plugin_include_content /
add_line( line )
scene = line.slice( /"plugin_include_content [^ {]+/ )
scene = scene.split( ':' ).last.delete( '"' )
# scene.sub!( /([0-9])([0-9])/, ' \1-\2' )
scene << '.wiki'
next
end
if scene.nil?
add_line( line )
next
end
unless @wiki_highlite.key?( scene )
warn "not found in cache: '#{scene}'"
warn @wiki_highlite.keys.inspect
exit 1
end
case line
when /<div class="level1">/
add_line( line )
add_line( "#{version}</p>" )
next
when /table/
table_line( scene, actor, line )
next
when /%(AMB|LIG|MIX|MUS|PRE|VID)%/
add_line( line )
next
end
highlite_patterns( scene, actor, line )
end
save_actor( actor )
end
$config = read_yaml( CONFIG_FILE )
read_colors( COLOR_CONFIG_FILE )
read_changed
@wiki_highlite = JSON.parse( File.read( WIKI_ACTORS ) )
# pp @wiki_highlite
@seen_actors = {}
@seen_output = {}
@wiki_highlite.each_pair do |scene, h|
h.each_key do |actor|
next if actor == :skip
next if @seen_actors.key?( actor )
next if /_SH$/ =~ actor
@seen_actors[ actor ] = scene
build_output( actor )
end
end
Dir.entries( ACTORS_DIR ).each do |file|
case file
when /[.]html$/
filename = "#{ACTORS_DIR}/#{file}"
next if @seen_output[ filename ]
puts "removing old file: #{filename}"
File.unlink( filename )
when /[.]pdf$/
filename = "#{ACTORS_DIR}/#{file}"
htmlname = filename.sub( /[.]pdf$/, '.html' )
next if File.exist?( htmlname )
puts "removing old file: #{filename}"
File.unlink( filename )
end
end
namespace = $config[ 'qspath' ].gsub( ':', '%3A' )
url = "https://#{$config[ 'host' ]}/doku.php?id=#{$config[ 'qspath' ]}&do=media&ns=#{namespace}"
wiki = "====== Actors Scripts as PDF ======
Get your script via the
[[#{url}||Media_Manager]]
"
@seen_output.keys.sort.each do |file|
actor = file.split( '/' ).last.sub( /[.]html$/, '' )
wiki << "Script for {{:#{$config[ 'qspath' ]}:actors:#{actor}.pdf|#{actor}}}\\\\\n"
end
file_put_contents( ACTORS_PDF_INDEX, wiki )
url = "https://#{$config[ 'host' ]}/doku.php?id=#{$config[ 'qspath' ]}&do=media&ns=#{namespace}"
wiki = "====== Actors Scripts as HTML ======
Get your script via the
[[#{url}||Media_Manager]]
"
@seen_output.keys.sort.each do |file|
actor = file.split( '/' ).last.sub( /[.]html$/, '' )
wiki << "Script for {{:#{$config[ 'qspath' ]}:actors:#{actor}.html|#{actor}}}\\\\\n"
end
file_put_contents( ACTORS_HTML_INDEX, wiki )
FileUtils.touch( "#{ACTORS_DIR}/run.log" )
exit 0
# eof