gregorias / anki-code-highlighter

Anki plugin for code syntax highlighting.
GNU Affero General Public License v3.0
55 stars 3 forks source link

Error message #34

Closed jsreaves closed 1 year ago

jsreaves commented 1 year ago

Hello,

I'm getting the error message below when I highlight a code snippet and then click the icon. When I click the icon without anything highlighted, I get the expected dialog boxes to choose the highlighter and language.

Debug info: Anki 2.1.56 (07fd88dd) Python 3.9.15 Qt 6.3.2 PyQt 6.3.1 Platform: macOS-13.1-arm64-arm-64bit Flags: frz=True ao=True sv=3 Add-ons, last update check: 2023-01-21 06:48:20 Add-ons possibly involved: ⁨Highlight Code⁩

Caught exception: Traceback (most recent call last): File "aqt.webview", line 562, in handler File "/Users/username/Library/Application Support/Anki2/addons21/112228974/ankieditorextra.py", line 76, in transform_field field = extract_field_from_web_editor(web_editor_html) File "/Users/username/Library/Application Support/Anki2/addons21/112228974/ankieditorextra.py", line 39, in extract_field_from_web_editor result = re.search('<anki-editable[^>]>(.)', File "re", line 201, in search TypeError: expected string or bytes-like object

gregorias commented 1 year ago

Hi, could you provide more detailed steps to reproduce this? To mitigate the impact of this bug, could you try the keyboard shortcut (Command + ')?

jsreaves commented 1 year ago
  1. Paste code into Front field of Basic Anki card template using shift-command-V to keep formatting.
  2. Click the icon for the plug in.
  3. Error dialog pops up.

The same thing happens when I use the keyboard shortcut (Cmd + ') instead of the icon. The reason I was using the icon in the first place is that I mis-read the keyboard shortcut and was trying (Cmd + +), which seems to change the font size.

I also tried disabling all other add-ons, but unfortunately, this didn't fix the problem.

As you can see from the error message,I am using Anki 2.1.56, which just came out.

jsreaves commented 1 year ago

I am able to reproduce the error when I paste in an example from this page: https://www.codecademy.com/resources/docs/python/recursion

I think the error depends on how the code snippet is formatted. For instance, when I paste the example code from the Python wiki page (with the shift key, to keep formatting), I do not get the error message I reported yesterday, but I do get a breakdown of the HTML tags when I click the code highlighting icon. Below is what appears in the Front field of Anki Add screen after I click the highlighting plugin. (This snippet shows what appears in normal mode, not in HTML editor mode.) As you can see, HTML is breaking through.

Maybe retained HTML tags in pasted code snippets are causing the re.search to fail, or to otherwise misbehave? An obvious solution would be to strip formatting when pasting, but this would force the user to add back line breaks and indentation.

n = int(input('Type a number, and its factorial will be printed: '))

<span style="font-weight: bold;">if</span> n &lt; 0:
    <span style="font-weight: bold;">raise</span> <span style="font-weight: bold;">ValueError</span>('You must enter a non-negative integer')

factorial = 1
<span style="font-weight: bold;">for</span> i <span style="font-weight: bold;">in</span> range(2, n + 1):
    factorial *= i

print(factorial)
gregorias commented 1 year ago

Thank you, but could you provide more details? I've tried 2.1.56 and couldn't reproduce the bug. What I need is:

  1. The HTML code of the field in which you make a selection.
  2. A screenshot of the selection before running this plugin.

Also note that this plugin only works on untagged code. You need to clean pasted code from HTML tags, because this plugin works on the HTML source and not on the rendered text you see in the WYSIWYG editor.

jsreaves commented 1 year ago

In my Anki preferences, I have enabled "Paste without shift key strips formatting." My initial assumption was that pasting in this way would remove HTML tags and allow the plugin to work. However, I have found that some tags are not stripped, and this leads to the error. In my tests, pasting without the shift key strips the <pre> tag but does not strip <div> tags. The error occurs when either of these tags is present.

Screenshot 2023-01-24 at 7 35 26 AM

Example 1: Paste with Cmd-V (strip formatting) -> Error

Screenshot 2023-01-24 at 7 21 12 AM

Example 2: Paste with Cmd-Shift-V -> Error

Screenshot 2023-01-24 at 7 26 41 AM

Example 3: Paste to text editor, then paste to Anki -> No error

Screenshot 2023-01-24 at 7 29 01 AM
dhiren2 commented 1 year ago

This happens because surroundCode() gets a non-text node from Range(). I created #35 to address this for now, though it does not address the underlying issue of it getting a non-text node.

gregorias commented 1 year ago

Thank you for your support and patience. FYI, I understand the issue now and am working on a fix. Should be done this weekend.