-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
html_output.py
1295 lines (1069 loc) · 46.6 KB
/
html_output.py
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# encoding: utf-8
import os, gettext, time, copy, sys, re
import traceback
import unit_tree.helpers as helpers
import wesnoth.wmlparser3 as wmlparser3
pics_location = "../../pics"
html_entity_horizontal_bar = '―'
html_header = '''
<!DOCTYPE html>
<html class="no-js wmlunits %(classes)s" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montaga%%7COpen+Sans:400,400i,700,700i" type="text/css" />
<link rel="icon" type="image/png" href="https://www.wesnoth.org/wesmere/img/favicon-32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="https://www.wesnoth.org/wesmere/img/favicon-16.png" sizes="16x16" />
<link rel="stylesheet" type="text/css" href="http://wesmere.localhost/wesmere/css/wesmere-1.1.0.css" />
<link rel="stylesheet" type="text/css" href="http://wesmere.localhost/wesmere/css/wmlunits-1.1.0.css" />
<script src="https://www.wesnoth.org/wesmere/js/modernizr.js"></script>
<script type="text/javascript" src="%(path)s/menu.js"></script>
<title>%(title)s - Wesnoth Units Database</title>
</head>
<body>
<div id="main">'''.strip()
top_bar = '''
<div id="nav" role="banner">
<div class="centerbox">
<div id="logo">
<a href="https://www.wesnoth.org/" aria-label="Wesnoth logo"></a>
</div>
<ul id="navlinks">
<li><a href="https://www.wesnoth.org/">Home</a></li>
<li><a href="https://forums.wesnoth.org/viewforum.php?f=62">News</a></li>
<li><a href="https://wiki.wesnoth.org/Play">Play</a></li>
<li><a href="https://wiki.wesnoth.org/Create">Create</a></li>
<li><a href="https://forums.wesnoth.org/">Forums</a></li>
<li><a href="https://wiki.wesnoth.org/Project">About</a></li>
</ul>
<div id="sitesearch" role="search">
<form method="get" action="https://wiki.wesnoth.org/">
<input id="searchbox" type="search" name="search" placeholder="Search" title="Search the wiki [Alt+Shift+f]" accesskey="f" tabindex="1" />
<span id="searchbox-controls">
<button id="search-go" class="search-button" type="submit" title="Search" tabindex="2">
<i class="search-icon" aria-hidden="true"></i>
<span class="sr-label">Search the wiki</span>
</button>
</span>
</form>
</div>
<div class="reset"></div>
</div>
</div>
<div id="content" role="main">'''.strip()
html_footer = '''
</div> <!-- end content -->
<div class="centerbox"><div id="lastmod">%(generation_note)s</div></div>
</div> <!-- end main -->
<div id="footer-sep"></div>
<div id="footer"><div id="footer-content"><div>
<a href="https://wiki.wesnoth.org/StartingPoints">Site Map</a> • <a href="http://status.wesnoth.org/">Site Status</a><br />
Copyright © 2003–2017 by <a rel="author" href="https://wiki.wesnoth.org/Project">The Battle for Wesnoth Project</a>.<br />
Site design Copyright © 2017 by Ignacio R. Morelle.
</div></div></div>
</body></html>
'''.strip()
html_clear_floats = '<div class="reset"></div>'
all_written_html_files = []
error_only_once = {}
def error_message(message):
if message in error_only_once: return
error_only_once[message] = 1
write_error(message)
helpers.error_message = error_message
def reset_errors():
error_only_once = {}
class MyFile:
"""
Python 2 is a bit weird with encodings, really should switch this to
Python 3.
"""
def __init__(self, filename, mode):
self.filename = filename
self.f = open(filename, mode + "b")
def write(self, x):
self.f.write(x.encode("utf8"))
def close(self):
self.f.close()
class Translation:
def __init__(self, localedir, langcode):
self.catalog = {}
self.localedir = localedir
self.langcode = langcode
class Dummy:
def gettext(self, x):
if not x: return ""
caret = x.find("^")
if caret < 0: return x
return x[caret + 1:]
self.dummy = Dummy()
def translate(self, string, textdomain):
if textdomain not in self.catalog:
try:
self.catalog[textdomain] = gettext.translation(
textdomain, self.localedir, [self.langcode])
self.catalog[textdomain].add_fallback(self.dummy)
except IOError:
self.catalog[textdomain] = self.dummy
except AttributeError:
self.catalog[textdomain] = self.dummy
except IndexError:
# not sure why, but this happens within the
# gettext.translation call sometimes
self.catalog[textdomain] = self.dummy
except ValueError:
self.catalog[textdomain] = self.dummy
r = self.catalog[textdomain].gettext(string)
return r
class GroupByRace:
def __init__(self, wesnoth, campaign):
self.wesnoth = wesnoth
self.campaign = campaign
def unitfilter(self, unit):
if not self.campaign: return True
return unit.campaigns and self.campaign == unit.campaigns[0]
def groups(self, unit):
return [T(unit.race, "plural_name")]
def group_name(self, group):
if not group: return "None"
return group
class GroupByNothing:
def __init__(self):
pass
def unitfilter(self, unit):
return True
def groups(self, unit):
return ["units"]
def group_name(self, group):
return "units"
class GroupByFaction:
def __init__(self, wesnoth, era):
self.wesnoth = wesnoth
self.era = era
def unitfilter(self, unit):
return self.era in unit.eras
def groups(self, unit):
return [x for x in unit.factions if x[0] == self.era]
def group_name(self, group):
era = self.wesnoth.era_lookup[group[0]]
if group[1]:
faction = era.faction_lookup[group[1]]
name = T(faction, "name")
name = name[name.rfind("=") + 1:]
else:
name = "factionless"
return name
global_htmlout = None
def T(tag, att):
if not tag: return "none"
return tag.get_text_val(att, translation = global_htmlout.translate)
class HTMLOutput:
def __init__(self, isocode, output, addon, campaign, is_era, wesnoth, verbose = False):
global global_htmlout
self.output = output
self.addon = addon
self.campaign = campaign
self.is_era = is_era
self.verbose = verbose
self.target = "index.html"
self.wesnoth = wesnoth
self.forest = None
self.translation = Translation(options.transdir, isocode)
self.isocode = isocode
global_htmlout = self
def translate(self, string, domain):
return self.translation.translate(string, domain)
def analyze_units(self, grouper, add_parents):
"""
This takes all units belonging to a campaign, then groups them either
by race or faction, and creates an advancements tree out of it.
"""
# Build an advancement tree forest of all units.
forest = self.forest = helpers.UnitForest()
units_added = {}
for uid, u in list(self.wesnoth.unit_lookup.items()):
if u.hidden: continue
if grouper.unitfilter(u):
forest.add_node(helpers.UnitNode(u))
units_added[uid] = u
#print(" %d/%d units" % (len(units_added), len(self.wesnoth.unit_lookup)))
# Always add any child units, even if they have been filtered out..
while units_added:
new_units_added = {}
for uid, u in list(units_added.items()):
for auid in u.advance:
if not auid in forest.lookup:
try:
au = self.wesnoth.unit_lookup[auid]
except KeyError:
error_message(
"Warning: Unit %s not found as advancement of %s\n" %
(auid, repr(uid)))
continue
forest.add_node(helpers.UnitNode(au))
new_units_added[auid] = au
units_added = new_units_added
if add_parents:
# Also add parent units
added = True
while added:
added = False
for uid, u in list(self.wesnoth.unit_lookup.items()):
if uid in forest.lookup: continue
for auid in u.advance:
if auid in forest.lookup:
forest.add_node(helpers.UnitNode(u))
added = True
break
forest.update()
# Partition trees by race/faction of first unit.
groups = {}
breadth = 0
for tree in list(forest.trees.values()):
u = tree.unit
ugroups = grouper.groups(u)
for group in ugroups:
groups[group] = groups.get(group, []) + [tree]
breadth += tree.breadth
thelist = list(groups.keys())
thelist.sort(key = lambda x: grouper.group_name(x))
rows_count = breadth + len(thelist)
# Create empty grid.
rows = []
for j in range(rows_count):
column = []
for i in range(6):
column.append((1, 1, None))
rows.append(column)
# Sort advancement trees by name of first unit and place into the grid.
def by_name(t):
x = T(t.unit, "name")
if x is None: return ""
return x
def grid_place(nodes, x):
nodes.sort(key = by_name)
for node in nodes:
level = node.unit.level
if level < 0: level = 0
if level > 5: level = 5
rows[x][level] = (1, node.breadth, node)
for i in range(1, node.breadth):
rows[x + i][level] = (0, 0, node)
grid_place(node.children, x)
x += node.breadth
return x
x = 0
for group in thelist:
node = helpers.GroupNode(group)
node.name = grouper.group_name(group)
rows[x][0] = (6, 1, node)
for i in range(1, 6):
rows[x][i] = (0, 0, None)
nodes = groups[group]
x += 1
x = grid_place(nodes, x)
self.unitgrid = rows
return len(forest.lookup)
def write_navbar(self, report_type):
def write(x): self.output.write(x)
all_written_html_files.append((self.isocode, self.output.filename))
languages = self.wesnoth.languages_found
langlist = list(languages.keys())
langlist.sort()
write(top_bar % {"path" : "../../"})
write("""
<div class="navbar">
""")
write('<ul class="navbar" role="menu">')
def abbrev(name):
abbrev = name[0]
word_seperators = [" ", "_", "+", "(", ")"]
for i in range(1, len(name)):
if name[i] in ["+", "(", ")"] or name[i - 1] in word_seperators and name[i] not in word_seperators:
abbrev += name[i]
return abbrev
def add_menu(id, name, class2=""):
write("""<li class="popuptrigger" role="menuitem" aria-haspopup="true"
onclick="toggle_menu(this, '""" + id + """', 2)"
onmouseover="toggle_menu(this, '""" + id + """', 1)"
onmouseout="toggle_menu(this, '""" + id + """', 0)">""")
write('<a class="' + class2 + '">' + name + "</a>")
write('<ul class="popupmenu" id="' + id + '" role="menu" aria-label="' + name + '">')
write('<li>' + name + '</li>')
# FIXME: This is legacy code needed for the Language menu, since it's
# a table and we can't make it otherwise since CSS column
# support is still hit-or-miss for some browsers still in use.
def add_menu2(id, name, class2=""):
write("""<li class="popuptrigger" role="menuitem" aria-haspopup="true"
onclick="toggle_menu(this, '""" + id + """', 2)"
onmouseover="toggle_menu(this, '""" + id + """', 1)"
onmouseout="toggle_menu(this, '""" + id + """', 0)">""")
write('<a class="' + class2 + '">' + name + "</a>")
write('<div class="popupmenu" id="' + id + '" role="menu" aria-label="' + name + '">')
write('<div>' + name + '</div>')
def end_menu():
write('</ul></li>\n')
# We may not have all the required info yet so defer writing the
# campaigns/eras navigation.
# Campaigns
x = self.translate("addon_type^Campaign", "wesnoth")
add_menu("campaigns_menu", x)
write("PLACE CAMPAIGNS HERE\n")
end_menu()
# Eras
x = self.translate("Era", "wesnoth")
add_menu("eras_menu", x)
write("PLACE ERAS HERE\n")
end_menu()
# Races / Factions
target = self.target
if self.campaign == "units":
target = "mainline.html"
if not self.is_era:
x = self.translate("Race", "wesnoth-lib")
add_menu("races_menu", x)
write('<li><a href="mainline.html" role="menuitem">%s</a></li>\n' % (
self.translate("all", "wesnoth-editor")))
r = {}, {}
for u in list(self.wesnoth.unit_lookup.values()):
race = u.race
racename = T(race, "plural_name")
m = 1
if u:
m = 0
rname = race.get_text_val("id") if race else "none"
if not rname:
rname = "none"
r[m][racename] = rname
racenames = sorted(r[0].items())
if list(r[1].items()):
racenames += [("-", "-")] + sorted(r[1].items())
for racename, rid in racenames:
if racename == "-":
write('<li>%s</li>' % html_entity_horizontal_bar)
else:
write('<li><a href="%s#%s" role="menuitem">%s</a></li>' % (
target, racename, racename))
end_menu()
else:
x = self.translate("Factions", "wesnoth-help")
add_menu("races_menu", x)
for row in self.unitgrid:
for column in range(6):
hspan, vspan, un = row[column]
if not un: continue
if isinstance(un, helpers.GroupNode):
html = "../%s/%s.html" % (
self.isocode, self.campaign)
write('<li><a href="%s#%s" role="menuitem">%s</a></li>' % (
html, un.name, un.name))
end_menu()
# Add entries for the races also to the navbar itself.
if not self.is_era:
class Entry: pass
races = {}
for uid, u in list(self.wesnoth.unit_lookup.items()):
if self.campaign != "units":
if self.campaign not in u.campaigns: continue
if u.race:
racename = T(u.race, "plural_name")
else:
racename = "none"
runits = races.get(racename, [])
runits.append(uid)
races[racename] = runits
racelist = sorted(races.keys())
got_menu = False
menuid = 0
for r in racelist:
if not r:
continue
if got_menu:
end_menu()
add_menu("units_menu" + str(menuid), r, "unitmenu")
menuid += 1
got_menu = True
c = self.campaign
if c == "units": c = "mainline"
write('<li><a href="%s#%s" role="menuitem">%s</a></li>' % (
target, r, r))
for uid in races[r]:
un = self.wesnoth.unit_lookup[uid]
if un.hidden: continue
if "mainline" in un.campaigns: addon = "mainline"
else: addon = self.addon
link = "../../%s/%s/%s.html" % (addon, self.isocode, uid)
name = self.wesnoth.get_unit_value(un,
"name", translation=self.translation.translate)
if not name:
error_message("Warning: Unit uid=" + uid + " has no name.\n")
name = uid
write('<li><a href="' + link + '" role="menuitem">' + name + '</a></li>')
if got_menu:
end_menu()
# Languages
x = self.translate("Language", "wesnoth")
add_menu2("languages_menu", x)
col = 0
maxcol = len(langlist) - 1
write("<table>")
write("<tr>")
for lang in langlist:
col += 1
write("<td>")
labb = lang
#underscore = labb.find("_")
#if underscore > 0: labb = labb[:underscore]
if self.addon == "mainline":
write('<a title="%s" href="../%s/%s" role="menuitem">%s</a>\n' % (
languages[lang], lang, self.target,
labb))
else:
write('<a title="%s" href="../%s/%s">%s</a>\n' % (
languages[lang], lang, "mainline.html",
labb))
write("</td>")
if col % 5 == 0:
if col < maxcol: write("</tr><tr>")
write("</tr>")
write("</table>")
write("</div></li>\n")
write('<li class="overviewlink"><a href="../../overview.html">Build Report</a></li>')
write("</ul>\n")
write("</div>\n")
def pic(self, u, x, recursion = 0):
if recursion >= 4:
error_message(
"Warning: Cannot find image for unit %s(%s).\n" % (
u.get_text_val("id"), x.name.decode("utf8")))
return None, None
image = self.wesnoth.get_unit_value(x, "image")
portrait = x.get_all(tag="portrait")
if not portrait:
bu = self.wesnoth.get_base_unit(u)
if bu:
portrait = bu.get_all(tag="portrait")
if portrait:
portrait = portrait[0].get_text_val("image")
if not image:
if x.name == b"female":
baseunit = self.wesnoth.get_base_unit(u)
if baseunit:
female = baseunit.get_all(tag="female")
return self.pic(u, female[0], recursion = recursion + 1)
else:
return self.pic(u, u, recursion = recursion + 1)
error_message(
"Warning: Missing image for unit %s(%s).\n" % (
u.get_text_val("id"), x.name.decode("utf8")))
return None, None
icpic = image_collector.add_image_check(self.addon, image)
if not icpic.ipath:
error_message("Warning: No picture %s for unit %s.\n" %
(image, u.get_text_val("id")))
picname = icpic.id_name
image = os.path.join(pics_location, picname)
if portrait:
picname = image_collector.add_image(self.addon, portrait,
no_tc=True)
portrait = os.path.join(pics_location, picname)
return image, portrait
def get_abilities(self, u):
anames = []
already = {}
for abilities in u.get_all(tag="abilities"):
try: c = abilities.get_all()
except AttributeError: c = []
for ability in c:
try:
id = ability.get_text_val("id")
except AttributeError as e:
error_message("Error: Ignoring ability " + ability.debug())
continue
if id in already: continue
already[id] = True
name = T(ability, "name")
if not name: name = id
if not name: name = ability.name.decode("utf8")
anames.append(name)
return anames
def get_recursive_attacks(self, this_unit):
def copy_attributes(copy_from, copy_to):
for c in copy_from.data:
if isinstance(c, wmlparser3.AttributeNode):
copy_to.data.append(c)
# Use attacks of base_units as base, if we have one.
base_unit = self.wesnoth.get_base_unit(this_unit)
attacks = []
if base_unit:
attacks = copy.deepcopy(self.get_recursive_attacks(base_unit))
base_attacks_count = len(attacks)
for i, attack in enumerate(this_unit.get_all(tag="attack")):
# Attack merging is order based.
if i < base_attacks_count:
copy_attributes(attack, attacks[i])
else:
attacks.append(attack)
return attacks
def write_units(self):
def write(x): self.output.write(x)
def _(x, c="wesnoth"): return self.translate(x, c)
rows = self.unitgrid
write("<table class=\"units\">\n")
write("<colgroup>")
for i in range(6):
write("<col class=\"col%d\" />" % i)
write("</colgroup>")
pic = image_collector.add_image("general",
"../../../images/misc/leader-crown.png", no_tc=True)
crownimage = os.path.join(pics_location, pic)
ms = None
for row in range(len(rows)):
write("<tr>\n")
for column in range(6):
hspan, vspan, un = rows[row][column]
if vspan:
attributes = ""
if hspan == 1 and vspan == 1:
pass
elif hspan == 1:
attributes += " rowspan=\"%d\"" % vspan
elif vspan == 1:
attributes += " colspan=\"%d\"" % hspan
if un and isinstance(un, helpers.GroupNode):
# Find the current multiplayer side so we can show the
# little crowns..
ms = None
if self.is_era:
try:
eid, fid = un.data
era = self.wesnoth.era_lookup[eid]
if fid:
ms = era.faction_lookup[fid]
except TypeError:
pass
racename = un.name
# TODO: we need to use a unique race id instead of a potentially duplicate
# name for the header id and link target!
attributes += " id=\"%s\" class=\"raceheader\"" % racename
write("<th" + attributes + ">")
write("<a href=\"#%s\">%s</a>" % (racename, racename))
write("</th>\n")
elif un:
u = un.unit
attributes += " class=\"unitcell\""
write("<td%s>" % attributes)
uid = u.get_text_val("id")
def uval(name):
return self.wesnoth.get_unit_value(u, name,
translation=self.translation.translate)
name = uval("name")
cost = uval("cost")
hp = uval("hitpoints")
mp = uval("movement")
xp = uval("experience")
level = uval("level")
crown = ""
if ms:
if un.id in ms.units:
crown = " ♟"
if un.id in ms.is_leader:
crown = " ♚"
uaddon = "mainline"
if "mainline" not in u.campaigns: uaddon = self.addon
link = "../../%s/%s/%s.html" % (uaddon, self.isocode, uid)
write("<div class=\"l\">L%s%s</div>" % (level, crown))
write("<a href=\"%s\" title=\"Id: %s\">%s</a><br/>" % (link, uid, name))
write('<div class="pic">')
image, portrait = self.pic(u, u)
write('<a href=\"%s\">' % link)
if crown == " ♚":
write('<div class="spritebg" style="background-image:url(\'%s\')">' % image)
write('<img src="%s" alt="(image)" />' % crownimage)
write("</div>")
else:
write('<img src="%s" alt="(image)" />' % image)
write('</a>\n</div>\n')
write("<div class=\"attributes\">")
attributes = (
(_("Cost: ", "wesnoth-help"), cost),
(_("HP: "), hp),
(_("XP: "), xp),
(_("MP: "), mp),
)
for attr_label, attr_value in attributes:
write('<span class="attribute-label">%s</span> <span class="attribute-value">%s</span><br />' % (attr_label.strip(), attr_value))
# Write info about abilities.
anames = self.get_abilities(u)
if anames:
write("\n<div class=\"abilities\">")
write(", ".join(anames))
write("</div>")
# Write info about attacks.
write("\n<div class=\"attacks\">")
attacks = self.get_recursive_attacks(u)
for attack in attacks:
n = T(attack, "number")
x = T(attack, "damage")
x = "%s - %s" % (x, n)
write("%s " % x)
r = T(attack, "range")
t = T(attack, "type")
write("%s (%s)" % (_(r), _(t)))
s = []
specials = attack.get_all(tag="specials")
if specials:
for special in specials[0].get_all(tag=""):
sname = T(special, "name")
if sname:
s.append(sname)
s = ", ".join(s)
if s: write(" (%s)" % s)
write("<br />")
write("</div>")
write("</div>")
write("</td>\n")
else:
write("<td class=\"empty\"></td>")
write("</tr>\n")
write("</table>\n")
def write_units_tree(self, grouper, title, add_parents):
self.output.write(html_header % {"path": "../../",
"title": title, "classes": "wmlunits-tree"})
n = self.analyze_units(grouper, add_parents)
self.write_navbar("units_tree")
self.output.write("<div class=\"main\">")
self.output.write("<h1>%s</h1>" % title)
self.write_units()
self.output.write(html_clear_floats)
self.output.write("</div>")
self.output.write(html_footer % {
"generation_note": "Last updated on " + time.ctime() + "."})
return n
def write_unit_report(self, output, unit):
def write(x): self.output.write(x)
def _(x, c="wesnoth"): return self.translate(x, c)
def find_attr(what, key):
if unit.movetype:
mtx = unit.movetype.get_all(tag=what)
mty = None
if mtx:
mty = mtx[0].get_text_val(key)
x = unit.get_all(tag=what)
y = None
if x:
y = x[0].get_text_val(key,
translation=self.translation.translate)
if y:
return True, y
if unit.movetype and mty != None:
return False, mty
return False, "-"
def uval(name):
return self.wesnoth.get_unit_value(unit, name,
translation=self.translation.translate)
# Write unit name, picture and description.
uid = unit.get_text_val("id")
uname = uval("name")
display_name = uname
self.output = output
write(html_header % {"path": "../../",
"title": display_name, "classes": "wmlunits-unit"})
self.write_navbar("unit_report")
self.output.write("<div class=\"main\">")
female = unit.get_all(tag="female")
if female:
fname = T(female[0], "name")
if fname and fname != uname:
display_name += "<br/>" + fname
write('<div class="unit-columns">')
write('<div class="unit-column-left">')
write("<h1>%s</h1>\n" % display_name)
write('<div class="pic">')
if female:
mimage, portrait = self.pic(unit, unit)
fimage, fportrait = self.pic(unit, female[0])
if not fimage: fimage = mimage
if not fportrait: fportrait = portrait
write('<img src="%s" alt="(image)" />\n' % mimage)
write('<img src="%s" alt="(image)" />\n' % fimage)
image = mimage
else:
image, portrait = self.pic(unit, unit)
write('<img src="%s" alt="(image)" />\n' % image)
write('</div>\n')
description = uval("description")
# TODO: what is unit_description?
if not description: description = uval("unit_description")
if not description: description = "-"
write("<p>%s</p>\n" % re.sub("\n", "\n<br />", description))
# Base info.
hp = uval("hitpoints")
mp = uval("movement")
xp = uval("experience")
vision = uval("vision")
jamming = uval("jamming")
level = uval("level")
alignment = uval("alignment")
write("<h2>Information</h2>\n")
write("<table class=\"unitinfo\">\n")
write("<tr>\n")
write("<th>%s" % _("Advances from: ", "wesnoth-help"))
write("</th><td>\n")
for pid in self.forest.get_parents(uid):
punit = self.wesnoth.unit_lookup[pid]
if "mainline" in unit.campaigns and "mainline" not in punit.campaigns:
continue
if "mainline" in unit.campaigns: addon = "mainline"
else: addon = self.addon
link = "../../%s/%s/%s.html" % (addon, self.isocode, pid)
name = self.wesnoth.get_unit_value(punit, "name",
translation=self.translation.translate)
write("\n<a href=\"%s\">%s</a>" % (link, name))
write("</td>\n")
write("</tr><tr>\n")
write("<th>%s" % _("Advances to: ", "wesnoth-help"))
write("</th><td>\n")
for cid in self.forest.get_children(uid):
try:
cunit = self.wesnoth.unit_lookup[cid]
if "mainline" in cunit.campaigns: addon = "mainline"
else: addon = self.addon
link = "../../%s/%s/%s.html" % (addon, self.isocode, cid)
if "mainline" in unit.campaigns and "mainline" not in cunit.campaigns:
continue
name = self.wesnoth.get_unit_value(cunit, "name",
translation=self.translation.translate)
except KeyError:
error_message("Warning: Unit %s not found.\n" % cid)
name = cid
if "mainline" in unit.campaigns: continue
link = self.target
write("\n<a href=\"%s\">%s</a>" % (link, name))
write("</td>\n")
write("</tr>\n")
for val, text in [
("cost", _("Cost: ", "wesnoth-help")),
("hitpoints", _("HP: ")),
("movement", _("Movement", "wesnoth-help") + ": "),
("vision", _("Vision", "wesnoth-help") + ": "),
("jamming", _("Jamming", "wesnoth-help") + ":"),
("experience", _("XP: ")),
("level", _("Level") + ": "),
("alignment", _("Alignment: ")),
("id", "Id: ")]:
x = uval(val)
if not x and val in ("jamming", "vision"): continue
if val == "alignment": x = _(x)
write("<tr>\n")
write("<th>%s</th>" % text)
write("<td class=\"val\">%s</td>" % x)
write("</tr>\n")
# Write info about abilities.
anames = self.get_abilities(unit)
write("<tr>\n")
write("<th>%s</th>" % _("Abilities: ", "wesnoth-help"))
write("<td class=\"val\">" + (", ".join(anames)) + "</td>")
write("</tr>\n")
write("</table>\n")
# Write info about attacks.
write("<h2>" + _("unit help^Attacks", "wesnoth-help") + " <small>(damage - count)</small></h2> \n")
write("<table class=\"unitinfo attacks\">\n")
write('<colgroup><col class="col0" /><col class="col1" /><col class="col2" /><col class="col3" /><col class="col4" /></colgroup>')
attacks = self.get_recursive_attacks(unit)
for attack in attacks:
write("<tr>")
aid = attack.get_text_val("name")
aname = T(attack, "description")
icon = attack.get_text_val("icon")
if not icon:
icon = "attacks/%s.png" % aid
image_add = image_collector.add_image_check(self.addon,
icon, no_tc = True)
if not image_add.ipath:
error_message("Error: No attack icon '%s' found for '%s'.\n" % (
icon, uid))
icon = os.path.join(pics_location, "unit$elves-wood$shaman.png")
else:
icon = os.path.join(pics_location, image_add.id_name)
write("<td><img src=\"%s\" alt=\"(image)\"/></td>" % icon)
write("<td><b>%s</b>" % aname)
r = T(attack, "range")
write("<br/>%s</td>" % _(r))
n = attack.get_text_val("number")
x = attack.get_text_val("damage")
x = "%s - %s" % (x, n)
write("<td><i>%s</i>" % x)
t = T(attack, "type")
write("<br/>%s</td>" % _(t))
s = []
specials = attack.get_all(tag="specials")
if specials:
for special in specials[0].get_all(tag=""):
sname = T(special, "name")
if sname:
s.append(sname)
else:
error_message(
"Warning: Weapon special %s has no name for %s.\n" % (
special.name.decode("utf8"), uid))
s = "<br/>".join(s)
write("<td>%s</td>" % s)
write("</tr>")
write("</table>\n")
# Write info about resistances.
resistances = [
("blade", "attacks/sword-human.png"),
("pierce", "attacks/spear.png"),
("impact", "attacks/club.png"),
("fire", "attacks/fireball.png"),
("cold", "attacks/iceball.png"),
("arcane", "attacks/faerie-fire.png")]
write("<h2>%s</h2>\n" % _("Resistances: ").strip(" :"))
write("<table class=\"unitinfo resistances\">\n")
write('<colgroup><col class="col0" /><col class="col1" /><col class="col2" /><col class="col3" /><col class="col4" /><col class="col5" /><col class="col6" /><col class="col7" /></colgroup>')
write("<tr>\n")
write("</tr>\n")
row = 0
for rid, ricon in resistances:
special, r = find_attr("resistance", rid)
if r == "-": r = 100