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)
#! /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) "sentences.txt")) (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")) imgbase-with-sentences)) ;; 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))) (string-join (map (lambda (sen) (string-append " - " sen)) sentences) "\n")))) ;; 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) "\n\n")))) (define make-md-file (lambda (als) (let* ((md-file-path (path-replace-extension text-file-path ".md")) (md-out (open-output-file md-file-path #:exists 'replace))) (display (string-join (map als-elm-to-mdn-sit als) "\n") md-out) (close-output-port md-out)))) (make-md-file imgbasestr-sentences-als)
text file convention
010 This is my book. My book is in my hand. 020 This is my book. My book is on my head. 030 This is your book. Your book is in your hand. ...
- (fn:1) https://write.as/bs2lr/batch-convert-image-files-with-racket
- (fn:2) https://wordsmith.social/bs2gdmteacher/s1-c3-l4