Skip to content

Commit

Permalink
Merge branch 'main' into jukent-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
jukent authored Aug 5, 2024
2 parents 6c0fd55 + c1cc880 commit 94720b7
Show file tree
Hide file tree
Showing 14 changed files with 261 additions and 259 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nightly-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ jobs:
if: ${{ github.repository_owner == 'ProjectPythia' }}
uses: ProjectPythia/projectpythia.github.io/.github/workflows/sphinx-link-checker.yaml@main
with:
path_to_source: 'site'
path_to_source: 'site'
2 changes: 1 addition & 1 deletion .github/workflows/publish-site.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ jobs:
needs: build
uses: ProjectPythia/cookbook-actions/.github/workflows/deploy-book.yaml@main
with:
publish_dir: 'site/_build/html'
publish_dir: 'site/_build/html'
14 changes: 7 additions & 7 deletions .github/workflows/update-cookbook-gallery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
shell: bash -l {0}
steps:
- name: Find Comment
uses: peter-evans/find-comment@v2
uses: peter-evans/find-comment@v3
id: fc
with:
issue-number: ${{ github.event.issue.number }}
Expand All @@ -23,7 +23,7 @@ jobs:

- name: Create comment
if: steps.fc.outputs.comment-id == ''
uses: peter-evans/create-or-update-comment@v3
uses: peter-evans/create-or-update-comment@v4
with:
issue-number: ${{ github.event.issue.number }}
body: |
Expand All @@ -33,7 +33,7 @@ jobs:
- name: Update comment
if: steps.fc.outputs.comment-id != ''
uses: peter-evans/create-or-update-comment@v3
uses: peter-evans/create-or-update-comment@v4
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
edit-mode: replace
Expand Down Expand Up @@ -118,7 +118,7 @@ jobs:

- name: Create pull request
id: cpr
uses: peter-evans/create-pull-request@v5
uses: peter-evans/create-pull-request@v6
with:
commit-message: 'Update cookbook gallery'
committer: GitHub <[email protected]>
Expand All @@ -132,7 +132,7 @@ jobs:
branch: cookbook-gallery-${{ github.event.issue.number }}

- name: Find Comment
uses: peter-evans/find-comment@v2
uses: peter-evans/find-comment@v3
id: fc
with:
issue-number: ${{ github.event.issue.number }}
Expand All @@ -141,7 +141,7 @@ jobs:

- name: Create comment
if: steps.fc.outputs.comment-id == ''
uses: peter-evans/create-or-update-comment@v3
uses: peter-evans/create-or-update-comment@v4
with:
issue-number: ${{ github.event.issue.number }}
body: |
Expand All @@ -151,7 +151,7 @@ jobs:
- name: Update comment
if: steps.fc.outputs.comment-id != ''
uses: peter-evans/create-or-update-comment@v3
uses: peter-evans/create-or-update-comment@v4
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
edit-mode: replace
Expand Down
9 changes: 3 additions & 6 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ channels:
- conda-forge
- nodefaults
dependencies:
- cffconvert
- matplotlib
- myst-nb
- pandas
- pip
- pyyaml
- pre-commit
- sphinx-panels
- cffconvert
- pip:
- sphinx-pythia-theme
- pyyaml
- sphinx-pythia-theme
8 changes: 3 additions & 5 deletions site/_extensions/cookbook_gallery_generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from gallery_generator import build_from_repos, generate_menu, generate_repo_dicts


def main(app):

with open("cookbook_gallery.txt") as fid:
Expand All @@ -15,8 +14,8 @@ def main(app):
for line in fid:
subtext = subtext + line

