Add web viewer workflow

This commit is contained in:
2024-05-30 08:44:30 +02:00
parent c18948b59c
commit 7847605153
4 changed files with 138 additions and 3 deletions

68
.github/prepare_web_viewer.py vendored Normal file
View File

@ -0,0 +1,68 @@
import argparse
from read_metadata import readMetadata
import re
import os
from pathlib import Path
import shutil
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="Web viewer formatter")
parser.add_argument("--src-path", type=str, required=True, help="Path to the .tex sources (for metadata)")
parser.add_argument("--out-path", type=str, required=True, help="Path of the output directory")
parser.add_argument("--pdfs-path", type=str, required=True, help="Path of the pdfs directory")
parser.add_argument("--template-path", type=str, default="./web-viewer", help="Path to the templates")
args = parser.parse_args()
notes_metadata = readMetadata(args.src_path)
table_of_content = ""
url_pdf_dir = "pdfs"
dest_pdf_dir = os.path.join(args.out_path, url_pdf_dir)
with open(os.path.join(args.template_path, "index.html"), "r") as f: index_template = f.read()
with open(os.path.join(args.template_path, "view.html"), "r") as f: viewer_template = f.read()
os.makedirs(args.out_path, exist_ok=True)
shutil.copytree(args.pdfs_path, dest_pdf_dir, dirs_exist_ok=True)
# Generate home page content
for year in sorted(notes_metadata.keys()):
for semester in sorted(notes_metadata[year].keys()):
for course in sorted(notes_metadata[year][semester]):
course_name = notes_metadata[year][semester][course]["name"]
course_content = notes_metadata[year][semester][course]["content"]
if (len(course_content) == 1) and (course_content[0]["name"] is None):
table_of_content += f"<h2><a href={Path(course_content[0]['url']).stem}.html>{course_name}</a></h2>\n"
else:
table_of_content += f"<h2>{course_name}</h2>\n"
table_of_content += "<ul>\n"
for content in course_content:
table_of_content += f"<li><h3><a href={Path(content['url']).stem}.html>{content['name']}</a></h3></li>\n"
table_of_content += "</ul>\n"
with open(os.path.join(args.out_path, "index.html"), "w") as f:
f.write(
re.sub(
r"<!-- begin-toc -->[\s\S]*<!-- end-toc -->",
f"<!-- begin-toc -->\n{table_of_content}\n<!-- end-toc -->",
index_template
)
)
# Generate viewer content
for year in notes_metadata.keys():
for semester in notes_metadata[year].keys():
for course in notes_metadata[year][semester]:
course_name = notes_metadata[year][semester][course]["name"]
course_content = notes_metadata[year][semester][course]["content"]
for content in course_content:
content_local_path = os.path.join(url_pdf_dir, content["url"])
content_html_name = f"{Path(content['url']).stem}.html"
with open(os.path.join(args.out_path, content_html_name), "w") as f:
page_content = re.sub(r"{{pdf-path}}", f"{content_local_path}", viewer_template)
page_content = re.sub(r"{{course-name}}", f"{Path(content['url']).name}", page_content)
f.write(page_content)

22
.github/web-viewer/index.html vendored Normal file
View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
font-family: monospace, monospace;
color: white;
background-color: #292929;
}
</style>
</head>
<body>
<h1>Table of contents</h1>
<!-- begin-toc -->
<!-- end-toc -->
</body>
</html>

23
.github/web-viewer/view.html vendored Normal file
View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{course-name}}</title>
<style>
html, body {
margin: 0;
padding: 0;
overflow: hidden;
}
</style>
</head>
<body style="height: 100vh; width: 100vw;">
<object data="{{pdf-path}}" type="application/pdf" width="100%" height="100%">
<p>
There has been an error. View the pdf <a href="{{pdf-path}}">here</a>
</p>
</object>
</body>
</html>

View File

@ -37,7 +37,10 @@ jobs:
- name: Compile - name: Compile
run: | run: |
bash ${GITHUB_WORKSPACE}/utils/compile.sh src ${GITHUB_WORKSPACE}/.compiled ${GITHUB_WORKSPACE}/.currpdfs bash ${GITHUB_WORKSPACE}/utils/compile.sh \
${GITHUB_WORKSPACE}/src \
${GITHUB_WORKSPACE}/.compiled \
${GITHUB_WORKSPACE}/.currpdfs
- name: Generate README - name: Generate README
run: | run: |
@ -47,14 +50,33 @@ jobs:
--readme-path ${GITHUB_WORKSPACE}/.compiled/README.md \ --readme-path ${GITHUB_WORKSPACE}/.compiled/README.md \
--gh-link https://github.com/NotXia/unibo-ai-notes/blob/pdfs --gh-link https://github.com/NotXia/unibo-ai-notes/blob/pdfs
- name: Move to pdfs branch - name: Move to pdfs branch
uses: s0/git-publish-subdir-action@develop uses: s0/git-publish-subdir-action@develop
env: env:
REPO: self REPO: self
BRANCH: pdfs BRANCH: pdfs
FOLDER: .compiled FOLDER: ${GITHUB_WORKSPACE}/.compiled
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMIT_NAME: "github-actions[bot]" COMMIT_NAME: "github-actions[bot]"
COMMIT_EMAIL: "github-actions[bot]@users.noreply.github.com" COMMIT_EMAIL: "github-actions[bot]@users.noreply.github.com"
MESSAGE: "🤖 Hello human, trying to not break anything ({sha})" MESSAGE: "🤖 Hello human, trying to not break anything ({sha})"
- name: Generate web viewer content
run: |
python3 ${GITHUB_WORKSPACE}/.github/prepare_web_viewer.py \
--src-path=${GITHUB_WORKSPACE}/src \
--out-path=/tmp/webviewer \
--pdfs-path=${GITHUB_WORKSPACE}/.compiled \
--template-path=${GITHUB_WORKSPACE}/.github/web-viewer \
- name: Move to pages branch
uses: s0/git-publish-subdir-action@develop
env:
REPO: self
BRANCH: pages
FOLDER: /tmp/webviewer
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMIT_NAME: "github-actions[bot]"
COMMIT_EMAIL: "github-actions[bot]@users.noreply.github.com"
MESSAGE: "🤖 Web developer stuff"