~pepe/pan.earth

5510017de32847a5f69b06404265553cf0df809d — Josef Pospíšil 2 years ago
Initial commit
A  => LICENSE +19 -0
@@ 1,19 @@
Copyright (c) 2021 Josef Pospíšil <josef.pospisil@laststar.eu>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.

A  => README.md +3 -0
@@ 1,3 @@
# panearth

[ ] add information about the panearth

A  => content/index.mdz +6 -0
@@ 1,6 @@
{:author "Josef Pospíšil <josef.pospisil@laststar.eu>"
 :title "Pan Earth"
 :template "/templates/index"}
---

## Hello!

A  => panearth/acts.janet +263 -0
@@ 1,263 @@
(import shawn :prefix "")
(import shawn/act :prefix "")
(import shawn/cocoon)
(import chidi)
(import mendoza/markup)
(import mendoza/render)
(import spork/temple)
(import spork/path)
(import ./app)
(use ./utils)

(temple/add-loader)
(markup/add-loader)

(defact Empty (make {}))

(defact Present
  {:effect
   (fn [_ {:site-title t} _]
     (print t " construction starts"))})

(defact SetDev
  {:update (fn [_ e] (put e :dev true))})

(defn log [& msgs]
  (make {:effect (fn [&] (eprint ;msgs))}))

(defn save-content [file content]
  (make
    {:effect
     (fn [_ {:content c :public p} _]
       (def nf
         (->> file
              (string/replace c p)
              (string/replace "mdz" "html")))
       (def dir (path/dirname nf))
       (if (not (os/stat dir)) (os/mkdir dir))
       (spit nf content)
       (print "Rendered " file " to " nf))}))