submit_btn_link = "https://github.com/ProjectPythia/cookbook-gallery/issues/new?assignees=ProjectPythia%2Feducation&labels=content%2Ccookbook-gallery-submission&template=update-cookbook-gallery.yaml&title=Update+Gallery+with+new+Cookbook"
submit_btn_txt = "Submit a new Cookbook"
submit_btn_link = "https://projectpythia.org/cookbook-guide.html"
submit_btn_txt = "How can I create a new Cookbook?"
menu_html = generate_menu(
repo_dicts, submit_btn_txt=submit_btn_txt, submit_btn_link=submit_btn_link
)
Expand All @@ -25,6 +24,5 @@ def main(app):
repo_dicts, "index", title=title, subtext=subtext, menu_html=menu_html
)


def setup(app):
app.connect("builder-inited", main)
app.connect("builder-inited", main)
160 changes: 86 additions & 74 deletions site/_extensions/gallery_generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import itertools, json, yaml, pathlib, subprocess, requests
from textwrap import dedent
from truncatehtml import truncate
import re


def _grab_binder_link(repo):
Expand Down Expand Up @@ -44,6 +44,12 @@ def _run_cffconvert(command):
error_message = stderr.decode("utf-8").strip()
raise RuntimeError(f"cffconvert command failed: {error_message}")

def _make_standard_name(name):
'''Take string input like LASTNAME, FIRSTNAME and return FIRSTNAME LASTNAME without comma'''
lastfirst = name.split(', ')
firstlast = lastfirst[::-1]
standard = ' '.join(firstlast)
return standard

def generate_repo_dicts(all_items):

Expand All @@ -61,7 +67,7 @@ def generate_repo_dicts(all_items):
cookbook_title = citation_dict["title"]
description = citation_dict["description"]
creators = citation_dict["creators"]
names = [item.get("name") for item in creators]
names = [_make_standard_name(creator.get("name")) for creator in creators]
authors = ", ".join(names)

gallery_info_url = f"https://raw.githubusercontent.com/ProjectPythia/{repo}/main/_gallery_info.yml"
Expand Down Expand Up @@ -108,14 +114,21 @@ def _generate_sorted_tag_keys(repo_dicts):
return sorted(key_set)


def _title_case_preserve(s):
return re.sub(r'\b(\w)', lambda m: m.group(1).upper(), s)

def _make_class(s):
return re.sub(r'^\d+', '', s.replace(" ", "-").lower())

def _generate_tag_set(repo_dicts, tag_key=None):

tag_set = set()
for repo_dict in repo_dicts:
for k, e in repo_dict["tags"].items():
tags = [_title_case_preserve(t) for t in e]
if tag_key and k != tag_key:
continue
for t in e:
for t in tags:
tag_set.add(t)

return tag_set
Expand All @@ -127,21 +140,19 @@ def _generate_tag_menu(repo_dicts, tag_key):
tag_list = sorted(tag_set)

options = "".join(
f'<li><label class="dropdown-item checkbox {tag_key}"><input type="checkbox" rel={tag.replace(" ", "-")} onchange="change();">&nbsp;{tag}</label></li>'
f'<li><label class="dropdown-item checkbox {tag_key}"><input type="checkbox" rel={_make_class(tag)} onchange="change();">&nbsp;{tag}</label></li>'
for tag in tag_list
)

