views:

170

answers:

4
+3  Q: 

Merging HTML files

I want to merge one HTML file into another. Not just include it, but merge.

Example

master.html:

<!DOCTYPE html>
<html>
  <head>
    <title>My cat</title>
  </head>
  <body>
    <h1>My cat is awesome!</h1>
  </body>
</html>

_index.html:

<!DOCTYPE html>
<html>
  <body>
    <p><img src="cat.jpg"/></p>
  </body>
</html>

Now I merge _index.html into master.html.

$ html_merge master.html _index.html > result.html

result.html

<!DOCTYPE html>
<html>
  <head>
    <title>My cat</title>
  </head>
  <body>
    <h1>My cat is awesome!</h1>
    <p><img src="cat.jpg"/></p>
  </body>
</html>

html_merge is a script what I'm looking for. I'll use it for building static websites.

I would like to use Ruby, but it's not required.

Update

I don't want to reinvent yet another template language. There is a lot of template languages with include/yield statement and partials support. Liquid, mustache and so on. I use them for different tasks. Now I need to merge HTML files, nothing more.

+1  A: 

Could this help ? (It's in Java, though...)

http://www.javaworld.com/javaworld/jw-07-2007/jw-07-xmlmerge.html

phtrivier
While this is useful for unique XML elements you will get problems when merging e.g. <p> tags inside body.Each <p> is its own paragraph so they should be just added.
Aurril
A: 

Think carefully about what such a script would look like, and specifically how you'd have to write the individual html files in order for them to be mergable. How would you associate nodes? by named divs? by insuring that each had parallel structure?

Steve B.
Parallel structures, I think. `<a><b>2</b></a>` merged to `<a><b>1</b></a>` becomes `<a><b>1</b><b>2</b></a>`.
NV
A: 

Doing a merge like that requires knowledge about the content which to me sounds like you need to use a template system that is able to create static pages.

I've used http://www.cheetahtemplate.org/ on some projects, but it might be a bit of an overkill for your needs.

Marko Teiste
A: 

This looks remarkably like the layout / template relationship in Rails. While this may seem a little heavy handed, it will let you have a layout that you want content merged into:

application.html.erb

<html>
  <head></head>
  <body>
    Some content before hand.
    <%= yield %>
    Some content "after hand"
  </body>
</html>

index.html.erb

<strong>This is my cat: <%= image_tag "cat.jpg" %></strong>.

If you wish to have static pages served from this you can generate the cached versions of the page.

Ryan Bigg