(defn render-content-file [file]
  (make
    {:watch
     (fn [_ e _]
       (try
         (do
           (def {:site-title st :dev dev} e)
           (put module/cache file nil)
           (def m (require file))
           (def fc @"")
           (with-dyns [:out fc]
             ((get-in m [:template 'render-dict :value])
               (merge m
                      {:content (render/render (m :content) @"")
                       :site-title st
                       :css (process-css e)
                       :file file
                       :dev dev})))
           (save-content file fc))
         ([e] (log "Error: " e " when rendering file: " file))))}))

(defn save-markup [file markup]
  (make
    {:update
     (fn [_ e]
       (put-in e [:markups file] markup))}))

(defn markup-blog-file [file]
  (make
    {:watch
     (fn [&]
       (put module/cache file nil)
       (save-markup file (require file)))}))

(defn index? [file]
  (string/find "index" file))

(defn render-blog-file [file]
  (make
    {:watch
     (fn [_ e _]
       (try
         (do
           (def {:site-title st :dev dev :markups ms
                 :files {:blog bfiles}} e)
           (def m (ms file))
           (def fc @"")
           (with-dyns [:out fc]
             ((get-in m [:template 'render-dict :value])
               (merge m
                      {:content (render/render (m :content) @"")
                       :site-title st
                       :css (process-css e)
                       :file file
                       :dev dev}
                      (if index?
                        {:markups ms
                         :posts (filter |(not (index? $)) bfiles)}
                        {}))))
           (save-content file fc))
         ([e] (log "Error: " e " when rendering file: " file))))}))

(defact RenderContent
  {:watch
   (fn [_ {:files {:content cf}} _]
     (seq [f :in cf]
       (render-content-file f)))})

(defact RenderPosts
  {:watch
   (fn [_ {:files {:blog bf}} _]
     (seq [f :in bf]
       (render-blog-file f)))})

(defact MarkupPosts
  {:watch
   (fn [_ {:files {:blog bf}} _]
     (seq [f :in bf]
       (markup-blog-file f)))})

(defn copy-file [file ftype]
  (make
    {:effect
     (fn [_ e _]
       (def {:public p :static s} e)
       (def dir
         (path/join p (get e ftype "")))
       (if-not (os/stat dir) (os/mkdir dir))
       (def nf
         (string/replace s p file))
       (spit nf (slurp file))
       (print "Copied " file " to " nf))}))

(defact CopyFiles
  {:watch
   (fn [_ {:files {:css cf}} _]
     (seq [f :in cf]
       (copy-file f :css)))})

(defn save-files [dir files]
  (make {:update
         (fn [_ e] (put-in e [:files dir] files))}))

(defn list-mdzs [dir]
  (->> dir
       os/dir
       (map |(path/join dir $))
       (filter |(string/has-suffix? "mdz" $))))

(defact ListContent
  {:watch
   (fn [_ {:content cd} _]
     (save-files :content (list-mdzs cd)))})

(defact ListPosts
  {:watch
   (fn [_ {:blog cd} _]
     (save-files :blog (list-mdzs cd)))})

(defact ListCss
  {:watch
   (fn [_ {:static s :css cd} _]
     (save-files :css (map |(path/join s cd $)
                           (os/dir (path/join s cd)))))})

(defn notify-modify [what]
  (def p
    (os/spawn
      ["inotifywait" "-q" "-e" "modify" what]
      :p {:in :pipe :out :pipe}))
  (:wait p)
  (->> (ev/read (p :out) :all)
       (string/replace " MODIFY " "")
       string/trim))

(defact TemplatesMonitor
  {:watch
   (fn [_ {:templates t} _]
     (cocoon/give
       (forever
         (def lc (notify-modify t))
         (cocoon/emerge
           (make {:watch (fn [&] RenderContent)
                  :effect (fn [&]
                            (put module/cache lc nil)
                            (print "Modified " lc))})))
       (cocoon/die)))})

(defact ContentMonitor
  {:watch
   (fn [_ {:content c} _]
     (cocoon/give
       (forever
         (def lc (notify-modify c))
         (cocoon/emerge
           (make {:watch (fn [&] (render-content-file lc))
                  :effect (fn [&] (print "Modified " lc))})))
       (cocoon/die)))})

(defact BlogMonitor
  {:watch
   (fn [_ {:blog b} _]
     (cocoon/give
       (forever
         (def lc (notify-modify b))
         (cocoon/emerge
           (make {:watch (fn [&] [(markup-blog-file lc)
                                  (render-blog-file lc)])
                  :effect (fn [&] (print "Modified " lc))})))
       (cocoon/die)))})

(defact CssMonitor
  {:watch
   (fn [_ {:css c :static s} _]
     (cocoon/give
       (forever
         (def lc (notify-modify (path/join s c)))
         (cocoon/emerge
           (make {:watch (fn [&] [(copy-file lc :css)])
                  :effect (fn [&] (print "Modified " lc))})))
       (cocoon/die)))})

(defact CopyLogo
  {:watch
   (fn [_ {:static s :logo logo} _]
     (copy-file (path/join s logo) ""))})

(defact StartChidi
  {:watch
   (fn [_ e _]
     (def {:chidi {:host host :port port}} e)
     (cocoon/give
       (chidi/start
         (app/handler e
                      {:render-content-file render-content-file
                       :render-all [RenderContent RenderPosts]
                       :list-content ListContent})
         host port
         app/good-supervisor)
       (cocoon/die)))
   :effect
   (fn [_ {:chidi {:host host :port port}} _]
     (print "Chidi Present on " host ":" port))})

(defact Monitor
  {:watch
   (fn [&]
     [ContentMonitor
      BlogMonitor
      TemplatesMonitor
      CssMonitor])})

(defact InitRendering
  {:watch
   (fn [&]
     [ListCss
      CopyFiles
      CopyLogo
      ListContent
      ListPosts
      RenderContent
      MarkupPosts
      RenderPosts])})

A  => panearth/app.janet +89 -0
@@ 1,89 @@
(import chidi :prefix "")
(import shawn/cocoon)

(use ./utils)

(defn good-supervisor
  [chan handling]
  (var last-connection nil)
  (forever
    (match (ev/take chan)
      [:emergence act] (cocoon/emerge act)
      [:close connection] (:close connection)
      [:error fiber]
      (do
        (def err (fiber/last-value fiber))
        (eprint err)
        (unless (or (= err "Connection reset by peer")
                    (= err "stream is closed"))
          (:write last-connection (internal-server-error))
          (ev/give chan [:conn last-connection])))
      [:conn connection]
      (do
        (set last-connection connection)
        (ev/go
          (fiber/new
            (fn [] (handling connection)) :tp) nil chan)))))

(defn- html-success [next-middleware]
  (fn [req]
    (success (next-middleware req) (content-type ".html"))))

(defn layout [args]
  (page app args))

(defn dashboard [envelope]
  (fn [&]
    (def {:site-title st :css css :files fs} envelope)
    (layout @{:title "Dashboard"
              :site-title st
              :css (process-css envelope)
              :content (page dash @{:files fs})})))

(defn render [render-content-file render-all]
  (fn [{:body {"file" file}}]
    (if (= file "all")
      (cocoon/emerge render-all)
      (cocoon/emerge (render-content-file file)))
    (response 303 "" {"Location" (vf file) "Content-Length" 0})))

(defn edit [envelope]
  (fn [{:query-params {"file" file}}]
    (def {:site-title st :css css :files fs} envelope)
    (layout @{:title (string "Editing " file)
              :site-title st
              :css (process-css envelope)
              :content (page edit @{:file-content
                                    (if (= :file (os/stat file :mode))
                                      (slurp file)
                                      `{:author ""
                                        :title ""
                                        :template "/templates/"}
                                      ---`)
                                    :file-name file})})))

(defn save [list-content]
  (fn [{:body {"file-name" fnm "file-content" fc}}]
    (spit fnm (string/replace-all "\r\n" "\n" fc))
    (cocoon/emerge list-content)
    (response 303 "" {"Location" (vf fnm) "Content-Length" 0})))

(defn handler [envelope useful-acts]
  (drive
    {"/__dashboard"
     {"" (-> (dashboard envelope)
             (guard-methods "GET")
             html-success)
      "/render" (-> (render (useful-acts :render-content-file)
                            (useful-acts :render-all))
                    (guard-methods "POST")
                    urlencoded)
      "/edit" (-> (edit envelope)
                  query-params
                  (guard-methods "GET")
                  html-success)
      "/save" (-> (save (useful-acts :list-content))
                  (guard-methods "POST")
                  urlencoded)}
     :not-found (stoic "public")}
    journal))

A  => panearth/init.janet +26 -0
@@ 1,26 @@
(import shawn :prefix "")
(import /panearth/acts :prefix "")

(def shawn
  (initialize @{:site-title "Pan Earth"
                :static "static"
                :logo "logo.svg"
                :css "css"
                :content "content"
                :blog "content/posts"
                :public "public"
                :templates "templates"
                :chidi {:host "0.0.0.0" :port 7777}}))

(def env-init
  {"dev" [Present
          SetDev
          InitRendering
          StartChidi
          Monitor]
   "prod" [Present
           InitRendering]})

(defn start [env]
  (:confirm shawn ;(env-init env))
  (:admit shawn))

A  => panearth/utils.janet +20 -0
@@ 1,20 @@
(use spork)

(def . string)

(def new-file-name "new-file.mdz")

(defn vf [file]
  (->> file
       (string/replace "content" "")
       (string/replace "mdz" "html")))

(defn process-css [e]
  (def {:static s
        :css css :files
        {:css fcss}} e)
  (->>
    fcss
    (map |(string/replace (path/join s css) css $))
    (filter |(string/has-suffix? ".css" $))
    sort))

A  => pe +7 -0
@@ 1,7 @@
#!/usr/bin/env janet

(import /panearth)

(defn main [_ &opt env]
  (default env "dev")
  (panearth/start env))

A  => project.janet +12 -0
@@ 1,12 @@
(declare-project
  :name "panearth"
  :author "Josef Pospíšil <josef.pospisil@laststar.eu>"
  :description "Website for the Pan Earth"
  :license "MIT"
  :repo "https://git.sr.ht/~pepe/panearth"
  :url "https://pan.earth"
  :dependencies ["spork"
                 "mendoza"
                 "https://git.sr.ht/~pepe/shawn"
                 "https://git.sr.ht/~pepe/chidi"])


A  => static/logo.svg +0 -0
A  => templates/app.temple +10 -0
@@ 1,10 @@
{$
(use spork/temple) (add-loader)
(import /templates/head) (import /templates/foot)
$}
{- (head/render-dict args) -}
<h1>{- (args :title) -}</h1>
<div>
{- (args :content) -}
</div>
{- (foot/render-dict args) -}

A  => templates/blog-index.temple +24 -0
@@ 1,24 @@
{$
(use spork/temple) (add-loader)
(import /templates/head) (import /templates/foot)
(defn anchor [post]
  (-&gt;&gt; post
    (string/replace &quot;content&quot; &quot;&quot;)
    (string/replace &quot;mdz&quot; &quot;html&quot;)))
$}
{- (head/render-dict args) -}
<div>
  <h1>{- (args :title) -}</h1>
  <div>
     {- (args :content) -}
  </div>
  <ul>
      {% (loop [p :in (args :posts)
                :let [pa (anchor p)
                      m (get-in args [:markups p])]] %}
    <li><strong><a href="{{ pa }}">{{ (m :title) }}</a></strong>
        by {{ (m :author) }} at {{ (m :date) }}</li>
  {% ) %}
  </ul>
</div>
{- (foot/render-dict args) -}

A  => templates/blog.temple +10 -0
@@ 1,10 @@
{$
(use spork/temple) (add-loader)
(import /templates/head) (import /templates/foot)
$}
{- (head/render-dict args) -}
  <h1>{- (args :title) -}</h1>
  <div>
    {- (args :content) -}
  </div>
{- (foot/render-dict args) -}

A  => templates/dash.temple +51 -0
@@ 1,51 @@
{$ (use /panearth/utils) $}

<div>
  <h2>Content files </h2>
  <spacer></spacer>
  <form action="/__dashboard/render" method="POST">
    <input type="hidden" name="file" value="all"/>
    <button>Render all</button>
  </form>
  <a class="button" href="/__dashboard/edit?file={- new-file-name -}">New file</a>
</div>
<ul>
  {% (loop [f :in (get-in args [:files :content])
 :let [vf (->> f
               (string/replace "content" "")
               (string/replace "mdz" "html"))]] %}
  <li>
    {{ f }}
    <a href="{- vf -}">View</a>
    <form action="/__dashboard/render" method="POST">
      <input type="hidden" name="file" value="{- f -}"/>
      <button>Render</button>
    </form>
    <a href="/__dashboard/edit?file={- f -}">Edit</a>
  </li>
  {% ) %}
</ul>
<h2>Blog files </h2>
<ul>
  {% (loop [f :in (get-in args [:files :blog])
 :let [vf (->> f
               (string/replace "content" "")
               (string/replace "mdz" "html"))]] %}
  <li>
    {{ f }}
    <a class="button" href="{- vf -}">View</a>
    <form action="/__dashboard/render" method="POST">
      <input type="hidden" name="file" value="{- f -}"/>
      <button>Render</button>
    </form>
    <a class="button" href="/__dashboard/edit?file={- f -}">Edit</a>
  </li>
  {% ) %}
</ul>
<h2>Css files </h2>
<ul>
  {% (loop [f :in (get-in args [:files :css])] %}
  <li>{{ f }}</li>
  {% ) %}
</ul>


A  => templates/edit.temple +13 -0
@@ 1,13 @@
{$ (use /panearth/utils) $}
{%
(def fnm (args :file-name))
(def new? (= fnm new-file-name))
%}
<form style="width: 60rem" action="/__dashboard/save" method="POST">
  <textarea name="file-content" cols="80" rows="20">{- (args :file-content) -}</textarea>
  {- (if new? `&lt;label for=&quot;name&quot;&gt;File name&lt;/label&gt;`) -}
  <input type={- (if new? (. `&quot;text&quot; placeholder=&quot;` fnm `&quot;`) `&quot;hidden&quot;`) -}
    name="file-name" value="{- (unless new? fnm) -}"/>
  <input type="submit" value="Save" />
  <a href="/__dashboard">Cancel</a>
</form>

A  => templates/foot.temple +5 -0
@@ 1,5 @@
    <footer>
    </footer>
  </body>
</html>


A  => templates/head.temple +22 -0
@@ 1,22 @@
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>{{ (args :title) }}</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    {% (each l (reverse (args :css)) %}
      <link rel="stylesheet" href="/{{ l }}" />
    {% )%}
  </head>
  <body>
    <nav>
      <a href="/">
        <img src="/logo.svg" />
      </a>
      {% (when (args :dev) %}
      <div>
        <a href="/__dashboard/edit?file={- (args :file) -}">Edit</a>
        <a href="/__dashboard">Dashboard</a>
      </div>
      {% ) %}
    </nav>

A  => templates/index.temple +10 -0
@@ 1,10 @@
{$
(use spork/temple) (add-loader)
(import /templates/head) (import /templates/foot)
$}
{- (head/render-dict args) -}
  <div>
    <h1></h1>
    {- (args :content) -}
  </div>
{- (foot/render-dict args) -}