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

Extract common utils in rendering SRAWN #82

Open
wants to merge 3 commits into
base: ci-diff
Choose a base branch
from
Open
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
40 changes: 19 additions & 21 deletions scripts/lint-filenames.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

#
# Copyright (c) 2019-20 Dan Trickey.
#
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Expand All @@ -24,41 +24,39 @@
#

from pathlib import Path
import re

from srawn_utils import ISSUE_FILENAME_PATTERN, YEAR_FOLDER_PATTERN

status = True

print("Verifying minutes filenames.")

repo = Path(".")

filename_regex = re.compile("^20\d{2}-\d{2}-\d{2}-srawn-\d{2}\.md$")
folder_regex = re.compile("^SR20\d{2}$")

year_folders = [f for f in repo.iterdir() if f.is_dir()]

for folder in year_folders:
result = re.match(folder_regex, folder.name)
result = YEAR_FOLDER_PATTERN.match(folder.name)

if result is not None:
print(f"Found valid folder name: {folder.name}")

for f in folder.iterdir():
# Test if there are any nested folders
# Test if there are any nested folders

if f.is_dir():
print(f"\tFound nested directory: {folder.stem}/{f.stem}")
status = False

# Verify file name

if f.is_dir():
print(f"\tFound nested directory: {folder.stem}/{f.stem}")
status = False

# Verify file name

file_result = re.match(filename_regex, f.name)
file_result = ISSUE_FILENAME_PATTERN.match(f.name)

if file_result is None:
print(f"\tFound bad file name: {f.name}")
status = False
else:
print(f"\tFound valid file name: {f.name}")
if file_result is None:
print(f"\tFound bad file name: {f.name}")
status = False
else:
print(f"\tFound valid file name: {f.name}")

if status:
print("All tests passed.")
Expand Down
18 changes: 5 additions & 13 deletions scripts/render-feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from feedgenerator import DefaultFeed
import mistune

import srawn_utils

