themes backend working

This commit is contained in:
dachus 2023-02-20 22:46:09 -06:00
parent 28938b6156
commit c75763c290
5 changed files with 133 additions and 56 deletions

View File

@ -1,11 +1,12 @@
/- blog
/+ dbug, default-agent
/+ blog-lib=blog, dbug, default-agent
::
%- agent:dbug
^- agent:gall
=> |%
+$ versioned-state
$% state-1
state-2
==
+$ state-0 (map path (pair html=@t md=@t))
+$ state-1
@ -13,57 +14,119 @@
files=(map path (pair html=@t md=@t))
drafts=(map path md=@t)
==
+$ state-2
$: %2
files=(map path [html=@t md=@t theme=@tas])
drafts=(map path md=@t)
themes=(map @tas css=@t)
==
--
=| state-1
=| state-2
=* state -
|_ bowl=bowl:gall
|_ =bowl:gall
+* this .
def ~(. (default-agent this %.n) bowl)
card card:agent:gall
++ on-init on-init:def
++ on-init
^- (quip card _this)
`this(themes (~(put by themes) %default default-theme:blog-lib))
++ on-save !>(state)
++ on-load
|= =vase
^- (quip card _this)
:: this is super messed up because state-0 wasn't tagged with %0
:: for later updates need to add %2, %3, etc. on -.q.vase
?. &(?=(^ q.vase) =(-.q.vase %1))
?: &(?=(^ q.vase) =(-.q.vase %0))
=+ !<(old=state-0 vase)
`this(state [%1 old ~])
=/ new=state-2
:+ %2
(~(urn by old) |=([=path html=@t md=@t] [html md %default]))
[~ ~]
:_ this(state new)
%- zing
%+ turn ~(tap by old)
|= [=path *]
:- [%pass /bind %arvo %e %disconnect `path]
[%pass /bind %arvo %e %connect `path dap.bowl]~
::
=+ !<(old=versioned-state vase)
?- -.old
%1 `this(state old)
%1
=/ new=state-2
:+ %2
(~(urn by files.old) |=([=path html=@t md=@t] [html md %default]))
[drafts.old ~]
:_ this(state new)
%- zing
%+ turn ~(tap by files.old)
|= [=path *]
:- [%pass /bind %arvo %e %disconnect `path]
[%pass /bind %arvo %e %connect `path dap.bowl]~
::
%2 `this(state old)
==
::
++ on-poke
|= [=mark =vase]
?> =(%blog-action mark)
=+ !<(act=action:blog vase)
?- -.act
%publish
:_ this(files (~(put by files) [path html md]:act))
[%pass /bind %arvo %e %serve `path.act dap.bowl /gen/blog/hoon ~]~
|^
?+ mark (on-poke:def mark vase)
%handle-http-request
(handle-http-request bowl !<([@tas inbound-request:eyre] vase))
::
%unpublish
:_ this(files (~(del by files) path.act))
[%pass /bind %arvo %e %disconnect `path.act]~
::
%save-draft
`this(drafts (~(put by drafts) [path md]:act))
::
%delete-draft
`this(drafts (~(del by drafts) path.act))
%blog-action
(handle-action bowl !<(act=action:blog vase))
==
::
++ handle-http-request
|= [=bowl:gall rid=@tas req=inbound-request:eyre]
^- (quip card _this)
=/ got (~(got by files) (rash url.request.req stap))
:_ this
%^ http-response-cards:blog-lib
rid
[200 ['Content-Type' 'text/html; charset=utf-8']~]
%- some
%- as-octs:mimes:html
%- crip
%+ weld
(trip html.got)
(add-style:blog-lib (~(got by themes) theme.got))
::
++ handle-action
|= [=bowl:gall act=action:blog]
^- (quip card _this)
?- -.act
%publish
:_ this(files (~(put by files) [path html md theme]:act))
[%pass /bind %arvo %e %connect `path.act dap.bowl]~
::
%unpublish
:_ this(files (~(del by files) path.act))
[%pass /bind %arvo %e %disconnect `path.act]~
::
%save-draft `this(drafts (~(put by drafts) [path md]:act))
%delete-draft `this(drafts (~(del by drafts) path.act))
%save-theme `this(themes (~(put by themes) [theme css]:act))
%delete-theme `this(themes (~(del by themes) theme.act))
==
--
::
++ on-agent on-agent:def
++ on-watch on-watch:def
++ on-watch
|= =path
^- (quip card _this)
?> ?=([%http-response *] path)
`this
::
++ on-peek
|= =path
^- (unit (unit cage))
?+ path ~
::
[%x %md ^] ``blog+!>(q:(~(got by files) t.t.path))
[%x %html ^] ``blog+!>(p:(~(got by files) t.t.path))
[%x %draft ^] ``blog+!>((~(got by drafts) t.t.path))
[%x %md ^] ``blog+!>(+<:(~(got by files) t.t.path))
[%x %html ^] ``blog+!>(-:(~(got by files) t.t.path))
[%x %draft ^] ``blog+!>((~(got by drafts) t.t.path))
[%x %theme @ ~] ``noun+!>((~(got by themes) i.t.t.path))
::
[%x %pages ~]
=; pages ``json+!>([%a pages])
@ -72,6 +135,10 @@
[%x %drafts ~]
=; names ``json+!>([%a names])
(turn ~(tap by drafts) |=([=^path *] (path:enjs:format path)))
::
[%x %themes ~]
=; themes ``json+!>([%a themes])
(turn ~(tap by themes) |=([t=@tas *] s+t))
::
[%x %all-bindings ~]
=; the-thing ``json+!>(the-thing)

View File

@ -1,28 +0,0 @@
|= [[now=@da eny=@uvJ byk=beak] ~ ~]
|= [authenticated=? =request:http]
:: TODO this code is ugly. Either:
:: split this into two generators
:: put the logic in a mark
=/ turl=tape (trip url.request)
=/ turl-len (lent turl)
=^ content-type=@t url.request
=+ md=(find ".md" turl)
?: &(?=(^ md) =((add u.md 3) turl-len))
:- 'text/plain; charset=utf-8'
(crip (weld "/md" (swag [0 (sub turl-len 3)] turl)))
:- 'text/html; charset=utf-8'
=+ html=(find ".html" turl)
?: &(?=(^ html) =((add u.html 5) turl-len))
(crip (weld "/html" (swag [0 (sub turl-len 5)] turl)))
(crip (weld "/html" turl))
::
^- simple-payload:http
:- [200 ['Content-Type' content-type]~]
%- some
%- as-octs:mimes:html
.^ @t %gx
;: weld
/(scot %p p.byk)/blog/(scot r.byk)
(rash url.request stap) /noun
==
==

34
lib/blog.hoon Normal file
View File

@ -0,0 +1,34 @@
|%
++ default-theme
^- @t
'''
h1, h2, h3, h4, h5, h6 {
color : black;
text-align: center;
}
img {
margin: auto;
max-height: 300px;
display: block;
}
body {
margin : 7vw;
font-size : 3vh;
color: #393939
}
'''
::
++ add-style
|= css=@t
^- tape
:(weld "<style>" (trip css) "<style/>")
::
++ http-response-cards
|= [id=@tas hed=response-header:http data=(unit octs)]
^- (list card:agent:gall)
=/ paths [/http-response/[id]]~
:~ [%give %fact paths %http-response-header !>(hed)]
[%give %fact paths %http-response-data !>(data)]
[%give %kick paths ~]
==
--

View File

@ -7,10 +7,12 @@
++ json
=, dejs:format
%- of
:~ [%publish (ot ~[path+pa html+so md+so])]
:~ [%publish (ot ~[path+pa html+so md+so theme+so])]
[%unpublish (ot ~[path+pa])]
[%save-draft (ot ~[path+pa md+so])]
[%delete-draft (ot ~[path+pa])]
[%save-theme (ot ~[theme+so css+so])]
[%delete-theme (ot ~[theme+so])]
==
--
::

View File

@ -1,8 +1,10 @@
|%
+$ action
$% [%publish =path html=@t md=@t] :: TODO needs to be text and md
$% [%publish =path html=@t md=@t theme=@tas]
[%unpublish =path]
[%save-draft =path md=@t]
[%delete-draft =path]
[%save-theme theme=@tas css=@t]
[%delete-theme theme=@tas]
==
--