Rename page feature for newLISP wiki
newLISP wiki allows a nice interface to manage wiki, in the Files:
The only thing that is missing is renaming pages. A smart rename should allow user to rename a page, and rename all the references to that page. In this article, I’ll try to document my implementation of this feature.
First of all, I learned about the way newLISP wiki handles HTTP requests. Whole wiki (I think) is placed in index.cgi file. Its starting point is placed after all the auxiliary functions, and then some typical page parts are set (like title and copyright). After this short introducing code (let me name it this way), conditional expressions start. They are responsible for detecting the user’s request and for handling it.
There are many different requests possible, like edit, backup and restore. In my case, I was interested in the files request, since it was the request which displayed a list of all pages, and actions that could be taken on them (like edit or delete). I wanted to add a new action, rename.
So, I wrote a new request, presented below. I added it after all the rest of requests, in hope I won’t brake anything.
-
;; request to rename a page
-
(if (CGI:get "rename")
-
(if (CGI:get "continue")
-
(letn ( page-old (CGI:get "rename")
-
page-old-filename (replace " " (dup page-old 1) "_")
-
page-new (CGI:get "new")
-
page-new-filename (replace " " (dup page-new 1) "_") )
-
(if (= (length (trim page-new)) 0)
-
(let ( body (append {<p>You haven’t entered any new name for the page } page-old {.</p>}
-
{<p>Try again on <a href="index.cgi?rename=} page-old
-
{">rename page</a>, go to }
-
{<a href="index.cgi?page=} page-old-filename {">} page-old {</a>, or }
-
{get back to <a href="index.cgi">Home</a>.</p>} ) )
-
(set ‘body body)
-
(set ‘page-name "Rename")
-
(CGI:put-page SETUP:template)
-
(exit))
-
(begin
-
(change-refs page-old page-new)
-
(rename-file (append "pages/" page-old-filename) (append "pages/" page-new-filename))
-
(display-page "Home")
-
(exit))))
-
(letn ( page-name (CGI:get "rename")
-
bdy (append {<p>You can rename the page } page-name {.</p>}
-
{<form action="index.cgi" method="post">}
-
{<input type="hidden" name="rename" value="} page-name {" />}
-
{<input type="hidden" name="continue" value="" />}
-
{<p>New page name: <input type="text" name="new" /></p>}
-
{<p>Do you really want to rename the page ? }
-
{<input type="submit" name="submit" class="button" value="Yes" /></p>}
-
{</form>}
-
{<p>You can cancel this action and get back to <a href="index.cgi">Home</a>.</p>}) )
-
(set ‘body bdy)
-
(set ‘page-name "Rename")
-
(CGI:put-page SETUP:template)
-
(exit))))
The request handling code can be splitted into two different parts. The first part starts in the line 23 and is responsible for displaying a confirmation message. User can confirm, that he wants to rename the page, or cancel the action. If user confirms, that the page should be renamed, the second part handles the request (it starts in the line 4). It simply changes all the references to the old page to new values and renames the page file (lines 19-22).
The code responsible for changing all the references to the old page name is shown below.
-
;; changes all references to the given page-old
-
;; to references to the given page-new
-
(define (change-refs page-old page-new)
-
(let ( page-old-filename (replace "_" (dup page-old 1) ".")
-
rexp (append {\[\[} page-old {\]\]})
-
subst (append {[[} page-new {]]})
-
files (slice (directory "pages/") 2) )
-
(dolist (fle files)
-
(let ( content (get-content fle) )
-
(if (find rexp content 0)
-
(begin
-
(replace rexp content subst 512)
-
(write-file (append "pages/" fle) content)))))))
Its job is simple: take a look at each file in the “pages” directory, and if you find some reference to the old page, change it to a reference to the new page.
The last thing I changed was adding a proper link to my request in the files table. It was so simple, that I think no code is needed to explain it.
After applying all those changes, I prepared a patch. Now everyone can modify newLISP wiki, and add TOC and rename functionality to this wonderful, small piece of software.
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.


Comments
No comments yet.
Sorry, the comment form is closed at this time.