From 6f5e7ca8c9335c0beb72c05fca96d000fa7483f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Salvador=20de=20la=20Puente=20Gonz=C3=A1lez?= Date: Tue, 19 Nov 2019 10:40:17 +0100 Subject: [PATCH] Fix deployment due to issues with sed in Travis environments --- gen-html.sh | 7 +--- scripts/add_analytics.py | 38 +++++++++++++++++++ .../analytics.html.part | 0 3 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 scripts/add_analytics.py rename analytics.html.part => scripts/analytics.html.part (100%) diff --git a/gen-html.sh b/gen-html.sh index 2b790e64c..fd11a95af 100755 --- a/gen-html.sh +++ b/gen-html.sh @@ -33,12 +33,7 @@ cp_to () { } add_analytics () { - analytics_snippet=$(#${analytics_snippet_escaped}#" {} \; + python3 scripts/add_analytics.py $1 } main () { diff --git a/scripts/add_analytics.py b/scripts/add_analytics.py new file mode 100644 index 000000000..e205ba9f1 --- /dev/null +++ b/scripts/add_analytics.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 + +""" +This script adds the analytics code snippet in 'analytics.html.part' +to all the HTML file (at the very end of the section) inside a +target folder received as its only parameter. +""" + +import os +import sys +import glob + + +def get_analytics_snippet(file_name): + file_path = os.path.join(os.path.dirname(__file__), file_name) + print(f'Obtaining analytics snippet from "{file_path}"') + with open(file_path, encoding='utf8') as snippet: + return snippet.read() + + +def main(target_dir): + analytics_snippet = get_analytics_snippet('analytics.html.part') + replacement = f'{analytics_snippet}' + search_path = os.path.join(os.getcwd(), target_dir) + glob_spec = f'{search_path}/**/*.html' + for file_path in glob.glob(glob_spec, recursive=True): + with open(file_path, encoding='utf8') as html_file: + contents = html_file.read() + + new_contents = contents.replace('', replacement) + + with open(file_path, mode='w') as html_file: + print(f'Adding analytics to "{file_path[len(os.getcwd()) + 1:]}"') + html_file.write(new_contents) + + +if __name__ == '__main__': + main(sys.argv[-1]) \ No newline at end of file diff --git a/analytics.html.part b/scripts/analytics.html.part similarity index 100% rename from analytics.html.part rename to scripts/analytics.html.part