configure local testing

pull/2/head
Yax 3 years ago
parent d0871c53cd
commit 40d1c7a867

1
.gitignore vendored

@ -8,3 +8,4 @@ htmlcov
venv
.vscode
posts/images
.idea/

@ -4,7 +4,10 @@ site:
serve: site
cd _site && python -m SimpleHTTPServer 2> /dev/null || python3 -m http.server
dock: site
site_local:
./makesite.py --params params-local.json
dock: site_local
docker run --name bloglocal -p 8000:8000 -v `pwd`/_site:/usr/share/nginx/html:ro -v `pwd`/local-nginx.conf:/etc/nginx/nginx.conf:ro nginx
undock:
@ -34,49 +37,4 @@ clean:
find . -name "*.pyc" -exec rm {} +
rm -rf .coverage htmlcov
REV = cat /tmp/rev.txt
example:
#
# Remove existing output directories.
rm -rf _site /tmp/_site
#
# Create params.json for makesite-demo.
echo '{ "base_path": "/makesite-demo", "site_url":' \
'"https://tmug.github.io/makesite-demo" }' > params.json
#
# Generate the website.
. ./venv && ./makesite.py
rm params.json
#
# Get current commit ID.
git rev-parse --short HEAD > /tmp/rev.txt
#
# Write a README for makesite-demo repository.
echo makesite.py demo > _site/README.md
echo ================ >> _site/README.md
echo This is the HTML/CSS source of an example static >> _site/README.md
echo website auto-generated with [sunainapai/makesite][makesite] >> _site/README.md
echo "([$$($(REV))][commit])". >> _site/README.md
echo >> _site/README.md
echo Visit "<https://tmug.github.io/makesite-demo>" to >> _site/README.md
echo view the example website. >> _site/README.md
echo >> _site/README.md
echo [makesite]: https://github.com/sunainapai/makesite >> _site/README.md
echo [commit]: https://github.com/sunainapai/makesite/commit/$$($(REV)) >> _site/README.md
echo [demo]: https://tmug.github.io/makesite-demo >> _site/README.md
#
# Publish makesite-demo.
mv _site /tmp
cd /tmp/_site && git init
cd /tmp/_site && git config user.name Makesite
cd /tmp/_site && git config user.email makesite@example.com
cd /tmp/_site && git add .
cd /tmp/_site && git commit -m "Auto-generated with sunainapai/makesite - $$($(REV))"
cd /tmp/_site && git remote add origin https://github.com/tmug/makesite-demo.git
cd /tmp/_site && git log
cd /tmp/_site && git push -f origin master
loc:
grep -vE '^[[:space:]]*#|^[[:space:]]*$$|^[[:space:]]*"""' makesite.py | wc -l
FORCE:

@ -27,35 +27,36 @@
"""Make static website/blog with Python."""
import sys
import argparse
import datetime
import json
import locale
import os
import shutil
import re
import glob
import json
import datetime
import shutil
import sys
import time
import unicodedata
from email import utils
from pathlib import Path
import unicodedata
import locale
import requests
import mistune
import requests
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import html
from pygments.lexers import get_lexer_by_name
# set user locale
locale.setlocale(locale.LC_ALL, "")
# initialize markdown
class HighlightRenderer(mistune.Renderer):
options = {"escape": False, "hard_wrap": True}
def block_code(self, code, lang):
def block_code(self, code, lang=None):
if not lang:
return "\n<pre><code>%s</code></pre>\n" % mistune.escape(code)
lexer = get_lexer_by_name(lang, stripall=True)
@ -256,7 +257,7 @@ def make_posts(
comments = resp.json()["data"]
out_comments = []
for comment in comments:
site=comment.get("site", "")
site = comment.get("site", "")
if site:
site_start = '<a href="' + site + '">'
site_end = '</a>'
@ -337,7 +338,7 @@ def make_list(
fwrite(dst_path, output)
def main():
def main(param_file):
# Create a new _site directory from scratch.
if os.path.isdir("_site"):
shutil.rmtree("_site")
@ -353,9 +354,9 @@ def main():
"stacosys_url": "",
}
# If params.json exists, load it.
if os.path.isfile("params.json"):
params.update(json.loads(fread("params.json")))
log("use params from " + param_file)
if os.path.isfile(param_file):
params.update(json.loads(fread(param_file)))
# Load layouts.
banner_layout = fread("layout/banner.html")
@ -394,7 +395,7 @@ def main():
# Create blog list pages.
page_size = 10
chunk_posts = [
blog_posts[i : i + page_size] for i in range(0, len(blog_posts), page_size)
blog_posts[i: i + page_size] for i in range(0, len(blog_posts), page_size)
]
page = 1
last_page = len(chunk_posts)
@ -429,17 +430,17 @@ def main():
page = page + 1
# Create category pages
catpost = {}
cat_post = {}
for post in blog_posts:
for cat in post["categories"]:
if cat in catpost:
catpost[cat].append(post)
if cat in cat_post:
cat_post[cat].append(post)
else:
catpost[cat] = [post]
for cat in catpost.keys():
cat_post[cat] = [post]
for cat in cat_post.keys():
params["category"] = cat
make_list(
catpost[cat],
cat_post[cat],
"_site/" + slugify(cat) + "/index.html",
list_layout,
item_nosummary_layout,
@ -472,17 +473,17 @@ def main():
)
# Create RSS feed by tag
tagpost = {}
tag_post = {}
for post in blog_posts:
for tag in post["tags"]:
if tag in tagpost:
tagpost[tag].append(post)
if tag in tag_post:
tag_post[tag].append(post)
else:
tagpost[tag] = [post]
for tag in tagpost.keys():
tag_post[tag] = [post]
for tag in tag_post.keys():
params["tag"] = tag
make_list(
tagpost[tag],
tag_post[tag],
"_site/rss." + slugify(tag) + ".xml",
rss_xml,
rss_item_xml,
@ -506,6 +507,8 @@ def main():
# Test parameter to be set temporarily by unit tests.
_test = None
if __name__ == "__main__":
main()
parser = argparse.ArgumentParser(description='Makesite')
parser.add_argument('--params', dest='param_file', type=str, default="params.json", help='Custom param file')
args = parser.parse_args()
main(args.param_file)

@ -7,11 +7,13 @@ import requests
import time
import json
def fread(filename):
"""Read file and close the file."""
with open(filename, "r") as f:
return f.read()
def get_nb_of_comments():
req_url = params["stacosys_url"] + "/comments/count"
query_params = dict(
@ -20,9 +22,11 @@ def get_nb_of_comments():
resp = requests.get(url=req_url, params=query_params)
return 0 if not resp.ok else int(resp.json()["count"])
def exit_program():
sys.exit(0)
# Default parameters.
params = {
"stacosys_token": "",

@ -0,0 +1,8 @@
{
"title": "Le blog du Yax",
"subtitle": "GNU, Linux, BSD et autres libertés",
"author": "Yax",
"site_url": "http://127.0.0.1:8000",
"stacosys_url": "http://127.0.0.1:8100",
"external_check": "./check_git.sh"
}
Loading…
Cancel
Save