Browse Source

Added creation of header and footer files!

Jesse Laprade 2 months ago
1 changed files with 86 additions and 34 deletions
  1. +86

+ 86
- 34
wg.fnl View File

@ -35,8 +35,31 @@
(local directories-required [:build :layout :copy :convert])
(local layout {:header ""
:footer ""})
(local files-required {:header "layout/"
:footer "layout/"})
(local contents-header
title: The title of your website
lang: en
<meta name=\"author\" content=\"Jesse Laprade\"/>
<meta name=\"description\" content=\"m455's blog\"/>
<meta name=\"keywords\" content=\"programming, documentation\"/>
[Home](/) - [Contact](/contact.html)
(local contents-footer
This website was built with [wg](
(fn map [func seq]
(let [seq []]
@ -61,9 +84,7 @@
(path-exists? (.. directory "/")))
(fn directory-create [dir]
(if (directory-exists? dir)
(print-format "Cannot create directory. '%s' already exists" dir)
(os.execute (string.format "mkdir -p %s" dir))))
(os.execute (string.format "mkdir -p %s" dir)))
;; Credit for most of this function comes from here:
@ -83,17 +104,27 @@
(table.insert seq dir)))
(fn file/directory-copy [source dest]
(os.execute (string.format "cp -r %s %s" source dest)))
(fn file-exists? [file]
(and (path-exists? file)
(not (directory-exists? file))))
(fn file-create [file]
(if (file-exists? file)
(print-format "Cannot create file. '%s' already exists" file)
(io.close ( file :w))))
(io.close ( file :w)))
(fn file/directory-copy [source dest]
(os.execute (string.format "cp -r %s %s" source dest)))
(fn file-write [file data]
(file-create file)
(with-open [file-out ( file :w)]
(file-out:write data)))
(fn files-missing []
(let [seq []]
(each [_ file (pairs files-required)]
(when (not (file-exists? file))
(table.insert seq file)))
;; ---------------------------------------
;; build
@ -104,31 +135,32 @@
;; Note: source and destination should both be full paths starting from
;; where wg.fnl is ran
(fn markdown->html [source destination]
(let [header (string.format "%s/%s" :layout (. layout :header))
footer (string.format "%s/%s" :layout (. layout :footer))
(let [header (string.format "%s/%s" :layout (. files-required :header))
footer (string.format "%s/%s" :layout (. files-required :footer))
input (string.format "%s %s %s" header source footer)
pandoc-cmd "pandoc -s -c /style.css %s -o %s --base-header-level=2"]
(os.execute (string.format pandoc-cmd input destination))))
;; TODO:
;; * Rename full-path to source
;; * Rename full-path-file to destination
;; Currently, if a directory exists, but is empty, it won't copy the
;; empty directory to the destination. If I want this behaviour, I can
;; add a let statement in the last (directory-exists? source) before the
;; recur to create a directory before it recurs.
(fn build/convert [dir]
(each [_ v (pairs (directory-contents dir))]
(let [full-path (.. dir "/" v)] ;; full-path is something like `convert/path/to/some/`
(if (and (file-exists? full-path)
(string.match full-path ".md")) ;; Only try to convert files with a .md suffix
(let [full-path-dir-pre (string.gsub full-path "(.*/).*.md" "%1") ;; returns convert/some/path/
full-path-dir (string.gsub full-path-dir-pre "^convert/" "build/") ;; returns build/some/path/
full-path-file-pre (string.gsub full-path ".md" ".html") ;; returns convert/some/path/file.html
full-path-file (string.gsub full-path-file-pre "^convert/" "build/")] ;; returns build/some/path/file.html
(let [source (.. dir "/" v)] ;; source will build up a long path through each recur, creating something like `convert/path/to/some/`
(if (and (file-exists? source)
(string.match source ".md")) ;; Only try to convert files with a .md suffix
(let [destination-dir-pre (string.gsub source "(.*/).*.md" "%1") ;; returns convert/some/path/
destination-dir (string.gsub destination-dir-pre "^convert/" "build/") ;; returns build/some/path/
destination-pre (string.gsub source ".md" ".html") ;; returns convert/some/path/file.html
destination (string.gsub destination-pre "^convert/" "build/")] ;; returns build/some/path/file.html
;; This (directory-create ...) is here, so Pandoc doesn't try
;; to output files into directories that don't exist yet.
(when (not (directory-exists? full-path-dir))
(directory-create full-path-dir))
(markdown->html full-path full-path-file))
(directory-exists? full-path)
(build/convert full-path)))))
(when (not (directory-exists? destination-dir))
(directory-create destination-dir))
(markdown->html source destination))
(directory-exists? source)
(build/convert source)))))
(fn build []
(file/directory-copy (.. :copy "/*") :build)
@ -138,30 +170,50 @@
;; init
;; ---------------------------------------
(fn init/start []
;; Create required directories
(each [_ dir (pairs directories-required)]
(when (not (directory-exists? dir))
(print-format "Creating '%s' directory..." dir)
(directory-create dir))))
(print-format "Creating '%s/'..." dir)
(directory-create dir)))
;; Create and populate required files
(let [file-header (. files-required :header)
file-footer (. files-required :footer)
files-required-seq [file-header file-footer]]
;; Create required files
(each [_ file (pairs files-required-seq)]
(when (not (file-exists? file))
(print-format "Creating '%s'..." file)
(file-create file)))
;; Populate required files
(file-write file-header contents-header)
(file-write file-footer contents-footer)))
(fn init/read-input []
(let [input ( 1)]
(if (= input :y)
(print "Cancelled creation of directories"))))
(print "Cancelled creation of directories or files"))))
(fn init/prompt []
(print "The following directories will be created:")
(print "The following directories or files will be created:")
;; List missing directories
(each [_ dir (pairs directories-required)]
(when (not (directory-exists? dir))
(print-format " %s" dir)))
(print-format " %s/" dir)))
;; List missing files
(each [_ file (pairs files-required)]
(when (not (file-exists? file))
(print-format " %s" file)))
(print "Do you want to create them? (y/n)")
(io.write "> ")
(fn init []
(if (> (# (directories-missing)) 0)
(if (or (> (# (directories-missing)) 0)
(> (# (files-missing)) 0))
(print "Required directories already exist")))
(print "Required directories or files already exist")))
;; Uncomment these to test