about summary refs log tree commit diff
path: root/src/rub/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/rub/__init__.py')
-rw-r--r--src/rub/__init__.py62
1 files changed, 45 insertions, 17 deletions
diff --git a/src/rub/__init__.py b/src/rub/__init__.py
index dd963e8..904f47a 100644
--- a/src/rub/__init__.py
+++ b/src/rub/__init__.py
@@ -1,5 +1,5 @@
 # Package initialization
-# Copyright (C) 2022  Nguyễn Gia Phong
+# Copyright (C) 2022-2023  Nguyễn Gia Phong
 #
 # This file is part of rub.
 #
@@ -16,33 +16,61 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with rub.  If not, see <https://www.gnu.org/licenses/>.
 
+from functools import cached_property
 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
+from rub import xml
 
-__all__ = ['glob_files', 'markdown', 'replace']
-md = MarkdownIt('gfm-like').use(footnote_plugin)
-jinja = Environment()
+__all__ = ['Rubber', 'xml']
 
 
-def glob_files(root: Path, suffix: str = '') -> list[Path]:
+def glob_files(root: Path, suffix=''):
     """Return the list of all files in given directory, recursively."""
-    return [Path(path)/file for path, dirs, files in walk(root)
+    return [Path(path).relative_to(root)/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:
+def replace(source: Path, destination: Path):
     """Replace destination with source directory."""
     rmtree(destination, ignore_errors=True)
     copytree(source, destination, dirs_exist_ok=True)
+
+
+class Rubber:
+    """Static generator."""
+
+    def __init__(self, generate_article, base, src, cache, out):
+        self.generate_article = generate_article
+        self.base, self.src = base, src
+        self.cache, self.out = cache, out
+
+    @cached_property
+    def tasks(self):
+        def assox():
+            for k in dir(self):
+                if not k.startswith('task_'): continue
+                v = getattr(self, k)
+                if callable(v): yield k, v
+
+        return dict(assox())
+
+    def task_base(self):
+        paths = glob_files(self.base)
+        return {'doc': 'copy base directory',
+                'file_dep': [self.base/path for path in paths],
+                'actions': [(replace, [self.base, self.out])],
+                'targets': [self.out/path for path in paths],
+                'clean': True}
+
+    def task_articles(self):
+        """process articles into XHTML"""
+        for path in glob_files(self.src, '.xml'):
+            source = self.src / path
+            destination = self.out / path
+            yield {'name': path, 'doc': f'process {path} into XHTML',
+                   'file_dep': [source],
+                   'actions': [(self.generate_article, [source, destination])],
+                   'targets': [destination], 'clean': True}