Skip to content

Commit

Permalink
Fix deployment due to issues with sed in Travis environments
Browse files Browse the repository at this point in the history
  • Loading branch information
delapuente committed Nov 19, 2019
1 parent dad48f7 commit 6f5e7ca
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
7 changes: 1 addition & 6 deletions gen-html.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ cp_to () {
}

add_analytics () {
analytics_snippet=$(<analytics.html.part)
# escape the multi-line string to be used as a replacement string in SED
# https://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed
IFS= read -d '' -r < <(sed -e ':a' -e '$!{N;ba' -e '}' -e 's/[&/\]/\\&/g; s/\n/\\&/g' <<<"$analytics_snippet")
analytics_snippet_escaped=${REPLY%$'\n'}
find $1 -name '*.html' -exec sed -i '' -e "s#</head>#${analytics_snippet_escaped}</head>#" {} \;
python3 scripts/add_analytics.py $1
}

main () {
Expand Down
38 changes: 38 additions & 0 deletions scripts/add_analytics.py
Original file line number Diff line number Diff line change
@@ -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 <head> 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}</head>'
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('</head>', 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])
File renamed without changes.

0 comments on commit 6f5e7ca

Please sign in to comment.