Merge branch 'hy-rewrite' into hexo

This commit is contained in:
SouthFox 2025-01-09 16:07:09 +08:00
commit c120d09225
3 changed files with 129 additions and 18 deletions

View file

@ -13,23 +13,22 @@ jobs:
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Set up Python 3.9 - name: Set up Python 3.13
uses: actions/setup-python@v2 uses: actions/setup-python@v5
with: with:
python-version: 3.9 python-version: 3.13
- name: Install dependencies - name: Install dependencies
run: | run: |
sudo apt-get install wget sudo apt-get install wget
python -m pip install --upgrade pip python -m pip install fonttools brotli requests hy
python -m pip install fonttools brotli requests
git config --global user.name "SouthFox-D" git config --global user.name "SouthFox-D"
git config --global user.email "southfoxdreamer@gmail.com" git config --global user.email "southfoxdreamer@gmail.com"
- name: Check Img - name: Check Img
run: | run: |
wget https://raw.githubusercontent.com/SouthFox-D/blog_img/main/imgList.json wget https://raw.githubusercontent.com/SouthFox-D/blog_img/main/imgList.json
python3 utils.py -b hy utils.hy -b
- name: Backup - name: Backup
env: env:

View file

@ -13,20 +13,15 @@ jobs:
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Set up Python 3.9 - name: Set up Python 3.13
uses: actions/setup-python@v2 uses: actions/setup-python@v5
with: with:
python-version: 3.9 python-version: 3.13
- name: Set up Node.js
uses: actions/setup-node@v1
with:
node-version: 17
- name: Install dependencies - name: Install dependencies
run: | run: |
python -m pip install --upgrade pip sudo apt-get install wget
python -m pip install fonttools brotli requests python -m pip install fonttools brotli requests hy
npm install -g hexo-cli npm install -g hexo-cli
npm install npm install
git config --global user.name "SouthFox-D" git config --global user.name "SouthFox-D"
@ -39,7 +34,7 @@ jobs:
run: | run: |
hexo clean hexo clean
hexo generate hexo generate
python3 utils.py -d hy utils.hy -d
pyftsubset Zpix.ttf --text-file=strdb.txt pyftsubset Zpix.ttf --text-file=strdb.txt
fonttools ttLib.woff2 compress -o Zpix.woff2 Zpix.subset.ttf fonttools ttLib.woff2 compress -o Zpix.woff2 Zpix.subset.ttf
mv Zpix.woff2 ./public/css/fonts mv Zpix.woff2 ./public/css/fonts

117
utils.hy Normal file
View file

@ -0,0 +1,117 @@
#!/usr/bin/env hy
(import requests)
(import argparse)
(import sys)
(import os)
(import json)
(import getopt)
(import re)
(import random)
(import datetime [datetime])
(defmacro -> [head #* args]
(setv ret head)
(for [node args]
(setv ret (if (isinstance node hy.models.Expression)
`(~(get node 0) ~ret ~@(rest node))
`(~node ~ret))))
ret)
(defmacro lfor-> [name head #* rest]
(setv ret head)
(for [node rest]
(setv ret `(lfor ~name ~ret ~node)))
ret)
(defn get-post-files []
(setv markdown-files [])
(for [[root dirs files] (os.walk "source")
-file files]
(let [file (os.path.join root -file)]
(when (and (in "posts" file) (file.endswith ".md"))
(markdown-files.append file))))
(return markdown-files))
(defn subset-font-file [post-files]
(let [str-set #{}]
(for [f post-files]
(with [o (open f "r" :encoding "utf-8")]
(for [w (o.read)]
(str-set.add w))))
(print f"{(len str-set)} characters in all files.")
(print (.join "" str-set))
(with [f (open "strdb.txt" "w")]
(f.write (.join "" str-set)))))
(defn add-github-discussion [post-files]
(let [repo-id "MDEwOlJlcG9zaXRvcnkyMjg3NDM0MjQ="
category-id "DIC_kwDODaJZAM4CA7bf"
url "https://api.github.com/graphql"
header {"Authorization" f"Bearer {(os.environ.get "DISCUSSIONS_TOKEN")}"}
query-data {"query" "query{repository(owner: \"southfox-d\", name: \"southfox-d.github.io\"){id discussions(first:100, categoryId: \"DIC_kwDODaJZAM4CA7bf\"){nodes{title}}}}"}
query-request (requests.post url :headers header :json query-data)]
(query-request.raise_for_status)
(let [current-year (str (. (datetime.now) year))
discussion-post (-> (r.json) :data :repository :discussions :nodes)
discussion-post-titles (lfor x discussion-post (:title x))
local-post-titles (lfor-> it post-files
(. (it.split "_posts/") [-1])
(it.strip ".md")
(+ it "/"))]
(for [post-title local-post-titles]
(when (not-in current-year post-title)
(continue))
(when (not-in post-title discussion-post-titles)
(let [create-data {"query" f"mutation{{createDiscussion(input: {{repositoryId: \"{repo-id}\", categoryId: \"{category-id}\", body: \"{(+ "https://blog.southfox.me/" post-title)}\", title: \"{post-title}\" }}) {{discussion {{id}}}}}}"}
create-request (requests.post url :headers header :json create-data)]
(create-request.raise_for_status)
(print f"Create post {post-title} discussion!")))))))
(defn download-ipfs-img [img-list]
(let [ipfs-gateway ["https://ipfs.io/ipfs/" "https://dweb.link/ipfs/"]]
(for [img-name img-list]
(when (= "" img-name)
(continue))
(setv img-url (+ (random.choice ipfs-gateway) img-name))
(setv r (requests.get img-url))
(r.raise_for_status)
(with [f (open (+ "./newimg/" img-name) "wb")]
(f.write (. r content))))))
(defn backup-ipfs-img [post-files]
(if (os.path.exists "imgList.json")
(setv img-json (with [f (open "imgList.json" "r" :encoding "utf-8")]
(json.load f)))
(setv img-json {"img" []}))
(setv now-img-list [])
(for [post-file post-files]
(with [f (open post-file "r" :encoding "utf-8")]
(let [post-text (f.read)
img-result (re.findall r"!\[(.*?)\]\((.*?)\)" post-text)]
(for [[_ img-url] img-result]
(when (= "" img-url)
(continue))
(when (in "ipfs" img-url)
(now-img-list.append (. (img_url.split "/") [-1])))))))
(setv download-need-img (list (. (set now-img-list) (difference (set (:img img-json))))))
(when download-need-img
(with [f (open "newimg/imgList.json" "w" :encoding "utf-8")]
(json.dump {"img" (sorted now-img-list)} f))
(download-ipfs-img download-need-img)))
(setv parser (argparse.ArgumentParser))
(parser.add_argument "-d" :dest "deploy" :action "store_true")
(parser.add_argument "-b" :dest "backup" :action "store_true")
(setv args (parser.parse_args))
(let [post-files (get-post-files)]
(when args.deploy
(subset-font-file post-files)
(add-github-discussion post-files))
(when args.backup
(os.makedirs "newimg" :exist_ok True)
(backup-ipfs-img post-files)))