diff --git a/docs/api/source/index.rst b/docs/api/source/index.rst index 81119962..18aa3fc0 100644 --- a/docs/api/source/index.rst +++ b/docs/api/source/index.rst @@ -28,5 +28,6 @@ Welcome to Trame's documentation! :caption: Tools tools.www + tools.app .. include:: ../../../README.rst diff --git a/docs/api/source/tools.app.rst b/docs/api/source/tools.app.rst new file mode 100644 index 00000000..0f19630e --- /dev/null +++ b/docs/api/source/tools.app.rst @@ -0,0 +1,18 @@ +Application file generator +============================== + +This small utility let you run a command line to generate a derived HTML file +from the original `static web client output `_ +to run a specific application from a launcher configuration. + +In order to use that tool, you will need to provide the path of the base +application to use as template along with the name of the application you +aim to run from your launcher config. +This tool will then create a new HTML file within the same directory that +will start the provided application name rather than the default "trame" one. + + +.. code-block:: bash + + python -m trame.tools.app --input ./www-content --name MySuperApp + # => create file MySuperApp.html from ./www-content/index.html diff --git a/trame/tools/app.py b/trame/tools/app.py new file mode 100644 index 00000000..53989012 --- /dev/null +++ b/trame/tools/app.py @@ -0,0 +1,58 @@ +r""" +From the directory containing the static content for a trame application to work, +generate another application specific HTML file. +""" +import argparse +import re +from pathlib import Path +import sys + +APP_PATTERN = re.compile(r'data-app-name="\w+"') + + +def create_app_file(input_file, output_file, app_name): + # Read in the file + with open(input_file, "r") as f_in: + content = f_in.read() + patched_content = APP_PATTERN.sub(f'data-app-name="{app_name}"', content) + with open(output_file, "w") as f_out: + f_out.write(patched_content) + + +def main(): + parser = argparse.ArgumentParser( + description="HTML app file generator for trame applications" + ) + + parser.add_argument( + "--name", + default="trame", + help="Application name to encode inside HTML {name}.html", + ) + + parser.add_argument( + "--input", + help="Input file to use as template", + ) + + args, _ = parser.parse_known_args() + + # Handle input + input_file = Path(args.input) + if not input_file.exists(): + parser.print_help() + sys.exit(0) + + if input_file.is_dir(): + input_file = input_file / "index.html" + + if not input_file.exists(): + parser.print_help() + sys.exit(0) + + output_file = input_file.with_stem(args.name) + create_app_file(input_file, output_file, args.name) + + +if __name__ == "__main__": + main()