Telling apart source of first argument #65

Open
opened 4 years ago by basus · 2 comments
basus commented 4 years ago (Migrated from github.com)

If I'm understanding things correctly, both ◊tagfn["arg"] and ◊tagfn{arg} will pass "arg" as the first argument to the tagfn function. Is there some way to tell these two cases apart? I'm thinking about tag functions where the argument between [] could be optional. For example, a ◊code tag where ◊code['lang]{...} treats the stuff inside {} as 'lang but treats it as plaintext if it's just ◊code{...}.

If I'm understanding things correctly, both `◊tagfn["arg"]` and `◊tagfn{arg}` will pass "arg" as the first argument to the `tagfn` function. Is there some way to tell these two cases apart? I'm thinking about tag functions where the argument between [] could be optional. For example, a `◊code` tag where `◊code['lang]{...}` treats the stuff inside {} as `'lang` but treats it as plaintext if it's just `◊code{...}`.
otherjoel commented 4 years ago (Migrated from github.com)

There’s not a way to tell them apart strictly by whether they occur in [ ] vs { }. I’m prettry sure that information gets discarded by the reader.

But you know that anything occurring inside the { } will (almost always) be either a string or a tagged X-expression. You could take advantage of that by testing if the first argument is a plain symbol:

(define (code . elems)
  (define-values (language code-elems)
    (cond [(symbol? (car elems)) (values (car elems) (cdr elems))]
          [else (values 'plaintext elems)]))
  ; ... now do what you like with code-elems depending on language
)

Or, if you don’t mind being a bit more verbose in your markup, you can use keyword arguments to implement optional stuff. E.g., ◊code[#:lang 'python]{...}. And use define-tag-function to handle those automatically

(require pollen/tag txexpr)

(define-tag-function (code attrs elems)
  (define language (attr-ref attrs 'lang 'plaintext)) ; supply a default value
  ; ... now do what you like with elems depending on language
)
There’s not a way to tell them apart strictly by whether they occur in `[ ]` vs `{ }`. I’m prettry sure that information gets discarded by the reader. But you know that anything occurring inside the `{ }` will (almost always) be either a string or a tagged X-expression. You could take advantage of that by testing if the first argument is a plain symbol: ```racket (define (code . elems) (define-values (language code-elems) (cond [(symbol? (car elems)) (values (car elems) (cdr elems))] [else (values 'plaintext elems)])) ; ... now do what you like with code-elems depending on language ) ``` Or, if you don’t mind being a bit more verbose in your markup, you can use keyword arguments to implement optional stuff. E.g., `◊code[#:lang 'python]{...}`. And use `define-tag-function` to handle those automatically ```racket (require pollen/tag txexpr) (define-tag-function (code attrs elems) (define language (attr-ref attrs 'lang 'plaintext)) ; supply a default value ; ... now do what you like with elems depending on language ) ```
mbutterick commented 4 years ago (Migrated from github.com)

I’m pretty sure that information gets discarded by the reader.

Yes, that’s correct.

> I’m pretty sure that information gets discarded by the reader. Yes, that’s correct.
This repo is archived. You cannot comment on issues.
No Milestone
No project
No Assignees
1 Participants
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: mbutterick/pollen-users#65
Loading…
There is no content yet.