> On Feb 26, 2019, at 6:53 AM, Brendan Stromberger 
> <brendanstromber...@gmail.com> wrote:
> 
> Has anyone written a library to generalize (at least to some extent) the 
> process of rendering a Pollen document to JSON?

When you say "Pollen document" I take it you mean an X-expression (because a 
rendered source file could just be stored as a string).

Here's a start for functions that could convert between X-expressions and JSON. 
One wrinkle is encoding tags in JSON. JSON doesn't have a symbol type distinct 
from a string, so in this case I've just stuffed the tag in a little hash table 
of its own.

This is not the only way to do it, of course. But JSON (like XML) is lightly 
disguised S-expressions (and all X-expressions are S-expressions) so it seems 
straightforward.


#lang racket
(require txexpr json)

(define (xexpr->json x)
  (jsexpr->string
   (let loop ([x x])
     (match x
       [(txexpr tag attrs elems) (list (hash tag "") (attrs->hash attrs) (map 
loop elems))]
       [other other]))))

(define (json->xexpr x)
  (let loop ([x (string->jsexpr x)])
    (match x
      [(list (hash-table (jstag "")) jsattrs jselems) (list* jstag (hash->attrs 
jsattrs) (map loop jselems))]
      [other other])))

(define tx '(p ((foo "bar")) "hello" (em "world")))
tx
(xexpr->json tx)
(json->xexpr (xexpr->json tx))

-- 
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 pollenpub+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to