简体   繁体   English

bash脚本,用于将html转换为树枝文件-将正则表达式与sed一起使用

[英]bash script for converting html to twig files - using regex with sed

I'm building a website that uses panini (HTML templating engine) on the frontend and twig (PHP templating engine) on the backend. 我正在建立一个在前端使用panini(HTML模板引擎)并在后端使用twig(PHP模板引擎)的网站。 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. 我一直在尝试制作一个bash脚本,该脚本从panini模板自动生成树枝模板,但是遇到了问题。

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与替代命令来做到这一点:

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. 这是一个较大的脚本的一部分,该脚本被声明为变量,该变量放置在here文档中,并输出到twig文件中。

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

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') }}"

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. 我遍历每个HTML页面以创建一个包含所有链接页面名称的数组。 I then fed these into the following sed command to create a list of sed commands for editing all the page links. 然后,我将这些输入到以下sed命令中,以创建用于编辑所有页面链接的sed命令列表。 This was saved to a separate file sedCommands.sed 这已保存到单独的文件sedCommands.sed

sedCommands=$( IFS=$'/n'; echo "s|{{root}}/${htmlLink}|{{ path_for(\\'${twigLink}\\') }}|g") 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. 一旦创建了树枝页面,我便遍历每个页面,并使用SedCommands.sed文件使用以下sed命令编辑页面链接。

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

Here is the full code. 这是完整的代码。

!/bin/bash !/ bin / bash

bash script for reformatting panini/html template to twig template bash脚本,用于将panini / html模板重新格式化为树枝模板

declare -a htmlFiles # create empty array 'htmlFiles' 声明-a htmlFiles#创建空数组'htmlFiles'

cd /home/thomas/Webdevelopment/elements_of_healing/src/pages # change location to target directory 'pages' cd / home / thomas / Webdevelopment / elements_of_healing / src / pages#将位置更改为目标目录“页面”

for file in .html # loop through all html files in 'pages' directory and add names to 'htmlFiles' array do htmlFiles=(${htmlFiles[ ]} "$file") done for .html中的文件#循环浏览'pages'目录中的所有html文件,并向'htmlFiles'数组添加名称htmlFiles =($ {htmlFiles [ ]}“ $ file”)完成

cd ~/bin # change location to output directory 'bin' cd〜/ bin#将位置更改为输出目录'bin'

for link in ${htmlFiles[*]} # loop through 'htmlFiles' array do 用于$ {htmlFiles [*]}中的链接#通过'htmlFiles'数组循环执行

htmlLink=$link # create variable that lists html page links twigLink="${link%.html}" # create variable that lists twig page links htmlLink = $ link#创建列出html页面链接的变量twigLink =“ $ {link%.html}”#创建列出嫩枝页面链接的变量

sedCommands=$( IFS=$'/n'; echo "s|{{root}}/${htmlLink}|{{ path_for(\\'${twigLink}\\') }}|g") # create sed commands for substituting html link syntax with twig syntax. sedCommands = $(IFS = $'/ n'; echo“ s | {{root}} / $ {htmlLink} | {{path_for(\\'$ {twigLink} \\')}} | g”)#创建sed命令用细枝语法替换html链接语法。 IFS command separates each sed command with a new line echo $sedCommands # print 'sedCommands' variable IFS命令用换行符分隔每个sed命令echo $ sedCommands#print'sedCommands'变量

done >sedCommands.sed # finish loop and redirect standard output to 'sedCommands.sed file' 完成> sedCommands.sed#完成循环并将标准输出重定向到“ sedCommands.sed文件”

chmod 775 sedCommands.sed # Make 'sedCommands.sed' file executable chmod 775 sedCommands.sed#使'sedCommands.sed'文件可执行

cd /home/thomas/Webdevelopment/elements_of_healing/src/pages # change location to target directory 'pages' cd / home / thomas / Webdevelopment / elements_of_healing / src / pages#将位置更改为目标目录“页面”

mkdir ~/bin/twig # create 'twig' directory in user 'bin' directory mkdir〜/ bin / twig#在用户'bin'目录中创建'twig'目录

for file in ${htmlFiles[*]} # loop through each html file in array and process using script below do $ {htmlFiles [*]}中的文件#遍历数组中的每个html文件,并使用以下脚本处理

script - sed prints content from 5th line until it finds the pattern 'FINISH' at 脚本-sed从第5行开始打印内容,直到在以下位置找到模式'FINISH'

the end of the file. 文件末尾。 Grep then excludes '' and the output is saved Grep然后排除“”,并保存输出

to the variable '$page_content' which is added to a here document template and 变量“ $ page_content”添加到此处文档模板,

outputted as a twig file in the following location ~/bin/twig/ 在以下位置〜/ bin / twig /中作为树枝文件输出

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

here document - creates twig template for each html page in the 'pages' directory 此处文档-为“页面”目录中的每个html页面创建树枝模板

cat > ~/bin/twig/${file/%.html/.twig} << EOF cat>〜/ bin / twig / $ {file /%。html / .twig} << EOF

{% extends 'main.twig' %} {%扩展了'main.twig'%}

{% block content %} {%封锁内容%}

$page_content $ page_content

{% endblock content %} {%endblock content%}

EOF 紧急行动

done 完成

cd ~/bin/twig # change location to directory containing twig file cd〜/ bin / twig#将位置更改为包含twig文件的目录

declare -a twigFiles # create empty array 'twigFiles' 声明-a twigFiles#创建空数组'twigFiles'

for file in *.twig # loop through all twig files in 'twig' directory and add to 'twigFiles' array do * .twig中的文件#遍历“ twig”目录中的所有twig文件并添加到“ twigFiles”数组中

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

done 完成

for file in ${twigFiles[*]} # loop through twig files do 用于$ {twigFiles [*]}中的文件#遍历树枝文件

chmod 775 $file # change permissions using chmod command sed -i -f ~/bin/sedCommands.sed $file # change link syntax using sedCommands.sed file chmod 775 $ file#使用chmod命令更改权限sed -i -f〜/ bin / sedCommands.sed $ file#使用sedCommands.sed文件更改链接语法

done 完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM