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

Block helper rewrite #276

Merged
merged 23 commits into from
Oct 22, 2023
Merged

Conversation

praschke
Copy link
Collaborator

@praschke praschke commented Oct 18, 2023

Closes #246, closes #277.

in handlebars, helpers can be passed information through positional or labeled arguments as well as two templated blocks, the truthy options.fn() and the falsy options.else(). these block arguments can be evaluated under whatever context as many times as the helper wants, and they nominally return strings. block helpers (as opposed to regular helpers) are only useful for control flow or composition of effects.

for some reason, handlebars does not always return strings as documented and specced, but objects. when evaluated, {{{.}}} gives the context object, {{{definition}}} gives the definition object from the context. in contrast, kibana's no-eval handlebars (written in typescript) returns the stringified counterparts, '[object Object]'. when yomichan's helpers attempt to treat the result of blocks as a non-string, things break.

the helpers affected by this behavior are

  • dumpObject
  • furigana
  • furiganaPlain
  • formatGlossary

uses of these helpers must be rewritten into non-block helpers, as normal arguments are capable of passing javascript objects from the context to the helper.

{{#dumpObject}}{{{.}}}{{/dumpObject}}
{{dumpObject .}}

{{#furigana}}{{{definition}}}{{/furigana}}
{{furigana definition}}

{{#furiganaPlain}}{{{definition}}}{{/furiganaPlain}}
{{furiganaPlain definition}}

{{#formatGlossary ../dictionary}}{{{.}}}{{/formatGlossary}}
{{formatGlossary ../dictionary .}}

in general, block helpers have been overused in yomichan, meaning the default templates and templates written by the community have been more verbose than necessary. this PR also changes the default templates and helper documentation to reflect better practices.

another bug was found due to regexReplace and regexMatch not excluding the final options object from the arguments iterated over, resulting in [object Object] sometimes being placed in the output.

the deprecated kanjiLinks and sanitizeCssClass have been removed.

the typeof helper now has non-portable behavior with regard to block arguments. the no-eval path should probably be taken on both chrome and firefox instead of using compileFnName to dynamically dispatch.

@praschke praschke requested a review from a team as a code owner October 18, 2023 12:58
@github-actions
Copy link

github-actions bot commented Oct 18, 2023

✔️ No visual differences introduced by this PR.

View Playwright Report (note: open the "playwright-report" artifact)

@djahandarie
Copy link
Collaborator

Nice, this looks great!! The more minimal syntax is a nice unexpected improvement.

What is the expected experience for users after this PR? Will that templates upgrade file fix people's templates automatically in all cases, or is some manual action required?

@praschke
Copy link
Collaborator Author

JP Mining Note uses formatGlossary, and the lines containing them match the lines in the default template. depending on how the template patcher works, it may automatically convert some customized templates. i've only looked at JPMN, there are more customized templates in the wild.

automatic patching also can't be counted on for new users, because someone with an up-to-date install copying JPMN would not be subject to the patch process.

manual work is required to be safe, which is why i included examples of the old and new syntax.

@djahandarie
Copy link
Collaborator

@praschke Hmm. Could we maybe add a button to "Fix broken yomichan templates" in the settings which would run the template patcher for people? I think the template is idempotent so it should be a fairly safe thing to include.

@praschke
Copy link
Collaborator Author

@djahandarie the settings upgrade process happens automatically, and a button would give the impression that all instances would be fixed automatically, when only the default templates are guaranteed to be fixed by the patch. i can't say that all templates in the wild would be fixed by such a button.

@djahandarie
Copy link
Collaborator

Hmm, "Attempt to fix template" button then? 😄

Or if you think even that would be confusing / overcommitting, maybe an addition to the README would be most appropriate then? I think we need some sort of easily-visible guidance for people regarding how to fix their templates 🤔

@praschke
Copy link
Collaborator Author

as well as the readme, i think the welcome page could be used to alert people after an update in case they've modified their templates from the default. i can look into that today or tomorrow.

@praschke praschke force-pushed the block-helper-rewrite branch 2 times, most recently from 7c322dc to d93a177 Compare October 22, 2023 18:13
@praschke
Copy link
Collaborator Author

there is now a migration guide in the readme. for new users of yomitan or users migrating from yomichan or yomibaba, the welcome page notes the situation and links to the guide in the readme.

if someone has custom templates and yomitan updates, then the welcome page will open once and a cautionary notification will be highlighted in red with a similar note and link.

Copy link
Collaborator

@djahandarie djahandarie left a comment

Choose a reason for hiding this comment

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

Looks great, I think this should cover most cases for users! Left one minor comment, otherwise good to approve.

djahandarie
djahandarie previously approved these changes Oct 22, 2023
@djahandarie djahandarie added this pull request to the merge queue Oct 22, 2023
Merged via the queue into themoeway:master with commit c3148c6 Oct 22, 2023
5 checks passed
@praschke praschke deleted the block-helper-rewrite branch October 23, 2023 09:19
@djahandarie djahandarie added the kind/bug The issue or PR is regarding a bug label Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug The issue or PR is regarding a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

anki: furigana-plain no longer works [object Object] for {glossary} when making anki cards through yomitan
2 participants