Skip to content

Commit 2d7aac6

Browse files
committed
Add CLI to generate the site map.
1 parent 79830e6 commit 2d7aac6

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/render_engine_cli/cli.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import click
66
from dateutil import parser as dateparser
77
from dateutil.parser import ParserError
8-
from render_engine import Collection
8+
from render_engine import Collection, Page
9+
from render_engine.site_map import SiteMap
910
from rich.console import Console
1011

1112
from render_engine_cli.event import ServerEventHandler
@@ -396,5 +397,42 @@ def templates(module_site: str, theme_name: str, filter_value: str):
396397
)
397398

398399

400+
@app.command
401+
@click.option(
402+
"--module-site",
403+
type=click.STRING,
404+
# help="The module (python file) and site (the Site object) for your site in the format module:site",
405+
callback=validate_module_site,
406+
)
407+
@click.option(
408+
"-x",
409+
"--xml",
410+
help="Generate site map as XML",
411+
type=click.BOOL,
412+
is_flag=True,
413+
default=False,
414+
)
415+
@click.option("-o", "--output", help="File to output site map to.", type=click.Path(exists=False))
416+
def sitemap(xml: bool, output: click.File, module_site: str):
417+
"""
418+
CLI for generating the site map in HTML (default) or XML to either the console (default) or a file.
419+
"""
420+
module, site_name = split_module_site(module_site)
421+
site = get_site(module, site_name)
422+
site_map = SiteMap(site.route_list, site.site_vars.get("SITE_URL", ""))
423+
if xml:
424+
site.theme_manager.engine.globals.update(site.site_vars)
425+
site_map_page = Page()
426+
site_map_page.template = "sitemap.xml"
427+
site_map_page.site_map = site_map
428+
content = site_map_page._render_content(site.theme_manager.engine)
429+
else:
430+
content = site_map.html
431+
if output:
432+
Path(output).write_text(content)
433+
else:
434+
print(content)
435+
436+
399437
if __name__ == "__main__":
400438
app()

0 commit comments

Comments
 (0)