util  Diff

Differences From Artifact [e734573397]:

To Artifact [189c348a0b]:


4
5
6
7
8
9
10







11
12
13
14
15
16
17
..
79
80
81
82
83
84
85
86



87
88
89
90
91
92
93
94
...
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
...
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335

336
337
338
339
340
341
342
343
344
345
346
347






348
349
350
351
352
353
354
355
356


357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
...
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
...
399
400
401
402
403
404
405
406
407
408
409

410
411
412
413


414
415





416
417
418
419
420
421
422
423
424
425
426
427
428
429
430


431
432
433
434
435

436
437
438
439
440
441
442
...
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
...
615
616
617
618
619
620
621
622
623
(import (chicken tcp)
		(chicken pretty-print)
		(chicken io)
		(chicken string)
		(chicken bitwise)
		(chicken random))








(tcp-read-timeout 300)

(define-record db (setter stories) (setter pages))
(define-record story title genre
			   desc key start)
(define-record page link story body choices)
(define *database* (make-db '() '()))
................................................................................
(define-record netstr sz val rest)
(define-record netreq body method path
			   query origin accept
			   ref ua)

(define (netreq-post-params req) (map (cut string-split <> "=")
								 (string-split (netreq-body req) "&")))
(define (post-field arr p) (let ((v (assoc p arr)))



							 (if (eq? #f v) "" (url-decode (cadr v)))))
(define (alist->netreq al body)
  (define (method->symbol m) (cond [(equal? m "GET") 'get]
								   [(equal? m "POST") 'post]
								   [(equal? m "PUT") 'put]
								   [else 'unknown]))
  (define (field key) 
	(let ((result (assoc key al)))
................................................................................

(define (respond req code user-header text links)
  (let* ([kind-pair (determine-response-kind (netreq-accept req)
											 (netreq-ua req))]
		 [kind (car kind-pair)]
		 [mime (cdr kind-pair)]
		 [header (string-append (if (equal? user-header "") ""
									 " :: ") user-header)])
	(define (open)
	  (case kind
		('html (string-append
				 "<!doctype html>\n<head>\n<title>fabulist" header "</title>\n"
				 "<link rel=\"stylesheet\" href=\"/style\" type=\"text/css\">"
				 "</head>\n<body>\n<h1>								<a href=\"/\">fabulist</a>" header "</h1>\n"))
		('text (string-append "# " header "\n\n"))))
................................................................................
								   final-text)])
	  response)))


(define (get-new-story req) 0)

(define (get-stylesheet req)
  (define style #<<_END_
	body {
	background: #370017;
	color: #FFE1ED;
	max-width: 35em;
	padding: 1em;
	margin: auto;
	font-family: Alegreya, Junicode, GaramondNo8, "Garamond Premier Pro", "Adobe Garamond", Garamond, serif;
	font-size: 15pt;
	}
	input, textarea {
	font-family: Alegreya, Junicode, GaramondNo8, "Garamond Premier Pro", "Adobe Garamond", Garamond, serif;
	font-size: 14pt;
	border: 1px solid #FF4C96;
	background: #610028;
	color: white;
	padding: 0.3em;
	flex-grow: 2;
	text-shadow: 0 0 10px #E84B8C;
	}
	code {
	font-family: Inconsolata, Monaco, monocode;
	padding: 0.2em 0.3em;
	background: #740C38;
	border: 1px solid #260010;
	}
	textarea {
	height: 6em;
	}
	input[type=submit] {
	position: absolute;
	flex-grow: 0;
	width: 30%;
	right: 0;
	top: 1em;
	text-shadow: 0 0 10px #E84B8C;
	}
	form > div {
	position: relative;
	display: flex;
	width: 100%;
	align-items: center;
	}
	form label {
	margin-right: 0.5em;
	margin-bottom: 0.5em;
	margin-top: 0.5em;
	font-weight: bold;
	}
	a[href] {
	color: #FF2E84;
	}
	h1 {
		font-weight: 100;
		margin-bottom: 0.0em;
	}
	h1 a[href] {
	color: #FF7AB1;
	text-decoration: none;
	}
_END_
	)
  (make-netresp 'ok (list (cons "Server" "")
						  (cons "Content-Type" "text/css")
						  (cons "Content-Length" (number->string
												   (string-length style))))
				style))

(define (get-new-chapter req)
  (let* ([path (netreq-path req)]
		 [place (follow-path (cdr path))]
		 [params (netreq-post-params req)])
	(if (eq? #f place) (error-not-found req)

		(let* ([story-key (text-path-story-key place)]
			   [story (text-path-story-rec place)]
			   [chapter-key (text-path-chapter-key place)])
		  (new-chapter-page req story-key story chapter-key)))))

(define (get-flag-chapter req) 0)
(define (post-flag-chapter req) 0)

(define (error-not-found req)
  (respond req 'nofile "error"
		   '((p "the path you have entered is not meaningful."))
		   '(("start over" . "/"))))







(define (display-index-page req)
  (respond req 'ok ""
		   '((p "welcome to this " (b "fabulist" ) " instance.")
			 (p "fabulist is a server for hosting collaborative interactive fiction." )
			 (p "select one of the links below."))
		   '(("start a new story" . "/init")
			 ("play an existing story" . "/find")
			 ("start a random story" . "/rand"))))



(define (new-chapter-page req key story last-page)
  (let ([new-url (string-append "/new/" key (if (eq? #f last-page) ""
												(string-append "/" last-page)))])
	(respond req 'ok (string-append (story-title story) " :: new chapter")
		   (list
			 '(p "write your chapter and hit " (b "commit") " to add it to the story")
			 `(form "POST" ,new-url
					,(if (eq? #f last-page) '(text "")
						(list 'field "choice that leads here" "choice-link" "text"))
					(field "chapter body" "chapter-body" "textarea")))
			 (if (eq? last-page #f) '()
			   (list
				 (cons "abandon chapter"
					   (string-append "/p/" key "/" last-page))
				 (cons "link to existing chapter"
					   (string-append "/link/" key "/" last-page)))))))

(define-record text-path
			   story-key story-rec chapter-key)

(define (follow-path path)
  (let* ([pathlen (length path)])
	(cond [(or (< pathlen 1)
................................................................................
					   [story-ent (alist-ref story-key
											 (db-stories *database*)
											 equal?)]
					   [chapter-key (cond [(= pathlen 2) (cadr path)]
										  [(eq? #f story-ent) #f]
										  [else (story-start story-ent)])])
				  (make-text-path story-key story-ent chapter-key))])))
  
(define (format-user-text str)
  (map (cut list 'p <>)
	   (string-split str "\n")))

(define (post-new-chapter req)
  (define (save-chapter key ch)
	(set! (db-pages *database*)
................................................................................
  (define (save-to-start story chap)
	(story-start-set! story chap))
  (define (add-to-page story-key dest new-key)
	(let* ([destkey (string-append story-key "." dest)]
		   [dest-pg (alist-ref destkey (db-pages *database*) equal?)]
		   [old-choices (page-choices dest-pg)])
	  (page-choices-set! dest-pg (append old-choices (list new-key)))))
	  
  (let* ([path (netreq-path req)]
		 [place (follow-path (cdr path))]
		 [params (netreq-post-params req)])

	(if (eq? #f place) (error-not-found req)
		(let* ([story-key (text-path-story-key place)]
			   [story (text-path-story-rec place)]
			   [chapter-key (text-path-chapter-key place)]


			   [choice-link (post-field params "choice-link")]
			   [chapter-body (post-field params "chapter-body")])





			  (let* ([new-key (generate-key 4)]
					 [full-key (string-append story-key "." new-key)]
					 [new-page
					   (make-page choice-link
								  story-key
								  (format-user-text chapter-body)
								  '())])
				(if (= (length path) 2)
					(if (eq? #f (story-start story))
						(begin (save-chapter full-key new-page)
							   (save-to-start story new-key)
							   (redirect (string-append "/p/" story-key "/" new-key)))
						(error-not-found req))
					; FIXME: insert error checking to make sure [chapter-key]
					; really exists; right now this will crash the program!


					(begin (save-chapter full-key new-page)
						   (add-to-page story-key chapter-key new-key)
						   (redirect (string-append "/p/" story-key "/" chapter-key))))

		  )))))



(define (get-story-chapter req)
  (define (render-chapter story-key story chapter)
	(define (lookup-chapter ch) (alist-ref (string-append story-key "." ch)
										   (db-pages *database*) equal?))
	(define (format-choice ch)
................................................................................
	  (let ([dest (lookup-chapter ch)])
		(cons (page-link dest)
			  (string-append "/p/" story-key "/" ch))))

	(let ([chap (lookup-chapter chapter)])
	  (if (eq? #f chap) (error-not-found req)
		  (respond req 'ok (story-title story)
					(page-body chap)
					(append
					  (map format-choice (page-choices chap))
					  (list (cons '(i "[branch]")
								  (string-append
									"/new/" story-key
									"/" chapter))))))))

  (let* ([path (netreq-path req)]
		 [place (follow-path (cdr path))])
	(if (eq? #f place) (error-not-found req)
		(let* ([story-key (text-path-story-key place)]
			   [story-ent (text-path-story-rec place)]
			   [chapter-key (text-path-chapter-key place)])
................................................................................
	(await-connections s)))
(let ([server (tcp-listen port 100 "127.0.0.1")])
  (await-connections server)
  (tcp-close server)))

(serve 4056)
#;(cond-expand
  (chicken-script )
  (else))







>
>
>
>
>
>
>







 







|
>
>
>
|







 







|







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|


|
|





|
>












>
>
>
>
>
>








|
>
>





|
|
|
|
|
|

|
|
|
|
|







 







|







 







|


|
>




>
>


>
>
>
>
>
|
|
<
|
|
|
|
|
|
|
|
|
|
|
|
>
>
|
|
|

<
>







 







|
|
|
|
|
|
|







 







|
|
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
..
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
...
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
...
266
267
268
269
270
271
272






























































273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
...
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
...
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382

383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400

401
402
403
404
405
406
407
408
...
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
...
581
582
583
584
585
586
587
588
589
(import (chicken tcp)
		(chicken pretty-print)
		(chicken io)
		(chicken string)
		(chicken bitwise)
		(chicken random))

(begin-for-syntax (import (chicken io)))
(include "scmlib/lisp-macro.scm")
(define-macro (static-import-string . body)
			  (call-with-input-file (car body)
									(cut read-string #f <>)))
(define *stylesheet* (static-import-string "fabulist.css"))

(tcp-read-timeout 300)

(define-record db (setter stories) (setter pages))
(define-record story title genre
			   desc key start)
(define-record page link story body choices)
(define *database* (make-db '() '()))
................................................................................
(define-record netstr sz val rest)
(define-record netreq body method path
			   query origin accept
			   ref ua)

(define (netreq-post-params req) (map (cut string-split <> "=")
								 (string-split (netreq-body req) "&")))
(define (post-field arr p)
  (let ((v (assoc p arr))) (if (or (null? v)
								   (eq? #f v)
								   (null? (cdr v))) ""
							   (url-decode (cadr v)))))
(define (alist->netreq al body)
  (define (method->symbol m) (cond [(equal? m "GET") 'get]
								   [(equal? m "POST") 'post]
								   [(equal? m "PUT") 'put]
								   [else 'unknown]))
  (define (field key) 
	(let ((result (assoc key al)))
................................................................................

(define (respond req code user-header text links)
  (let* ([kind-pair (determine-response-kind (netreq-accept req)
											 (netreq-ua req))]
		 [kind (car kind-pair)]
		 [mime (cdr kind-pair)]
		 [header (string-append (if (equal? user-header "") ""
									" :: ") user-header)])
	(define (open)
	  (case kind
		('html (string-append
				 "<!doctype html>\n<head>\n<title>fabulist" header "</title>\n"
				 "<link rel=\"stylesheet\" href=\"/style\" type=\"text/css\">"
				 "</head>\n<body>\n<h1>								<a href=\"/\">fabulist</a>" header "</h1>\n"))
		('text (string-append "# " header "\n\n"))))
................................................................................
								   final-text)])
	  response)))


(define (get-new-story req) 0)

(define (get-stylesheet req)






























































  (make-netresp 'ok (list (cons "Server" "fabulist")
						  (cons "Content-Type" "text/css")
						  (cons "Content-Length" (number->string
												   (string-length *stylesheet*))))
				*stylesheet*))

(define (get-new-chapter req)
  (let* ([path (netreq-path req)]
		 [place (follow-path (cdr path))]
		 [params (netreq-post-params req)])
	(if (or (eq? #f place)
			(eq? #f (text-path-story-rec place))) (error-not-found req)
		(let* ([story-key (text-path-story-key place)]
			   [story (text-path-story-rec place)]
			   [chapter-key (text-path-chapter-key place)])
		  (new-chapter-page req story-key story chapter-key)))))

(define (get-flag-chapter req) 0)
(define (post-flag-chapter req) 0)

(define (error-not-found req)
  (respond req 'nofile "error"
		   '((p "the path you have entered is not meaningful."))
		   '(("start over" . "/"))))

(define (error-bad-request req back)
  (respond req 'badreq "error"
		   '((p "the data you have supplied is not valid."))
		   `(("return to story" . ,back)
			 ("start over" . "/"))))

(define (display-index-page req)
  (respond req 'ok ""
		   '((p "welcome to this " (b "fabulist" ) " instance.")
			 (p "fabulist is a server for hosting collaborative interactive fiction." )
			 (p "select one of the links below."))
		   '(("start a new story" . "/init")
			 ("play an existing story" . "/find")
			 ("start a random story" . "/rand")
			 ("set your author details" . "/auth")
			 )))

(define (new-chapter-page req key story last-page)
  (let ([new-url (string-append "/new/" key (if (eq? #f last-page) ""
												(string-append "/" last-page)))])
	(respond req 'ok (string-append (story-title story) " :: new chapter")
			 (list
			   '(p "write your chapter and hit " (b "commit") " to add it to the story")
			   `(form "POST" ,new-url
					  ,(if (eq? #f last-page) '(text "")
						   (list 'field "choice that leads here" "choice-link" "text"))
					  (field "chapter body" "chapter-body" "textarea")))
			 (if (eq? last-page #f) '()
				 (list
				   (cons "abandon chapter"
						 (string-append "/p/" key "/" last-page))
				   (cons "link to existing chapter"
						 (string-append "/link/" key "/" last-page)))))))

(define-record text-path
			   story-key story-rec chapter-key)

(define (follow-path path)
  (let* ([pathlen (length path)])
	(cond [(or (< pathlen 1)
................................................................................
					   [story-ent (alist-ref story-key
											 (db-stories *database*)
											 equal?)]
					   [chapter-key (cond [(= pathlen 2) (cadr path)]
										  [(eq? #f story-ent) #f]
										  [else (story-start story-ent)])])
				  (make-text-path story-key story-ent chapter-key))])))

(define (format-user-text str)
  (map (cut list 'p <>)
	   (string-split str "\n")))

(define (post-new-chapter req)
  (define (save-chapter key ch)
	(set! (db-pages *database*)
................................................................................
  (define (save-to-start story chap)
	(story-start-set! story chap))
  (define (add-to-page story-key dest new-key)
	(let* ([destkey (string-append story-key "." dest)]
		   [dest-pg (alist-ref destkey (db-pages *database*) equal?)]
		   [old-choices (page-choices dest-pg)])
	  (page-choices-set! dest-pg (append old-choices (list new-key)))))

  (let* ([path (netreq-path req)]
		 [place (follow-path (cdr path))]
		 [params (netreq-post-params req)]
		 [raw-path (foldl string-append "" (intersperse (cdr path) "/"))])
	(if (eq? #f place) (error-not-found req)
		(let* ([story-key (text-path-story-key place)]
			   [story (text-path-story-rec place)]
			   [chapter-key (text-path-chapter-key place)]
			   [chapter-full-key (if (eq? #f chapter-key) #f
									 (string-append story-key "." chapter-key))]
			   [choice-link (post-field params "choice-link")]
			   [chapter-body (post-field params "chapter-body")])
		  (cond [(or (equal? chapter-body "")
					 (and (equal? choice-link "")
						  (not (eq? chapter-key #f))))
				 (error-bad-request req (string-append "/p/" raw-path))]
				[else
				  (let* ([new-key (generate-key 4)]
						 [full-key (string-append story-key "." new-key)]

						 [new-page (make-page choice-link
											  story-key
											  (format-user-text chapter-body)
											  '())])
					(if (= (length path) 2); new story or new branch?
						(if (eq? #f (story-start story))
							(begin (save-chapter full-key new-page)
								   (save-to-start story new-key)
								   (redirect (string-append "/p/" story-key "/" new-key)))
							(error-not-found req))
						; FIXME: insert error checking to make sure [chapter-key]
						; really exists; right now this will crash the program!
						(if (eq? #f (alist-ref chapter-full-key (db-pages *database*) equal?))
							(error-not-found req)
							(begin (save-chapter full-key new-page)
								   (add-to-page story-key chapter-key new-key)
								   (redirect (string-append "/p/" story-key "/" chapter-key))))


						))])))))


(define (get-story-chapter req)
  (define (render-chapter story-key story chapter)
	(define (lookup-chapter ch) (alist-ref (string-append story-key "." ch)
										   (db-pages *database*) equal?))
	(define (format-choice ch)
................................................................................
	  (let ([dest (lookup-chapter ch)])
		(cons (page-link dest)
			  (string-append "/p/" story-key "/" ch))))

	(let ([chap (lookup-chapter chapter)])
	  (if (eq? #f chap) (error-not-found req)
		  (respond req 'ok (story-title story)
				   (page-body chap)
				   (append
					 (map format-choice (page-choices chap))
					 (list (cons '(i "[branch]")
								 (string-append
								   "/new/" story-key
								   "/" chapter))))))))

  (let* ([path (netreq-path req)]
		 [place (follow-path (cdr path))])
	(if (eq? #f place) (error-not-found req)
		(let* ([story-key (text-path-story-key place)]
			   [story-ent (text-path-story-rec place)]
			   [chapter-key (text-path-chapter-key place)])
................................................................................
	(await-connections s)))
(let ([server (tcp-listen port 100 "127.0.0.1")])
  (await-connections server)
  (tcp-close server)))

(serve 4056)
#;(cond-expand
(chicken-script )
(else))