sitegen_no_content.st
· 3.2 KiB · Smalltalk
Surowy
Playground
site: cmd[
Array .new >pages
"**/*.pstk" Directory .glob each[
if[ dup .file? :then pages .push :else void ]
]
#page: module/extend[ static/style.css: File .slurp >style ]
source-of: fn[ .[ Path .[ "." of[ path name ] relative ] ] ]
dest-of: fn[ .[
Path .[
"." of[ path name Path .parse .name ".html" + ] relative
"content/" "site/" String .replace ] ] ]
Directory .[
try[ site: rmr :catch void ]
static: site: copy ]
File .[ NB[ Files I don't have a better way to copy in atm ]
"../../stvm.js" site/stvm.js: copy
"../../stkweb.js" "site/stkweb.js" copy
"../../lib/browser/browser.stk" "site/browser.stk" copy
"../../lib/core/core.stk" "site/core.stk" copy
]
Set .of[ pages each[ dest-of Path .parse .dir ] ] >destFolders
destFolders each[ Directory .ensure ]
pages each[ >p p. dest-of p> source-of /t StackTalk .load-file ]
]
#page: module[
"" >title
"" >contents
"" >imports
"" >script
@: fn[ write ]
@title: fn[ @ title @ ]
@style: fn[ @ style @ ]
@contents: fn[ @ contents @ ]
@imports: fn[ @ imports @ ]
@script: fn[ @ script @ ]
@header: fn[
"<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatile' cotent='IE=edge'/>
<meta name='viewport' content=''/>
<title>" @title "</title>
</head>
" @
]
@body: fn[
"<body>
<style>
" @style
"</style>
<h2><a href='./'>" @title "</a></h2>
<main role='main' class='conatainer'>"
@contents
"</main>
"
@imports
"<script type='module'>"
@script
"</script>
</body>" @
]
render: fn[
@header @body "</html>" @
]
]
#markup: module[
nl: fn[ /nl @ ]
proc-tag: fn[
>#tag >#proc
"<" @ #tag. @ " " @
#proc. Proc .blocks each[ >#attr
#attr. @ "=\"" @
#proc. #attr. Proc .run? escape @
"\"" @
]
">" @
temp[ #tag #proc done did ]
false >done
[ done: [ void true ] change ] >did
#proc. proc: Proc .run?
if[ $: #proc. has-flag? :then $ did ]
if[ done. ~: #proc. has-flag? or not :then @ ]
"</" @ #tag. @ ">" @
]
$: fn[ "<code>" @ escape @ "</code>" @ ]
quot-tag: fn[ >#tag >q "<" @ #tag. @ ">" @ q> run @ "</" @ #tag> @ ">" @ ]
tag: fn[ >#tag >#def
if[ #def. proc?
:then #def> #tag> proc-tag
:else #def> #tag> quot-tag ] ]
escape: fn[ "<" "<" String .replace ">" ">" String .replace ]
---: fn[ "<hr/>" @ ]
%w[ h1 h2 h3 h4 p ul ol li code ] each[ dup fry[ _ tag ] fn ]
br: fn[ [ :~ ] br: tag ]
link: fn[ >q "<a href='" @ q. to: "link needs :to" Proc .run! @ "'>" @ q> proc: "link needs text!" Proc .run! @ "</a>" @ ]
code-block: fn[ "<pre><code>" @ run @ "</code></pre>" @ ]
]
page: fn[
>q w: File .open ask[ use[ #page #markup ] your[ q> ] run render close ]
]
| 1 | site: cmd[ |
| 2 | Array .new >pages |
| 3 | "**/*.pstk" Directory .glob each[ |
| 4 | if[ dup .file? :then pages .push :else void ] |
| 5 | ] |
| 6 | #page: module/extend[ static/style.css: File .slurp >style ] |
| 7 | source-of: fn[ .[ Path .[ "." of[ path name ] relative ] ] ] |
| 8 | dest-of: fn[ .[ |
| 9 | Path .[ |
| 10 | "." of[ path name Path .parse .name ".html" + ] relative |
| 11 | "content/" "site/" String .replace ] ] ] |
| 12 | Directory .[ |
| 13 | try[ site: rmr :catch void ] |
| 14 | static: site: copy ] |
| 15 | File .[ NB[ Files I don't have a better way to copy in atm ] |
| 16 | "../../stvm.js" site/stvm.js: copy |
| 17 | "../../stkweb.js" "site/stkweb.js" copy |
| 18 | "../../lib/browser/browser.stk" "site/browser.stk" copy |
| 19 | "../../lib/core/core.stk" "site/core.stk" copy |
| 20 | ] |
| 21 | Set .of[ pages each[ dest-of Path .parse .dir ] ] >destFolders |
| 22 | |
| 23 | destFolders each[ Directory .ensure ] |
| 24 | pages each[ >p p. dest-of p> source-of /t StackTalk .load-file ] |
| 25 | ] |
| 26 | |
| 27 | #page: module[ |
| 28 | |
| 29 | "" >title |
| 30 | "" >contents |
| 31 | "" >imports |
| 32 | "" >script |
| 33 | |
| 34 | @: fn[ write ] |
| 35 | @title: fn[ @ title @ ] |
| 36 | @style: fn[ @ style @ ] |
| 37 | @contents: fn[ @ contents @ ] |
| 38 | @imports: fn[ @ imports @ ] |
| 39 | @script: fn[ @ script @ ] |
| 40 | |
| 41 | @header: fn[ |
| 42 | "<!DOCTYPE html> |
| 43 | <html lang='en'> |
| 44 | <head> |
| 45 | <meta charset='utf-8'> |
| 46 | <meta http-equiv='X-UA-Compatile' cotent='IE=edge'/> |
| 47 | <meta name='viewport' content=''/> |
| 48 | <title>" @title "</title> |
| 49 | </head> |
| 50 | " @ |
| 51 | ] |
| 52 | @body: fn[ |
| 53 | "<body> |
| 54 | <style> |
| 55 | " @style |
| 56 | "</style> |
| 57 | <h2><a href='./'>" @title "</a></h2> |
| 58 | <main role='main' class='conatainer'>" |
| 59 | @contents |
| 60 | "</main> |
| 61 | " |
| 62 | @imports |
| 63 | "<script type='module'>" |
| 64 | @script |
| 65 | "</script> |
| 66 | </body>" @ |
| 67 | ] |
| 68 | render: fn[ |
| 69 | @header @body "</html>" @ |
| 70 | ] |
| 71 | ] |
| 72 | |
| 73 | #markup: module[ |
| 74 | nl: fn[ /nl @ ] |
| 75 | |
| 76 | proc-tag: fn[ |
| 77 | >#tag >#proc |
| 78 | "<" @ #tag. @ " " @ |
| 79 | #proc. Proc .blocks each[ >#attr |
| 80 | #attr. @ "=\"" @ |
| 81 | #proc. #attr. Proc .run? escape @ |
| 82 | "\"" @ |
| 83 | ] |
| 84 | ">" @ |
| 85 | |
| 86 | temp[ #tag #proc done did ] |
| 87 | false >done |
| 88 | [ done: [ void true ] change ] >did |
| 89 | |
| 90 | #proc. proc: Proc .run? |
| 91 | |
| 92 | if[ $: #proc. has-flag? :then $ did ] |
| 93 | if[ done. ~: #proc. has-flag? or not :then @ ] |
| 94 | |
| 95 | "</" @ #tag. @ ">" @ |
| 96 | ] |
| 97 | |
| 98 | $: fn[ "<code>" @ escape @ "</code>" @ ] |
| 99 | quot-tag: fn[ >#tag >q "<" @ #tag. @ ">" @ q> run @ "</" @ #tag> @ ">" @ ] |
| 100 | |
| 101 | tag: fn[ >#tag >#def |
| 102 | if[ #def. proc? |
| 103 | :then #def> #tag> proc-tag |
| 104 | :else #def> #tag> quot-tag ] ] |
| 105 | |
| 106 | escape: fn[ "<" "<" String .replace ">" ">" String .replace ] |
| 107 | |
| 108 | ---: fn[ "<hr/>" @ ] |
| 109 | %w[ h1 h2 h3 h4 p ul ol li code ] each[ dup fry[ _ tag ] fn ] |
| 110 | |
| 111 | br: fn[ [ :~ ] br: tag ] |
| 112 | link: fn[ >q "<a href='" @ q. to: "link needs :to" Proc .run! @ "'>" @ q> proc: "link needs text!" Proc .run! @ "</a>" @ ] |
| 113 | code-block: fn[ "<pre><code>" @ run @ "</code></pre>" @ ] |
| 114 | ] |
| 115 | |
| 116 | page: fn[ |
| 117 | >q w: File .open ask[ use[ #page #markup ] your[ q> ] run render close ] |
| 118 | ] |