批量查找html中的一行并在其下方添加一些文字

发布于 2024-11-28 13:30:35 字数 614 浏览 0 评论 0原文

我为此苦苦挣扎了几个小时,但无法让它正常工作。我想做的是找到一行

<div id="content"> 

并在下面添加几行,其中包含一些变量,例如

<div class="post">
    <h2 class="title"><a href="#">**var**</a></h2>
    <p class="meta"><span class="date">**var**</span>
    <span class="posted">Posted by <a href="#">**var**</a></span></p>
    <div style="clear: both;">
        &nbsp;
    </div>
    <div class="entry moreless">
        **var** 
    </div>
</div>

是否可能?如果是这样怎么办?

——大卫

I'm struggling with this for few hours, but I can't get it to work properly. What I wanna do is find a line

<div id="content"> 

and add few lines below that contain some variables like

<div class="post">
    <h2 class="title"><a href="#">**var**</a></h2>
    <p class="meta"><span class="date">**var**</span>
    <span class="posted">Posted by <a href="#">**var**</a></span></p>
    <div style="clear: both;">
         
    </div>
    <div class="entry moreless">
        **var** 
    </div>
</div>

Is it possible? If so how?

--David

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酒忠诚 2024-12-05 13:30:35

更好的工具可以解决这个问题。但Windows批处理是可能的!

假设下面的文本是名为 TEMPLATE.HTML 的最外层模板文件。内容触发行可以缩进,但该解决方案不允许触发后有额外的字符。 (这个限制可以解决,但会减慢速度)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Sample template for building a page</title>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>
<body>
  <div id="content">
  </div>
</body>
</html>

下面的示例内容文件(名为 CONTENT_HTML.TXT)包含指定为 !varName! 的变量。并且批处理文件将使用延迟扩展来执行变量替换。这意味着如果您希望内容包含感叹号,则必须以某种方式对其进行编码。有两种选择。一种是使用 HTML 字符表示法 !这对于文本内容来说效果很好,但如果您想要 HTML 注释,则不起作用。对于评论,我定义了一个 !X!将扩展为 !

<div class="post">
    <h2 class="title"><a href="#">!TITLE!</a></h2>
    <p class="meta"><span class="date">!DATE!</span>
    <span class="posted">Posted by <a href="#">!AUTHOR!</a></span></p>
    <div style="clear: both;">
         
    </div>
    <div class="entry moreless">
        Example with exclamation point!<br />
        Another example with exclamation point!X!
    </div>
    <!X!--
      sample comment
    -->
</div>

最后,这是一个批处理文件,它将把所有内容放在一起并创建一个名为 FINAL.HTML 的输出文件。我对变量的值进行了硬编码,但它们可以很容易地作为参数传入。

@echo off
setlocal enableDelayedExpansion
set X=^^^!
set TITLE=Sample HTML build
set DATE=%date%
set AUTHOR=Santa Clause
set TEMPLATE="template.html"
set CONTENT="content_html.txt"
set FINAL="final.html"

<%TEMPLATE% (
  for /f %%A in ('type %TEMPLATE%^|find /c /v ""') do (
      for /l %%N in (1 1 %%A) do (
      set "ln="
      set/p "ln="
      echo(!ln!
      set "test=!ln:*<=<!"
      if "!test!"=="<div id="content">" (
        for /f "usebackq delims=" %%L in (%CONTENT%) do echo(%%L
      )
    )
  )
)>%FINAL%

此解决方案存在一些限制

  1. 空行(仅包含空格的行)将从内容部分中删除。可能不是 HTML 的问题。消除此限制将使解决方案变得复杂并减慢速度。
  2. 以 ; 开头的行将从内容部分中删除。这个限制很容易消除,但代码看起来很难看。它涉及指定。作为最终 FOR /F 循环中的 EOL 字符。
  3. 模板文件必须使用 Windows 换行符标准。使用的 Unix 风格文件使用此解决方案将会失败。这也可以解决,但同样会使事情变得复杂并减慢速度。
  4. 正如我之前所说,模板中的内容触发器可以有前导空格,但行中不能有任何尾随字符。消除此限制会减慢该过程。
  5. 模板中的行限制为 1024 个字符 模板
  6. 中每行的尾随控制字符被删除

There are much better tools available to solve this. But it is possible with Windows batch!

Assume the text below is the outermost template file named TEMPLATE.HTML. The content trigger line can be indented, but this solution does not allow extra characters after the trigger. (This limitation could be solved, but it would slow things down)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Sample template for building a page</title>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>
<body>
  <div id="content">
  </div>
</body>
</html>

The sample content file below (named CONTENT_HTML.TXT) contains variables specified as !varName! and the batch file will use delayed expansion to perform the variable substitution. This means that if you want the content to contain an exclamation, it must be encoded somehow. There are two choices. One is to use the HTML character notation ! This works fine for text content, but will not work if you want an HTML comment. For comments I define an !X! variable that will be expanded to !

<div class="post">
    <h2 class="title"><a href="#">!TITLE!</a></h2>
    <p class="meta"><span class="date">!DATE!</span>
    <span class="posted">Posted by <a href="#">!AUTHOR!</a></span></p>
    <div style="clear: both;">
         
    </div>
    <div class="entry moreless">
        Example with exclamation point!<br />
        Another example with exclamation point!X!
    </div>
    <!X!--
      sample comment
    -->
</div>

Finally here is a batch file that will put everything together and create an output file named FINAL.HTML. I hard coded the values for the variables, but they could just as easily be passed in as arguments.

@echo off
setlocal enableDelayedExpansion
set X=^^^!
set TITLE=Sample HTML build
set DATE=%date%
set AUTHOR=Santa Clause
set TEMPLATE="template.html"
set CONTENT="content_html.txt"
set FINAL="final.html"

<%TEMPLATE% (
  for /f %%A in ('type %TEMPLATE%^|find /c /v ""') do (
      for /l %%N in (1 1 %%A) do (
      set "ln="
      set/p "ln="
      echo(!ln!
      set "test=!ln:*<=<!"
      if "!test!"=="<div id="content">" (
        for /f "usebackq delims=" %%L in (%CONTENT%) do echo(%%L
      )
    )
  )
)>%FINAL%

There are some limitations with this solution

  1. Empty lines (lines containing nothing but spaces) will be stripped from the Content portion. Probably not a problem with HTML. Eliminating this limitation will complicate and slow down the solution.
  2. Lines beginning with ; will be stripped from the Content portion. This limitation is easily removed, but the code is ugly looking. It involves specifying <LF> as the EOL character in the final FOR /F loop.
  3. The Template file must use the Windows newline standard of <CR><LF>. Unix style files using <LF> will fail with this solution. This also can be solved, but again it will complicate and slow things down.
  4. As I stated earlier, the content trigger in the Template can have leading spaces, but it cannot have any trailing characters on the line. Removing this limitation will slow the process down.
  5. Lines are limited to 1024 characters in Template
  6. Trailing control characters are stripped from each line in Template
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文