html - 拆分 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 编程语言?(服务器或客户端)
解决方案
tag=section
sed -n "/<$tag>/,/<\/$tag>/p" section.inc
这应该是您的起点:
您可以将目标 HTML 标记名称指定到tag
环境变量中;
sed
将提取由您的标签分隔的文件内容并将其放入文件路径
推荐阅读
- python - 如何让 Python 为参数/kwarg 吐出所有定义的值?
- c# - 如何交换大字符串
- ajax - 使用 Ajax 填充 Wicket 表
- r - 将多个因子列组合成一个数字列
- ruby - PG::ConnectionBad: could not connect to server
- c# - Parsing Razor Pages or Razor project in WPF application?
- python - 如何在比较两个长二维列表时减少执行时间
- c# - Server-Side Request Forgery Fortify Fix
- bash - 如果使用绝对路径运行,Bash 脚本会出现意外行为
- c# - 当有多个实现接口的类时,如何指定构造函数注入