1

Suppose I have 3 files:

index.html

And here is foo:
<!-- include: includes/foo.html -->

This can be done similarly: <code> <!-- include: includes/bar.txt --> </code>

includes/foo.html

FOO CONTENTS
FOO CONTENTS
FOO CONTENTS

includes/bar.txt

BAR CONTENTS
BAR CONTENTS
BAR CONTENTS
BAR CONTENTS
BAR CONTENTS

How can I replace the includes automatically with a command like awk 'some magic code' index.html > compiled.html?

compiled.html

And here is foo:
FOO CONTENTS
FOO CONTENTS

This can be done similarly: <code> BAR CONTENTS BAR CONTENTS BAR CONTENTS BAR CONTENTS BAR CONTENTS </code>

Additional comments

  • If no such file is found, a warning or error (preferred) should occur (<!-- include: includes/does-not-exist.txt -->)
  • The <!-- include: ... --> line must not be in compiled.html
  • I am flexible on the include syntax. It could be <include "includes/foo.html"> if it's easier.
  • I want something like "pointer files", but without a hardcoded filename and without the pointer file line being included
  • The command must not require me to manually specify all of the includes. (awk 'some magic code' index.html includes/foo.html includes/bar.txt > compiled.html is not allowed)
  • It just needs to work on most filenames and should not require too many tools (for example, I'd rather not install PHP server just to use <? include "includes/bar.txt" ?>)
terdon
  • 242,166
wjwrpoyob
  • 440
  • https://metacpan.org/pod/Template::Toolkit Is it a paid task? – Gilles Quénot Mar 01 '23 at 19:08
  • @GillesQuénot that is possible to use but requires Perl and Template::Toolkit. Is there less dependencies version? I do not understand your "is it paid task" question. – wjwrpoyob Mar 01 '23 at 19:13
  • 2
    You listed a bunch of things you want to do (as opposed to posting your code that you want help with) so @GillesQuénot is just asking if this is a job post where you want to pay someone to create a tool for you. – Ed Morton Mar 01 '23 at 19:40
  • Also, please edit your post to include what you already tried and where you failed. That way you can avoid receiving answers that you already know won't work. – AdminBee Mar 02 '23 at 08:03

1 Answers1

2

With perl:

perl -0777 -pe 'while (s{<!-- include: (.*?) -->}{
  open I, "<", $1; <I>}ge) {}' index.html > compiled.html

(the while loop to make it recursive to allow includes to include more files).

The capture group (.*?) allow filename retrieving to include them in the File Handle with $1.

With error handling:

perl -0777 -pe '
  while (
    s{<!-- include: (.*?) -->}{
      if (open I, "<", $1) {
        <I>;
      } else {
        warn "$1: $!\n";
        $ret = 1; 
        "<!-- failed-include: $1 -->"
      }
    }ge
  ) {}
  END {exit $ret}' index.html > compiled.html
  • Better to not use global File Handle. – Gilles Quénot Mar 01 '23 at 20:43
  • @GillesQuénot, while that's true as a better programming practice in general, here for a self contained one liner with no function nor package being used, that won't make much difference and in my tests degrades performance. – Stéphane Chazelas Mar 02 '23 at 08:36
  • Very 'sioux' anyway, GG – Gilles Quénot Mar 02 '23 at 09:40
  • @GillesQuénot what does Very 'sioux' mean in this context? I've just never come across that expression (I know the Sioux are a Native American tribe) – Ed Morton Mar 02 '23 at 14:12
  • @EdMorton, in French, "Sioux" (the name of that tribe) can be used to express an idea of "cunning" or "clever", as I guess they were/are renown to be cunning. – Stéphane Chazelas Mar 02 '23 at 15:35
  • Thanks, good explanation. With ChatGPT: What means from French expression "c'est Sioux" in the context to explain a clever thing?

    In French, "c'est Sioux" means "it's difficult" or "it's tricky". It's an idiomatic expression that comes from the name of the Native American Sioux tribe, which is known for their bravery and resilience. In this context, "Sioux" is used to describe a situation or task that is challenging, requires effort, or is difficult to achieve.

    – Gilles Quénot Mar 02 '23 at 16:30
  • 1
    Therefore, when someone says "c'est Sioux" to explain a clever thing, it means that the thing is clever, but it was not easy to achieve or understand. The person may be acknowledging the difficulty involved in the process of coming up with or executing the clever thing. – Gilles Quénot Mar 02 '23 at 16:30
  • @GillesQuénot & Stéphane Interesting, thanks for the explanations. I thought I spoke (or at least understood!) French passably but apparently that particular idiom wasn't taught in my Scottish high school :-). – Ed Morton Mar 02 '23 at 17:18
  • @EdMorton une ruse de Sioux is an idiom you might have heard, though likely not in a French language class room. – Stéphane Chazelas Mar 02 '23 at 17:28
  • It was usually used in French tech community in early 2000's ^^ I think of https://linuxfr.org by example https://duckduckgo.com/?q=site%3Alinuxfr.org+%22c%27est+sioux%22&t=lm&ia=web – Gilles Quénot Mar 02 '23 at 17:50
  • Ah, high school for me ended in 1982 so I missed that period. Any time I'm in France and speaking French everyone else typically quickly switches to English to avoid having to endure hearing French with a Scottish accent. And instead they have to endure hearing English with a Scottish accent. – Ed Morton Mar 02 '23 at 18:03