Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugins/news: Add support for multiple post authors #111

Merged
merged 2 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions pages/news.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ <h1>{% trans "News and Announcements" %}</h1>
<h2><a href="{% url post.path %}">{{ post.title }}</a></h2>
<p style="font-size: small;">
Posted on <time>{{ post.date|date:"Y-m-d" }}</time>
{% if post.author %}
{% if post.authors %}
by
{% for author in post.authors|slice:":-2" %}
<span class="author">{% if author.url %}<a href="{{ author.url }}">{{ author.name }}</a>{% else %}{{ author.name }}{% endif %}{% if author.github %} (<a href="https://github.com/{{ author.github }}">@{{ author.github }}</a>){% endif %}</span>,
{% endfor %}
{% for author in post.authors|slice:"-2:-1" %}
<span class="author">{% if author.url %}<a href="{{ author.url }}">{{ author.name }}</a>{% else %}{{ author.name }}{% endif %}{% if author.github %} (<a href="https://github.com/{{ author.github }}">@{{ author.github }}</a>){% endif %}</span> and
{% endfor %}
{% for author in post.authors|slice:"-1:" %}
<span class="author">
{% if post.author_url %}
<a href="{{ post.author_url }}">{{ post.author }}</a>
{% else %}
{{ post.author }}
{% endif %}
{% if post.author_github %}
(<a href="https://github.com/{{ post.author_github }}">@{{ post.author_github }}</a>)
{% endif %}
</span>
{% if author.url %}<a href="{{ author.url }}">{{ author.name }}</a>{% else %}{{ author.name }}{% endif %}{% if author.github %} (<a href="https://github.com/{{ author.github }}">@{{ author.github }}</a>){% endif %}</span>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why this is a single line?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because otherwise it will insert newlines into the HTML source, which creates a space before the comma.

{% endfor %}
{% endif %}
</p>

Expand Down
55 changes: 31 additions & 24 deletions plugins/news.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@
POSTS_PATH = "news/"
POSTS = []
AUTHOR_METADATA = {
'': {
'name': 'Mixxx Team',
'url': 'https://github.com/orgs/mixxxdj/people',
'email': 'core-team@mixxx.org',
},
'Mixxx': {
'name': 'Mixxx Team',
'url': 'https://github.com/orgs/mixxxdj/people',
"name": "Mixxx Team",
"url": "https://github.com/orgs/mixxxdj/people",
'email': 'core-team@mixxx.org',
},
'Be.': {
Expand Down Expand Up @@ -46,6 +41,7 @@
'email': 'amcrehan@gmail.com',
},
}
AUTHOR_METADATA[""] = AUTHOR_METADATA["Mixxx"]


def getNode(template, context, name="subject"):
Expand Down Expand Up @@ -79,8 +75,8 @@ def preBuild(site):

# Find a specific defined variable in the page context,
# and throw a warning if we're missing it.
def find(name, warn=True):
value = page.context().get(name, "")
def find(name, warn=True, fallback=""):
value = page.context().get(name, fallback)
if warn and not value:
logging.warning(
"Missing info '%s' for post %s" % (name, page.path)
Expand All @@ -90,21 +86,32 @@ def find(name, warn=True):
# Build a context for each post
postContext = {}
postContext["title"] = find("title")
postContext["author"] = find("author")
postContext["author_url"] = find("author_url", warn=False)
postContext["author_github"] = find("author_github", warn=False)
postContext["author_email"] = find("author_email", warn=False)
author_metadata = AUTHOR_METADATA.get(postContext["author"])
if author_metadata:
if "name" in author_metadata:
postContext["author"] = author_metadata["name"]
if not postContext["author_url"]:
postContext["author_url"] = author_metadata.get("url", "")
if not postContext["author_github"]:
postContext["author_github"] = author_metadata.get(
"github", "")
if not postContext["author_email"]:
postContext["author_email"] = author_metadata.get("email", "")

author = find("author", warn=False)
if "," in author:
authors = [{"name": name.strip()} for name in author.split(",")]
else:
authors = [{
"name": find("author"),
"url": find("author_url", warn=False),
"github": find("author_github", warn=False),
"email": find("author_email", warn=False),
}]

postContext["authors"] = []
for author in authors:
author_metadata = AUTHOR_METADATA.get(author["name"])
if author_metadata:
if "name" in author_metadata:
author["name"] = author_metadata["name"]
if not author.get("url"):
author["url"] = author_metadata.get("url", "")
if not author.get("github"):
author["github"] = author_metadata.get("github", "")
if not author.get("email"):
author["email"] = author_metadata.get("email", "")

postContext["authors"].append(author)

postContext["date"] = find("date")
postContext["path"] = posixpath.join("/", page.path)
Expand Down
20 changes: 10 additions & 10 deletions templates/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ <h1>{{ title }}</h1>
{# Post content #}
<p style="font-size: small;">
Posted on <time>{{ date|date:"Y-m-d" }}</time>
{% if author %}
{% if authors %}
by
{% for author in authors|slice:":-2" %}
<span class="author">{% if author.url %}<a href="{{ author.url }}">{{ author.name }}</a>{% else %}{{ author.name }}{% endif %}{% if author.github %} (<a href="https://github.com/{{ author.github }}">@{{ author.github }}</a>){% endif %}</span>,
{% endfor %}
{% for author in authors|slice:"-2:-1" %}
<span class="author">{% if author.url %}<a href="{{ author.url }}">{{ author.name }}</a>{% else %}{{ author.name }}{% endif %}{% if author.github %} (<a href="https://github.com/{{ author.github }}">@{{ author.github }}</a>){% endif %}</span> and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not very familiar with cactus, but is there any way to reduce the duplication using a macro or smth?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. We could put it into a separate template and include that here, but that also would also lead to superfluous newlines

{% endfor %}
{% for author in authors|slice:"-1:" %}
<span class="author">
{% if author_url %}
<a href="{{ author_url }}">{{ author }}</a>
{% else %}
{{ author }}
{% endif %}
{% if author_github %}
(<a href="https://github.com/{{ author_github }}">@{{ author_github }}</a>)
{% endif %}
</span>
{% if author.url %}<a href="{{ author.url }}">{{ author.name }}</a>{% else %}{{ author.name }}{% endif %}{% if author.github %} (<a href="https://github.com/{{ author.github }}">@{{ author.github }}</a>){% endif %}</span>
{% endfor %}
{% endif %}
</p>
{% block post %}{% endblock %}
Expand Down