From 547ce09b45d770e591d078548a196ac6e73e452e Mon Sep 17 00:00:00 2001 From: SouthFox Date: Fri, 1 Sep 2023 15:39:24 +0800 Subject: [PATCH] [back/refactor] fetch and build post --- src/main/backend/handlers.clj | 58 ++++++++++++++++------------------ src/main/backend/hugo/site.clj | 52 ++++++++++++++++-------------- 2 files changed, 56 insertions(+), 54 deletions(-) diff --git a/src/main/backend/handlers.clj b/src/main/backend/handlers.clj index 14e5b58..bb43c52 100644 --- a/src/main/backend/handlers.clj +++ b/src/main/backend/handlers.clj @@ -11,18 +11,15 @@ (generate-string content)) (defn clean-html - [docs hugo] - (let [replace-str (if hugo - "/hp/" - "#/item/")] - (-> (.select docs "style[data-emotion-css~=^[a-z0-9]*$]") - (.remove)) - (doseq [a (.select docs "a")] - (.attr a "href" - (-> (.attr a "href") - (str/replace "https://link.zhihu.com/?target=" "") - (str/replace "https://zhuanlan.zhihu.com/p/" replace-str) - (url-decode)))))) + [docs replace-str] + (-> (.select docs "style[data-emotion-css~=^[a-z0-9]*$]") + (.remove)) + (doseq [a (.select docs "a")] + (.attr a "href" + (-> (.attr a "href") + (str/replace "https://link.zhihu.com/?target=" "") + (str/replace "https://zhuanlan.zhihu.com/p/" replace-str) + (url-decode))))) (defn clean-images [docs] @@ -53,38 +50,39 @@ (apply str (mapv build-catalog-item catalog)))) (defn process-hu-post - [page hugo] + [page {:keys [content-type replace-str wrap-fn]}] (let [post-content (.getElementsByClass page "Post-RichTextContainer") title (.getElementsByClass page "Post-Title") post-time (.getElementsByClass page "ContentItem-time")] - (clean-html post-content hugo) + (clean-html post-content replace-str) (clean-images post-content) (render-linkcard post-content) {:status 200 - :body {:content (.toString post-content) - :title (.text title) - :time (first (str/split (.text post-time) #"・")) - :catalog (build-catalog post-content)}})) - + :headers content-type + :body (wrap-fn + {:content (.toString post-content) + :title (.text title) + :time (first (str/split (.text post-time) #"・")) + :catalog (build-catalog post-content)})})) (defn fetch-hu-post - [post-id & {:keys [hugo]}] + [post-id params] (let [post-url (str/join ["https://zhuanlan.zhihu.com/p/" post-id]) page (-> (client/get post-url) :body Jsoup/parse) docs (.getElementsByClass page "Post-RichTextContainer")] - (if (empty? docs) - {:status 404 - :body {:content "Not Found" - :title "Not Found"}} - (process-hu-post page hugo)))) + {:status 404 + :headers (:content-type params) + :body {:content "Not Found" + :title "Not Found"}} + (process-hu-post page params)))) (defn build-api-hu-post [request] - (let [post-id (-> request :path-params :id) - content (fetch-hu-post post-id)] - {:status (:status content) - :headers {"Content-Type" "application/json; charset=utf-8"} - :body (wrap-json (:body content))})) + (let [post-id (-> request :path-params :id)] + (fetch-hu-post post-id + {:content-type {"Content-Type" "application/json; charset=utf-8"} + :replace-str "#/item/" + :wrap-fn wrap-json}))) diff --git a/src/main/backend/hugo/site.clj b/src/main/backend/hugo/site.clj index 9851d79..39d2772 100644 --- a/src/main/backend/hugo/site.clj +++ b/src/main/backend/hugo/site.clj @@ -3,29 +3,33 @@ [hiccup.page :as page])) +(def hugo-post-template + #(page/html5 + {:escape-strings? false} + [:html + [:head + [:meta {:name "viewport" :content "width=device-width, initial-scale=1.0"}] + [:meta {:name "referrer" :content "no-referrer"}] + (page/include-css "/css/app.css")] + [:body + [:div {:id "app"} + [:div {:class "container p-2 mx-auto"} + [:div {:class "flex flex-row flex-wrap py-4"} + [:div {:class "w-full sm:w-1/3 md:w-1/4 px-2"} + [:div {:class "sticky top-0 p-4 bg-slate-300 rounded-xl w-full"} + [:div (:catalog %)]]] + [:div {:class "w-full sm:w-2/3 md:w-3/4 pt-1 px-2"} + [:h1 (:title %)] + [:time (:time %)] + [:hr] + [:div + (:content %)]]]]]]])) + (defn build-hugo-post [request] - (let [post-id (-> request :path-params :id) - result (handlers/fetch-hu-post post-id {:hugo true}) - content (:body result)] - {:status (:status result) - :headers {"Content-Type" "text/html; charset=utf-8"} - :body (page/html5 {:escape-strings? false} - [:html - [:head - [:meta {:name "viewport" :content "width=device-width, initial-scale=1.0"}] - [:meta {:name "referrer" :content "no-referrer"}] - (page/include-css "/css/app.css")] - [:body - [:div {:id "app"} - [:div {:class "container p-2 mx-auto"} - [:div {:class "flex flex-row flex-wrap py-4"} - [:div {:class "w-full sm:w-1/3 md:w-1/4 px-2"} - [:div {:class "sticky top-0 p-4 bg-slate-300 rounded-xl w-full"} - [:div (:catalog content)]]] - [:div {:class "w-full sm:w-2/3 md:w-3/4 pt-1 px-2"} - [:h1 (:title content)] - [:time (:time content)] - [:hr] - [:div - (:content content)]]]]]]])})) + (let [post-id (-> request :path-params :id)] + (handlers/fetch-hu-post + post-id + {:content-type {"Content-Type" "text/html; charset=utf-8"} + :replace-str "/hp/" + :wrap-fn hugo-post-template})))