diff options
| author | Christoph Schlosser <christoph@linux.com> | 2025-08-24 17:10:11 +0200 |
|---|---|---|
| committer | Christoph Schlosser <christoph@linux.com> | 2025-08-24 17:10:11 +0200 |
| commit | 9bd3fd1bce788dce610ad7276ce1f1634019c380 (patch) | |
| tree | 59898b74445bf4a9e751b4b7bec46f85a7ad43d8 /g2h | |
| parent | 66dfe0e665ebeb0dce1ff290b77245e4a9fadd88 (diff) | |
| download | git2hugo-9bd3fd1bce788dce610ad7276ce1f1634019c380.tar.gz | |
g2h: Add unit tests and restructure class layout
Instead of having a large generator class use individual pages
Diffstat (limited to 'g2h')
| -rw-r--r-- | g2h/__init__.py | 0 | ||||
| -rw-r--r-- | g2h/cli.py | 8 | ||||
| -rw-r--r-- | g2h/gen.py | 49 | ||||
| -rw-r--r-- | g2h/log_page.py | 43 | ||||
| -rw-r--r-- | g2h/overview_page.py | 15 | ||||
| -rw-r--r-- | g2h/page.py | 18 | ||||
| -rw-r--r-- | g2h/page_factory.py | 13 |
7 files changed, 93 insertions, 53 deletions
diff --git a/g2h/__init__.py b/g2h/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/g2h/__init__.py @@ -2,7 +2,7 @@ import argparse import os from .git import Git -from .gen import Generator +from .page_factory import PageFactory def parse_args(): parser = argparse.ArgumentParser( @@ -25,9 +25,9 @@ def main(): project_name = args.name if args.name is not None else os.path.basename(args.repo_path) tags = git.get_tags() - gen = Generator(args.out_dir) - gen.render_overview(project_name, branches, tags) - gen.render_log(project_name, commits, branches, tags) + factory = PageFactory(args.out_dir, project_name) + factory.new_overview(branches, tags).render() + factory.new_log(commits, branches, tags).render() if __name__ == '__main__': main() diff --git a/g2h/gen.py b/g2h/gen.py deleted file mode 100644 index 9591d87..0000000 --- a/g2h/gen.py +++ /dev/null @@ -1,49 +0,0 @@ -from jinja2 import Environment, FileSystemLoader -import os - -def ref_names_for_commit(refs, commit_sha): - ref_names = [] - for ref in refs: - if ref.commit.hexsha == commit_sha: - ref_names.append(ref.name) - - return ref_names - -class Generator: - def __init__(self, output_dir): - self.output_dir = output_dir - template_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'templates')) - self.templates = Environment(loader=FileSystemLoader(template_dir)) - - def _render(self, template, context, out_path): - template = self.templates.get_template(template) - content = template.render(context) - out_file = os.path.join(self.output_dir, out_path) - os.makedirs(os.path.dirname(out_file), exist_ok=True) - with open(out_file, 'w', encoding='utf-8') as f: - f.write(content) - - def render_log(self, name, commits, branches, tags): - commits_context = [] - for commit in commits: - refs = ref_names_for_commit(branches, commit.hexsha) + ref_names_for_commit(tags, commit.hexsha) - commit_context = { - 'committed_datetime': commit.committed_datetime, - 'refs': refs, - 'summary': commit.summary - } - commits_context.append(commit_context) - - context = { - 'commits': commits_context, - 'title': name + ': Log' - } - self._render('log.md', context, 'l.md') - - def render_overview(self, name, branches, tags): - context = { - 'branches': branches, - 'tags': tags, - 'title': name - } - self._render('overview.md', context, '_index.md') diff --git a/g2h/log_page.py b/g2h/log_page.py new file mode 100644 index 0000000..684a758 --- /dev/null +++ b/g2h/log_page.py @@ -0,0 +1,43 @@ +import os + +from .page import Page + +def ref_names_for_commit(refs, commit_sha): + ref_names = [] + if refs is None: + return ref_names + for ref in refs: + if ref.commit.hexsha == commit_sha: + ref_names.append(ref.name) + + return ref_names + +class LogPage(Page): + def __init__(self, out_dir, name, commits, branches, tags): + super(LogPage, self).__init__('log.md', out_dir, 'l.md', name) + self.commits = commits + self.branches = branches + self.tags = tags + + def _commits_with_refs(self): + commits_context = [] + for commit in self.commits: + refs = ref_names_for_commit(self.branches, commit.hexsha) + ref_names_for_commit(self.tags, commit.hexsha) + commit_context = { + 'committed_datetime': commit.committed_datetime, + 'refs': refs, + 'summary': commit.summary + } + commits_context.append(commit_context) + return commits_context + + def context(self): + commits_context = [] + if self.commits is not None: + commits_context = self._commits_with_refs() + + return { + 'commits': commits_context, + 'title': self.name + ': Log' + } + diff --git a/g2h/overview_page.py b/g2h/overview_page.py new file mode 100644 index 0000000..1693b9a --- /dev/null +++ b/g2h/overview_page.py @@ -0,0 +1,15 @@ +from .page import Page + +class OverviewPage(Page): + def __init__(self, out_dir, name, branches, tags): + super(OverviewPage, self).__init__('overview.md', out_dir, '_index.md', name) + self.branches = branches + self.tags = tags + + def context(self): + return { + 'branches': self.branches, + 'tags': self.tags, + 'title': self.name + } + diff --git a/g2h/page.py b/g2h/page.py new file mode 100644 index 0000000..44f97ee --- /dev/null +++ b/g2h/page.py @@ -0,0 +1,18 @@ +from jinja2 import Environment, FileSystemLoader +import os + +class Page: + def __init__(self, template_file, out_dir, out_file, name): + self.out_file_path = os.path.join(out_dir, out_file) + self.name = name + template_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'templates')) + self.template = Environment(loader=FileSystemLoader(template_dir)).get_template(template_file) + + def context(self): + raise NotImplementedError("Must override context") + + def render(self): + content = self.template.render(self.context()) + os.makedirs(os.path.dirname(self.out_file_path), exist_ok=True) + with open(self.out_file_path, 'w', encoding='utf-8') as f: + f.write(content) diff --git a/g2h/page_factory.py b/g2h/page_factory.py new file mode 100644 index 0000000..dae9675 --- /dev/null +++ b/g2h/page_factory.py @@ -0,0 +1,13 @@ +from .overview_page import OverviewPage +from .log_page import LogPage + +class PageFactory: + def __init__(self, out_dir, name): + self.out_dir = out_dir + self.name = name + + def new_overview(self, branches, tags): + return OverviewPage(self.out_dir, self.name, branches, tags) + + def new_log(self, commits, branches, tags): + return LogPage(self.out_dir, self.name, commits, branches, tags) |