Code Monkey home page Code Monkey logo

Comments (5)

beniTrainor avatar beniTrainor commented on August 22, 2024 1

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.

from org_to_anki.

beniTrainor avatar beniTrainor commented on August 22, 2024

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?

from org_to_anki.

beniTrainor avatar beniTrainor commented on August 22, 2024

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.

from org_to_anki.

beniTrainor avatar beniTrainor commented on August 22, 2024

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.

from org_to_anki.

c-okelly avatar c-okelly commented on August 22, 2024

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

from org_to_anki.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.