Last active
November 9, 2017 00:01
-
-
Save lambrospetrou/18eabb55c3598a151ac8ba49a862ccf1 to your computer and use it in GitHub Desktop.
CSV to Markdown converter written in Racket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; The first line of the input is considered to be the heading! | |
; It accepts either one command line argument with the CSV file path | |
; or it reads the input from `stdin`. | |
#lang racket | |
(require csv-reading) | |
(define (getInputPort) | |
(cond | |
[(> (vector-length (current-command-line-arguments)) 0) | |
(open-input-file (vector-ref (current-command-line-arguments) 0))] | |
[#t (current-input-port)])) | |
(define (getInputLines) (csv-map (λ (l) l) (getInputPort))) | |
(define (process-line row) | |
(string-append | |
"|" | |
(foldr string-append "" (flatten (map (λ (column) | |
(list " " column " |")) row))) | |
"\n")) | |
(define (process-header heading) | |
(list | |
(process-line heading) | |
(process-line (map (λ (column) "---") heading)))) | |
(define (markdownLines inputLines) | |
(append | |
(process-header (first inputLines)) | |
(map process-line (rest inputLines)))) | |
(for-each display (markdownLines (getInputLines))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment