diff --git a/scribblings/utils.scrbl b/scribblings/utils.scrbl index e54c343..ff5c9c7 100644 --- a/scribblings/utils.scrbl +++ b/scribblings/utils.scrbl @@ -169,6 +169,19 @@ violation for func will be generated. @examples[#:eval utils-evaluator (map-sexp (λ (x) (add1 (cast x Number))) '(1 2 (4 10) 3)) ]} + +@defproc*[([(read-org-sexp [str String]) Any] + [(unorg [str String]) Any])]{ + +Reads a @racket[sexp] from a string produced by Org-mode for a named table. + +@racket[unorg] is a shortcut for @racket[read-org-sexp]. + +@examples[#:eval utils-evaluator +(unorg "(#t \"#t\" \"#t \" '(1 2 \"#f\"))") +]} + + @section{Additional graph utilities} @section{Pretty printing} diff --git a/utils-untyped.rkt b/utils-untyped.rkt index c1413f8..8d2b710 100644 --- a/utils-untyped.rkt +++ b/utils-untyped.rkt @@ -10,9 +10,7 @@ (provide ;; Functions - (contract-out [read-org-sexp (-> string? (listof any/c))] - [unorg (-> string? (listof any/c))] - [unstringify-pairs (-> (listof (general-pair/c string? any/c)) + (contract-out [unstringify-pairs (-> (listof (general-pair/c string? any/c)) (listof (general-pair/c symbol? any/c)))] [read-org-variable-mapping (-> string? variable-mapping?)] [unorgv (-> string? variable-mapping?)] diff --git a/utils.rkt b/utils.rkt index b9c02b8..ce5e8f0 100644 --- a/utils.rkt +++ b/utils.rkt @@ -6,6 +6,7 @@ eval-with eval1-with extract-symbols any->string stringify-variable-mapping string->any map-sexp + read-org-sexp unorg ;; Syntax auto-hash-ref/explicit auto-hash-ref/:) @@ -185,3 +186,18 @@ (test-case "map-sexp" (check-equal? (map-sexp (λ (x) (add1 (cast x Number))) '(1 2 (4 10) 3)) '(2 3 (5 11) 4)))) + +(: read-org-sexp (-> String Any)) +(define read-org-sexp + (compose ((curry map-sexp) (match-lambda + [(and (? string?) str) (string->any str)] + [x x])) + string->any)) +(define unorg read-org-sexp) + +(module+ test + (test-case "read-org-sexp" + (check-equal? (read-org-sexp "((\"a\" \"(and a b)\") (\"b\" \"(or b (not a))\"))") + '((a (and a b)) (b (or b (not a))))) + (check-equal? (unorg "(#t \"#t\" \"#t \" '(1 2 \"#f\"))") + '(#t #t #t '(1 2 #f)))))