From 9897b80e34bb64b138a340acd37a44c2ab02c17c Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Sun, 23 Jan 2022 02:50:42 +0700 Subject: Draft basic utilities --- .gitignore | 1 + pyproject.toml | 4 ++-- src/rub.py | 1 - src/rub/__init__.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/rub/__main__.py | 21 +++++++++++++++++++++ 5 files changed, 72 insertions(+), 3 deletions(-) delete mode 100644 src/rub.py create mode 100644 src/rub/__init__.py create mode 100644 src/rub/__main__.py diff --git a/.gitignore b/.gitignore index 849ddff..dbb91d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ dist/ +eg/ diff --git a/pyproject.toml b/pyproject.toml index e73aab8..6f6167b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi" [project] name = "rub" -version = "0.0.0" +version = "0.0.1" description = "A static generator" readme = "README.md" requires-python = ">=3.7" @@ -18,5 +18,5 @@ classifiers = [ "Operating System :: OS Independent", "Programming Language :: Python", "Topic :: Utilities" ] -dependencies = [ "markdown-it-py" ] +dependencies = [ "doit", "Jinja2", "markdown-it-py[linkify,plugins]" ] urls = { SourceHut = "https://sr.ht/~cnx/rub" } diff --git a/src/rub.py b/src/rub.py deleted file mode 100644 index 4c91ec4..0000000 --- a/src/rub.py +++ /dev/null @@ -1 +0,0 @@ -from markdown_it import MarkdownIt diff --git a/src/rub/__init__.py b/src/rub/__init__.py new file mode 100644 index 0000000..dd963e8 --- /dev/null +++ b/src/rub/__init__.py @@ -0,0 +1,48 @@ +# Package initialization +# Copyright (C) 2022 Nguyễn Gia Phong +# +# This file is part of rub. +# +# Rub is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Rub is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with rub. If not, see . + +from os import walk +from os.path import join +from pathlib import Path +from shutil import copytree, rmtree + +from jinja2 import Environment +from markdown_it import MarkdownIt +from mdit_py_plugins.footnote import footnote_plugin + +__all__ = ['glob_files', 'markdown', 'replace'] +md = MarkdownIt('gfm-like').use(footnote_plugin) +jinja = Environment() + + +def glob_files(root: Path, suffix: str = '') -> list[Path]: + """Return the list of all files in given directory, recursively.""" + return [Path(path)/file for path, dirs, files in walk(root) + for file in files if file.endswith(suffix)] + + +def markdown(source: Path, destination: Path) -> None: + """Convert source Markdown to destination HTML segment.""" + template = jinja.from_string(source.read_text()) + destination.write_text(md.render(template.render())) + + +def replace(source: Path, destination: Path) -> None: + """Replace destination with source directory.""" + rmtree(destination, ignore_errors=True) + copytree(source, destination, dirs_exist_ok=True) diff --git a/src/rub/__main__.py b/src/rub/__main__.py new file mode 100644 index 0000000..8c17c86 --- /dev/null +++ b/src/rub/__main__.py @@ -0,0 +1,21 @@ +# Package's executable +# Copyright (C) 2022 Nguyễn Gia Phong +# +# This file is part of rub. +# +# Rub is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Rub is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with rub. If not, see . + +from doit import run + +if __name__ == '__main__': run(globals()) -- cgit 1.4.1