Browse Source

Initial editor implementation

* GUI layout
* file saving

* load function
* deploy function
J. Sims 8 months ago
  1. 101


@ -0,0 +1,101 @@
HTML writer for
This application will serve as a platform to edit and deploy posts.
;; TODO:
;; * load function
;; * deploy function
(import [datetime [date]])
(import [string [Template]])
(import [tkinter [*]])
(import [tkinter [ttk]])
;; Post template
;; This is, essentially, a direct copy of the de facto standard layout
;; used for hand-written posts. This should only be changed after by-hand
;; experimentation in hand-written test posts, and it should be applied to
;; all existing posts before being changed here.
(setv post-template
(Template ;"${title}\n${date}\n${summary}\n${content}"))
"{% if type == \"rss\" %}\n\t{% extends \"rss-item.xml\" %}\n{% else %}\n\t{% extends \"blog-post.html\" %}\n{% endif %}\n\n{% block title %}${title}{% endblock title %}\n{% block date %}${date}{% endblock date %}\n{% block summary %}\n\t${summary}{% endblock summary %}\n{% block content %}\n\t${content}\n{% endblock content %}"))
;; Setup top-level and main Frame
(setv root (Tk))
(.title root "Post Editor (for posting)")
(setv mainframe (.Frame ttk root :padding "3 3 12 12"))
(.grid mainframe :column 0 :row 0 :sticky '(N W E S))
(.columnconfigure root 0 :weight 1)
(.rowconfigure root 0 :weight 1)
;; Setup widgets for writing posts
(setv post-title (StringVar))
(setv title-entry (.Entry ttk mainframe :width 70 :textvariable post-title))
(.grid title-entry :column 1 :row 0 :sticky '(N W))
(.grid (.Label ttk mainframe :text "Title") :column 0 :row 0 :sticky '(W))
; post-date generated with appropriate datetime function on save
(setv post-summary (Text mainframe :width 80 :height 5 :wrap "word"))
(.grid post-summary :column 1 :row 1 :sticky '(N W))
(.grid (.Label ttk mainframe :text "Summary") :column 0 :row 1 :sticky '(W))
(setv post-content (Text mainframe :width 80 :height 25 :wrap "word"))
(.grid post-content :column 1 :row 2 :sticky '(N W))
(.grid (.Label ttk mainframe :text "Content") :column 0 :row 2 :sticky '(W))
;; Define functionality
;; thunk
;; load a file into the content boxes
;; TODO:
;; * look into Jinja loading facilities
(defn load [#* args]
;; thunk
;; save current contents of editor to disk
;; TODO:
;; * look into Jinja writing facilities
;; * possibly add alternate draft saving?
(defn save [#* args]
(setv post-dir "templates/posts/")
(setv post-date (.isoformat (.today date)))
(with [fd (open (+ post-dir post-date ".html") "w")]
(.write fd (.safe-substitute
:title (.get post-title)
:date post-date
:summary (.get post-summary "1.0" "end")
:content (process-content (.get post-content "1.0" "end"))))))
;; string -> string
;; split the string into paragraphs, delimited by `\n\n`,
;; and wrap them in HTML `<p><\p>` pairs
(defn process-content [raw-input]
(return (+ "<p>\n\t\t"
(.replace raw-input "\n\n" "\n\t</p>\n\t<p>\n\t\t")
;; thunk
;; save the current contents of the editor then
;; execute a shell script to deploy the website
(defn deploy [#* args]
;; Setup functionality buttons
(.grid (.Button ttk mainframe :text "Load" :command load) :column 0 :row 3 :sticky '(N S))
(.grid (.Button ttk mainframe :text "Save" :command save) :column 1 :row 3 :sticky '(N S))
(.grid (.Button ttk mainframe :text "Deploy" :command deploy) :column 2 :row 3 :sticky '(N S))
;; Final adjustments and run
(for [child (.winfo-children mainframe)]
(.grid-configure child :padx 5 :pady 5))
(.mainloop root)