Skip to content

Instantly share code, notes, and snippets.

@bowbahdoe

bowbahdoe/bb.edn Secret

Created May 27, 2022 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bowbahdoe/c0227ea32e9d307e25f548674627b0fa to your computer and use it in GitHub Desktop.
Save bowbahdoe/c0227ea32e9d307e25f548674627b0fa to your computer and use it in GitHub Desktop.
{:paths ["."]
:deps {babashka/fs {:mvn/version "0.1.6"}
babashka/process {:mvn/version "0.1.1"}
clj-rss/clj-rss {:mvn/version "0.4.0"}}}
(ns build
(:require [babashka.process :refer [process]]
[babashka.fs :as fs]
[clojure.string :as string]
[clojure.edn :as edn]
[clj-rss.core :as clj-rss])
(:import (java.nio.file Files)
(java.nio.file.attribute FileAttribute)
(java.time LocalDate ZoneId)
(java.time.format DateTimeFormatter)))
(defn load-posts []
(reverse
(sort-by
(fn [post]
(LocalDate/parse (:date post) (DateTimeFormatter/ofPattern
"M/d/u")))
(for [markdown-file (map str (fs/glob "./pages" "**{.md}"))]
(let [[metadata contents] (string/split (slurp markdown-file) #"\n" 2)
original-contents contents
metadata (edn/read-string metadata)
temp-file (Files/createTempFile "" ".md" (into-array FileAttribute []))
contents (str contents "\n\n----\n #### [<- Index](/) \n")
contents (str contents "<script src=\"https://utteranc.es/client.js\"\n repo=\"bowbahdoe/mccue.dev-comments\"\n issue-term=\"pathname\"\n theme=\"github-light\"\n crossorigin=\"anonymous\"\n async>\n</script>")
contents (if (= (:type metadata) :post)
(str "<div style=\"display: inline\"> by: <b style=\"display: inline\"> "
(or (:author metadata) "Ethan McCue")
"</b></div>\n "
contents)
contents)
contents (str contents "<style>div.sourceCode { padding:10px; } code.sourceCode { font-size:85%; }")
_ (spit (str temp-file) contents)]
(merge metadata
{:original-contents original-contents
:contents contents
:temp-file temp-file
:markdown-file markdown-file}))))))
(defn pandoc [{:keys [in out title]}]
(let [cmd ["pandoc" "-F" "mermaid-filter" "--from" "gfm" "--to" "html" "--standalone"
"--highlight-style=zenburn"
in
"--lua-filter=links-to-html.lua"
"--metadata" (str "title=" title)
"-o"
(str "./target/" out)]]
@(process cmd {:inherit true})))
(defn write-index [posts]
(let [index-contents (slurp "index.md")
{posts :post
questions :question} (group-by :type posts)
questions (map (fn [question] (update question :title #(str "Q: " %)))
questions)
temp-file (Files/createTempFile "" ".md" (into-array FileAttribute []))
post-lines (fn [posts]
(string/join "\n"
(for [post posts
:when (not (:hidden post))]
(str "\n* `(" (:date post) ")` ["
(:title post) "](" (:markdown-file post) ")"))))
contents (as-> index-contents _
(string/replace _ #"&posts&"
(post-lines posts))
(string/replace _ #"&questions&"
(post-lines questions)))]
(spit (str temp-file) contents)
(spit "out.md" contents)
(pandoc {:in (str temp-file)
:out "index.html"
:title "mccue.dev"})))
(defn post-path
[post]
(str (subs (:markdown-file post) 0 (- (count (:markdown-file post)) 3))))
(defn write-posts
[posts]
(doseq [post posts]
(pandoc {:in (str (:temp-file post))
:out (str (post-path post) ".html")
:title (:title post)})))
(defn write-recipes
[]
(doseq [recipe (map str (fs/glob "./recipes/mary_mathews" "**{.docx}"))]
(let [cmd ["pandoc" "--to" "html" "--standalone"
recipe
"--lua-filter=links-to-html.lua"
"-o"
(str "./target/"
(subs recipe 0 (- (count recipe) 5)) ".html")]]
@(process cmd {:inherit true}))))
(defn write-rss
[posts]
(let [content (apply clj-rss/channel-xml
(cons {:title "mccue.dev"
:link "https://mccue.dev"
:description ""}
(map (fn [post]
{:title (:title post)
:link (str "https://mccue.dev/" (post-path post))
:description (:original-contents post)
:pubDate (.toInstant
(.atStartOfDay
(LocalDate/parse (:date post) (DateTimeFormatter/ofPattern
"M/d/u"))
(ZoneId/of "EST" ZoneId/SHORT_IDS)))})
(filter (complement :hidden)
posts))))]
(spit "./target/feed.xml" content)
(spit "./target/feed" content)))
(defn write-sheepmaster
[]
(let [work "sheepmaster.docx"
cmd ["pandoc" "--to" "html" "--standalone"
work
"--lua-filter=links-to-html.lua"
"-o"
(str "./target/writing/macaulay/"
(subs work 0 (- (count work) 5)) ".html")]]
@(process cmd {:inherit true})))
(defn write-404
[]
(pandoc
{:in "404.md"
:out "404.html"
:title "404"}))
(let [posts (load-posts)]
(write-posts posts)
(write-index posts)
(write-rss posts)
(write-recipes)
(write-sheepmaster)
(write-404)
(fs/copy-tree "assets" "target" {:replace-existing true}))

{:type :question :title "How do I stop a thread" :date "08/01/21"}

Question from Arbee#3030:

How do i stop a thread?

And how do I stop my bot then? lol

You can interrupt the thread, or cancel the task and then it is up to your code to check the Thread.isInterrupted flag.

Some built in methods which throw interrupted exceptions will do that check for you (like those on HttpClient) but without shutting down you cannot force a thread to stop.

Im kinda lost rn... how do I stop my bot without stopping the thread it is running in?

well - you can communicate to the bot's thread

And how do I do that?

Sharing a reference to an AtomicBoolean would do the trick.

So if your bot has an AtomicBoolean it can check it occasionally to see if the value is true. If it's true, it can clean itself up - and atomic boolean is safe to change from another thread.

But it might be difficult depending on how your bot framework is built

Repos

bowbahdoe/magic-bean - GitHub bowbahdoe/leftright-map-java - GitHub bowbahdoe/edn-format - GitHub bowbahdoe/microhttp-ring-adapter - GitHub

Posts

&posts&

Q/A

&questions&

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment