(use-modules (ice-9 ftw))

(texmacs-module (markdown tests))

(lazy-plugin-force)

(display "\nRunning all tests in subdirectories\n")

(define (run-test filename statinfo flag)
  (cond ((== flag 'regular)
         ;(display* "Checking " filename "\n")
         (when (string-ends? filename ".tm")
           (display* "  Converting " filename "... ")
           (with new-name (string-append (string-drop-right filename 3) ".out.md")
             (file-convert filename new-name)
             (display " done.\n")))
         #t)
        ((== flag 'directory)
         (display* "\nEntering directory: " filename "\n")
           #t)
        (else #f)))

(let* ((u (url-resolve "$TEXMACS_HOME_PATH/plugins/markdown/tests" "r"))
       (vanilla (url->system (url-append u "vanilla")))
       (hugo (url->system (url-append u "hugo")))
       (save-hugo (get-preference "texmacs->markdown:hugo-extensions"))
       (save-width (get-preference "texmacs->markdown:paragraph-width")))
  (set-preference "texmacs->markdown:flavour" "vanilla")
  (set-preference "texmacs->markdown:paragraph-width" 79)
  (ftw vanilla run-test)
  (set-preference "texmacs->markdown:flavour" "hugo")
  (set-preference "texmacs->markdown:paragraph-width" #f)
  (ftw hugo run-test)
  (set-preference "texmacs->markdown:hugo-extensions" save-hugo)
  (set-preference "texmacs->markdown:paragraph-width" save-width))
