0

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
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Thomas
  • 3
  • 1

2 Answers2

0

You could try something like:

$ echo "panini    href=\"{{root}}/contact-details.html\""|sed "s/{{root}}\/contact-details.html/{{ path_for('contact-details') }}/g"
panini    href="{{ path_for('contact-details') }}"
Idriss Neumann
  • 3,760
  • 2
  • 23
  • 32
0

thanks for the answers to my question. I found a way eventually. I looped through each HTML page to create an array of all the link page names. I then fed these into the following sed command to create a list of sed commands for editing all the page links. This was saved to a separate file sedCommands.sed

sedCommands=$( IFS=$'/n'; echo "s|{{root}}/${htmlLink}|{{ path_for(\'${twigLink}\') }}|g")

Once I'd created the twig pages I looped through each one and edited the page links with the following sed command using the SedCommands.sed file.

sed -i -f ~/bin/sedCommands.sed $fileName

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 names to 'htmlFiles' array do htmlFiles=(${htmlFiles[]} "$file") done

cd ~/bin # change location to output directory 'bin'

for link in ${htmlFiles[*]} # loop through 'htmlFiles' array do

htmlLink=$link # create variable that lists html page links twigLink="${link%.html}" # create variable that lists twig page links

sedCommands=$( IFS=$'/n'; echo "s|{{root}}/${htmlLink}|{{ path_for(\'${twigLink}\') }}|g") # create sed commands for substituting html link syntax with twig syntax. IFS command separates each sed command with a new line echo $sedCommands # print 'sedCommands' variable

done >sedCommands.sed # finish loop and redirect standard output to 'sedCommands.sed file'

chmod 775 sedCommands.sed # Make 'sedCommands.sed' file executable

cd /home/thomas/Webdevelopment/elements_of_healing/src/pages # change location to target directory 'pages'

mkdir ~/bin/twig # create 'twig' directory in user 'bin' directory

for file in ${htmlFiles[*]} # loop through each html file in array and process using script below do

script - sed prints content from 5th line until it finds the pattern 'FINISH' at

the end of the file. Grep then excludes '' and the output is saved

to the variable '$page_content' which is added to a here document template and

outputted as a twig file in the following location ~/bin/twig/

page_content=$( sed -n -e '5,/FINISH/p' $file | grep -v '')

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 # create empty array 'twigFiles'

for file in *.twig # loop through all twig files in 'twig' directory and add to 'twigFiles' array do

twigFiles=(${twigFiles[*]} "$file")

done

for file in ${twigFiles[*]} # loop through twig files do

chmod 775 $file # change permissions using chmod command sed -i -f ~/bin/sedCommands.sed $file # change link syntax using sedCommands.sed file

done

Thomas
  • 3
  • 1