abstract subprocess management

This commit is contained in:
xenia 2021-06-06 23:16:08 -04:00
parent 1d9c60bb28
commit fecf69febf
3 changed files with 37 additions and 46 deletions

View File

@ -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"))

View File

@ -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)

30
util.rkt Normal file
View File

@ -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)