sentences text file to markdown file

The Racket language lets me enjoy scripting, so that the adding machine does work that would be repetitive drudgery the human being. Automating “the boring stuff” is what computers are for.

This little script makes it easy for me to share pictures that have been batch-converted with Racket. (fn:1) I have a script that takes a text file of image basenames (such as “010”) with sentences and generates an html page to print out as a worksheet. The worksheets have been working well for our GDM (Graded Direct Method) class, but uploading the pictures with Markdown to WriteFreely is going to get more cumbersome as the sentences get longer and more numerous.

An hour or two of focus this evening let me WriteFreely blog the fourth lesson in a smooth manner. (fn:2)

Dr Racket Screenshot of text to markdown script

Racket code

#! /usr/bin/env racket
#lang racket

(define dir (vector-ref (current-command-line-arguments) 0))
;; allow for different versions
(define text-file
  (if (< 1 (vector-length  (current-command-line-arguments)))
      (vector-ref (current-command-line-arguments) 1)
(define working-directory (build-path (current-directory-for-user) dir))
(define text-file-path (build-path working-directory text-file))

(define get-img-and-sentences-list
  (lambda ()
    (let* ((input (open-input-file text-file-path))
	   (instring (port->string input)))
      (close-input-port input) 
      (string-split instring "\n\n"))))

(define imgbase-with-sentences (get-img-and-sentences-list))

(define imgbasestr-sentences-als
  (map (lambda (s) (string-split s "\n"))

;; for each elmnt of alist make markdown
;; ![dir imgbase](     "Sentence 1") and then ul of sentence/answers
;; ![C3-L4 010](   "This is my book.")
;;    - This is my book
;;    - My book is in my hand.

(define mdn-img-ttl
  (lambda (dr als-elm) ;; directory imgbasename
    (let ((ib (car als-elm))) ;; imgbasename
    (string-append "![" dr " " ib "]"))))

(define mdn-img-lnk
  (lambda (als-elm)
    (let ((imgalt (if (< 1 (length als-elm))
		      (cadr als-elm) "")))
      (string-append "(    " "\"" imgalt "\"" ")"))))

(define mdn-img
  (lambda (dr als-elm)
    (string-append (mdn-img-ttl dr als-elm)
		   (mdn-img-lnk als-elm) "\n\n")))

;; markdown sentences unordered list
(define mdn-sen-uls
  (lambda (als-elm) ;; alist element
    (let ((sentences (cdr als-elm)))
       (map (lambda (sen)
	      (string-append " - " sen))
;; alist element to markdown situation
(define als-elm-to-mdn-sit
  (lambda (lst)
    (let* ((imgbase (car lst))
	   (sntncs (cdr lst))
	   (md-img-ttl (string-append dir " " imgbase)))
      (string-append (mdn-img dir lst)
		     (mdn-sen-uls lst)

(define make-md-file
  (lambda (als)
    (let* ((md-file-path
	    (path-replace-extension text-file-path ".md"))
	    (open-output-file md-file-path #:exists 'replace)))
	(map als-elm-to-mdn-sit als) "\n")
      (close-output-port md-out))))
(make-md-file imgbasestr-sentences-als)

Emacs Screenshot text to markdown script

text file convention

This is my book.
My book is in my hand.

This is my book.
My book is on my head.

This is your book.
Your book is in your hand.

#RacketLanguage #FS2PF #EPGDM #Emacs #DrRacket #Programming #Scripting #Education #FreeSoftwareForFreeTeachers #FS4FT