From cd853748898955682642a653ac19b3d828598a94 Mon Sep 17 00:00:00 2001 From: beiyuouo Date: Wed, 2 Mar 2022 14:14:16 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat=20=E2=9C=A8:=20add=20github=20actions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/main.yml | 39 +++++++++++++++++++++ .gitignore | 4 ++- generate.py | 70 ++++++++++++++++++++++++++++++++++++++ mkdocs.yml | 30 ++++++++++++++++ overrides/main.html | 17 +++++++++ requirements.txt | 3 ++ 6 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/main.yml create mode 100644 generate.py create mode 100644 mkdocs.yml create mode 100644 overrides/main.html create mode 100644 requirements.txt diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..57045f6 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,39 @@ +# This is a basic workflow to help you get started with Actions + +name: CI + +# Controls when the action will run. Triggers the workflow on push or pull request +# events but only for the master branch +on: + push: + branches: [ main ] + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + build: + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v2 + + - name: Set up Python 3.x + uses: actions/setup-python@v2 + with: + python-version: 3.x + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - name: Update dir + run: python generate.py + + - name: Deploy docs + uses: mhausenblas/mkdocs-deploy-gh-pages@nomaterial + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 70df1fe..4366672 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,6 @@ ._* *.iml .DS_Store -local.properties \ No newline at end of file +local.properties + +docs/ \ No newline at end of file diff --git a/generate.py b/generate.py new file mode 100644 index 0000000..b3601ed --- /dev/null +++ b/generate.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import os +import shutil +from urllib.parse import quote + +EXCLUDE_DIRS = ['.git', 'docs', '.vscode', 'overrides', '.github', 'script'] +README_MD = ['README.md', 'readme.md', 'index.md'] +INDEX_FILE = 'index.md' + +IMAGE_EXTS = ['jpg', 'png', 'svg', 'jpeg', 'gif', 'webp'] + + +def list_image(course: str): + imagelist_text = f'# {os.path.basename(course)}\n' + + for root, dirs, files in os.walk(course): + files.sort() + readme_path = '{}/{}'.format(root, INDEX_FILE) + level = root.replace(course, '').count(os.sep) + for f in files: + if f.split('.')[-1].lower() in IMAGE_EXTS: + imagelist_text += '![]({}){{ width="200" }}\n'.format( + os.path.join(os.path.basename(root), f)) + return imagelist_text, readme_path + + +def generate_md(course: str, filelist_texts: str, readme_path: str, topic: str): + final_texts = ['\n\n'.encode(), filelist_texts.encode()] + topic_path = os.path.join('docs', topic) + if not os.path.isdir(topic_path): + os.mkdir(topic_path) + with open(os.path.join(topic_path, '{}.md'.format(course)), 'wb') as file: + file.writelines(final_texts) + + +if __name__ == '__main__': + if os.path.exists('docs'): + shutil.rmtree('docs') + if not os.path.isdir('docs'): + os.mkdir('docs') + + topics = list( + filter(lambda x: os.path.isdir(x) and (x not in EXCLUDE_DIRS), + os.listdir('.'))) # list topics + + for topic in topics: + topic_path = os.path.join('.', topic) + target_path = os.path.join('docs', topic) + + if os.path.exists(target_path): + shutil.rmtree(target_path) + shutil.copytree(topic_path, target_path) + + courses = list( + filter( + lambda x: os.path.isdir(os.path.join(topic_path, x)) and + (x not in EXCLUDE_DIRS), os.listdir(topic_path))) # list courses + + for course in courses: + course_path = os.path.join(".", topic, course) + imagelist_text, readme_path = list_image(course_path) + generate_md(course, imagelist_text, readme_path, topic) + + with open('README.md', 'rb') as file: + mainreadme_lines = file.readlines() + + with open('docs/index.md', 'wb') as file: + file.writelines(mainreadme_lines) diff --git a/mkdocs.yml b/mkdocs.yml new file mode 100644 index 0000000..af32e6d --- /dev/null +++ b/mkdocs.yml @@ -0,0 +1,30 @@ +site_name: EmojiPackage + +theme: + name: material + custom_dir: overrides + features: + - navigation.indexes + +repo_url: https://github.com/getActivity/EmojiPackage + +markdown_extensions: + - def_list + - pymdownx.tasklist: + custom_checkbox: true + - attr_list + - md_in_html + - pymdownx.emoji: + emoji_index: !!python/name:materialx.emoji.twemoji + emoji_generator: !!python/name:materialx.emoji.to_svg + +plugins: + - search: + lang: + - en + - zh + +extra: + analytics: + provider: google + property: UA-145083103-1 diff --git a/overrides/main.html b/overrides/main.html new file mode 100644 index 0000000..a8b2333 --- /dev/null +++ b/overrides/main.html @@ -0,0 +1,17 @@ +{% extends "base.html" %} + +{% block disqus %} + +{% endblock %} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..24d631e --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +mkdocs==1.2.3 +mkdocs-material==7.3.6 +pymdown-extensions From 9613f598047d250a0b3dc8bb7d5010c657346e85 Mon Sep 17 00:00:00 2001 From: beiyuouo Date: Wed, 2 Mar 2022 14:15:18 +0800 Subject: [PATCH 2/3] Update main.yml --- .github/workflows/main.yml | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 57045f6..4bab384 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,4 +1,4 @@ -# This is a basic workflow to help you get started with Actions +# This is a basic workflow to help you get started with Actions name: CI @@ -6,7 +6,10 @@ name: CI # events but only for the master branch on: push: - branches: [ main ] + branches: [master] + pull_request: + branches: [master] + workflow_dispatch: # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: @@ -23,16 +26,16 @@ jobs: - name: Set up Python 3.x uses: actions/setup-python@v2 with: - python-version: 3.x + python-version: 3.x - name: Install dependencies run: | python -m pip install --upgrade pip pip install -r requirements.txt - + - name: Update dir run: python generate.py - + - name: Deploy docs uses: mhausenblas/mkdocs-deploy-gh-pages@nomaterial env: From 84e9217a7bd288ba7f60f8c11abac6c660a996cb Mon Sep 17 00:00:00 2001 From: beiyuouo Date: Wed, 2 Mar 2022 14:27:13 +0800 Subject: [PATCH 3/3] Update generate.py --- generate.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/generate.py b/generate.py index b3601ed..93c879e 100644 --- a/generate.py +++ b/generate.py @@ -10,6 +10,8 @@ INDEX_FILE = 'index.md' IMAGE_EXTS = ['jpg', 'png', 'svg', 'jpeg', 'gif', 'webp'] +IMAGE_URL_PREFIX = 'https://github.com/beiyuouo/hainanu-course-comments/blob/main/' +BIN_URL_PREFIX = 'https://github.com/beiyuouo/hainanu-course-comments/raw/main/' def list_image(course: str): @@ -21,8 +23,7 @@ def list_image(course: str): level = root.replace(course, '').count(os.sep) for f in files: if f.split('.')[-1].lower() in IMAGE_EXTS: - imagelist_text += '![]({}){{ width="200" }}\n'.format( - os.path.join(os.path.basename(root), f)) + imagelist_text += '![]({}){{ width="200" }}\n'.format(os.path.join(f)) return imagelist_text, readme_path @@ -53,15 +54,9 @@ def generate_md(course: str, filelist_texts: str, readme_path: str, topic: str): shutil.rmtree(target_path) shutil.copytree(topic_path, target_path) - courses = list( - filter( - lambda x: os.path.isdir(os.path.join(topic_path, x)) and - (x not in EXCLUDE_DIRS), os.listdir(topic_path))) # list courses - - for course in courses: - course_path = os.path.join(".", topic, course) - imagelist_text, readme_path = list_image(course_path) - generate_md(course, imagelist_text, readme_path, topic) + topic_path = os.path.join(".", topic) + imagelist_text, readme_path = list_image(topic_path) + generate_md('index', imagelist_text, readme_path, topic) with open('README.md', 'rb') as file: mainreadme_lines = file.readlines()