htmlreplacerebolrebol3rebol2

Find/replace string that spans lines in a file with REBOL


I have an HTML page, and I need to replace a couple of lines in it. However, using replace can't seem to find anything bigger than a single line.

This is what I would like to replace (there are multiple instances of it in the page):

....
<div class="logo">
    <img src="resources/logo.svg" />
    <span>e</span>
    <span class="text">Market</span>
</div>
...

Here's the code I am trying, but it doesn't work:

index-html: read %index.html    

logo-div: {<div class="logo">
<img src="resources/logo.svg" />
<span>e</span>
<span class="text">Market</span>
</div>}

new-div: {...}

out: replace/all index-html logo-div new-div

write %index.html out

Including ^/ in my logo-div string to denote the newlines doesn't help.

How can I find this entire string?

(I am using Rebol2, but I assume the functionality will be the same or very similar in Rebol3.)


Solution

  • I'm not sure about replace but this is a good opportunity for parse to work well:

    index-html: read %index.html
    out: copy ""
    new-div: "..."
    
    ;;parse rules
    
    title: [
        {<div class="logo">} thru {</div>} (append out new-div) ;;appends replacement when it finds that class
    ]
    
    rule: [
        some [
            title |
            copy char skip (append out char)  ;;copies every other char char
        ]
    ]
    
    parse index-html rule
    
    write %index.html out
    

    Don't have the index file, but this should work. Take some time to carefully study string parsing. It is very powerful.