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

Feature/replace-get-highlighted-text #313

Open
wants to merge 42 commits into
base: v4
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
663b04e
Add JIT highlighting of morphs
RobHelgeson Nov 27, 2024
05ff0b6
Add Rubification of sting - built in filters cannot run after custom …
RobHelgeson Nov 27, 2024
2df17f9
Allow filter to run after built-in filter
RobHelgeson Nov 27, 2024
9056db7
Add documentation
RobHelgeson Nov 27, 2024
d84799b
Fix am-highlight-morphs parse html issue
RobHelgeson Nov 27, 2024
1117111
Allow cards where you want to use am-highlight-morphs to not have the…
RobHelgeson Nov 27, 2024
3401040
Address PR comments
RobHelgeson Nov 28, 2024
bd73b95
Update documentation
RobHelgeson Nov 28, 2024
d2e2cf4
Update documentation
RobHelgeson Nov 28, 2024
0e2573a
Updates for code review
RobHelgeson Nov 29, 2024
fdeb76e
Fix bad morph highlighting in ruby html
RobHelgeson Nov 29, 2024
832a255
Fix bad morph highlighting in ruby html - WIP
RobHelgeson Nov 29, 2024
b23ebd8
experemental
RobHelgeson Nov 30, 2024
005f2dd
Reimpl of highlight text using regexes.
RobHelgeson Dec 2, 2024
eb34b3a
Update highlight comments.
RobHelgeson Dec 2, 2024
ea14846
Update documentation.
RobHelgeson Dec 2, 2024
6072576
WIP - WIP
RobHelgeson Dec 4, 2024
fb5ce61
Update jit formatter with changes from experimental branch
RobHelgeson Dec 9, 2024
7189712
Checkpoint commit - WIP
RobHelgeson Dec 10, 2024
7a3ae5f
Checkpoint speeeeedy! - WIP
RobHelgeson Dec 10, 2024
f7841ee
Checkpoint - WIP
RobHelgeson Dec 10, 2024
c8e8883
Checkpoint - WIP
RobHelgeson Dec 10, 2024
994355f
Checkpoint - WIP
RobHelgeson Dec 10, 2024
fdc6a07
Checkpoint - WIP
RobHelgeson Dec 10, 2024
c16ba70
Checkpoint - feature complete
RobHelgeson Dec 11, 2024
78aee2e
Add JIT highlighting
RobHelgeson Dec 11, 2024
eb57cba
Documentation and dead code
RobHelgeson Dec 11, 2024
f2b702f
Cleanup
RobHelgeson Dec 11, 2024
47f7c14
Replace get_highlighted_text
RobHelgeson Dec 11, 2024
b5af09f
Rename some variables for clarity
RobHelgeson Dec 12, 2024
20336c1
Formatting per PR request.
RobHelgeson Dec 12, 2024
da5eb66
Renamed some symbols for clarity.
RobHelgeson Dec 13, 2024
4b4be12
Renamed some symbols for clarity.
RobHelgeson Dec 13, 2024
b530660
updated guide
mortii Dec 14, 2024
f8c56d1
fixed broken test
mortii Dec 14, 2024
d569b52
deleted empty file
mortii Dec 14, 2024
e58e9d7
Address text parsing bug where rubies were allowed to overlap
RobHelgeson Dec 21, 2024
af3449b
Support text based rubies
RobHelgeson Jan 3, 2025
0d897a4
Pr cleanup
RobHelgeson Jan 6, 2025
bda97d0
Implement jit formatting for furigana, kanji and kana.
RobHelgeson Jan 6, 2025
c4eb14d
added temp verbose dev logging
mortii Jan 7, 2025
648054c
Create subclasses for all text formatting options
RobHelgeson Jan 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions ankimorphs/highlight_morphs_jit.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,19 @@ def highlight_morphs_jit(
if not card_morphs:
return field_text

return text_highlighting.get_highlighted_text(
am_config=am_config,
morphemes=card_morphs,
text=_dehtml(field_text),
use_html_rubies=True,
styles = _get_styles(filter_name)

return (
f"<span class='{filter_name}'>"
+ styles
+ " "
Copy link
Owner

@mortii mortii Jan 7, 2025

Choose a reason for hiding this comment

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

I don't think you can inline styles like this... It doesn't work for me at least.

Copy link
Author

Choose a reason for hiding this comment

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

Odd, this worked for me.

I must have some other things allowing it to work. I'll double check.

Copy link
Author

Choose a reason for hiding this comment

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

Can you send me either a screenshot, or the html, produced by (something like):

<div>{{am-highlight:Reading}}</div>
<div>{{am-highlight-furigana:Reading}}</div>
<div>{{am-highlight-kana:Reading}}</div>
<div>{{am-highlight-kanji:Reading}}</div>

I'm trying to break it over here but it looks like it works for me.

Copy link
Owner

Choose a reason for hiding this comment

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

Screenshot from 2025-01-07 17-07-50

Does the font size change if you do this?

    if filter_name == "am-highlight-kana":
        return """<style>
.am-highlight-kana {
    & ruby {
        display: inline-block;
        visibility: hidden;

        & rt {
            font-size: 999;
            margin-top: -1em;
            visibility: visible;
        }
    }
}
</style>"""

do you have a css-injector add-on activated?

Copy link
Author

@RobHelgeson RobHelgeson Jan 7, 2025

Choose a reason for hiding this comment

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

font-size: 999; the 999 needs a measurement like rem, px or %.

Edit: yes changing font size does affect the output.

Copy link
Owner

Choose a reason for hiding this comment

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

font-size: 999; the 999 needs a measurement like rem, px or %

right, does a valid change like that work for you?

Copy link
Owner

@mortii mortii Jan 7, 2025

Choose a reason for hiding this comment

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

I get the same result if I remove the styles:

    highlighted_jit_text = (
        f"testinggg<span class='{filter_name}'>"
        + " "
        + text_highlighting.get_highlighted_text(
            am_config=am_config,
            morphemes=card_morphs,
            text=_dehtml(field_text),
            use_html_rubies=filter_name != "am-highlight",
        )
        + "</span>"
    )

Screenshot from 2025-01-07 17-31-55

If i change the font size nothing happens.

Copy link
Author

@RobHelgeson RobHelgeson Jan 7, 2025

Choose a reason for hiding this comment

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

😮

that's... odd.

is it possible that you may have some of the css that we were experimenting with before, still active somewhere?


I'll attach some screenshots, if you can try this, we can see where your styles are coming from vs where mine are coming from.

grab: AnkiWebView Inspector https://ankiweb.net/shared/info/31746032

inspect kana element by right clicking and inspecting:
image

click Computed in the right panel of the inspector:
image

click the circled arrow icon on the property you want to see where it "came from" (I cannot screenshot the circled icon because it's on mouseover only, but its a grey circle next to the value of the property):
image

this will take you to the styles tab, where you can see what styles are loaded and applied:
image

and by clicking on the top right <style> here, it will highlight where that style got loaded in.
image

for me, that is this block:
image

I know that's a HUGE pain in the butt. :D but I don't know why this would not be working for you but is for me (so far)

Copy link
Owner

Choose a reason for hiding this comment

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

Aha, I realize why.

When using {{am-highlight-kana:kana:Front}} I don't get any ruby/rt elements at all:

Screenshot from 2025-01-07 19-23-58

but I do get them when only using {{am-highlight-kana:Front}}
Screenshot from 2025-01-07 19-26-52

Copy link
Author

Choose a reason for hiding this comment

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

When using {{am-highlight-kana:kana:Front}} I don't get any ruby/rt elements at all:

aha, this makes sense. The built-in filter removes all kanji from the string before we even "see it" so we process a "kana only" string.

This has the negative side effect of not allowing us to use {{am-highlight-kana:kana:Front}} for backward compatibility (i.e. when on mobile).

The built in furigana and kanji filters will still work because the morphs match on kanji (most of the time) with kana readings removed from the parsed string.

I can't speak for you the project, but I'll suggest that having that as a known issue may be the appropriate action, with the recommendation to either:

  • set your card with {{am-highlight-kana:furigana:Front}} and have kana locally and furigana on mobile.
  • set your card template to be "mobile aware" and use {{am-highlight-kana:Front}} locally and {{kana:Front}} on mobile (I do not know how to do this at the moment)

+ text_highlighting.get_highlighted_text(
am_config=am_config,
morphemes=card_morphs,
text=_dehtml(field_text),
use_html_rubies=filter_name != "am-highlight",
)
+ "</span>"
)


Expand Down Expand Up @@ -149,3 +157,34 @@ def _dehtml(
text = anki.utils.strip_html(text)

return text_preprocessing.get_processed_text(am_config, text) if am_config else text


def _get_styles(filter_name: str) -> str:
"""Get local styles for this run, based on the filter name."""

if filter_name == "am-highlight-kana":
return """<style>
.am-highlight-kana {
& ruby {
display: inline-block;
visibility: hidden;

& rt {
font-size: inherit;
margin-top: -1em;
visibility: visible;
}
}
}
</style>"""

if filter_name == "am-highlight-kanji":
return """<style>
.am-highlight-kanji {
& ruby rt {
display: none;
}
}
</style>"""

return ""