Compare commits

..

No commits in common. "f1e41fc4d0c9e758dbbae13398c964f2e4b888ad" and "aab9cfff0bf69aefa0f625224dd12a688276581c" have entirely different histories.

6 changed files with 30 additions and 54 deletions

View file

@ -25,7 +25,7 @@ be used as the title of the HTML page.
- [X] write the `gen_gmi` function - [X] write the `gen_gmi` function
- [X] make the line "---\n" of the footer only present in the code, not in the footer - [X] make the line "---\n" of the footer only present in the code, not in the footer
file file
- [X] make a config file for things like - [-] make a config file for things like
- certain variables should be made `CONSTANT` in the `__init__.py` - certain variables should be made `CONSTANT` in the `__init__.py`
- [X] also update the "/gemlog/index.gmi" with the list of articles - [X] also update the "/gemlog/index.gmi" with the list of articles
- [X] write the sync features - [X] write the sync features
@ -33,11 +33,8 @@ be used as the title of the HTML page.
- [ ] write tests for it - [ ] write tests for it
- [ ] move config file to `~/.config` with placeholder values - [ ] move config file to `~/.config` with placeholder values
- [X] check for a config file in `~/.config` - [X] check for a config file in `~/.config`
- [X] maybe save the old Bash script somewhere for posterity ? - [ ] maybe save the old Bash script somewhere for posterity ?
- [X] reorganise the directory where the content is - [ ] reorganise the directory where the content is
- [X] replace `print`s with `logging`
- [ ] make a call graph
- [ ] rewrite `process_file` to make it easier to test
## My setup ## My setup

View file

@ -3,7 +3,7 @@ requires = ["hatchling"]
build-backend = "hatchling.build" build-backend = "hatchling.build"
[project] [project]
name = "gmirator" name = "Gmirator"
version = "0.0.1" version = "0.0.1"
authors = [ authors = [
{ name="Steven GIBONE", email="steven.gibone@zaclys.net" }, { name="Steven GIBONE", email="steven.gibone@zaclys.net" },

View file

@ -1,10 +1,7 @@
import logging
from pathlib import Path from pathlib import Path
import json import json
logging.basicConfig(level=logging.INFO, format='%(message)s')
xdg_path = Path("~/.config/gmirator/config.json").expanduser() xdg_path = Path("~/.config/gmirator/config.json").expanduser()
cfg_file = xdg_path if xdg_path.exists() else "config.json" cfg_file = xdg_path if xdg_path.exists() else "config.json"
with open(cfg_file) as f: with open(cfg_file) as f:

View file

@ -1,4 +1,3 @@
import logging
import typer import typer
import re import re
import shutil import shutil
@ -17,13 +16,13 @@ app = typer.Typer(help="generates your gmi and html files")
@app.command("gmi") @app.command("gmi")
def gen_gmi(): def gen_gmi():
logging.info(" Generating GMI files...") print("Generating GMI files...")
make_index([DIR_SOURCE / "index.gmi", DIR_SOURCE / "gemlog/index.gmi"]) make_index([DIR_SOURCE / "index.gmi", DIR_SOURCE / "gemlog/index.gmi"])
make_footer(GMI_FOOTER, DIR_SOURCE) make_footer(GMI_FOOTER, DIR_SOURCE)
logging.info(" Done!") print("Done!")
def make_index(listing: list[Path]): def make_index(listing: list[Path]):
logging.info(" Populating gemlog index ...") print("Populating gemlog index ...")
title, date = "", "" title, date = "", ""
files = (DIR_SOURCE / "gemlog").rglob(r'*.gmi') files = (DIR_SOURCE / "gemlog").rglob(r'*.gmi')
files = [ \ files = [ \
@ -33,7 +32,7 @@ def make_index(listing: list[Path]):
articles = "" articles = ""
for file in files[::-1]: for file in files[::-1]:
title, date = find_title_date(file) title, date = find_title_date(file)
logging.info(f" \u2713 {date} {title.strip()}") print(f"\u2713 {date} {title.strip()}")
articles += f"=> /gemlog/{file.stem}.gmi {date} - {title}" articles += f"=> /gemlog/{file.stem}.gmi {date} - {title}"
for path in listing: for path in listing:
@ -51,7 +50,7 @@ def make_footer(footer_path: Path, directory: Path, footer_str: str ="\n---\n"):
with open(footer_path) as file: with open(footer_path) as file:
footer = file.read() footer = file.read()
logging.info(" Adding footers...") print("Adding footers...")
files = directory.rglob(r'*.gmi') files = directory.rglob(r'*.gmi')
for file in files: for file in files:
with open(file, "rb+") as f: with open(file, "rb+") as f:
@ -61,8 +60,8 @@ def make_footer(footer_path: Path, directory: Path, footer_str: str ="\n---\n"):
break break
f.write((footer_str + footer).encode("utf-8")) f.write((footer_str + footer).encode("utf-8"))
f.truncate() f.truncate()
logging.info(f" \u2713 {file}") print(f"\u2713 {file}")
logging.info(" Footers added !") print("Footers added !")
def find_title_date(filename: Path): def find_title_date(filename: Path):
title, date = "", filename.stem[:10] title, date = "", filename.stem[:10]
@ -78,9 +77,9 @@ def gen_html():
for file in files: for file in files:
gmi2html(file.as_posix()) gmi2html(file.as_posix())
logging.info("---\nDone processing gmi files") print("---\nDone processing gmi files\n")
logging.info("Copying static files...\n") print("\nCopying static files...\n")
shutil.copytree(DIR_ASSETS, DIR_O_HTML, dirs_exist_ok=True) shutil.copytree(DIR_ASSETS, DIR_O_HTML, dirs_exist_ok=True)
def gmi2html(file: str): def gmi2html(file: str):
@ -88,7 +87,7 @@ def gmi2html(file: str):
new_file_path = Path(new_file_path).with_suffix('.html') new_file_path = Path(new_file_path).with_suffix('.html')
new_file_path.parent.mkdir(parents=True, exist_ok=True) new_file_path.parent.mkdir(parents=True, exist_ok=True)
logging.info(f"Processing: {new_file_path}") print(f"Processing: {new_file_path}")
with open(file, 'r') as gemlog: with open(file, 'r') as gemlog:
contents, title = process_file(gemlog) contents, title = process_file(gemlog)
@ -123,9 +122,7 @@ def process_file(ifile: TextIOWrapper):
continue continue
else: else:
newline, n = re.subn(r'^```.*', "<pre>", newline) newline, n = re.subn(r'^```.*', "<pre>", newline)
if n: in_preformated = True if n else False
in_preformated = True
continue
if re.match(r'^[a-zA-Z]', newline): if re.match(r'^[a-zA-Z]', newline):
newline = "<p>" + newline.strip() + "</p>\n" newline = "<p>" + newline.strip() + "</p>\n"

View file

@ -1,14 +1,14 @@
import typer import typer
import gmirator.generate import generate
import gmirator.synchronise import synchronise
app = typer.Typer( app = typer.Typer(
add_completion=False, add_completion=False,
help="gmirator helps you publish in both gmi and html" help="gmirator helps you publish in both gmi and html"
) )
app.add_typer(gmirator.generate.app, name='gen') app.add_typer(generate.app, name='gen')
app.add_typer(gmirator.synchronise.app, name='sync') app.add_typer(synchronise.app, name='sync')
if __name__ == "__main__": if __name__ == "__main__":
app() app()

View file

@ -1,5 +1,3 @@
from typing import Annotated
import logging
import typer import typer
import subprocess import subprocess
import shlex import shlex
@ -14,31 +12,18 @@ from gmirator import (
app = typer.Typer(help="syncs your files with configured remote") app = typer.Typer(help="syncs your files with configured remote")
@app.command("gmi") @app.command("gmi")
def sync_gmi( def sync_gmi():
dry_run: Annotated[ print("Synchronising GMI content with remote...")
bool, typer.Option(help="dry runs the synchronisation") print(f'rsync -aP -e "ssh -p {RPORT}" {DIR_SOURCE} {REM_GMI}')
] = False subprocess.call(
): shlex.split(f'rsync -aP -e "ssh -p {RPORT}" {DIR_SOURCE} {REM_GMI}')
logging.info(" Start synchronising GMI content with remote...") )
sync_with_remote(RPORT, DIR_SOURCE, REM_GMI, dry_run) print("Done!")
logging.info(" Synchronisation of Gemini content with remote : done!")
@app.command("html") @app.command("html")
def sync_html( def sync_html():
dry_run: Annotated[ print("Synchronising HTML content with remote...")
bool, typer.Option(help="dry runs the synchronisation")
] = False
):
logging.info(" Start synchronising HTML content with remote...")
sync_with_remote(RPORT, DIR_O_HTML, REM_HTML, dry_run)
logging.info(" Synchronisation of HTML content with remote: done!")
def sync_with_remote(port: str, source, dest, dry_run: bool):
if dry_run:
rsync_command = f'rsync -aP --dry-run -e "ssh -p {port}" {source} {dest}'
else:
rsync_command = f'rsync -aP -e "ssh -p {port}" {source} {dest}'
logging.info(" " + rsync_command)
subprocess.call( subprocess.call(
shlex.split(rsync_command) shlex.split(f'rsync -aP -e "ssh -p {RPORT}" {DIR_O_HTML} {REM_HTML}')
) )
print("Done!")