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] make the line "---\n" of the footer only present in the code, not in the footer
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`
- [X] also update the "/gemlog/index.gmi" with the list of articles
- [X] write the sync features
@ -33,11 +33,8 @@ be used as the title of the HTML page.
- [ ] write tests for it
- [ ] move config file to `~/.config` with placeholder values
- [X] check for a config file in `~/.config`
- [X] maybe save the old Bash script somewhere for posterity ?
- [X] 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
- [ ] maybe save the old Bash script somewhere for posterity ?
- [ ] reorganise the directory where the content is
## My setup

View file

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

View file

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

View file

@ -1,4 +1,3 @@
import logging
import typer
import re
import shutil
@ -17,13 +16,13 @@ app = typer.Typer(help="generates your gmi and html files")
@app.command("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_footer(GMI_FOOTER, DIR_SOURCE)
logging.info(" Done!")
print("Done!")
def make_index(listing: list[Path]):
logging.info(" Populating gemlog index ...")
print("Populating gemlog index ...")
title, date = "", ""
files = (DIR_SOURCE / "gemlog").rglob(r'*.gmi')
files = [ \
@ -33,7 +32,7 @@ def make_index(listing: list[Path]):
articles = ""
for file in files[::-1]:
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}"
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:
footer = file.read()
logging.info(" Adding footers...")
print("Adding footers...")
files = directory.rglob(r'*.gmi')
for file in files:
with open(file, "rb+") as f:
@ -61,8 +60,8 @@ def make_footer(footer_path: Path, directory: Path, footer_str: str ="\n---\n"):
break
f.write((footer_str + footer).encode("utf-8"))
f.truncate()
logging.info(f" \u2713 {file}")
logging.info(" Footers added !")
print(f"\u2713 {file}")
print("Footers added !")
def find_title_date(filename: Path):
title, date = "", filename.stem[:10]
@ -78,9 +77,9 @@ def gen_html():
for file in files:
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)
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.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:
contents, title = process_file(gemlog)
@ -123,9 +122,7 @@ def process_file(ifile: TextIOWrapper):
continue
else:
newline, n = re.subn(r'^```.*', "<pre>", newline)
if n:
in_preformated = True
continue
in_preformated = True if n else False
if re.match(r'^[a-zA-Z]', newline):
newline = "<p>" + newline.strip() + "</p>\n"

View file

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

View file

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