Rebol / Red Parse html rules return true but nothing is inserted

I have parsing rules that return true, but it does not insert my text as expected: html does not change, whereas it should be inserted at the end of the main closing div. I tried using a counter, for example How to parse HTML tags using REBOL?

Update: I also don't know how to break out of parsing as soon as counter = 0, so as not to insert text before the 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
0
source share
1 answer

Here's a solution with a debug probe

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, .

, . [ -... ]

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
]

]

+1

All Articles