Skip to content

Instantly share code, notes, and snippets.

@lambrospetrou
Last active November 9, 2017 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lambrospetrou/18eabb55c3598a151ac8ba49a862ccf1 to your computer and use it in GitHub Desktop.
Save lambrospetrou/18eabb55c3598a151ac8ba49a862ccf1 to your computer and use it in GitHub Desktop.
CSV to Markdown converter written in Racket
; 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