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

Create mdbook-tera-backend renderer #80

Merged
merged 17 commits into from
Nov 9, 2023
Merged

Create mdbook-tera-backend renderer #80

merged 17 commits into from
Nov 9, 2023

Conversation

sakex
Copy link
Collaborator

@sakex sakex commented Sep 15, 2023

Here is the prototype, it does almost everything we want already, I just need to port the actual LanguagePicker

You can see that the code to create a custom component is pretty straightforward (see the test component in src/custom_components/test_component.rs).

You can check the test in src/custom_component_renderer/book_directory_renderer.rs to see how it all works.

Fixes #70.

@mgeisler
Copy link
Collaborator

Hey @sakex, thanks for putting this up! I think my only major concern is how people (myself included! 🙂) will create and modify the custom components?

I had hoped they would be defined in the theme, just like I see Tera allows you to create new macros in a template file.

I feel this is crucial since I need a way of looking at the output when modify the templates. So I need to be able to run mdbool serve and see the output right away.

@sakex
Copy link
Collaborator Author

sakex commented Sep 16, 2023

A separate custom-made component is definitely doable.

It could look like

book.toml

[output.i18-helper.components]
MyCustomComponent = "path/to/comp.html"
comp.html

<div>
some stuff in there, maybe tera templates too
</div>
index.html
...
<MyCustomComponent />
...

@sakex
Copy link
Collaborator Author

sakex commented Sep 16, 2023

Because in my test component, I'm generating the HTML using DOM manipulations with the scraper crate. But we don't have to do it that way, we can also do HTML::parse on a string and then inject the parsed node to the original tree.

@sakex
Copy link
Collaborator Author

sakex commented Sep 16, 2023

For complex and common components, we will create them inside of the crate. Otherwise, users will be able to create their own. I'll add that feature in a further CL though, if that works for you.

LanguagePicker should definitely live here for instance. Also I'd really like to create an <ExercizeCodeBlock /> and a <WasmCodeBlock /> in the future. Because the components created here don't need to be used in the template, they can also be added to the markdown directly

@mgeisler
Copy link
Collaborator

A separate custom-made component is definitely doable.

It could look like

book.toml

[output.i18-helper.components]
MyCustomComponent = "path/to/comp.html"
comp.html

<div>
some stuff in there, maybe tera templates too
</div>

This is the main question: how do I create a component with custom logic? With loops, variables, if statements, etc? I have the impression that I need to write Rust code for that?

I liked using Tera much more: there we can keep our code to a minimum. I don't really like code: more code is normally more trouble 😄 Put differently, I don't want us to maintain a template system here. Pushing all logic to the template library should make our life easier.

It also makes things clearer for downstream users: just modify index.hbs to hook in the call that loads a macro from mdbook-i18n-helpers.html and then use those macros where you like. I think it would even be possible to let downstream users override parts of the macros using the inheritance system in Tera, but I'm not 100% sure.

@sakex
Copy link
Collaborator Author

sakex commented Sep 16, 2023 via email

src/main.rs Outdated Show resolved Hide resolved
@sakex sakex force-pushed the renderer branch 4 times, most recently from 7ec1470 to 5dd8cbe Compare September 20, 2023 01:41
src/lib.rs Outdated Show resolved Hide resolved
@sakex sakex force-pushed the renderer branch 2 times, most recently from 2dd19a4 to 55ced24 Compare September 26, 2023 19:15
Comment on lines 41 to 42
{% for identifier, language_name in get_context(key="output.i18n.languages")
%}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is get_context still there? I think it got removed and that this can be replaced with

Suggested change
{% for identifier, language_name in get_context(key="output.i18n.languages")
%}
{% for identifier, language_name in get_context(key="ctx.config.output.i18n.languages") %}

I don't think you show the corresponding book.toml for this anywhere? This makes the example a bit complicated in my opinion.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can fix this before/after merging this PR — we'll have time to go over the documentation before we release this on crates.io.

Copy link
Collaborator

@mgeisler mgeisler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @sakex, I think this has been cooking more than needed now!

Thank you for fixing many small thing. From my testing, I can see that this is a huge step forward! It allows us to solve some very long-standing and important problems in Comprehensive Rust (as shown in #35 (comment)).

mgeisler added a commit that referenced this pull request Nov 9, 2023
We will soon add another crate to the workspace (#80). The new crate
is a mdbook backend, which can be used to enhance mdbook-i18n-helpers.
The backend isn’t strongly tried to mdbook-i18n-helpers. It therefore
makes sense to give the new crate its own version numbers and its own
changelog file.
@sakex sakex enabled auto-merge (squash) November 9, 2023 15:23
@sakex sakex closed this Nov 9, 2023
auto-merge was automatically disabled November 9, 2023 15:25

Pull request was closed

@sakex sakex reopened this Nov 9, 2023
@sakex sakex enabled auto-merge (squash) November 9, 2023 15:43
@sakex sakex merged commit 3044744 into google:main Nov 9, 2023
13 checks passed
dyoo pushed a commit to dyoo/mdbook-i18n-helpers that referenced this pull request Nov 9, 2023
Create renderer

Co-authored-by: Martin Geisler <martin@geisler.net>
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

Successfully merging this pull request may close these issues.

Write mdbook renderer with strong templating system
3 participants