I'm building a website that uses panini (HTML templating engine) on the frontend and twig (PHP templating engine) on the backend. I've been trying to make a bash script that automatically generates the twig template from the panini template but I've run into a problem.
One of the things I need to change is the syntax for the page links. Here is an example:
panini href="{{root}}/contact-details.html"
twig href="{{ path_for('contact-details') }}"
I've been trying to use sed with the substitute command to do this:
sed 's/{{root}}/contact_details.html/{{ path_for('contact-details') }}/g'
This is part of a larger script that gets declared as a variable which is placed in a here document and outputted to a twig file.
I'd really appreciate any help that anyone can give me with this. I've been struggling with this for a while.
Here is the full code:
#!/bin/bash
# bash script for reformatting panini/html template to twig template
declare -a htmlFiles # create empty array 'htmlFiles'
cd /home/thomas/Webdevelopment/elements_of_healing/src/pages # change location to target directory 'pages'
for file in *.html # loop through all html files in 'pages' directory and add to 'htmlFiles' array
do
htmlFiles=(${htmlFiles[*]} "$file")
done
# cd /home/thomas/bin # change location to users 'bin' directory
mkdir ~/bin/twig # create 'twig' directory in user 'bin' directory
# cd twig # change location to new 'twig' directory
for file in ${htmlFiles[*]} # loop through each html file in array and process using script below
do
# script - sed replaces the link syntax for html to syntax for twig then prints content from 4th line until it finds the pattern 'FINISH' at
# the end of the file. Grep then excludes '<!-- FINISH -->' and the output is saved
# to the variable $page_content which is added to a here document template and
# outputted as a twig file
# this is the part that's giving me grief!
# version 1 - if I hard code the links the script works!
page_content=$( sed -e "s|{{root}}/contact-details.html|{{ path_for('contact-details') }}|g" -n -e '5,/FINISH/p' $file | grep -v '<!-- FINISH -->')
# html=$file # create variables for html page links
# twig="${file%.html}" # create variables for twig page links
# version 2 - variable approach - I can't get this version to work, I think the problem is the variables are always the same name as the page being created, not sure how to get around that?
# page_content=$( sed -e "s/{{root}}\/${html}\.html/{{ path_for(\'${twig}\') }}/g" -n -e '5,/FINISH/p' $file | grep -v '<!-- FINISH -->')
# version 3 - regex approach - argghhhhh!!! I've tried everything I can think of but it still won't work!
# page_content=$( sed -e "s|{{root}}/*|{{ path_for(\'*\') }}|g" -n -e '5,/FINISH/p' $file | grep -v '<!-- FINISH -->')
# SSpage_content=$( sed -e "s|{{root}}/[-a-z]\.html|{{ path_for(\'hello\') }}|g" -n -e '5,/FINISH/p' $file | grep -v '<!-- FINISH -->')
# href="{{ path_for('contact-details') }}"
# href="{{root}}/contact_details.html"
#here document - creates twig template for each html page in the 'pages' directory
cat > ~/bin/twig/${file/%.html/.twig} << _EOF_
{% extends 'main.twig' %}
{% block content %}
$page_content
{% endblock content %}
_EOF_
done
cd ~/bin/twig # change location to directory containing twig file
# declare -a twigFiles
declare -a html
for file in *.twig # loop through all html files in 'pages' directory and add to 'htmlFiles' array
do
twigFiles=(${twigFiles[*]} "$file")
done
for file in ${twigFiles[*]} # loop through twig files and change permissions using chmod command
do
chmod 775 $file
done