Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hfun to sort type of pages #276

Open
luraess opened this issue Nov 13, 2023 · 6 comments
Open

Add hfun to sort type of pages #276

luraess opened this issue Nov 13, 2023 · 6 comments

Comments

@luraess
Copy link

luraess commented Nov 13, 2023

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
Copy link
Owner

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
Copy link
Author

luraess commented Nov 15, 2023

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
Copy link
Owner

tlienart commented Nov 16, 2023

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.

  • accessing a global variable from a hfun can be done via getgvar(:name_of_var) (so getlvar(...) for a local context variable and getgvar for the global one)
  • second part:
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
Copy link
Author

luraess commented Nov 16, 2023

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
Copy link
Owner

tlienart commented Nov 17, 2023

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
Copy link
Author

luraess commented Nov 19, 2023

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants