首页 > 解决方案 > 拆分 HTML 文件

, 到单独的包含文件中?

问题描述

我想通过<section>标签将 HTML 文件拆分为单独的文件。

一个例子可能是:

mypage.html

<!DOCTYPE html>
<html>
    <head>
         ...
    </head>
<body>
    <!-- Section 1 -->
    <section class="foo">
        ...
    </section>

    <!-- Section 2 -->
    <section class="bar">
        ...
    </section>

    <!-- Section 3 -->
    ...
</body>
</html>

然后将按如下方式列举期望的结果:

/mypage.html            # (original file)
/mypage-split.html      # (original file, with placeholders to replace the section back in)

# component/include files (that of course will not be valid HTML, since it's just a portion and won't start with `DOCTYPE` or `html`)
/sections/mypage-1.htmlinc      # (section 1 markup)
/sections/mypage-2.inc          # (section 2 markup)
...
/sections/mypage-n.html

如何执行此拆分?

shell 脚本可能是最简单的方法,但我的脚本技能非常有限。

或者,是否有任何 Web 标准可以将 HTML 页面的组件保存在单独的文件中(由浏览器或 Web 服务器支持),而不必求助于 Web 编程语言?(服务器或客户端)

标签: htmlscriptingincludetransformationserver-side-includes

解决方案


tag=section
sed -n "/<$tag>/,/<\/$tag>/p" section.inc

这应该是您的起点:
您可以将目标 HTML 标记名称指定到tag环境变量中;
sed将提取由您的标签分隔的文件内容并将其放入文件路径


推荐阅读