main.scm 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. (use-modules (commonmark)
  2. (sxml simple))
  3. ;; =======
  4. ;; HELPERS
  5. ;; =======
  6. (define displayln
  7. (λ (sth)
  8. (display (simple-format #f "~a\n" sth))))
  9. (define (map* proc lst)
  10. (cond [(null? lst) '()]
  11. [(pair? (car lst))
  12. (cons (map* proc (car lst))
  13. (map* proc (cdr lst)))]
  14. [else
  15. (cons (proc (car lst))
  16. (map* proc (cdr lst)))]))
  17. (define (stringify* lst)
  18. (map* (λ (val)
  19. (cond
  20. [(number? val) (number->string val)]
  21. [(string? val) val]
  22. [else (simple-format #f "~s" val)]))
  23. lst))
  24. ;; =============================
  25. ;; LIBRARY INTERFACE ABSTRACTION
  26. ;; =============================
  27. (define* (md-file->sxml file-path #:key (encoding "UTF-8"))
  28. (call-with-input-file file-path
  29. (λ (port)
  30. (set-port-encoding! port encoding)
  31. (md-string->sxml port))))
  32. (define* (md-file->xml-string file-path #:key (encoding "UTF-8"))
  33. (sxml->xml-string
  34. (md-file->sxml file-path #:encoding encoding)))
  35. (define* (md-string->sxml input)
  36. ;; takes a string or a port
  37. (commonmark->sxml input))
  38. (define* (sxml->xml-file sxml-tree file-path #:key (encoding "UTF-8"))
  39. (call-with-output-file file-path
  40. (λ (port)
  41. (set-port-encoding! port encoding)
  42. (sxml->xml sxml-tree port))))
  43. (define* (sxml->xml-string sxml-tree)
  44. (call-with-output-string
  45. (λ (port)
  46. (sxml->xml sxml-tree port))))
  47. (define document
  48. (string-join '("A CommonMark document"
  49. "============="
  50. ""
  51. "1. here is a list"
  52. "2. with another item"
  53. ""
  54. " this is some code"
  55. ""
  56. "A regular paragraph")
  57. "\n"))
  58. ;; =============
  59. ;; USAGE EXAMPLE
  60. ;; =============
  61. (define (main)
  62. (displayln "read from file")
  63. (displayln (md-file->xml-string "example.md"))
  64. (displayln "read from string")
  65. (displayln (md-string->sxml document))
  66. (displayln "write to file")
  67. (displayln (sxml->xml-file (md-string->sxml document) "example.html"))
  68. (displayln "write to string")
  69. (displayln (sxml->xml-string (md-string->sxml document))))
  70. (main)