From 51d301073cda4d7290d578c72fe227f8ccb23df6 Mon Sep 17 00:00:00 2001 From: interstar Date: Thu, 1 Oct 2020 17:56:10 -0300 Subject: [PATCH] sorting recent changes and links in rss feeds --- src/clj_ts/card_server.clj | 24 ++++++++++++------------ src/clj_ts/pagestore.clj | 19 ++++++++++++------- src/clj_ts/server.clj | 5 +++-- src/clj_ts/static_export.clj | 26 +++++++++++++++++++++----- src/clj_ts/types.cljc | 3 +++ 5 files changed, 51 insertions(+), 26 deletions(-) diff --git a/src/clj_ts/card_server.clj b/src/clj_ts/card_server.clj index 0b08195..0e76424 100644 --- a/src/clj_ts/card_server.clj +++ b/src/clj_ts/card_server.clj @@ -41,7 +41,7 @@ (defrecord CardServerRecord - [wiki-name site-root port-no start-page facts-db page-store page-exporter] + [wiki-name site-url port-no start-page facts-db page-store page-exporter] ldb/IFactsDb (raw-db [cs] (dnn cs raw-db)) (all-pages [cs] (dnn cs all-pages) ) @@ -119,13 +119,12 @@ (defn update-pagedir! [new-pd new-ed] - (let [ - new-ps (pagestore/make-page-store - new-pd - new-ed)] + (let [new-ps + (pagestore/make-page-store + new-pd + new-ed)] (set-page-store! new-ps) - (regenerate-db!) - )) + (regenerate-db!))) (defn page-exists? [page-name] (-> (.page-store (server-state)) @@ -201,7 +200,7 @@ i "Orphan Pages" (.orphan-pages db) :orphanpages item1) :recentchanges - (let [src (pagestore/load-recent-changes ps) ] + (let [src (.read-recentchanges ps) ] (common/package-card "recentchanges" :system :markdown src src)) @@ -359,14 +358,15 @@ Bookmarked " timestamp ",, <" url "> ;; RecentChanges as RSS -(defn rss-recent-changes [] - (let [make-link (fn [s] +(defn rss-recent-changes [link-fn] + (let [ps (:page-store (server-state)) + make-link (fn [s] (let [m (re-matches #"\* \[\[(\S+)\]\] (\(.+\))" s) [pname date] [(second m) (nth m 2)]] {:title (str pname " changed on " date) - :link (pagestore/page-name->url (server-state) pname)} + :link (link-fn pname)} )) - rc (-> (pagestore/read-page (server-state) "systemrecentchanges") + rc (-> (.read-recentchanges ps) string/split-lines (#(map make-link %)))] (rss/channel-xml {:title "RecentChanges" diff --git a/src/clj_ts/pagestore.clj b/src/clj_ts/pagestore.clj index ceb4c1a..90fb5f1 100644 --- a/src/clj_ts/pagestore.clj +++ b/src/clj_ts/pagestore.clj @@ -81,6 +81,13 @@ (pages-as-new-directory-stream [this] (java.nio.file.Files/newDirectoryStream page-path "*.md")) + (read-recentchanges [ps] + (.read-system-file ps "recentchanges") ) + + (write-recentchanges! [ps new-rc] + (.write-system-file! ps "recentchanges" new-rc) + ) + ) @@ -135,18 +142,17 @@ (defn dedouble [s] (string/replace s #"\/\/" "/")) (defn page-name->url [server-state page-name] - (dedouble (str (-> server-state .site-url) "/view/" page-name)) + (dedouble (str (-> server-state :site-url) "/view/" page-name)) ) - ;; RecentChanges ;; We store recent-changes in a system file called "recentchanges". (defn update-recent-changes! [ps pagename] - (let [pn "recentchanges" - rcc (.read-system-file ps pn) + (let [ + rcc (.read-recentchanges ps) filter-step (fn [xs] (filter #(not (string/includes? % (str "[[" pagename "]]"))) xs ) ) curlist (-> rcc string/split-lines filter-step ) @@ -155,12 +161,11 @@ curlist )] (println "Updating recentchanges ... adding " pagename) - (.write-system-file! ps pn (string/join "\n" (take 80 newlist)) ) + (.write-recentchanges! ps (string/join "\n" (take 80 newlist)) ) )) -(defn load-recent-changes [ps] - (.read-system-file ps "recentchanges")) + ;; API for writing a file diff --git a/src/clj_ts/server.clj b/src/clj_ts/server.clj index 2764e7b..299e6f8 100644 --- a/src/clj_ts/server.clj +++ b/src/clj_ts/server.clj @@ -315,7 +315,7 @@ :default "HelloWorld" :parse-fn str] - ["-l" "--links LINK" "Internal Links" + ["-l" "--links LINK" "Export Links" :default "./" :parse-fn str] @@ -338,7 +338,8 @@ opts (get xs :options) ps (pagestore/make-page-store (:directory opts) (:export-dir opts)) - pe (export/make-page-exporter ps "" (:links opts))] + dx (println (:site opts) (:links opts)) + pe (export/make-page-exporter ps (:site-url opts) (:links opts))] (println " Welcome to Cardigan Bay diff --git a/src/clj_ts/static_export.clj b/src/clj_ts/static_export.clj index 7f0b969..607c73f 100644 --- a/src/clj_ts/static_export.clj +++ b/src/clj_ts/static_export.clj @@ -57,8 +57,10 @@ :export-link-pattern export-link-pattern}) (report [ex] - (str "Export Extension :\t" (ep (:export-extension ex)) " -Export Link Pattern :\t" (ep (:export-link-pattern ex)))) + (str "Export Extension :\t" (ep export-extension ) " +Export Link Pattern :\t" (ep export-link-pattern) " +Example Exported Link :\t" (.page-name->exported-link ex "ExamplePage") + )) (page-name->export-file-path [ex page-name] (-> ex .page-store .export-path @@ -67,6 +69,8 @@ Export Link Pattern :\t" (ep (:export-link-pattern ex)))) (page-name->exported-link [ex page-id] (str export-link-pattern page-id export-extension )) + (api-path [ex] + (.resolve (-> ex .page-store .export-path) "api")) (load-template [ex] (try @@ -92,6 +96,16 @@ USING DEFAULT") (defn make-page-exporter [page-store export-extension export-link-pattern] (->PageExporter page-store export-extension export-link-pattern)) + + +(defn export-recentchanges-rss [server-state] + (let [api-path (-> server-state :page-exporter .api-path) + rc-rss (.resolve api-path "rc-rss.xml") + link-fn (fn [p-name] + (str (:site-url server-state) p-name)) ] + (spit (.toString rc-rss) (card-server/rss-recent-changes link-fn)) + )) + (defn export-page [page-name server-state tpl] (let [ps (:page-store server-state) ex (:page-exporter server-state) @@ -125,7 +139,6 @@ USING DEFAULT") (defn export-all-pages [server-state] - (if (= :not-available (.all-pages server-state)) :not-exported (let [tpl (-> server-state :page-exporter .load-template) @@ -133,9 +146,12 @@ USING DEFAULT") (doseq [p-name (.all-pages server-state)] (println "Exporting " p-name) (export-page p-name server-state tpl) - )))) + ) + (println "Export recentchanges rss") + (export-recentchanges-rss server-state)))) (defn export-one-page [page-name server-state] (let [tpl (-> server-state :page-exporter .load-template)] - (export-page page-name server-state tpl)) + (export-page page-name server-state tpl) + (export-recentchanges-rss server-state)) ) diff --git a/src/clj_ts/types.cljc b/src/clj_ts/types.cljc index df222c2..5d52258 100644 --- a/src/clj_ts/types.cljc +++ b/src/clj_ts/types.cljc @@ -19,6 +19,8 @@ (report [ps]) (similar-page-names [ps p-name]) (pages-as-new-directory-stream [ps]) + (read-recentchanges [ps]) + (write-recentchanges! [ps new-rc] ) ) (defprotocol IPageExporter @@ -26,5 +28,6 @@ (page-name->export-file-path [ex page-name]) (page-name->exported-link [ex page-name]) (load-template [ex]) + (api-path [ex]) (report [ex]) )