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

HTML elements not rendered correctly in question statements #80

Open
3 of 4 tasks
beniTrainor opened this issue Oct 4, 2020 · 5 comments
Open
3 of 4 tasks

HTML elements not rendered correctly in question statements #80

beniTrainor opened this issue Oct 4, 2020 · 5 comments
Assignees
Labels

Comments

@beniTrainor
Copy link

beniTrainor commented Oct 4, 2020

Please follow the template below to report your issue.

  • Raw text
  • Error report provided by Anki
  • Operatin system
  • Original file type

Please include the following information:

Any general information

Hi there! I want to be able to write the following question:

* How do you generate <li> elements with pug

But when I run org_to_anki the tags ("<", ">") dissappear and the line breaks into multiple lines, like so:

How do you generate
elements with pug

If instead I use &lt; and &gt; in the question statement it works fine. But the character sequences are fairly tedious to write. Is there any way around this?

I've also tried using accents (`) to escape the characters but it gives me the following:

How do you generate `
` elements with pug

Raw text of the file you tried to upload

* How do you generate <li> elements with pug

Error report from the popup

There's no error report from Anki.

What is your operating system

Ubuntu 16.04.

What was the original file type

Text file.

@beniTrainor
Copy link
Author

I've found standard library function that does exactly that:

from html import escape
escape("<li>")

Output:

 '&lt;li&gt;'

I think it only works on certain versions of Python though, but I'm not completely sure. If you want more information, check out this StackOverflow question from which I found the answer.

Could this be included into org_to_anki to automatically encode HTML special characters?

@beniTrainor
Copy link
Author

I've made a simple (maybe dirty) hack to get around this by modifying the _formatFile function located at src/org_to_anki/org_parser/parseData.py:

def _formatFile(filePath):# (filePath: str):

    from html import escape

    with open(filePath, mode="r", encoding="utf-8") as file:
        data = file.read().split('\n')

        # escape HTML special chars
        data = [escape(line) for line in data]

    return data

Now it works like a charm.

Let me know if you want me to create a PR or something to include this feature/fix. I haven't written tests or documentation though. It's just a little hack to get me going.

@beniTrainor
Copy link
Author

It turns out my previous answer created another problem. Now, the code blocks with < / > are escaped into &lt; / &gt;, respectively.

So, I've reverted the change made and added the following to src/org_to_anki/AnkiClasses/AnkiQuestionFactory.py:

class AnkiQuestionFactory:
    ...
    def buildQuestion(self):

        from html import escape

        ...

        for line in self.currentQuestions:
            line = self.utils.removeAsterisk(line)
            line = self.utils.formatLine(line)
+          # Escape HTML special characters
+          line = escape(line)
            line = self.utils.parseAnswerLine(line, self.filePath, newQuestion)
            newQuestion.addQuestion(line)
        ...

Now it does what I want.

I haven't followed much the conventions used for the project because I just wanted to get this to work. Maybe it would be better to move the changes into the DeckBuilderUtils or somewhere else.

@c-okelly
Copy link
Owner

c-okelly commented Oct 5, 2020

Sorry for the delay in getting back to you! busy weekend.

I will have a look over it it the next few days I would like to be able to support such functionality. I think your last comment for
line = escape(line) looks like the right idea. There should be some way of encoding it so that it get correctly escaped.

I will mark this as a bug and try get a fix done this week

@c-okelly c-okelly reopened this Oct 5, 2020
@c-okelly c-okelly added the bug label Oct 5, 2020
@c-okelly c-okelly self-assigned this Oct 5, 2020
@beniTrainor
Copy link
Author

Ok, thanks! By the way, I've found another related issue. Even with this fix, if you write HTML characters in answer statements they don't get encoded correctly and end up breaking the lines.

Example file:

* Question <li>
** Answer <li>

Output (in Anki):

Question <li>
- Answer
- 

Note: the answer line breaks into two.

I think if you were to escape all text (questions and answers), except for code blocks (which already escape these characters) it would work.

I don't know exactly what exactly has to be done. I'll try to look into it if I have time this week.

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

No branches or pull requests

2 participants