Redirect to edit mode if a connected user try to access a non-existing page.

This commit is contained in:
Feufochmar 2021-05-06 18:29:39 +02:00
parent 8c96fb25a1
commit ab0dffea07
1 changed files with 107 additions and 100 deletions

View File

@ -99,107 +99,114 @@
; /notes/show/xxx
; Show a given page of the notepad. A page is stored under a scribble-like format.
; If the page does not exists and user is logged in, redirect to the /notes/edit/xxx page.
(define pages:notepad:page-show
(pages:adaptable-template
(lambda (param)
(define connected-usr (get-user-from-weblet-parameter param))
(define secured? (check-secured? param))
(define can-edit? (and connected-usr secured?))
(define page (weblet-parameter-ref param 'page #f))
(define file (and page (string-append notepad-dir "/" page)))
(define has-page? (file-exists? file))
(cond
(has-page?
; Read the file and parse it into a list structure with scribble
(define parsed
(call-with-input-file file
(lambda (in) (scrib:read-inside in))))
; Collect metadata and transform the structure into the content
(define (collect-result parsed hsh)
; Recursively apply the parsing:
; - filter out #f values (metadata tags)
; - Two successive "\n" (i.e a blank line) are replaced by a '(br)
(define (recur lst)
(filter
values
(map
(lambda (x) (collect-result x hsh))
(reverse
(foldl
(lambda (val res)
(if (and (not (null? res))
(equal? "\n" val)
(equal? "\n" (car res)))
(cons '(br) res)
(cons val res)))
'()
lst)))))
; Parse and transform the content
(cond
; Not a list => output as is
((not (list? parsed)) parsed)
(#t
(case (car parsed)
; @title{Title} : metadata: title of the page
; @author{Author} : metadata: author of the page
; @date{Date} : metadata: date of the page
((title author date)
(hash-set! hsh (car parsed) (apply string-append (cdr parsed)))
#f)
; @link[path]{Text} : content: link to another page
((link)
`(a ((href ,(symbol->string (cadr parsed))))
,@(recur (cddr parsed))))
; @image[path]{Alt text} : content: image
((image)
(define alt (apply string-append (cddr parsed)))
`(img ((src ,(symbol->string (cadr parsed)))
(alt ,alt)
(title ,alt))))
; @strong{Text} : content : strong
; @str{Text} : content : strong
((strong str)
`(strong ,@(recur (cdr parsed))))
; @emphase{Text} : content : emphase
; @emp{Text} : content : emphase
((emphase emp)
`(em ,@(recur (cdr parsed))))
; @section{Text} : content : title and start of section
((section)
`(h2 ,@(recur (cdr parsed))))
; @subsection{Text} : content : title and start of subsection
((subsection)
`(h3 ,@(recur (cdr parsed))))
; @subsubsection{Text} : content : title and start of subsubsection
((subsubsection)
`(h4 ,@(recur (cdr parsed))))
; @paragraph[justify]{Text} : content : paragraph with an optional justification
; @para[justify]{Text} : content : paragraph with an optional justification
((paragraph para)
(if (not (null? (cdr parsed)))
(case (cadr parsed)
((left) `(p ((style "text-align: left;")) ,@(recur (cddr parsed))))
((right) `(p ((style "text-align: right;")) ,@(recur (cddr parsed))))
((center centered) `(p ((style "text-align: center;")) ,@(recur (cddr parsed))))
(else `(p ,@(recur (cdr parsed)))))
#f))
; Other kind of lists: recursed on them
(else
(recur parsed))
))))
(define result (make-hash))
(hash-set! result 'content
(define (pages:notepad:page-show param)
(define connected-usr (get-user-from-weblet-parameter param))
(define secured? (check-secured? param))
(define can-edit? (and connected-usr secured?))
(define page (weblet-parameter-ref param 'page #f))
(define file (and page (string-append notepad-dir "/" page)))
(define has-page? (file-exists? file))
(cond
(has-page?
; Read the file and parse it into a list structure with scribble
(define parsed
(call-with-input-file file
(lambda (in) (scrib:read-inside in))))
; Collect metadata and transform the structure into the content
(define (collect-result parsed hsh)
; Recursively apply the parsing:
; - filter out #f values (metadata tags)
; - Two successive "\n" (i.e a blank line) are replaced by a '(br)
(define (recur lst)
(filter
values
(map
(lambda (x) (collect-result x hsh))
(reverse
(foldl
(lambda (val res)
(if (and (not (null? res))
(equal? "\n" val)
(equal? "\n" (car res)))
(cons '(br) res)
(cons val res)))
'()
lst)))))
; Parse and transform the content
(cond
; Not a list => output as is
((not (list? parsed)) parsed)
(#t
(case (car parsed)
; @title{Title} : metadata: title of the page
; @author{Author} : metadata: author of the page
; @date{Date} : metadata: date of the page
((title author date)
(hash-set! hsh (car parsed) (apply string-append (cdr parsed)))
#f)
; @link[path]{Text} : content: link to another page
((link)
`(a ((href ,(symbol->string (cadr parsed))))
,@(recur (cddr parsed))))
; @image[path]{Alt text} : content: image
((image)
(define alt (apply string-append (cddr parsed)))
`(img ((src ,(symbol->string (cadr parsed)))
(alt ,alt)
(title ,alt))))
; @strong{Text} : content : strong
; @str{Text} : content : strong
((strong str)
`(strong ,@(recur (cdr parsed))))
; @emphase{Text} : content : emphase
; @emp{Text} : content : emphase
((emphase emp)
`(em ,@(recur (cdr parsed))))
; @section{Text} : content : title and start of section
((section)
`(h2 ,@(recur (cdr parsed))))
; @subsection{Text} : content : title and start of subsection
((subsection)
`(h3 ,@(recur (cdr parsed))))
; @subsubsection{Text} : content : title and start of subsubsection
((subsubsection)
`(h4 ,@(recur (cdr parsed))))
; @paragraph[justify]{Text} : content : paragraph with an optional justification
; @para[justify]{Text} : content : paragraph with an optional justification
((paragraph para)
(if (not (null? (cdr parsed)))
(case (cadr parsed)
((left) `(p ((style "text-align: left;")) ,@(recur (cddr parsed))))
((right) `(p ((style "text-align: right;")) ,@(recur (cddr parsed))))
((center centered) `(p ((style "text-align: center;")) ,@(recur (cddr parsed))))
(else `(p ,@(recur (cdr parsed)))))
#f))
; Other kind of lists: recursed on them
(else
(recur parsed))
))))
(define result (make-hash))
(define cnt (collect-result parsed result))
( (pages:template
#:title (hash-ref result 'title "Sans titre")
#:author (hash-ref result 'author "feuforeve.fr")
#:content
`(article
,@(collect-result parsed result)
,@(if can-edit?
`((hr)
(a ((href ,(string-append "/notes/edit/" page))) "Éditer") ""
(a ((href ,(string-append "/notes/delete/" page))) "Supprimer"))
'())
))
result)
(#t
(notepad:error 'not-found))))))
,@cnt
,@(if can-edit?
`((hr)
(a ((href ,(string-append "/notes/edit/" page))) "Éditer") ""
(a ((href ,(string-append "/notes/delete/" page))) "Supprimer"))
'())
))
param))
( can-edit?
; Page does not exists, but user can edit => redirect to page creation
(redirect-to
(string-append "/notes/edit/" page)
see-other))
( #t
(pages:notepad:error param 'not-found))))
; /notes/edit/xxx
; Edit an existing page, or create a new page with a given title. User must be logged in.