I need a script that searches files for SSI and replaces the include with the actual HTML

StackOverflow https://stackoverflow.com/questions/18377549

  •  26-06-2022
  •  | 
  •  

سؤال

I am developing the front end code of a website which I will be handing over to some developers for them to integrate it with the backend. The site will be written in .NET but I'm developing the front end code with static HTML files (and a bit of javascript).

Because the header, footer and a few other elements are the same across all pages I am using Server Side Includes in my development environment. However, every time I hand the code to the developers I need to manually replace each SSI with the actual HTML by copying and pasting. This is starting to get tedious.

I have tried writing a bash script to do this but my bash knowledge is extremely limited so I have failed miserably (I'm not really sure where to start).

What I tried to achieve was:

  • Loop through all the HTML files in my project
  • Look for an include ( <!--#include file="myfile.html"--> )
  • If one is found, replace the include with the HTML from the file specified in the include
  • Keep doing this until there are no more includes and move on to the next file

Does anyone know of a script that can do this, or can point me in the right direction for achieving this myself? I'm happy for it to be in any language as long as I can run it on my Mac.

Thanks.

EDIT

It is safe to assume that all instances of <!--#include file="myfile.html"--> are on their own line.

هل كانت مفيدة؟

المحلول

The specification does not cover every details, so I have the following assumptions.

  1. The <!--#include file="myfile.html"--> line stays its own. Other characters are removed.
  2. Included files does not contains additional includes.
  3. In project directory no subdirs has to be checked.

In this case something like this can do the job. It is in :

#!/usr/bin/bash

search=${1:-./}

replace() {
  while read -r x; do
    if [[ "$x" =~ \<!--#include\ file=\"([^\.]+.html)\"--\> ]]; then
      cat "${BASH_REMATCH[1]}";
    else
      echo "$x"
    fi
  done <"$1"
}

while read f; do
  replace "$f" > tmp_$$.tmp && mv tmp_$$.tmp "$f"
done < <(find $search  -maxdepth 1 -name '*.html')

It reads all the *.html files in the specified directory (not recursively). If no args given it checks the current directory. For each line it calls replace function. Replace searches for include lines. If one found, then prints the content of the file to the stdout, otherwise the original line is presented.

Lets consider to files:

cat >master.html <<XXX
<html>
<!--#include file="myfile.html"-->
</html>
XXX

cat >myfile.html <<XXX
<title>
My file
</title>
XXX

Result:

$ cat master.html
<html>
<title>
My file
</title>
</html>
$ cat myfile.html
<title>
My file
</title>

I hope this could help...

نصائح أخرى

On your dev machine, use your browser to display the web page, and then save the 'result' with an appropriate file name/in an output directory.

Thus, if you had mainfile.html which executed various time/last-mod directives and which included fileA.inc and fileB.inc at appropriate places, the resulting display (and save-able HTML file) will comprise all four/five components.

=dn

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top