Skip to content

Commit

Permalink
[FEAT] Deploy docs to README (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
AzulGarza authored Oct 6, 2023
2 parents 3a1d0f9 + 6470c84 commit a4af6cd
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/deploy_readme.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: CI

on:
push:
branches: [main]
workflow_dispatch:

defaults:
run:
shell: bash -l {0}

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
deploy-readme:
runs-on: ubuntu-latest
steps:
- name: Clone repo
uses: actions/checkout@v2
with:
persist-credentials: false

- name: Set up environment
uses: mamba-org/setup-micromamba@v1
with:
environment-file: environment.yml
create-args: python=${{ matrix.python-version }}
cache-environment: true

- name: Install pip requirements
run: pip install ./

- name: Install Quarto
run: nbdev_install_quarto

- name: Create readme docs
env:
README_HOST_URL: ${{ secrets.README_HOST_URL }}
README_CATEGORY: ${{ secrets.README_CATEGORY }}
run: ./action_files/create_readme_docs.sh

- name: Set up Git User
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
- name: Push PNGs to readme_docs branch
run: |
git checkout readme_docs || git checkout --orphan readme_docs
git --work-tree=nbs add "*.png"
git --work-tree=nbs commit -m "Add/Update PNG files" || echo "No changes to commit"
git push https://${{secrets.GITHUB_TOKEN}}@github.com/${{github.repository}} HEAD:readme_docs
- name: Deploy to readme
uses: readmeio/rdme@v8
with:
rdme: docs ./nbs/_docs/docs/ --key=${{ secrets.README_API_KEY }} --version=2.0
Empty file added action_files/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions action_files/create_readme_docs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

BASE_DIR="nbs/docs/"
SUB_DIRS=("tutorials" "getting-started" "how-to-guides")

for sub_dir in "${SUB_DIRS[@]}"; do
DIR="$BASE_DIR$sub_dir/"
if [[ -d "$DIR" ]]; then
find "$DIR" -type f -name "*.ipynb" -not -path "*/.ipynb_checkpoints/*" | while read -r ipynb_file; do
md_file="${ipynb_file%.ipynb}.md"
md_file="${md_file/docs/_docs/docs}"
quarto render "$ipynb_file" --to md
python -m action_files.modify_markdown --file_path "$md_file"
done
else
echo "Directory $DIR does not exist."
fi
done
66 changes: 66 additions & 0 deletions action_files/modify_markdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os
import re

import fire
from dotenv import load_dotenv

load_dotenv()


def to_snake_case(s):
s = re.sub(r'(?<!^)(?=[A-Z])', '_', s).lower()
s = re.sub(r'\W', '_', s)
s = re.sub(r'_+', '_', s)
return s

def modify_markdown(
file_path,
host_url=os.environ['README_HOST_URL'],
category=os.environ['README_CATEGORY'],
):
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
dir_path = os.path.dirname(file_path)
if not dir_path.endswith("/"):
dir_path += "/"
host_url += dir_path

# Extract and remove the first markdown header
pattern_header = re.compile(r'^#\s+(.*)\n+', re.MULTILINE)
match = pattern_header.search(content)

if match:
title = match.group(1)
content = pattern_header.sub('', content, count=1) # remove the first match
else:
title = 'Something Amazing'
slug = to_snake_case(title)

# Prepare the new header
header = f"""---
title: "{title}"
slug: "{slug}"
excerpt: "Learn how to do {title} with TimeGPT"
category: {category}
hidden: false
---
"""

# Remove parts delimited by ::: :::
pattern_delimited = re.compile(r':::.*?:::', re.DOTALL)
content = pattern_delimited.sub('', content)

# Modify image paths
pattern_image = re.compile(r'!\[\]\((.*?)\)')
modified_content = pattern_image.sub(r'![](' + host_url + r'\1)', content)

# Concatenate new header and modified content
final_content = header + modified_content

with open(file_path, 'w', encoding='utf-8') as file:
file.write(final_content)

if __name__=="__main__":
fire.Fire(modify_markdown)

0 comments on commit a4af6cd

Please sign in to comment.