Browse Source

editor: generalize `process-content`

This way the same logic can be re-used for saving and loading files
main
J. Sims 8 months ago
parent
commit
40759bcbd4
  1. 27
      editor.hy

27
editor.hy

@ -19,7 +19,7 @@ This application will serve as a platform to edit and deploy posts.
;; 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}"))
(Template
"{% 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<p>\n\t\t${content}\t</p>\n{% endblock content %}"))
;; Setup top-level and main Frame
@ -67,13 +67,28 @@ This application will serve as a platform to edit and deploy posts.
:title (.get post-title)
:date post-date
:summary (.get post-summary "1.0" "end")
:content (process-content (.get post-content "1.0" "end"))))))
:content (process-content-write (.get post-content "1.0" "end"))))))
;; string string -> string
;; process raw-input according to mode
;; if mode is "w", wrap paragraphs in HTML "<p>" tags
;; if mode is "r", replace HTML "<p>" tags with "\n\n"
(defn _process-content [raw-input mode]
(setv read-chars "\n\n")
(setv write-chars "\n\t</p>\n\t<p>\n\t\t")
(return (if (= mode "r")
(.replace raw-input write-chars read-chars)
(.replace raw-input read-chars write-chars))))
;; string -> string
;; split the string into paragraphs, delimited by `\n\n`,
;; and wrap them in HTML paragraph tags `<p></p>`
(defn process-content [raw-input]
(return (.replace raw-input "\n\n" "\n\t</p>\n\t<p>\n\t\t")))
;; call `_process-content` with a mode of "w"
(defn process-content-write [raw-input]
(return (_process-content raw-input "w")))
;; string -> string
;; call `_process-content` with a mode of "r"
(defn process-content-read [raw-input]
(return (_process-content raw-input "r")))
;; thunk
;; save the current contents of the editor then