working_dir = Path('.')
feed = DefaultFeed(
Expand All @@ -18,26 +19,17 @@
md = mistune.create_markdown()


for md_path in sorted(working_dir.glob("SR20*/*.md")):
filename_match = re.match("^(20\d{2}-\d{2}-\d{2})-srawn-(\d{2})$", md_path.stem)
if not filename_match:
exit(f"{md_path.stem} does not match format. Run the linter.")
date, issue = filename_match.groups()

folder_match = re.match("^(SR20\d{2})$", md_path.parent.name)
if not folder_match:
exit(f"{md_path.parent.name} does not match format. Run the linter.")
sryear, = folder_match.groups()

for issue in srawn_utils.get_all_issues(working_dir):
md_path = issue.path
link = f"https://studentrobotics.org/srawn/{md_path.parent.stem}/{md_path.stem}.html"
content = md(md_path.read_text())

feed.add_item(
title=f"{sryear} Issue {issue}",
title=issue.title,
link=link,
description=content,
unique_id=link,
pubdate=datetime.date.fromisoformat(date),
pubdate=issue.date,
content=content
)

Expand Down
38 changes: 17 additions & 21 deletions scripts/render-indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import jinja2

import srawn_utils

# cspell:disable-next-line
templateLoader = jinja2.FileSystemLoader(searchpath="./templates")
templateEnv = jinja2.Environment(loader=templateLoader)
Expand All @@ -13,29 +15,23 @@
working_dir = Path('.')
html_dir = working_dir / "out/html"

years = sorted(working_dir.glob("SR20*"), reverse=True)
years = srawn_utils.get_years(working_dir)

# render main index
with html_dir.joinpath("index.html").open("w") as fh:
fh.write(archive_template.render(years=years))

# render year indices
for year in years:
year_html_path = html_dir / year
year_path = working_dir / year
issues = sorted(year_path.glob("*.md"))
issue_data = []
for md_path in issues:
filename_match = re.match("^(20\d{2}-\d{2}-\d{2})-srawn-(\d{2})$", md_path.stem)
if not filename_match:
exit(f"{md_path.stem} does not match format. Run the linter.")
date, issue = filename_match.groups()

folder_match = re.match("^(SR20\d{2})$", md_path.parent.name)
if not folder_match:
exit(f"{md_path.parent.name} does not match format. Run the linter.")
sryear, = folder_match.groups()
issue_data.append((md_path.stem, f"{date}: {sryear} Issue {issue}"))

with year_html_path.joinpath("index.html").open("w") as fh:
fh.write(year_template.render(year=year, issues=issue_data))
with srawn_utils.exit_on_invalid():
# render year indices
for year in years:
year_html_path = html_dir / year
year_path = working_dir / year
issue_data = []
for issue in srawn_utils.get_year_issues(year_path):
issue_data.append((
issue.path.stem,
f"{issue.date_text}: {issue.title}",
))

with year_html_path.joinpath("index.html").open("w") as fh:
fh.write(year_template.render(year=year, issues=issue_data))
23 changes: 8 additions & 15 deletions scripts/render-mjml.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import jinja2
import mistune

import srawn_utils

FONTS: Dict[int, Tuple[str, int, Optional[int]]] = {
0: ("Open Sans", 20, 25), # Paragraph
1: ("Open Sans", 35, None),
Expand Down Expand Up @@ -62,24 +64,15 @@ def render_paragraph(self, text: str, *, font_size: int, font_family: str, line_
if not md_path.is_file():
exit(f"{md_path} is not a file")

filename_match = re.match("^(20\d{2}-\d{2}-\d{2})-srawn-(\d{2})$", md_path.stem)
if not filename_match:
exit(f"{md_path.stem} does not match format. Run the linter.")
date, issue = filename_match.groups()

folder_match = re.match("^(SR20\d{2})$", md_path.parent.name)
if not folder_match:
exit(f"{md_path.parent.name} does not match format. Run the linter.")
sryear, = folder_match.groups()

with srawn_utils.exit_on_invalid():
parsed_issue_path = srawn_utils.parse_path(md_path)

with md_path.open("r") as fh:
raw_markdown = fh.read()

# Remove title from old issues. This prevents duplicate titles in the archive
# without changing the original files.
raw_markdown = re.sub("^# SR\(A\)WN \d{4} [—-]+ \d+$", "", raw_markdown, flags=re.MULTILINE)

raw_markdown = re.sub(r"^# SR\(A\)WN \d{4} [—-]+ \d+$", "", raw_markdown, flags=re.MULTILINE)

md = mistune.create_markdown(renderer=MJMLRenderer())
content = md(raw_markdown)
Expand All @@ -90,9 +83,9 @@ def render_paragraph(self, text: str, *, font_size: int, font_family: str, line_
template = templateEnv.get_template("newsletter.mjml.j2")

output = template.render(
date=date,
sryear=sryear,
issue=issue,
date=parsed_issue_path.date_text,
sryear=parsed_issue_path.sryear,
issue=parsed_issue_path.issue_number,
content=content,
)
print(output)
74 changes: 74 additions & 0 deletions scripts/srawn_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import contextlib
import dataclasses
import datetime
import re
from collections.abc import Iterator
from pathlib import Path

YEAR_FOLDER_PATTERN = re.compile(r"^(SR20\d{2})$")
ISSUE_FILENAME_PATTERN = re.compile(r"^(20\d{2}-\d{2}-\d{2})-srawn-(\d{2}).md$")


class InvalidPath(ValueError):
pass


@dataclasses.dataclass(frozen=True)
class ParsedIssuePath:
path: Path
date: datetime.date
issue_number: str
sryear: str

@property
def title(self) -> str:
return f"{self.sryear} Issue {self.issue_number}"

@property
def date_text(self) -> str:
return self.date.isoformat()


def parse_path(path: Path) -> ParsedIssuePath:
filename_match = ISSUE_FILENAME_PATTERN.match(path.name)
if not filename_match:
raise InvalidPath(
f"{path.name!r} does not match format. Run the linter.",
)
date, issue_number = filename_match.groups()

folder_match = YEAR_FOLDER_PATTERN.match(path.parent.name)
if not folder_match:
raise InvalidPath(
f"{path.parent.name!r} does not match format. Run the linter.",
)
sryear, = folder_match.groups()

return ParsedIssuePath(
path,
datetime.date.fromisoformat(date),
issue_number,
sryear,
)


@contextlib.contextmanager
def exit_on_invalid() -> Iterator[None]:
try:
yield
except InvalidPath as e:
exit(str(e))


def get_years(root: Path) -> list[Path]:
return sorted(root.glob("SR20*"), reverse=True)


def get_year_issues(year_root: Path) -> list[ParsedIssuePath]:
paths = sorted(year_root.glob("*.md"))
return [parse_path(x) for x in paths]


def get_all_issues(root: Path) -> list[ParsedIssuePath]:
paths = sorted(root.glob("SR20*/*.md"))
return [parse_path(x) for x in paths]
Loading