评论 div 关闭自动化
我正在寻找一种解决方案来解决我收到的一大堆代码——它的数量很大,所以我正在寻找一种编程方法的建议来评论什么 div 在哪里关闭。
示例:
在
<div id="wrapper-item">
<div id="outer-item">
<div class="inner-item">
<h1>Just Some Placekeeper Copy</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing.</p>
</div>
</div>
</div>
之后,我很好奇最好的方法是什么。
<div id="wrapper-item">
<div id="outer-item">
<div class="inner-item">
<h1>Just Some Placekeeper Copy</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing.</p>
</div><!-- .inner-item -->
</div><!-- #outer-item -->
</div><!-- #wrapper-item -->
我尝试了几次正则表达式但没有任何乐趣
I'm looking for a solution to a rats nest of code I was handed - it's massive in volume, so I'm looking for suggestions to a programmatic approach to commenting what div closes where.
Example:
BEFORE
<div id="wrapper-item">
<div id="outer-item">
<div class="inner-item">
<h1>Just Some Placekeeper Copy</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing.</p>
</div>
</div>
</div>
AFTER
<div id="wrapper-item">
<div id="outer-item">
<div class="inner-item">
<h1>Just Some Placekeeper Copy</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing.</p>
</div><!-- .inner-item -->
</div><!-- #outer-item -->
</div><!-- #wrapper-item -->
I tried a few Regex attempts with no joy, I'd be curious of what the best approach is..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果它是有效的 xhtml,那么您可以将其放入 XML 文档中,然后通过查找所有 div 标记来处理 div 标记,然后添加具有所需注释节点的同级标记。
If it is valid xhtml, then you could just put it in an XML Document and then proceed to process the div tags by finding all of them, then adding a sibling that has the comment node that you want.
单独的正则表达式可能会起作用,但最好编写一个由正则表达式增强的程序。获取
之间的文本div id="
和>
,然后将其添加到列表或堆栈中,并按顺序将其与 int 索引链接。继续扫描它并找到 '' 标记,弹出堆栈中最新的文本,并将其格式化为评论。A solitary regular expression might work, but it might be better to write a program that is enhanced by regular expressions. Get the text between
< div id="
and>
, then add that in a list or stack, and link it, in order, with an int index. Continue scanning it and upon finding a '' tag, pop the newest text on the stack, and format it to become a comment.我只是使用脚本来正确缩进 HTML,这样我就可以一目了然地检查所有标签是否正确关闭——缩进正确地返回到触及左边距。为了检查开头与结尾相匹配,我使用了 SciTe 或 Komodo Edit 等折叠文本编辑器,这样我就可以通过打开和折叠部分来浏览正确缩进的代码。
如果有人对缩进脚本(用 tcl 编写)感兴趣,我可以将其上传到某个地方。或者,您可以尝试使用 HTMLTidy 之类的工具来进行格式化。
I just use a script to properly indent the HTML so I can check at a glance if all tags are closed properly -- the indenting correctly comes back to touch the left margin. To check what opening matches what closing I use a folding text editor like SciTe or Komodo Edit so I can browse the properly indented code by opening and collapsing sections.
If anybody is interested in the indenting script (written in tcl) I can upload it somewhere. Alternatively you can try using something like HTMLTidy to do the formatting.
您可以使用特定语言的 XML/DOM API 并尝试它处理注释对象的方式。
例如,Python -- http://docs.python.org/库/xml.dom.html#comment-objects
You can use a XML/DOM API for a particular language and play with how it handles Comment Objects.
For example, Python -- http://docs.python.org/library/xml.dom.html#comment-objects
只需使用一个合适的编辑器来突出显示开始/结束标签即可。甚至 NotePad2 也这样做。大多数编辑器还可以选择标签和内容,或仅选择内容。许多编辑器还会为您重新格式化 html(如果您敢的话)。
评论标签的末尾将会变得不同步,并且对于您或您的同事来说更难以理解。
如果你最终成功地向每个 div、span、p、ul 等标签添加冗余注释,我想你会发现代码更加臃肿且难以阅读。
Just use a decent editor that highlights the start/end tag. Even NotePad2 does this. Most editors can also select tag and contents, or just contents. Many editors will also reformat the html for you (if you dare).
Commenting the end of the tag is going to get out of sync and be even harder to follow for you or your coworkers.
If you do end up successfully adding redundant comments to every div, span, p, ul, etc tag, I think you'll find the code is even more bloated and unreadable.