|
| 1 | +import pathlib |
| 2 | +from textwrap import dedent |
| 3 | + |
| 4 | +import yaml |
| 5 | +from truncatehtml import truncate |
| 6 | + |
| 7 | + |
| 8 | +def build_from_items(items, filename, title='Gallery', subtitle=None, menu_html='', max_descr_len=300): |
| 9 | + |
| 10 | + # Build the gallery file |
| 11 | + panels_body = [] |
| 12 | + for item in items: |
| 13 | + if not item.get('thumbnail'): |
| 14 | + item['thumbnail'] = '/_static/images/ebp-logo.png' |
| 15 | + thumbnail = item['thumbnail'] |
| 16 | + |
| 17 | + author_strs = set() |
| 18 | + institution_strs = set() |
| 19 | + for a in item['authors']: |
| 20 | + author_name = a.get('name', 'Anonymous') |
| 21 | + author_email = a.get('email', None) |
| 22 | + if author_email: |
| 23 | + _str = f'<a href="mailto:{author_email}">{author_name}</a>' |
| 24 | + else: |
| 25 | + _str = author_name |
| 26 | + author_strs.add(_str) |
| 27 | + |
| 28 | + institution_name = a.get('institution', None) |
| 29 | + if institution_name: |
| 30 | + institution_url = a.get('institution_url', None) |
| 31 | + if institution_url: |
| 32 | + _str = f'<a href="{institution_url}">{institution_name}</a>' |
| 33 | + else: |
| 34 | + _str = institution_name |
| 35 | + institution_strs.add(_str) |
| 36 | + |
| 37 | + authors_str = f"<strong>Author:</strong> {', '.join(author_strs)}" |
| 38 | + if institution_strs: |
| 39 | + institutions_str = f"<strong>Institution:</strong> {' '.join(institution_strs)}" |
| 40 | + else: |
| 41 | + institutions_str = '' |
| 42 | + |
| 43 | + ellipsis_str = '<a class="modal-btn"> ... more</a>' |
| 44 | + short_description = truncate(item['description'], max_descr_len, ellipsis=ellipsis_str) |
| 45 | + |
| 46 | + if ellipsis_str in short_description: |
| 47 | + modal_str = f""" |
| 48 | +<div class="modal"> |
| 49 | +<div class="content"> |
| 50 | +<img src="{thumbnail}" class="modal-img" /> |
| 51 | +<h3 class="display-3">{item["title"]}</h3> |
| 52 | +{authors_str} |
| 53 | +<br/> |
| 54 | +{institutions_str} |
| 55 | +<p class="my-2">{item['description']}</p> |
| 56 | +<p class="mt-3 mb-0"><a href="{item["url"]}" class="btn btn-outline-primary btn-block">Visit Website</a></p> |
| 57 | +</div> |
| 58 | +</div> |
| 59 | +""" |
| 60 | + else: |
| 61 | + modal_str = '' |
| 62 | + |
| 63 | + panels_body.append( |
| 64 | + f"""\ |
| 65 | +--- |
| 66 | +:column: |
| 67 | +
|
| 68 | +<div class="d-flex gallery-card"> |
| 69 | +<img src="{thumbnail}" class="gallery-thumbnail" /> |
| 70 | +<div class="container"> |
| 71 | +<a href="{item["url"]}" class="text-decoration-none"><h4 class="display-4 p-0">{item["title"]}</h4></a> |
| 72 | +<p class="card-subtitle">{authors_str}<br/>{institutions_str}</p> |
| 73 | +<p class="my-2">{short_description}</p> |
| 74 | +</div> |
| 75 | +</div> |
| 76 | +{modal_str} |
| 77 | +
|
| 78 | +""" |
| 79 | + ) |
| 80 | + |
| 81 | + panels_body = '\n'.join(panels_body) |
| 82 | + |
| 83 | + if subtitle: |
| 84 | + stitle = f'<span class="display-3">Displaying "{subtitle}" tags</span>' |
| 85 | + else: |
| 86 | + stitle = '' |
| 87 | + |
| 88 | + panels = f""" |
| 89 | +# {title} |
| 90 | +
|
| 91 | +{stitle} |
| 92 | +
|
| 93 | +{menu_html} |
| 94 | +
|
| 95 | +````{{panels}} |
| 96 | +:column: col-12 |
| 97 | +:card: +mb-4 w-100 |
| 98 | +:header: d-none |
| 99 | +:body: p-3 m-0 |
| 100 | +:footer: p-1 |
| 101 | +
|
| 102 | +{dedent(panels_body)} |
| 103 | +```` |
| 104 | +
|
| 105 | +<div class="modal-backdrop"></div> |
| 106 | +<script src="/_static/custom.js"></script> |
| 107 | +""" |
| 108 | + |
| 109 | + pathlib.Path(f'{filename}.md').write_text(panels) |
| 110 | + |
| 111 | + |
| 112 | +def main(app): |
| 113 | + |
| 114 | + with open('cookbook_gallery.yaml') as fid: |
| 115 | + all_items = yaml.safe_load(fid) |
| 116 | + |
| 117 | + title = 'Pythia Cookbooks Gallery' |
| 118 | + build_from_items(all_items, 'cookbook-gallery', title=title) |
| 119 | + |
| 120 | + |
| 121 | +def setup(app): |
| 122 | + app.connect('builder-inited', main) |
0 commit comments