-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypertext.py
658 lines (527 loc) · 18.9 KB
/
hypertext.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
#!/usr/bin/python3
"""
html generation
"""
import enum
import statistics
import string
from typing import Any, Dict, List, Optional, Tuple, Type
import locations
from util import collection, grammar, log, taxonomy, translator
from util.common import (
flatten,
is_date,
pretty_date,
sanitize_link,
strip_date,
titlecase,
)
from util.image import Image, categorize, split, uncategorize
from util.metrics import metrics
from util.static import search_data_path, search_js, stylesheet, video_js
Where = enum.Enum('Where', 'Gallery Taxonomy Sites Timeline Detective')
Side = enum.Enum('Side', 'Left Right')
scripts = (
"""
<!-- fancybox is excellent, this project is not commercial -->
<link rel="stylesheet" href="/jquery.fancybox.min.css"/>
<script src="/jquery-3.6.0.min.js" defer></script>
<script src="/jquery.fancybox.min.js" defer></script>
<script>
function flip(elem) {
const label = 'is-flipped';
if (elem.classList.contains(label)) {
elem.classList.remove(label);
} else {
elem.classList.add(label);
setTimeout(() => {
if (elem.classList.contains(label)) {
elem.classList.remove(label);
}
}, 7000);
}
}
</script>
"""
+ f"""
<script src="/{video_js.path}" defer></script>
"""
)
blurb = 'Explore high quality scuba diving pictures'
def title(lineage: List[str], where: Where, scientific: Dict[str, Any]) -> Tuple[str, str]:
"""html head and target path"""
if not lineage:
impl: Type[Title] = TopTitle
else:
assert lineage
impl = {
Where.Gallery: GalleryTitle,
Where.Taxonomy: TaxonomyTitle,
Where.Sites: SitesTitle,
}[where]
html, path = impl(where, lineage, scientific).run()
return html, path + '.html'
def head(display: str, path: str, where: Where) -> str:
"""top of the document"""
if display.endswith('Gallery'):
desc = (
f'{blurb} organized into a tree structure by '
"subject's common names. Such as anemone, fish, "
'nudibranch, octopus, sponge.'
)
elif display.endswith('Taxonomy'):
desc = (
f'{blurb} organized into a tree structure by '
"subject's scientific classification. Such as Arthropoda, "
'Cnidaria, Mollusca.'
)
elif display.endswith('Sites'):
sites = locations.site_list()
desc = f'{blurb} from {sites} organized into a tree structure by dive site.'
elif display.endswith('Timeline'):
desc = f'{blurb} organized into a timeline by location'
else:
desc = description(display, where)
assert '.html' not in path, path
path = path.replace('index', '')
canonical = f'https://diving.anardil.net/{path}'
return f"""
<!DOCTYPE html>
<html lang="en">
<head>
<title>{display}</title>
<link rel="canonical" href="{canonical}"/>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name=description content="{desc}">
<link rel="stylesheet" href="/{stylesheet.path}"/>
</head>
<body>
<div class="wrapper">
<div class="title">
"""
def description(title: str, where: Where) -> str:
if where == Where.Sites:
words = locations.where_to_words(title)
last = words[-1]
suffix = ''
if is_date(last):
suffix = f' on {pretty_date(last)}'
words = words[:-1]
else:
suffix = ', organized by dive site and date'
if len(words) > 1:
# Swap the context and site
body = ' '.join(words[1:]) + f', {words[0]}'
else:
body = words[0]
return f'{blurb} from {body}{suffix}.'
if where == Where.Gallery:
more = len(title.split(' ')) > 1
related = ' and related organisms' if more else ''
title = grammar.plural(title).replace('Various ', '')
return f'{blurb} of {title}{related}.'
if where == Where.Taxonomy:
words = title.split(' ')
if len(words) > 1 and words[-2].istitle() and words[-1].islower():
# ... Genus species
name = ' '.join(words[-2:])
return f'{blurb} of {name} and related organisms.'
return f'{blurb} of members of {title}.'
assert False, (where, title)
def lineage_to_link(lineage: List[str], side: Side, key: Optional[str] = None) -> str:
"""get a link to this page"""
if not lineage:
assert key
name = key
else:
name = ' '.join(lineage)
if key and side == Side.Left:
name = f'{key} {name}'
if key and side == Side.Right:
name = f'{name} {key}'
return sanitize_link(name)
def image_to_name_html(image: Image, where: Where) -> str:
"""create the html gallery link, entry, or nothing for this image"""
if where in (Where.Gallery, Where.Taxonomy):
return ''
name_url = _image_to_gallery_link(image)
if name_url:
name_html = f'<a class="top elem gallery" href="{name_url}">{image.name}</a>'
else:
metrics.counter('images without gallery link')
name_html = f'<p class="top elem nolink">{image.name}</p>'
return name_html
def image_to_site_html(image: Image, where: Where) -> str:
"""create the html site link, entry, or nothing for this image"""
if where == Where.Sites:
return ''
site_url = _image_to_sites_link(image)
return f'<a class="top elem sites" href="{site_url}">{image.site()}</a>'
def html_direct_image(image: Image, where: Where, lazy: bool) -> str:
if image.is_image:
return _direct_image_html(image, where, lazy)
else:
return _direct_video_html(image, where)
# PRIVATE
def _direct_image_html(image: Image, where: Where, lazy: bool) -> str:
assert image.is_image
metrics.counter('html direct images')
name_html = image_to_name_html(image, where)
site_html = image_to_site_html(image, where)
lazy_load = 'loading="lazy"' if lazy else ''
location = image.location()
fullsize = image.fullsize()
thumbnail = image.thumbnail()
caption = f'{image.name}, {location}'
depth = image.approximate_depth()
if depth:
low, high = depth
if high - low < 10:
average = int(statistics.mean(depth))
extra = f"~{average}'"
else:
extra = f"{low}' ~ {high}'"
caption += f', {extra}'
return f"""
<div class="card" onclick="flip(this);">
<div class="card_face card_face-front zoom-wrapper">
<img class="zoom" height=225 width=300 {lazy_load} alt="{image.name}" src="{thumbnail}">
</div>
<div class="card_face card_face-back">
{name_html}
{site_html}
<a class="top elem timeline" data-fancybox="gallery" data-caption="{caption}" href="{fullsize}">
<h3>Zoom</h3>
</a>
<h3 class="top elem">Close</h3>
</div>
</div>
"""
def _direct_video_html(image: Image, where: Where) -> str:
assert image.is_video
metrics.counter('html direct videos')
name_html = image_to_name_html(image, where)
site_html = image_to_site_html(image, where)
fullsize = image.fullsize()
thumbnail = image.thumbnail()
allowed = string.ascii_letters + string.digits
unique = 'video_' + ''.join(c for c in image.identifier() if c in allowed)
# disableRemotePlayback is to stop Android from suggesting a cast
# playsinline is get iOS to play the video at all
# Safari always chooses the first element regardless of support with autoplay
return f"""
<div class="card" onclick="flip(this);">
<div class="card_face card_face-front">
<video
class="clip"
height=225 width=300
disableRemotePlayback preload playsinline muted loop
>
<source src="{thumbnail}" type="video/mp4">
Your browser does not support the HTML5 video tag.
</video>
</div>
<div class="card_face card_face-back">
{name_html}
{site_html}
<a class="top elem timeline" data-fancybox href="#{unique}">
Full Video
</a>
<p class="top elem">Close</p>
<video
controls muted preload="none"
id="{unique}" style="display:none;"
>
<source src="{fullsize}" type="video/mp4">
Your browser does not support the HTML5 video tag.
</video>
</div>
</div>
"""
def _image_to_gallery_link(image: Image) -> Optional[str]:
"""get the /gallery link
there could be mulitple subjects in this image, just take the first for now
"""
first = next(collection.expand_names([image]))
name = first.simplified()
if name not in collection.all_valid_names():
return None
page = sanitize_link(first.normalized())
return f'/gallery/{page}'
def _image_to_sites_link(image: Image) -> str:
"""get the /sites/ link"""
when, where = image.location().split(' ', 1)
return locations.sites_link(when, where)
class Title:
"""Produce the HTML and title for a particular group of pages"""
def __init__(self, where: Where, lineage: List[str], scientific: Dict[str, Any]) -> None:
self.where = where
self.lineage = lineage
self.scientific = scientific
def run(self) -> Tuple[str, str]:
"""Produce html, path"""
raise NotImplementedError
class GalleryTitle(Title):
"""html head and title section for gallery pages"""
def run(self) -> Tuple[str, str]:
side = Side.Left
# check for scientific name for gallery
slink = sname = taxonomy.gallery_scientific(self.lineage, self.scientific)
sname = taxonomy.simplify(sname)
if slink.endswith(' sp.'):
slink = slink.replace(' sp.', '')
scientific_common_name = slink.lower().endswith(self.lineage[0].lower())
if scientific_common_name:
self.lineage = [slink[-len(self.lineage[0]) :]] + [
titlecase(e) for e in self.lineage[1:]
]
else:
self.lineage = [titlecase(e) for e in self.lineage]
_title = ' '.join(self.lineage)
display = uncategorize(_title)
slink = sanitize_link(slink)
path = sanitize_link(f'gallery/{_title.lower()}')
html = head(display, path, Where.Gallery)
# create the buttons for each part of our name lineage
for i, name in enumerate(self.lineage):
if i == 0 and scientific_common_name:
name = f'<em>{name}</em>'
partial = self.lineage[i:]
_link = f'/gallery/{lineage_to_link(partial, side)}'.lower()
html += f"""
<a href="{_link}">
<h1 class="top">{name}</h1>
</a>
"""
html += """
<div class="top buffer"></div>
<a href="/gallery/">
<h1 class="top switch gallery">Gallery</h1>
</a>
"""
if slink:
html += f"""
<a href="/taxonomy/{slink}" class="scientific crosslink">{sname}</a>
</div>
"""
else:
metrics.counter('titles in gallery without taxonomy link')
html += f"""
<p class="scientific">{sname}</p>
</div>
"""
return html, path
class TaxonomyTitle(Title):
"""html head and title section for taxonomy pages"""
def translate_lineage(self) -> str:
lineage: List[str] = flatten(word.split(' ') for word in self.lineage)[::-1]
translated = [translator.translate(name) for name in lineage if name != 'sp.']
if lineage[0].islower():
size = 2
last = len(self.lineage[-1].split(' '))
size = max(size, last)
translated = translated[:size]
names = []
for translation in translated:
if translation in names:
continue
names.append(translation)
assert names, lineage
return ' '.join(names)
def run(self) -> Tuple[str, str]:
_title = ' '.join(self.lineage)
side = Side.Right
path = sanitize_link(f'taxonomy/{_title}')
html = head(' '.join(self.lineage[-2:]), path, Where.Taxonomy)
html += """
<a href="/taxonomy/">
<h1 class="top switch taxonomy">Taxonomy</h1>
</a>
<div class="top buffer"></div>
"""
# create the buttons for each part of our name lineage
for i, name in enumerate(self.lineage):
name = taxonomy.simplify(name)
partial = self.lineage[: i + 1]
link = f'/taxonomy/{lineage_to_link(partial, side)}'
html += f"""
<a href="{link}">
<h1 class="top">{name}</h1>
</a>
"""
# check for common name for taxonomy
name = ''
history = ' '.join(self.lineage).split(' ')
while history and not name:
name = self.scientific.get(' '.join(history), '')
history = history[:-1]
name = titlecase(name)
link = split(categorize(name.lower()))
link = sanitize_link(link)
english = self.translate_lineage()
if link:
html += f"""
<a href="/gallery/{link}" class="scientific crosslink">{name}</a>
<p class="scientific">{english}</p>
</div>
"""
else:
metrics.counter('titles in taxonomy without gallery link')
assert not name, name
html += f"""
<p class="scientific">{english}</p>
</div>
"""
return html, path
class SitesTitle(Title):
"""html head and title section for sites pages"""
def is_dive(self) -> bool:
*_, last = ' '.join(self.lineage).split(' ')
return is_date(last)
def get_date(self) -> str:
*_, last = ' '.join(self.lineage).split(' ')
assert is_date(last), last
return last
def get_hint(self) -> str:
last = self.lineage[-1]
if ' ' in last:
*parts, _ = last.split(' ')
return ' '.join(parts)
return self.lineage[-2]
def run(self) -> Tuple[str, str]:
_title = ' '.join(self.lineage)
path = sanitize_link(f'sites/{_title}')
html = head(_title, path, Where.Sites)
html += """
<a href="/sites/">
<h1 class="top switch sites">Sites</h1>
</a>
<div class="top buffer"></div>
"""
dive_info = None
name = ''
if self.is_dive():
date = self.get_date()
dive_info = log.search(date, self.get_hint())
name = pretty_date(date)
# create the buttons for each part of our name lineage
for i, _name in enumerate(self.lineage):
if is_date(_name):
continue
partial = self.lineage[: i + 1]
link = f'/sites/{lineage_to_link(partial, Side.Right)}'
html += f"""
<a href="{link}">
<h1 class="top">{strip_date(_name)}</h1>
</a>
"""
html += f"""\
<h3 class="tight">{name}</h3>
"""
if dive_info:
html += log.dive_info_html(dive_info)
html += """\
</div>
"""
return html, path
def switcher_button(where: Where, long: bool = False) -> str:
"""Get the switcher button for this site"""
_timeline = """
<a href="/timeline/">
<h1 class="top switch">{}</h1>
</a>
"""
_gallery = """
<a href="/gallery/">
<h1 class="top switch gallery">{}</h1>
</a>
"""
_detective = """
<a href="/detective/">
<h1 class="top switch detective">{}</h1>
</a>
"""
_sites = """
<a href="/sites/">
<h1 class="top switch sites">{}</h1>
</a>
"""
_taxonomy = """
<a href="/taxonomy/">
<h1 class="top switch taxonomy">{}</h1>
</a>
"""
return {
Where.Timeline: _timeline,
Where.Gallery: _gallery,
Where.Detective: _detective,
Where.Sites: _sites,
Where.Taxonomy: _taxonomy,
}[where].format(long_name(where) if long else short_name(where))
def long_name(where: Where) -> str:
"""Get the full name"""
return where.name
def short_name(where: Where) -> str:
"""Get the abbreviated switcher button for this site"""
return {
Where.Timeline: '📅',
Where.Gallery: '📸',
Where.Detective: '🔍', # '🕵️',
Where.Sites: '🌎',
Where.Taxonomy: '🔬',
}[where]
class TopTitle(Title):
"""
HTML head and title section for top level pages. The most interesting
part of this is the switcher, which allows us to move between the sites
"""
def sub_line(self) -> str:
if self.where == Where.Timeline:
return ''
return f"""
<div class="search">
<form class="search_random" action="javascript:;" onsubmit="randomPage()">
<button type="submit">Random</button>
</form>
<form class="search_text" autocomplete="off" action="javascript:;" onsubmit="searcher()">
<input type="text" id="search_bar" placeholder="" autofocus>
<button type="submit">Search</button>
</form>
</div>
<div id="search_results" class="search_results">
<script src="/{search_data_path}" defer></script>
<script src="/{search_js.path}" defer></script>
</div>
"""
def run(self) -> Tuple[str, str]:
_title = titlecase(self.where.name)
display = uncategorize(_title)
if self.where == Where.Gallery:
display = titlecase(display)
carousel = [
Where.Timeline,
Where.Gallery,
Where.Detective,
Where.Sites,
Where.Taxonomy,
]
start = carousel.index(self.where)
indicies = [start - 2, start - 1, start, start + 1, start + 2]
indicies = [i % len(carousel) for i in indicies]
parts = [
switcher_button(carousel[indicies[0]]),
switcher_button(carousel[indicies[1]]),
switcher_button(carousel[indicies[2]], long=True),
switcher_button(carousel[indicies[3]]),
switcher_button(carousel[indicies[4]]),
]
spacer = '<div class="top buffer"></div>\n'
path = sanitize_link(self.where.name.lower() + '/index')
html = head(display, path, self.where)
html += spacer.join(parts)
html += self.sub_line()
html += """
</div>
"""
return html, path