return f"""
<div class="dropdown">
<button class="btn btn-sm btn-outline-primary mx-1 dropdown-toggle" type="button" id="{tag_key}Dropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{tag_key.title()}
</button>
<ul class="dropdown-menu" aria-labelledby="{tag_key}Dropdown">
{options}
</ul>
</div>
"""
:::{{dropdown}} {tag_key}
<div class="dropdown">
<ul>
{options}
</ul>
</div>
:::
"""


def generate_menu(repo_dicts, submit_btn_txt=None, submit_btn_link=None):
Expand Down Expand Up @@ -174,7 +185,7 @@ def build_from_repos(
):

# Build the gallery file
panels_body = []
grid_body = []
for repo_dict in repo_dicts:
repo = repo_dict["repo"]
github_url = repo_dict["github_url"]
Expand All @@ -195,82 +206,83 @@ def build_from_repos(
tag_list = sorted((itertools.chain(*tag_dict.values())))
tag_list_f = [tag.replace(" ", "-") for tag in tag_list]
tags = [
f'<span class="badge bg-primary mybadges">{tag}</span>'
f'<span class="badge bg-primary mybadges">{_title_case_preserve(tag)}</span>'
for tag in tag_list_f
]
tags = "\n".join(tags)
tag_class_str = " ".join(tag_list_f)
tag_classes = " ".join(tag_list_f)

description = repo_dict["description"]
ellipsis_str = '<a class="modal-btn"> ... more</a>'
short_description = truncate(description, max_descr_len, ellipsis=ellipsis_str)

if ellipsis_str in short_description:
modal_str = f"""
<div class="modal">
<div class="content">
<img src="{thumbnail_url}" class="modal-img" />
<h3 class="display-3">{cookbook_title}</h3>
{authors_str}
<p class="my-2">{description}</p>
<p class="my-2">{tags}</p>
<p class="mt-3 mb-0"><a href="{cookbook_url}" class="btn btn-outline-primary btn-block">Visit Website</a></p>
</div>
</div>
"""
<div class="modal">
<div class="content">
<img src="{thumbnail_url}" class="modal-img" />
<h3 class="display-3">{cookbook_title}</h3>
{authors_str}
<p class="my-2">{description}</p>
<p class="my-2">{tags}</p>
<p class="mt-3 mb-0"><a href="{cookbook_url}" class="btn btn-outline-primary btn-block">Visit Website</a></p>
</div>
</div>
"""
modal_str = '\n'.join([m.lstrip() for m in modal_str.split('\n')])
else:
modal_str = ""

panels_body.append(
f"""\
---
:column: + tagged-card {tag_class_str}
<div class="d-flex gallery-card">
<img src="{thumbnail_url}" class="gallery-thumbnail" />
<div class="container">
<a href="{cookbook_url}" class="text-decoration-none"><h4 class="display-4 p-0">{cookbook_title}</h4></a>
<p class="card-subtitle">{authors_str}</p>
<br/>
<p class="my-2">{short_description}</p>
</div>
</div>
{modal_str}
+++
<div class="tagsandbadges">
{tags}
<div>{status_badges}</div>
</div>
"""
)
new_card = f"""
:::{{grid-item-card}}
:shadow: md
:class-footer: card-footer
:class-card: tagged-card {tag_classes}
<div class="d-flex gallery-card">
<img src="{thumbnail_url}" class="gallery-thumbnail" />
<div class="container">
<a href="{cookbook_url}" class="text-decoration-none"><h4 class="display-4 p-0">{cookbook_title}</h4></a>
<p class="card-subtitle">{authors_str}</p>
<p class="my-2">{short_description} </p>
</div>
</div>
{modal_str}
+++
<div class="tagsandbadges">
{tags}
<div>{status_badges}</div>
</div>
:::
"""

grid_body.append('\n'.join([m.lstrip() for m in new_card.split('\n')]))

panels_body = "\n".join(panels_body)

stitle = f"#### {subtitle}" if subtitle else ""
stext = subtext if subtext else ""

panels = f"""
# {title}
{stitle}
{stext}
{menu_html}
````{{panels}}
:column: col-12
:card: +mb-4 w-100
:header: d-none
:body: p-3 m-0
:footer: p-1
{dedent(panels_body)}
````
grid_body = "\n".join(grid_body)

grid = f"""
# {title}
{stitle}
{stext}
{menu_html}
::::{{grid}} 1
:gutter: 0
{grid_body}
<div class="modal-backdrop"></div>
<script src="../html/_static/custom.js"></script>
"""

<div class="modal-backdrop"></div>
<script src="/_static/custom.js"></script>
"""
grid = '\n'.join([m.lstrip() for m in grid.split('\n')])

pathlib.Path(f"{filename}.md").write_text(panels)
pathlib.Path(f"{filename}.md").write_text(grid)
Loading

0 comments on commit 94720b7

Please sign in to comment.