From fecf69febf0e2e2856717f8d475e98310b95395e Mon Sep 17 00:00:00 2001 From: haskal Date: Sun, 6 Jun 2021 23:16:08 -0400 Subject: [PATCH] abstract subprocess management --- ext-mathml/main.rkt | 26 +++----------------------- ext-syntax/main.rkt | 27 ++++----------------------- util.rkt | 30 ++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 46 deletions(-) create mode 100644 util.rkt diff --git a/ext-mathml/main.rkt b/ext-mathml/main.rkt index e27781c..77eb963 100644 --- a/ext-mathml/main.rkt +++ b/ext-mathml/main.rkt @@ -1,7 +1,7 @@ #lang racket/base -(require racket/list racket/match racket/port racket/runtime-path racket/string - xml) +(require racket/list racket/match racket/runtime-path racket/string xml + "../util.rkt") (define-runtime-path *js-path* "./mathml-render.js") @@ -14,27 +14,7 @@ (map transform-xexpr xexprs)) (define (run-mathjax source block?) - (define-values [proc out in err] - (subprocess #f #f #f "/usr/bin/env" "node" (path->string *js-path*))) - (define out-str #f) - (define err-str "") - (define out-reader (thread (λ () (set! out-str (port->string out))))) - (define err-reader (thread (λ () (set! err-str (port->string err))))) - - (write-string source in) - (flush-output in) - (close-output-port in) - - (subprocess-wait proc) - (thread-wait out-reader) - (thread-wait err-reader) - - (define err-trimmed (string-trim err-str)) - (unless (string=? "" err-trimmed) - (error "mathjax process raised error!" err-trimmed)) - - (unless out-str - (error "mathjax process didn't return anything!")) + (define out-str (run-external (list "/usr/bin/env" "node" (path->string *js-path*)) source)) (match-define (list 'math attrs bodies ...) (string->xexpr out-str)) `(math ,(cons (list 'display (if block? "block" "inline")) diff --git a/ext-syntax/main.rkt b/ext-syntax/main.rkt index eab53d5..0c1c1c7 100644 --- a/ext-syntax/main.rkt +++ b/ext-syntax/main.rkt @@ -1,6 +1,7 @@ #lang racket/base -(require racket/list racket/match racket/port racket/runtime-path racket/string xml) +(require racket/list racket/match racket/runtime-path racket/string xml + "../util.rkt") (define-runtime-path *python-path* "syntax-render.py") (define-runtime-path *css-path* "css") @@ -14,28 +15,8 @@ (map transform-xexpr xexprs)) (define (run-pygments lang source) - (define-values [proc out in err] - (subprocess #f #f #f "/usr/bin/env" "python3" (path->string *python-path*) lang)) - (define out-str #f) - (define err-str "") - (define out-reader (thread (λ () (set! out-str (port->string out))))) - (define err-reader (thread (λ () (set! err-str (port->string err))))) - - (write-string source in) - (flush-output in) - (close-output-port in) - - (subprocess-wait proc) - (thread-wait out-reader) - (thread-wait err-reader) - - (define err-trimmed (string-trim err-str)) - (unless (string=? "" err-trimmed) - (error "pygments process raised error!" err-trimmed)) - - (unless out-str - (error "pygments process didn't return anything!")) - + (define out-str + (run-external (list "/usr/bin/env" "python3" (path->string *python-path*) lang) source)) (string->xexpr out-str)) (define (transform-xexpr xexpr) diff --git a/util.rkt b/util.rkt new file mode 100644 index 0000000..e5b63c4 --- /dev/null +++ b/util.rkt @@ -0,0 +1,30 @@ +#lang racket/base + +(require racket/string racket/port) + +(provide run-external) + +(define (run-external argv source) + (define-values [proc out in err] + (apply subprocess #f #f #f "/usr/bin/env" argv)) + (define out-str #f) + (define err-str "") + (define out-reader (thread (λ () (set! out-str (port->string out))))) + (define err-reader (thread (λ () (set! err-str (port->string err))))) + + (write-string source in) + (flush-output in) + (close-output-port in) + + (subprocess-wait proc) + (thread-wait out-reader) + (thread-wait err-reader) + + (define err-trimmed (string-trim err-str)) + (unless (string=? "" err-trimmed) + (error "child process raised error!" err-trimmed)) + + (unless out-str + (error "child process didn't return anything!")) + + out-str)