Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #254 from Nadeau-Innovations/press-releases
Browse files Browse the repository at this point in the history
feat: added press release content type
  • Loading branch information
engnadeau authored Nov 3, 2023
2 parents ff82304 + 98455c8 commit d80c1cd
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 66 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,19 @@ serve:

.PHONY: post
post:
poetry run python scripts/hugo_new.py post --branch
poetry run python scripts/hugo_new.py post

.PHONY: event
event:
poetry run python scripts/hugo_new.py event --branch
poetry run python scripts/hugo_new.py event

.PHONY: project
project:
poetry run python scripts/hugo_new.py project --branch
poetry run python scripts/hugo_new.py project

.PHONY: press-release
press-release:
poetry run python scripts/hugo_new.py press-release --is_post_archetype

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Debug and Info
Expand Down
7 changes: 7 additions & 0 deletions content/press-release/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
banner:
image: heroes/news.jpg
cms_exclude: true
title: Press Releases
view: 2
---
5 changes: 3 additions & 2 deletions content/press.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ sections:
exclude_future: false
exclude_past: false
featured_only: false
folders: ""
folders:
- press-release
publication_type: ""
tag: press
tag: ""
offset: 0
sort_ascending: false
sort_by: Date
Expand Down
89 changes: 28 additions & 61 deletions scripts/hugo_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,92 +4,59 @@
from string import ascii_lowercase, digits, whitespace

import fire
import nbformat as nbf


def sanitize_fname(s: str):
valid = ascii_lowercase + digits + whitespace
# lowercase, remove invalid chars, remove whitespace,
# replace whitespace with -
valid_chars = ascii_lowercase + digits + whitespace
s = s.lower()
s = filter(lambda x: x in valid, s)
s = filter(lambda x: x in valid_chars, s)
s = "".join(s)
s = s.split()
s = "-".join(s)
return s


def get_title():
def new(kind: str, is_post_archetype: bool = False):
# get title
title = input(">>> Note title: ")
logging.info(f"Title: {title}")
return title


def new(kind: str, notebook: bool = False, branch: bool = False):
# get title
title = get_title()
slug = sanitize_fname(title)
logging.info(f"Slug: {slug}")

# set path
hugo_path = f"{kind}/{slug}"
index_path = "content" / Path(hugo_path) / "index.md"

# create md document
cmd = ["hugo", "new", hugo_path]
logging.info(f"Hugo command: {' '.join(cmd)}")
subprocess.run(cmd)

# create notebook if needed
if notebook:
create_notebook(path=index_path)
extra_args = []
if is_post_archetype:
logging.info("Using post archetype")
extra_args.extend(["--kind", "post"])
logging.info(f"Extra args: {extra_args}")

# create branch if needed
if branch:
# define commands
commands = [
# create md document
["hugo", "new", hugo_path] + extra_args,
# checkout master
cmd = ["git", "checkout", "master"]
logging.info(f"Git command: {' '.join(cmd)}")
subprocess.run(cmd)

["git", "checkout", "master"],
# create branch
cmd = ["git", "checkout", "-b", slug]
logging.info(f"Git command: {' '.join(cmd)}")
subprocess.run(cmd)

["git", "checkout", "-b", slug],
# push branch
cmd = ["git", "push", "-u", "origin", slug]
logging.info(f"Git command: {' '.join(cmd)}")
subprocess.run(cmd)

["git", "push", "-u", "origin", slug],
# commit document to branch
cmd = ["git", "add", str(index_path)]
logging.info(f"Git command: {' '.join(cmd)}")
subprocess.run(cmd)

cmd = ["git", "commit", "-m", f"feat: added {kind}"]
logging.info(f"Git command: {' '.join(cmd)}")
subprocess.run(cmd)

["git", "add", str(index_path)],
["git", "commit", "-m", f"feat: added {kind}"],
# use gh cli to open pr with title and empty body
cmd = ["gh", "pr", "create", "--title", f'"feat: {title}"', "--body", '""']
logging.info(f"GitHub command: {' '.join(cmd)}")
subprocess.run(cmd)

# open document
cmd = ["code", str(index_path.parent)]
logging.info(f"VSCode command: {' '.join(cmd)}")
subprocess.run(cmd)

["gh", "pr", "create", "--title", f'"feat: {title}"', "--body", '""'],
# open document
["code", str(index_path.parent)],
]

def create_notebook(path: Path):
# get metadata and text
with open(path) as f:
metadata = f.read()
text = "\n"

# write notebook content
nb = nbf.v4.new_notebook()
nb["cells"] = [nbf.v4.new_raw_cell(metadata), nbf.v4.new_markdown_cell(text)]
path = path.with_suffix(".ipynb")
logging.info(f"Writing notebook: {path}")
nbf.write(nb, path)
for cmd in commands:
logging.info(f"Running command: {' '.join(cmd)}")
subprocess.run(cmd)


if __name__ == "__main__":
Expand Down

0 comments on commit d80c1cd

Please sign in to comment.