#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Makefile that user cat to load html and sed to replace


    Hi,

    Long time lurker but first time poster. I have a Makefile question and wasn't sure where to post the question but so I hope I picked the right category.

    I'm working with backbone templates and have split my html in multiple files.
    The index files includes this {template} text. I'm planing to use tha Makefile to load the text from multiple HTML files and replace the {template} variable with the content from those files.

    My HTML files looks like this:

    file.html
    <div>
    <a href='#'>my link</a>
    </div>

    index.html
    <body>
    {template}
    </body>


    Then I have a makefile that looks like such:

    Makefile
    include:
    $(eval data := $(shell cat "file.html"))
    sed -i '' 's/$({template})/$(data)/' index.html


    The sed code works lika a charm and it replaces it with the data value if I force it to have text, but the cat breaks and gives me an 'Error 1'


    I've tried both...
    $(eval data := $(shell grep -nr file.html))
    $(eval data := $(shell cat "file.html"))

    They both fail when they reach <, >, ", ' or #.
    I've not yet figured out how to load the html data into the variable.

    So I'm looking for guidance from you guys and girl.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    928
    First off, please surround code with [code]...[/code] tags. This preserves formatting (which is especially important for makefiles where whitespace is significant).


    There are several problems here:

    • $(xxx yyy) runs the command "xxx yyy" so unless you have an executable on your path named "shell", $(shell cat file) is going to fail. You probably meant $(cat file).
    • Each line of a makefile runs within its own shell. Variables set in one line won't be present in the next line. You need to combine the lines to do this by using shell commands like ; or && to join commands, or eliminate the need for the preceding line by interpolating the file directly into the sed command.
    • But even if you do all that, your sed command is going to turn into s/{template}/...</div>/ and it's going to choke.



    You'll have an easier time if you use existing template software for this. Here's a quick example of how it would look using Template Toolkit. (And there are many many choices for templating software out there if you need something different.)

    Code:
    F:\temp>cat file.tt
    <div>
    <a href='#'>my link</a>
    </div>
    
    F:\temp>cat index.tt
    <body>
    [% INCLUDE file.tt %]
    </body>
    
    
    F:\temp>cat makefile
    index.html: index.tt file.tt
            tt-render index.tt > index.html
    
    F:\temp>dmake index.html
    tt-render index.tt > index.html
    
    F:\temp>cat index.html
    <body>
    <div>
    <a href='#'>my link</a>
    </div>
    
    </body>
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);

IMN logo majestic logo threadwatch logo seochat tools logo