On Sat, Feb 10, 2018 at 12:14:26AM +0100, Ricardo Wurmus wrote: > pelzflorian (Florian Pelz) writes: > > > How for example would this excerpt from my website better be rendered > > as XML or is this or another non-XML approach better? > > I’d much prefer XML here. One reason is that I find it very hard to > understand the syntax and how it is processed in your example. […] I agree. > The translatable text could be written as two or more strings (it > doesn’t matter that they end up in the same paragraph). […] Yes, you are right, the context will still be clear for translators because the consecutive line numbers are next to each other in the PO file and they do not appear at two unrelated places in the code. > With sxpath we can easily pick tagged substrings by specifying their > path. Or we could fold over the parse tree and transform it by applying > an arbitrary transformation. > > Here’s a trivial example of such a transform: > > --8<---------------cut here---------------start------------->8--- > (use-modules (sxml simple) (sxml transform)) > ;; This is the translated string, wrapped in some tag to make it a valid > ;; XML fragment. > (define tr "Click here") > > ;; Convert to SXML, then transform it. > (pre-post-order (xml->sxml tr) > ;; When we find the tag “link” wrap the contents in a URL anchor. > `((link . ,(lambda (tag . kids) > `(a (@ (href "http://gnu.org")) ,kids))) > ;; Just wrap the contents in a tag for everything else > (*default* . ,(lambda (tag . kids) `(,tag ,@kids))) > ;; Unwrap all text > (*text* . ,(lambda (_ txt) txt)))) > > => (*TOP* (translation "Click " (a (@ (href "http://gnu.org")) "here"))) > --8<---------------cut here---------------end--------------->8--- > > Using “pre-post-order” directly is verbose as you need to specify the > *default* and *text* handlers, but this can easily be hidden by a > friendlier procedure that would present a user interface that wouldn’t > look too different from what your macro presents to users. > > --8<---------------cut here---------------start------------->8--- > (define (foo str . handlers) > (pre-post-order (xml->sxml (string-append "" str "")) > `(,@handlers > (*default* . ,(lambda (tag . kids) `(,tag ,@kids))) > (*text* . ,(lambda (_ txt) txt))))) > > (foo "Click here" > `(link . ,(lambda (tag . contents) > `(a (@ (href "/help")) ,@contents)))) > --8<---------------cut here---------------end--------------->8--- > So I took your approach and ran with it: https://pelzflorian.de/git/pelzfloriande-website/commit/?id=77e1c30532f0bf834ddc6709c7883c17bb46740f I did not use SXPath. This one still looks ugly: (div (@ (id "powered-by")) ,@(__ "Powered by GNU Guile and \ Haunt." `(link . ,(lambda (tag attr text) (a-href (cadadr attr) text))))) I suppose I should have used SXPath for it to get the URL out of the (@ (url "…")) attribute? Thank you all so much for your code and your help. Regards, Florian