And .... it works. THANKS.

On Friday, March 24, 2017 at 11:09:20 PM UTC+1, Paul Atlan wrote:
>
> Thanks Joel, I was slowly getting my way to something like this. 
> I'd figured out the weirdness of using when/splice and when, although I 
> hadn't grokked that '(@ ...) is a pollen tagged x--expression ... 
> I also had to figure out that, outside of the "string append" function, we 
> need to escape the \date{ string  :-)
>
> I'm at the point where I've actually split the latex-source into three 
> substrings that I splice together. Your solution is obviously much more 
> elegant - I'll patch that in. 
>
> Thanks again to both for taking the time to answer, and sorry Matthew for 
> the cryptic clues :-)
>
> On Friday, March 24, 2017 at 10:09:15 PM UTC+1, Joel Dueck wrote:
>>
>> It's a bit esoteric for sure. The problem is something I didn't think of 
>> when I saw Matthew's remarks because I didn't fill in the context. Short 
>> story is, you can't actually use when/splice directly inside a call to 
>> string-append.
>>
>> When its "if" condition evaluates to anything other than #f, ◊when/splice 
>> produces an X-expression with the @ symbol in front. E.g. 
>> when/splice[#t]{xyz} produces '(@ "xyz") In the context of a Pollen 
>> tagged X-expression, @ means "splice this x-expression into the 
>> surrounding one" -- or essentially, "drop the @ and the outermost 
>> parentheses".
>>
>> However, this when/splice is not directly inside a Pollen tagged 
>> X-expression. Instead it's inside a list of arguments to a string-append 
>> function call. And string-append expects everything you send it to be a 
>> string and nothing else. So you really can't use when/splice in there 
>> since it returns an X-expression and not a string.
>>
>> (Also: using symbol->string doesn't work because it expects a symbol as 
>> input, and '(@ "\\date{"...) isn't a symbol but a list.)
>>
>> The thing to be sure of, then, is that any function calls inside 
>> string-append produce only strings.
>>
>> So, one good approach would be to prepare a value, outside of 
>> string-append, which evaluates to either the entire \date{} command or 
>> an empty string, then use that:
>>
>> ◊(define maybe-date (hash-ref metas 'doc-publish-date #f))
>> ◊(define date-command (if maybe-date 
>>                           (string-append "\\date{" (pubdate->english 
>> maybe-date) "}")
>>                            ""))
>>
>> Then inside the string-append call, just replace the whole \date{} line 
>> with:
>>
>> ◊date-command
>>
>> Hopefully I've got that right...
>>
>> On Thursday, March 23, 2017 at 6:29:25 PM UTC-5, Paul Atlan wrote:
>>
>>> Thanks Matthew and Joel, 
>>> * yes, I took the brackets literally - oops
>>> * I know I could just skip the date thing, but where's the teaching 
>>> moment in that ....
>>>
>>> ... as shown by the fact that I'm still struggling: 
>>> * I've defined the "maybe-date" outside of the ◊(define latex-source 
>>> ◊string-append{} loop. So that works. 
>>> * however, 
>>> ◊when/splice[maybe-date]{
>>>  \date{◊(pubdate->english maybe-date)}}
>>> gives me:
>>>
>>> string-append: contract violation
>>>   expected: string?
>>>   given: '(@ "\\date{" "Saturday, February 20th, 2016" "}")
>>>   argument position: 74th
>>>   other arguments...:... [165 total] ...
>>>
>>>
>>> I guess instead of a string I'm outputting "something" (a quote?) that's 
>>> a concatenation o strings ...
>>> I thought I could convert this into a simple string by using 
>>> symbol->string but no joy. 
>>> I'm pretty sure I'm missing something obvious about lists and strings 
>>> here, but I can't figure out what ...
>>>
>>>
>>> On Thursday, March 23, 2017 at 3:25:23 AM UTC+1, Joel Dueck wrote:
>>>>
>>>> Matthew’s answer is great. I would just add that the main reason I made 
>>>> us of dates in the metas was that I planned to have an RSS feed for 
>>>> chapters, and things syndicated in a feed need to have a date stamp. But 
>>>> the feed is also coded so as to omit anything that doesn’t have a 
>>>> 'doc-publish-date in the metas.
>>>>
>>>> So if you won’t be publishing an RSS feed, and if your book doesn’t 
>>>> particularly need dates on each chapter, you could just remove any 
>>>> reference to the date in the templates.
>>>>
>>>> On Wednesday, March 22, 2017 at 12:20:29 AM UTC-5, Paul Atlan wrote:
>>>>>
>>>>> I'm slowly winding my way up the learning curve by adapting Joel 
>>>>> Dueck's work to a personal web-book I want to write. 
>>>>> I've hit a snag due to my limited understanding of racket, probably. 
>>>>>
>>>>>
>>>>> The template.pdf.p files contains the following line to generate a 
>>>>> nicely formatted LaTex date:
>>>>> \date{◊(pubdate->english (hash-ref metas 'doc-publish-date))}
>>>>>
>>>>> *metas *is generated from meta values in the source files, with one 
>>>>> of the keys being "doc-publish-date".
>>>>>
>>>>> *pubdate->English * is a helper function to format everything nicely. 
>>>>>
>>>>> This works perfectly if I don't forget the "doc-publish-date" key in 
>>>>> the source files. If I do (or don't want to set it), the *hash-ref* 
>>>>> fails and stops the rendering process. 
>>>>>
>>>>> The *hash-ref* documentation (hash-ref 
>>>>> <http://docs.racket-lang.org/reference/hashtables.html?q=hash-ref%20#%28def._%28%28quote._~23~25kernel%29._hash-ref%29%29>)
>>>>>  mentions 
>>>>> that one can write: 
>>>>>      
>>>>> hash-ref hash key [failure-result]
>>>>>
>>>>> were *failure-result* is either a procedure or is a value and gets 
>>>>> returned instead of the missing key-value. 
>>>>>
>>>>> I've been trying to use this in order to fail gracefully when 
>>>>> *doc-publish-date 
>>>>> *is missing:
>>>>> * either by returnnig a blank value (so the LaTex output will be " 
>>>>> \date() " 
>>>>> * or, even better, by skipping the whole \date function if the 
>>>>> *hash-ref* function fails.  
>>>>>
>>>>> Unfortunately all my efforts to set the failure-result have failed. 
>>>>> I've tried e.g. 
>>>>> \date{◊(pubdate->english (hash-ref metas 'doc-publish-date [] ))}
>>>>> in order to return an empty string, but racket chokes by telling me [] 
>>>>> is not a procedure.
>>>>>
>>>>> And I can't find reference to something resembling if-error in the 
>>>>> racket documentation. 
>>>>>
>>>>> Any pointers would be greatly appreciated!
>>>>>
>>>>> P. 
>>>>>
>>>>>
>>>>>  
>>>>>
>>>>

-- 
You received this message because you are subscribed to the Google Groups 
"Pollen" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to