Rebol / Red Parse html rules returns true but nothing is inserted
之后如何仅标记第一个主div的结尾(当div-count为0时第一次),而不是第二次,或者是否有办法有条件地将变量值解析为变量值?
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>
<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 [
"<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
答案 0 :(得分:3)
您似乎需要混合使用opt
和if
个关键字。考虑一个简化的例子:
count: 0
div: ['div some integer! /div]
probe parse [
div 1 2 3 /div
div 4 5 6 /div
div 7 8 9 /div
][
some [
div (count: count + 1) opt [if (count = 1) mark:]
]
]
probe mark
此处,在每个div
规则匹配后,计数器的增量发生。之后有一个可选的匹配 - 如果计数器在1
,则标记当前输入位置,否则解析按原样继续。由于此规则是可选的,因此尽管成功或失败,解析仍将继续。
此外,如果您想要打破解析循环(即some
,any
或while
),您可以使用break
(返回成功)或reject
(退回失败),再次与opt
,if
和条件paren!
表达相结合。
答案 1 :(得分:2)
这是使用您的方法在Red和Rebol中工作的解决方案,并在规则中添加一些自我修改
rules: [
thru <div class="main">
(div-count: 1 clear rules/5/8 )
some [
"<div" (probe div-count: div-count + 1) skip
|
"</div>" mark: (
probe div-count: div-count - 1
if div-count = 0 [
insert mark "closing main div" append rules/5/8 [to end]
]
) [] skip
| skip
]
]