Repository: hedyorg/hedy

Issue:
Newlines do not work in quiz answers
Hi @LotusterHaar 

\n in an answer does not render properly:

![image](https://user-images.githubusercontent.com/1003685/136151389-6caea596-d633-4b9f-b5be-e05b0a1bec89.png)

(the text was adapted in #881 but that did not work)


Discussion:
I had a similar issue when implementing the GFM and sending text from app.py to the front-end. This solution worked for me:

`obj.html(obj.html().replace(/\n/g,'<br/>'));` 
> I had a similar issue when implementing the GFM and sending text from app.py to the front-end. This solution worked for me:
> 
> `obj.html(obj.html().replace(/\n/g,'<br/>'));`

Thank you fot this solution. It helped me find a solution for the quiz. 
Can I presume that all the /n newlines have a space before the newline like: " \n". Because I can see in the quiz files that this is often the case, but not consistently
I think this is a mistake (because yamls are hard to edit this way) and
should all be \n without space.

On Sun, 17 Oct 2021 at 12:30, LterHaar ***@***.***> wrote:

> Can I presume that all the /n newlines have a space before the newline
> like: " \n". Because I can see in the quiz files that this is often the
> case, but not consistently
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/Felienne/hedy/issues/887#issuecomment-945088302>, or
> unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AAHVBJN4RLDWK7DWUU2OTXLUHKQS5ANCNFSM5FNT4EVQ>
> .
> Triage notifications on the go with GitHub Mobile for iOS
> <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
> or Android
> <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
>
>

> I think this is a mistake (because yamls are hard to edit this way) and should all be \n without space.
> […](#)
> On Sun, 17 Oct 2021 at 12:30, LterHaar ***@***.***> wrote: Can I presume that all the /n newlines have a space before the newline like: " \n". Because I can see in the quiz files that this is often the case, but not consistently — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub <[#887 (comment)](https://github.com/Felienne/hedy/issues/887#issuecomment-945088302)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AAHVBJN4RLDWK7DWUU2OTXLUHKQS5ANCNFSM5FNT4EVQ> . Triage notifications on the go with GitHub Mobile for iOS <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.

I have found a solution that will replace each \n (so \n with and without space). But only in the values of the code and option_text
Hi @LterHaar!

We overlooked something, namely the repetition of answers:

![image](https://user-images.githubusercontent.com/1003685/138293427-491670bc-f9db-40fe-b9ed-081f425e8818.png)

Can you fix that?

@rix0rrr Ik zag dat je mijn backend functies hebt omgeschreven waaronder de loop waarin de replace van newlines naar br plaatsvind. Ik zie dat er niets met de functie` escape_newlines(x)` wordt gedaan. Ik vermoed dat deze in de question_options_for(question) moet worden aangeroepen. Heb je enig idee waar precies? Of kan je me op weg helpen?

Resolving patch:
diff --git a/app.py b/app.py
--- a/app.py
+++ b/app.py
@@ -25,7 +25,7 @@
 import textwrap
 
 # app.py
-from flask import Flask, request, jsonify, session, abort, g, redirect, Response, make_response, url_for
+from flask import Flask, request, jsonify, session, abort, g, redirect, Response, make_response, url_for, Markup
 from flask_helpers import render_template
 from flask_compress import Compress
 
@@ -910,7 +910,14 @@ def current_language():
 @app.template_filter()
 def nl2br(x):
     """Turn newlines into <br>"""
-    return x.replace('\n', '<br />')
+    # The input to this object will either be a literal string or a 'Markup' object.
+    # In case of a literal string, we need to escape it first, because we have
+    # to be able to make a distinction between safe and unsafe characters.
+    #
+    # In case of a Markup object, make sure to tell it the <br> we're adding is safe
+    if not isinstance(x, Markup):
+      x = Markup.escape(x)
+    return x.replace('\n', Markup('<br />'))
 
 @app.template_global()
 def hedy_link(level_nr, assignment_nr, subpage=None, lang=None):


Test patch: