get todo list working with native factor.
rewrote storage routines to not use run-file. Removed regular expressions and use paser-combinators instead.cvs
parent
64de158286
commit
d99fce030f
|
@ -36,6 +36,8 @@ default-responders
|
||||||
USE: parser
|
USE: parser
|
||||||
|
|
||||||
: l1
|
: l1
|
||||||
|
"../parser-combinators/lazy.factor" run-file
|
||||||
|
"../parser-combinators/parser-combinators.factor" run-file
|
||||||
"cont-html.factor" run-file
|
"cont-html.factor" run-file
|
||||||
"cont-responder.factor" run-file
|
"cont-responder.factor" run-file
|
||||||
"cont-utils.factor" run-file ;
|
"cont-utils.factor" run-file ;
|
||||||
|
|
|
@ -45,6 +45,8 @@ USE: todo
|
||||||
USE: arithmetic
|
USE: arithmetic
|
||||||
USE: logic
|
USE: logic
|
||||||
USE: kernel
|
USE: kernel
|
||||||
|
USE: lazy
|
||||||
|
USE: parser-combinators
|
||||||
|
|
||||||
: todo-stylesheet ( -- string )
|
: todo-stylesheet ( -- string )
|
||||||
#! Return the stylesheet for the todo list
|
#! Return the stylesheet for the todo list
|
||||||
|
@ -189,15 +191,23 @@ USE: kernel
|
||||||
"Register" login-form
|
"Register" login-form
|
||||||
] simple-page ;
|
] simple-page ;
|
||||||
|
|
||||||
: re-matches ( a b -- b )
|
: username-parser ( -- parser )
|
||||||
drop drop t ;
|
#! Return a parser which parses a valid todo username.
|
||||||
|
#! That is, it contains only lowercase, uppercase and digits.
|
||||||
|
[ letter? ] satisfy
|
||||||
|
[ LETTER? ] satisfy <|>
|
||||||
|
[ digit? ] satisfy <|> <!+> just ;
|
||||||
|
|
||||||
|
: is-valid-username? ( password -- bool )
|
||||||
|
#! Return true if the username parses correctly
|
||||||
|
username-parser call ;
|
||||||
|
|
||||||
: login-details-valid? ( name password -- )
|
: login-details-valid? ( name password -- )
|
||||||
#! Ensure that a valid username and password were
|
#! Ensure that a valid username and password were
|
||||||
#! entered. In particular, ensure that only alphanumeric
|
#! entered. In particular, ensure that only alphanumeric
|
||||||
#! data was entered to prevent security problems by
|
#! data was entered to prevent security problems by
|
||||||
#! using .., etc in the name.
|
#! using .., etc in the name.
|
||||||
drop "[a-zA-Z0-9]*" re-matches ;
|
drop is-valid-username? ;
|
||||||
|
|
||||||
: get-registration-details ( -- name password )
|
: get-registration-details ( -- name password )
|
||||||
#! Get the registration details from the user putting
|
#! Get the registration details from the user putting
|
||||||
|
@ -309,9 +319,13 @@ USE: kernel
|
||||||
] form
|
] form
|
||||||
] bind ;
|
] bind ;
|
||||||
|
|
||||||
|
: priority-parser ( -- parser )
|
||||||
|
#! Return a parser for parsing priorities
|
||||||
|
[ digit? ] satisfy just ;
|
||||||
|
|
||||||
: todo-details-valid? ( priority description -- bool )
|
: todo-details-valid? ( priority description -- bool )
|
||||||
#! Return true if a valid priority and description were entered.
|
#! Return true if a valid priority and description were entered.
|
||||||
str-length 0 > swap "[0-9]" re-matches and ;
|
str-length 0 > swap priority-parser call and ;
|
||||||
|
|
||||||
: get-new-todo-item ( -- <todo-item> )
|
: get-new-todo-item ( -- <todo-item> )
|
||||||
#! Enter a new item to the current todo list.
|
#! Enter a new item to the current todo list.
|
||||||
|
|
|
@ -34,6 +34,10 @@ USE: lists
|
||||||
USE: arithmetic
|
USE: arithmetic
|
||||||
USE: stdio
|
USE: stdio
|
||||||
USE: kernel
|
USE: kernel
|
||||||
|
USE: prettyprint
|
||||||
|
USE: unparser
|
||||||
|
USE: url-encoding
|
||||||
|
|
||||||
|
|
||||||
: <todo> ( user password -- <todo> )
|
: <todo> ( user password -- <todo> )
|
||||||
#! Create an empty todo list
|
#! Create an empty todo list
|
||||||
|
@ -65,30 +69,47 @@ USE: kernel
|
||||||
#! Print the string with quotes around it
|
#! Print the string with quotes around it
|
||||||
"\"" write write "\"" print ;
|
"\"" write write "\"" print ;
|
||||||
|
|
||||||
|
: write-item ( <todo-item> -- )
|
||||||
|
#! write the item in a manner that can be later re-read
|
||||||
|
[
|
||||||
|
"complete?" get [ "yes" url-encode print ] [ "no" url-encode print ] ifte
|
||||||
|
"priority" get url-encode print
|
||||||
|
"description" get url-encode print
|
||||||
|
] bind ;
|
||||||
|
|
||||||
|
: write-items ( list -- )
|
||||||
|
#! write the todo list items
|
||||||
|
dup length unparse print
|
||||||
|
[ write-item ] each ;
|
||||||
|
|
||||||
: write-todo ( <todo> -- )
|
: write-todo ( <todo> -- )
|
||||||
#! Write the todo list to the current output stream
|
#! Write the todo list to the current output stream
|
||||||
#! in a format that if loaded by the parser will result
|
#! in a format that if loaded by the parser will result
|
||||||
#! in a <todo> again.
|
#! in a <todo> again.
|
||||||
[
|
[
|
||||||
"USE: namespaces" print
|
"user" get url-encode print
|
||||||
"USE: lists" print
|
"password" get url-encode print
|
||||||
"<namespace> [" print
|
"items" get write-items
|
||||||
"password" get print-quoted
|
|
||||||
"password" print-quoted "set" print
|
|
||||||
"user" get print-quoted
|
|
||||||
"user" print-quoted "set" print
|
|
||||||
"items" get [ namespace>alist ] map "items" swons print
|
|
||||||
"cdr [ alist>namespace ] map" print
|
|
||||||
"items" print-quoted "set" print
|
|
||||||
"] extend" print
|
|
||||||
] bind ;
|
] bind ;
|
||||||
|
|
||||||
: store-todo ( <todo> filename -- )
|
: store-todo ( <todo> filename -- )
|
||||||
#! store the todo list in the given file.
|
#! store the todo list in the given file.
|
||||||
<filebw> [ write-todo ] with-stream ;
|
<filebw> [ write-todo ] with-stream ;
|
||||||
|
|
||||||
|
: read-todo ( -- <todo> )
|
||||||
|
#! Read a todo list from the current input stream.
|
||||||
|
read url-decode read url-decode <todo>
|
||||||
|
read str>number [
|
||||||
|
dup
|
||||||
|
<namespace> [
|
||||||
|
read url-decode "yes" = "complete?" set
|
||||||
|
read url-decode "priority" set
|
||||||
|
read url-decode "description" set
|
||||||
|
] extend add-todo-item
|
||||||
|
] times ;
|
||||||
|
|
||||||
: load-todo ( filename -- <todo> )
|
: load-todo ( filename -- <todo> )
|
||||||
run-file ;
|
<filebr> [ read-todo ] with-stream ;
|
||||||
|
|
||||||
: password-matches? ( password <todo> -- <todo> )
|
: password-matches? ( password <todo> -- <todo> )
|
||||||
#! Returns the <todo> if the password matches otherwise
|
#! Returns the <todo> if the password matches otherwise
|
||||||
|
@ -132,7 +153,7 @@ USE: kernel
|
||||||
#! Return true if item1 is a higher priority than item2
|
#! Return true if item1 is a higher priority than item2
|
||||||
>r item-priority r> item-priority str-lexi> ;
|
>r item-priority r> item-priority str-lexi> ;
|
||||||
|
|
||||||
: todo-items ( <todo> -- )
|
: todo-items ( <todo> -- alist )
|
||||||
#! Return a list of items for the given todo list.
|
#! Return a list of items for the given todo list.
|
||||||
[ "items" get ] bind [ priority-comparator ] sort ;
|
[ "items" get ] bind [ priority-comparator ] sort ;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue