Skip to content

Commit 02cf83a

Browse files
authored
External links gallery: add support for an arbitrary number of authors (#99)
* Fix UnboundLocalError: local variable 'authors_str' referenced before assignment error * Fix bug
1 parent 6dcd250 commit 02cf83a

File tree

1 file changed

+8
-23
lines changed

1 file changed

+8
-23
lines changed

content/_ext/yaml_gallery_generator.py

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import itertools
12
import pathlib
23
from textwrap import dedent
34

@@ -7,19 +8,13 @@
78
def _tag_in_item(item, tag_str=None):
89
if tag_str is None:
910
return True
10-
all_tags = []
11-
for k, e in item['tags'].items():
12-
all_tags.extend(e)
11+
all_tags = list(item['tags'].values())
1312
return tag_str in all_tags
1413

1514

1615
def _generate_sorted_tag_keys(all_items):
1716

18-
key_set = set()
19-
for item in all_items:
20-
for k, e in item['tags'].items():
21-
key_set.add(k)
22-
17+
key_set = set(itertools.chain(*[item['tags'].keys() for item in all_items]))
2318
return sorted(key_set)
2419

2520

@@ -77,33 +72,23 @@ def build_from_items(items, filename, display_name, menu_html):
7772
if not item.get('thumbnail'):
7873
item['thumbnail'] = '/_static/images/ebp-logo.png'
7974
thumbnail = item['thumbnail']
80-
81-
tag_set = set()
82-
for k, e in item['tags'].items():
83-
for t in e:
84-
tag_set.add(t)
85-
86-
tag_list = sorted(tag_set)
75+
tag_list = sorted((itertools.chain(*item['tags'].values())))
8776
tags = [
8877
f'{{link-badge}}`"/pages/links/{tag.replace(" ", "-")}.html",{tag},cls=badge-primary badge-pill text-light`'
8978
for tag in tag_list
9079
]
9180
tags = '\n'.join(tags)
9281

9382
authors = [a.get('name', 'anonymous') for a in item['authors']]
83+
authors_str = f"Created by: {', '.join(authors)}"
9484

95-
if len(authors) == 1:
96-
authors_str = f'Created by: {authors[0]}'
97-
elif len(authors) == 2:
98-
authors_str = f'Created by: {authors[0]} and {authors[1]}'
99-
100-
email = [a.get('email', None) for a in item['authors']][0]
85+
email = [a.get('email') for a in item['authors']][0]
10186
email_str = '' if email is None else f'Email: {email}'
10287

103-
affiliation = [a.get('affiliation', None) for a in item['authors']][0]
88+
affiliation = [a.get('affiliation') for a in item['authors']][0]
10489
affiliation_str = '' if affiliation is None else f'Affiliation: {affiliation}'
10590

106-
affiliation_url = [a.get('affiliation_url', None) for a in item['authors']][0]
91+
affiliation_url = [a.get('affiliation_url') for a in item['authors']][0]
10792
affiliation_url_str = '' if affiliation_url is None else f'{affiliation} Site: <{affiliation_url}>'
10893

10994
panels_body.append(

0 commit comments

Comments
 (0)