문제

How do I define an M4 macro that accepts variable number of arguments and processes each argument? I've looked at "shift" and "foreach (from the examples)" macros but can't seem to put together my own macro using them. Thanks for any help.

define(UL, `wrap each arg in <li> tag, then wrap the whole output in <ul>')

UL(foo, bar, baz) #returns <ul><li>foo</li><li>bar</li><li>baz</li></ul>
도움이 되었습니까?

해결책

I will propose this as a solution, although I will not guarantee that it does not contain a quoting error.

define(`UL',`<ul>IT($@)<\ul>')dnl
define(`LI',`ifelse(`$1',`', `', `<li>`$1'<\li>')')dnl
define(`IT',`ifelse(`$#', `0',, `$#', `1',`LI(`$1')', 
    `LI(`$1')IT(shift($@))')')dnl
dnl
UL(foo,bar,baz)
UL(`UL',`LI',`IT')
UL(``UL'',``LI'',``IT'')

For posterity, here is my original solution which certainly does contain a quoting error:

define(`UL',`<ul>IT($@)<\ul>')dnl
define(`LI',`ifelse(`$1',`', `', `<li>$1<\li>')')dnl
define(`IT',`ifelse(`$#', `0',, `$#', `1',`LI(`$1')', 
    `LI(`$1')IT(shift($*))')')dnl
dnl
UL(foo,bar,baz)
UL(`UL',`LI',`IT')
UL(``UL'',``LI'',``IT'')

In the first solution, the final invocation of UL is not exactly what I expect, but I'm so unsure of m4 quoting rules that I really don't know what to expect, so I suspect there is a quoting error. Help appreciated.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top