From 8d20fa59204301da2102d8d09d00d20a10ccbfc2 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Thu, 24 Nov 2011 14:39:05 +0100 Subject: Automatically insert wiki links on wiki pages. --- src/mulk/benki/wiki.clj | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/mulk/benki/wiki.clj b/src/mulk/benki/wiki.clj index ce9a803..b55b7e8 100644 --- a/src/mulk/benki/wiki.clj +++ b/src/mulk/benki/wiki.clj @@ -11,13 +11,35 @@ noir.core) (:require [noir.session :as session] [noir.response :as response] - [clojure.java.jdbc :as sql])) + [clojure.java.jdbc :as sql]) + (:import [org.jsoup.Jsoup])) (def page_revisions (table :wiki_page_revisions)) (def pages (table :wiki_pages)) +(defn- html-insert-wikilinks [text] + (clojure.string/replace + text + #"\p{javaUpperCase}+\p{javaLowerCase}+\p{javaUpperCase}+\p{javaLowerCase}+\w+" + (fn [x] (fmt nil "~a" (link :wiki x) x)))) + + +(defn- wikilinkify [tag-soup] + (let [doc (org.jsoup.Jsoup/parse tag-soup) + leaf-nodes (filter #(empty? (.children %)) + (.select doc "*"))] + (doseq [node leaf-nodes] + (.html node (html-insert-wikilinks (.html node)))) + (-> doc (.select "body") (.html)))) + +(defn- unwikilinkify [tag-soup] + (let [doc (org.jsoup.Jsoup/parse tag-soup)] + (-> doc (.select ".benkilink") (.remove)) + (-> doc (.select "body") (.html)))) + + (defpage "/wiki" [] ;; NB. response/redirect calls options/resolve-uri. (response/redirect "/wiki/Home")) @@ -30,10 +52,9 @@ (with-dbt (first @(select page_revisions (where (=* :id (Integer/parseInt revision-id)))))) (with-dbt (first @revisions-with-title)))] - ;; FIXME: Insert WikiLinks. (layout (fmt nil "~A — Benki~@[/~A~] " title revision-id) (if revision - [:div#wiki-page-content (:content revision)] + [:div#wiki-page-content (wikilinkify (:content revision))] [:div#wiki-page-content [:p "This page does not exist yet."]]) [:hr] [:div#wiki-page-footer {:style "text-align: right"} @@ -93,6 +114,6 @@ (sql/insert-values :wiki_page_revisions [:page :title :content :author :format] - [page-id title content user "html5"]) + [page-id title (unwikilinkify content) user "html5"]) {:stetus 200, :headers {}, :body ""}) {:status 403, :headers {}, :body ""})))) -- cgit v1.2.3