parallel/serial pandoc conversion with scsh-process added by wasamasa on Wed Aug 25 22:41:22 2021

(import scheme)
(import (chicken base))
(import (chicken file))
(import (chicken format))
(import (chicken io))
(import (chicken pathname))
(import (chicken process))
(import (chicken process-context))
(import (srfi 1))
(import (srfi 18))
(import scsh-process)

(define (convert in-path out-path)
  (print in-path "->" out-path " ...")
  (run (pandoc -t markdown -o ,out-path ,in-path))
  (print in-path "->" out-path " [x]"))

(define (convert-parallel directories)
  (let* ((in-paths (append-map (lambda (dir)
                                 (find-files dir test: ".*\\.rst"))
                               directories))
         (threads (map (lambda (in-path)
                         (make-thread
                          (lambda ()
                            (let ((out-path (pathname-replace-extension in-path ".md")))
                              (convert in-path out-path)))))
                       in-paths)))
    (for-each thread-start! threads)
    (let loop ()
      (when (not (every (lambda (thread) (eqv? (thread-state thread) 'dead))
                        threads))
        (thread-sleep! 0.05)
        (loop)))))

(define (convert-serial directories)
  (let ((in-paths (append-map (lambda (dir)
                                (find-files dir test: ".*\\.rst"))
                              directories)))
    (for-each (lambda (in-path)
                (let ((out-path (pathname-replace-extension in-path ".md")))
                  (convert in-path out-path)))
              in-paths)))

(define main
  (case-lambda
   (()
    (fprintf stderr "usage: ~a <licenses.json> [directory]\n" (program-name))
    (exit 1))
   (directories
    (convert-parallel directories))))

(apply main (command-line-arguments))