forth/html.fs

66 lines
1.8 KiB
Forth
Executable File

\ Generate markup to standard output
: [http-html-head] ( -- )
." Content-Type: text/html" cr cr
.\" <!DOCTYPE HTML PUBLIC \"-//W3C/DTD HTML 4.01//EN\" "
.\" \"http://www,w3,org/TR/html4/strict.dtd\">"
." <html><head>"
;
\ (gforth does not support strings with embedded new-line characters)
\ HTML tags: html, head, title, style, meta, body, h1/2/3/4/5/6, p, strong (b), em (i), ul, ol, li, dl, dt, dd, table, thead, tbody, tr, th, td, a
: [<] [char] < emit ;
: [>] [char] > emit ;
: [</] ." </" ;
: [/>] ." />" ;
: "tag" ( -- ) ." tag" ;
: [tag] ( addr u -- ) [<] type [>] ;
: [/tag] ( addr u -- ) [</] type [>] ;
: [tag-$] ( $-addr $-u t-addr t-u -- ) [tag] type ;
: [tag-$/] ( $-addr $-u t-addr t-u -- )
2dup [tag] 2over type [/tag] ;
: [tag+] ( +-addr +-u t-addr t-u -- )
[<] type space type [>] ;
: [tag+$] ( $-addr $-u +-addr +-u t-addr t-u -- )
[<] type space type [>] type ;
: [tag+$/] ( $-addr $-u +-addr +-u t-addr t-u -- )
2dup [<] type space 2over type [>] 2over type [/tag] ;
: [tag/] ( addr u -- ) [<] type space [/>] ;
: "p" ( -- ) s" p" ;
: [p] ( -- ) "p" [tag] ;
: [p-$] ( addr u -- ) [p] type ; \ "p" [tag-$] ;
: [p-$/] ( addr u -- ) "p" [tag-$/] ;
: [p+] ( +-addr +-u -- ) "p" [tag+] ;
: [p+$] ( $-addr $-u +-addr +-u -- ) "p" [tag+$] ;
: [p+$/] ( $-addr $-u +-addr +-u -- ) "p" [tag+$/] ;
: [/p] ( -- ) "p" [/tag] ;
: [p/] ( -- ) "p" [tag/] ;
.( Testing ...) cr
"p" type cr
[p] cr
s" This is a p-$ example." [p-$] cr
s" This is a p-$/ example." [p-$/] cr
s" a1=v1 a2=v2" [p+] cr
s" This is a p+$ example." s" a1=v1 a2=v2" [p+$] cr
s" This is a p+$/ example." s" a1=v1 a2=v2" [p+$/] cr
[/p] cr
[p/] cr
bye
: [html] [<] ." html" [>] ;
: [html-a] ( attr-a u -- ) [<] ." html " type [>] ;
: [/html] ( -- ) [</] ." html" type [>] ;
: [h1] [<] ." html" [>] ;