Skip to content

Commit

Permalink
added more build arguments (#4)
Browse files Browse the repository at this point in the history
* added more build arguments
  • Loading branch information
Florian Maas authored Jun 2, 2022
1 parent d4fdd7b commit a7af66d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
7 changes: 6 additions & 1 deletion docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ If not specified, the template files will be placed in the current directory.

## `homepage build`

Build the custom homepage from template files and write the output to a directory named `public`, so the resulting homepage is found in `public/homepage.html` This command should be run within the directory that contains the template files created with `homepage init`.
Build the custom homepage from template files and write the output to a directory named `public`, so the resulting homepage is found in `public/homepage.html` This command should be run within the directory that contains the template files created with `homepage init`.

### Arguments

- `--output-dir <DIR>`: Optional. Name of a directory (relative to the current directory) to create and place the resulting page in. Defaults to `public`
- `--output-file <FILE>`: Optional. Name of the resulting html file. Defaults to homepage.html.
19 changes: 18 additions & 1 deletion simple_homepage/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,21 @@ def build(self) -> None:
parser = argparse.ArgumentParser(
description="Build the homepage from template files and settings.yaml"
) # noqa: F841
HomepageGenerator().build()
parser.add_argument(
"--output-dir",
dest="dir",
default="public",
type=str,
help="""Optional. Name of a directory (relative to the current directory) to create and place the resulting page in.
Defaults to `public`. """,
)
parser.add_argument(
"--output-file",
dest="file",
default="homepage.html",
type=str,
help="""Optional. Name of the resulting html file. Defaults to homepage.html.""",
)
args = parser.parse_args(sys.argv[2:])
dict_args = vars(args)
HomepageGenerator(output_dir=dict_args["dir"], output_file=dict_args["file"]).build()
17 changes: 11 additions & 6 deletions simple_homepage/homepage_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ class HomepageGenerator:
"""

def __init__(
self, template_dir: str = "template", settings_yaml: "str" = "settings.yaml", output_dir: str = "public"
self,
template_dir: str = "template",
settings_yaml: "str" = "settings.yaml",
output_dir: str = "public",
output_file: str = "index.html",
) -> None:
self.settings_yaml = settings_yaml
self.output_dir = output_dir
self.output_file = output_file
self.template_dir = template_dir
self.env = Environment(loader=FileSystemLoader(template_dir))

Expand All @@ -27,7 +32,7 @@ def build(self) -> None:
self._copy_static_dir()
self._render_page()
self._add_images_list_to_js()
logging.info(f"Page built. The resulting homepage can be found at {self.output_dir}/homepage.html")
logging.info(f"Page built. The resulting homepage can be found at {self.output_dir}/{self.output_file}")

def _verify_required_files_exist(self) -> None:
expected_files = [
Expand Down Expand Up @@ -71,14 +76,14 @@ def _copy_static_dir(self) -> None:

def _render_page(self) -> None:
"""
Render the Jinja2 templates _homepage.html and _stylesheet.css into homepage.html and stylesheet.css.
Render the Jinja2 templates _homepage.html and _stylesheet.css into self.output_file and stylesheet.css.
"""

with open(self.settings_yaml, "rt") as f:
settings = yaml.safe_load(f.read())

template = self.env.get_template("_homepage.html")
with open(f"{self.output_dir}/homepage.html", "w+") as file:
with open(f"{self.output_dir}/{self.output_file}", "w+") as file:
html = template.render(settings=settings)
file.write(html)

Expand All @@ -92,9 +97,9 @@ def _add_images_list_to_js(self) -> None:
Scan for any images and add this as a list to the homepage.js files, so JavaScript can randomly pick
one to display when the page is opened.
"""
images = [f'"static/images/{image}"' for image in os.listdir("public/static/images")]
images = [f'"static/images/{image}"' for image in os.listdir(f"{self.output_dir}/static/images")]

with open("public/static/homepage.js", "r+") as f:
with open(f"{self.output_dir}/static/homepage.js", "r+") as f:
content = f.read()
f.seek(0, 0)
line = f'var images = new Array({",".join(images)});'
Expand Down
15 changes: 15 additions & 0 deletions tests/test_simple_homepage.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,18 @@ def test_build_command(tmp_path):
]
for file in expected_files:
assert os.path.isfile(file)


def test_build_command_arguments(tmp_path):
with run_within_dir(tmp_path):
subprocess.check_call(shlex.split("homepage init")) == 0
subprocess.check_call(shlex.split("homepage build --output-dir test --output-file test.html")) == 0
expected_files = [
"settings.yaml",
"test/test.html",
"test/static/stylesheet.css",
"test/static/homepage.js",
"test/static/images/placeholder.png",
]
for file in expected_files:
assert os.path.isfile(file)

0 comments on commit a7af66d

Please sign in to comment.