Rebol/Red Parse html规则返回true,但未插入任何内容 [英] Rebol / Red Parse html rules returns true but nothing is inserted
问题描述
我有一个解析规则,该规则返回true,但未按预期插入文本:html不变,而应在主结束div的末尾插入.我试图使用类似>如何使用REBOL解析HTML标记的计数器?
I have a parse rules that returns true but it doesn't insert my text as expected : the html is unchanged whereas it should have inserted at the end of the main closing div. I tried to use a counter like How to parse inside HTML tags with REBOL?
更新:我也不知道如何在counter = 0时立即退出解析,以免在main之后的最后一个div之前插入文本.
Update: I also don't know how to break out of the parsing as soon as counter = 0 so as not to insert text before last closing div after main.
content: {<div class="main">
<h1>
Big TITLE
</h1>
<div>
<section>
<p>a paragraph</p>
</section>
<section>
<p>a paragraph</p>
</section>
<section>
<p>a paragraph</p>
</section>
</div>
<div>
<p>Blah Blah</p>
</div>
</div>
<div>
Another Div
</div>
}
rules: [
thru <div class="main">
(div-count: 1)
some [
to "<div" (++ div-count) thru "<div" thru ">"
|
to </div> mark: (-- div-count if div-count = 0 [insert mark "closing main div"]) thru </div>
]
to end
]
parse content rules
推荐答案
以下是使用探针进行调试的解决方案
Here a solution with probe to debug
rules: [
thru <div class="main">
(div-count: 1)
some [
"<div" (probe ++ div-count) skip
|
"</div>" mark: ( probe -- div-count if div-count = 0 [insert mark "closing main div"]) skip
| skip
]
]
parse/all content rules
您的规则存在的问题是,永远不会或很少会减去div计数.解析指针会直接指向下一个打开的 div ,因为 to 始终是第一个满足的条件.
The problems with your rules are, that the div-count is never or seldom subtracted. The parse pointer goes straight to the next opening div as to is always the first fulfilled condition.
如果在成功情况后添加要结束,则可以突围或者更好地从解析中返回.如果不确定,请使用方括号将 [成功的子规则...分组]分组
You can break out or better return from parse if you add a to end after a successful condition. If you are unsure use brackets for grouping [ sucessful sub-rules ... to end ]
带有结束规则的示例
end-rule: [] ; or none
rules: [
thru <div class="main">
(div-count: 1)
some [
["<div" (++ div-count) skip]
|
["</div>"mark: (-- div-count if div-count = 0 [insert mark "closing main div" end-rule: [to end]]) end-rule ]
| skip
]
]
这篇关于Rebol/Red Parse html规则返回true,但未插入任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!