[REBOL] comments for beginner ugly code ;-)
From: sags::apollo::lv at: 13-Mar-2005 23:23
Hi, Rebolers!
I tried to make a simple web site creation script (attached) that
makes web site from set of templates and simple article files and
chapter structure based on directory structure.
It would be interesting for me to get some opinions of yours
about coding style and security of my approach.
brgds
Janeks
-- Attached file included as plaintext by Ecartis --
-- Desc: Text from file 'mySite.r'
REBOL []
;index page template
index-page: %/c/www/sitex/index.html
;banner page template
banner-page: %/c/www/sitex/banner.html
;menu page template
menu-page: %/c/www/sitex/menu.html
;content template
content-page: %/c/www/sitex/content.html
;chapters directory
chapters-dir: %/c/www/sitex/
;chapter info file
chapter-info: %dirinfo.txt
;extension for article file
article-extension: %.art
;Mesage for empty directories
no-articles-msg: "<br>Under construction"
;script name
script-name: to-string second split-path system/options/script
;cgi functions
do %cgiFuncs.r
html: ""
cgi-vars: []
emit: func [data] [repend html data]
;frame/fields selector
selFrame: func [ cgi-vars ] [
; look for page defs
switch/default select cgi-vars "page" [
"banner" [ makeBanner banner-page ]
"menu" [ makeMenu menu-page]
"content" [ makeContent content-page]
][
emit "Error: no such page type! <br> Probably your index template file with frames definitions
are not correct!"
]
]
makeBanner: func [ templateFile ] [
if error? try [
emit read templateFile
][
emit "Error: could not find template file!"
]
]
makeMenu: func [ templateFile ] [
if error? try [
templ: read templateFile
][
emit "Error: could not find template file!"
quit
]
replace templ "$head" "head content"
replace templ "$title" "Menu"
either select cgi-vars "subChap" [
pathElems: parse select cgi-vars "subChap" "/"
pathStr: rejoin [ {<a href="} script-name {?page=menu">Home</a>} ]
foreach pathEl copy/part pathElems back tail pathElems [
repend pathStr rejoin [ { / <a target="menu" href="} script-name "?page=menu&subChap="
pathEl {">} pathEl "</a>" ]
]
repend pathStr rejoin [ { / } last pathElems ]
replace templ "$path" pathStr
][
replace templ "$path" ""
]
replace templ "$menulist" getMenuLinks chapters-dir
emit templ
emit mold to-string second split-path system/options/script
; emit get-modes chapters-dir 'creation-date
]
makeContent: func [ templateFile ] [
if error? try [
templ: read templateFile
][
emit "Error: could not find template file!"
]
; select cgi-vars "myLink"
; to-file rejoin [ chapters-dir select cgi-vars "myLink"]
; first read/lines to-file join form underDir fileName
if select cgi-vars "myLink" [
artFile: to-file rejoin [ chapters-dir select cgi-vars "myLink"]
artSerie: procArtFile artFile
replace templ "$heading" artSerie/1
replace templ "$content" artSerie/2
emit templ
]
; replace templ "$title" "Menu"
]
procArtFile: func [ artFile ] [
artFile: to-file rejoin [ chapters-dir select cgi-vars "myLink"]
artText: read/lines artFile
artHead: third artText
artText: copy/part next next next artText tail artText
contText: "article"
; processing part of article text
; here could be usable also makeDoc.r
foreach artline artText [
either 0 < length? artline [
repend contText rejoin [ artLine " " ]
][
repend contText <br>
]
]
rez: []
append rez artHead
append rez contText
return rez
]
getMenuLinks: func [ fullDir ] [
result: "<ul>"
either select cgi-vars "subChap" [
aDirectory: to-file rejoin [ fullDir select cgi-vars "subChap" "/" ]
; repend result select cgi-vars "subChap"
][
aDirectory: fullDir
; repend result "Home"
]
numFiles: 0
foreach fileName read aDirectory [
either #"/" = last fileName [
repend result rejoin [ {<li><a target="menu" href="} script-name "?page=menu&subChap="
]
if select cgi-vars "subChap" [ repend result rejoin [ select cgi-vars "subChap" "/" ]
]
repend result replace copy fileName "/" ""
repend result {">}
if error? try [
repend result read to-file join form aDirectory join fileName form chapter-info
][
rejoin [ result replace copy fileName "/" "" ]
]
repend result "</a>"
underDir: to-file join form aDirectory fileName
repend result "<ul>"
foreach fileName2 read underDir [
either #"/" = last fileName2 [
repend result rejoin [ {<li><a target="menu" href="} script-name "?page=menu&subChap="
]
repend result fileName
repend result rejoin [ replace copy fileName2 "/" "" {">} ]
either exists? to-file join form underDir join fileName2 form chapter-info [
repend result read to-file join form underDir join fileName2 form chapter-info
][
repend result replace fileName2 "/" ""
]
repend result "</a>"
][
if article-extension = suffix? fileName2 [
repend result rejoin [ {<li><a target="content" href="} script-name "?page=content&mylink="
fileName ]
if select cgi-vars "subChap" [ repend result rejoin [ select cgi-vars "myLink" "/" ]
]
repend result fileName2
repend result {">}
repend result first read/lines to-file join form underDir fileName2
repend result "</a>"
]
]
]
repend result "</ul>"
][
if article-extension = suffix? fileName [
repend result rejoin [ {<li><a target="content" href="} script-name "?page=content&mylink="
]
if select cgi-vars "subChap" [ repend result rejoin [ select cgi-vars "subChap" "/" ]
]
repend result rejoin [ fileName {">} ]
repend result first read/lines to-file join form aDirectory fileName
repend result "</a>"
numFiles: numFiles + 1
]
]
]
repend result "</ul>"
if numFiles = 0 [
repend result no-articles-msg
]
return result
]
mystr: input-cgi
either mystr [
foreach [var value] decode-cgi mystr [
insert cgi-vars form value
insert cgi-vars form var
]
selFrame cgi-vars
][
html: read index-page
]
print html