tlienart / Xranklin.jl

Experimental repo for a refactoring of Franklin.jl
https://tlienart.github.io/Xranklin.jl
MIT License
40 stars 1 forks source link

Add `hfun` to sort type of pages #276

Open luraess opened 10 months ago

luraess commented 10 months ago

Following up on #273 about specific point

an imprecise feature request to have {{ispage}} figure out the type of page --> this should not be handled by ispage, it should be handled by you coding a hfun_if_is_list_posts_page and having your specific logic injecting whatever needs to be injected if the directory meets your own constraints.

Could you give a hint on how to create such hfun_if_is_list_posts_page function that would return a list of folders to further pass to {{ispage}} based on a input list defined in e.g. config.md:

In config.md:

post_dirs = news, events

In utils.jl:

hfun_if_is_list_posts_page()
    # retrieve post_dirs list from config file
end

In head.html:

# [...]
    <div class="content">

      {{ispage index.html 404.html}}
        <section class="container centered">

      {{else}} {{ispage if_is_list_posts_page()}} # if_is_list_posts_page() should return here /news/ /events/
        <section class="container list">
          <h1 class="title">
            <a class="title-link" href="/{{_relative_url}}">
              <!-- Posts -->
              {{header}}
            </a>
          </h1>

      {{else}} {{ispage if_is_list_posts_page()}} # if_is_list_posts_page() should return here /news/* /events/*
        <section class="container post">
          {{insert post_header.html}}

      {{else}} {{ispage /tags/*}}
        <section class="container list">
# [...]
tlienart commented 10 months ago

can you try {{ispage e"if_is_list_posts_page()"}}?

separately you could also write hfun_inject_if_posts_page() and have your block be

{{else}} {{inject_if_posts_page}}
...

with that function returning the whole HTML. You also have access to relative_url , header etc (via the local context get_lc()).

luraess commented 10 months ago

Thanks! I also added a attempt to draft what I want to do in the sandbox repo: https://github.com/Unil-SGC/multipost/tree/main

Ideally, I have post_dir = ("news", "events") in https://github.com/Unil-SGC/multipost/blob/main/config.md

Then, in utils.jl I could have a hfun that reads in post_dir: https://github.com/Unil-SGC/multipost/blob/7f1d47cd25688b96ca6967dbf3e80b4c75138cda/utils.jl#L68-L75

and modifies the strings to construct the correct ones to be passed in _layout/head.html here https://github.com/Unil-SGC/multipost/blob/7f1d47cd25688b96ca6967dbf3e80b4c75138cda/_layout/head.html#L78 and here https://github.com/Unil-SGC/multipost/blob/7f1d47cd25688b96ca6967dbf3e80b4c75138cda/_layout/head.html#L87

I am though unsure how to access a global variable defined in config.md and also how to pass the string from the hfun to the call side in head.html.

tlienart commented 10 months ago

I am though unsure how to access a global variable defined in config.md and also how to pass the string from the hfun to the call side in head.html.

function hfun_inject_some_html()
    return """<img src="https://upload.wikimedia.org/wikipedia/commons/c/c1/Erizo_de_mar_viol%C3%A1ceo_%28Sphaerechinus_granularis%29%2C_Madeira%2C_Portugal%2C_2019-05-31%2C_DD_36.jpg">"""
end

then in any .md or .html call {{inject_some_html}}

luraess commented 10 months ago

Thanks, that's the hfun I came up with https://github.com/Unil-SGC/multipost/blob/852210a1beb0a4465d6f16c0265b8ceefead59f6/utils.jl#L68-L74. But calling it in head https://github.com/Unil-SGC/multipost/blob/852210a1beb0a4465d6f16c0265b8ceefead59f6/_layout/head.html#L78 seems not to perform the expected result. I may be doing something wrong with the triple quotes wrapping. Curiously, populating the hfun with @show does not output anything.

tlienart commented 10 months ago

no you can't nest these. What I was saying is that instead of having

{{ispage something}}
HTML to inject
{{end}}

you would do

{{inject_if_page}}

(no ispage!)

and


function hfun_inject_if_page()
    # here do the page check using rpath of current context + retrieving global var post_dirs or whatever
    if condition_is_verified
        return """"HTML to inject"""
    end
    return ""
end
luraess commented 10 months ago

Thanks for the hints. I got something to work, but it's not super concise - see here: https://github.com/Unil-SGC/multipost/blob/f7d35787e8c926386f21bfc58088d18615ef89a5/utils.jl#L99-L144. This to be called from _head.html: https://github.com/Unil-SGC/multipost/blob/f7d35787e8c926386f21bfc58088d18615ef89a5/_layout/head.html#L73-L86.

Also, I am still missing the

      {{else}} {{ispage /news/* /events/*}}
        <section class="container post">
          {{insert post_header.html}}

part as I am unsure how to "convert" the {{insert post_header.html}} when calling it from an hfun.

It's maybe finally just easier to manually list post folder in {{ispage }} construct...