0

When calling:

f [(h test) (h test2)]

I want to get:

"<p><h1>test</h1><h1>test2</h1></p>"

Instead I get:

"<h1>test2</h1></p>"

I can't see why my code below doesn't work. Note that I want to use g function below because I have several h like functions, g is called by each of them to factorize them. So don't get rid off g, it's on purpose.

html: copy ""
emit: func [code] [repend html code] 

f: func [param [block!]] [
    html: copy ""
    emit: func [code] [repend html code]  
    emit <p>
    foreach p param [
        emit p
    ]
    emit </p>
    return html
]

g: func ['arg [string! word!] /local html] [  
    return h :arg
]

h: func ['arg [string! word!]] [
    either word? arg [
        text: to-string arg
    ][
        text: arg
    ]

    html: copy ""
    emit: func [code] [repend html code]  

    print text
    emit <h1>
    emit text
    emit </h1>
    return html
]

f [(h test) (h test2)]

Update:

Now I get error in red: Script Error: html is not in the specified context

  f: func[param [block!] /local html][
      html: copy ""
      emit: func [code] [repend html code]  
      emit <p>
      foreach p param [
          emit p
      ]
      emit </p>
      return html
  ]

  g: func['arg [string! word!] /local html][  
    return h :arg
  ]


  h: func['arg [string! word!] /local html][

      either word? arg [text: to-string arg][
        text: arg
      ]


      html: copy ""
      emit: func [code] [repend html code]  

      print text
      emit <h1>
      emit text
      emit </h1>

      return html

  ]

  f [(h test) (h test2)]
user310291
  • 36,946
  • 82
  • 271
  • 487

2 Answers2

2

Your problem is the usage of a global html: copy "" everywhere and new initailzing the already emitted html. If you make it local with /local html in the spec blocks either manually or by replacing func with funct in Rebol2 or function in Red, it should work

>>f [ [h test] [h test2]]
test
test2
== "<p><h1>test</h1><h1>test2</h1></p>"
>> 
sqlab
  • 6,412
  • 1
  • 14
  • 29
  • I have updated code to make html local see above but in red I got Script Error: html is not in the specified context – user310291 Oct 29 '17 at 12:08
  • It works in Red if you use function. You have to define **html**, **emit** a.s.o. in the same context – sqlab Oct 29 '17 at 16:08
1

Ok, here a slightly optimized version for Red and Rebol without funct or function

emit: func [code html] [repend html code]  

f: func[param [block!] /local html][
    html: copy ""
    emit <p> html
    foreach p param [
        emit p html
    ]
    emit </p> html
    return html
]

g: func['arg [string! word!] l][  
    return h :arg
]


h: func['arg [string! word!] /local html text][
    either word? arg [text: to-string arg][
       text: arg
    ]
    html: copy ""
    print text
    emit <h1> html
    emit text html
    emit </h1> html
    return html
]

>>f [ [h test]   [h test2]]
test
test2
== "<p><h1>test</h1><h1>test2</h1></p>"
sqlab
  • 6,412
  • 1
  • 14
  • 29