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

Possibly escape apostrophes differently #33

Open
araichev opened this issue May 23, 2018 · 1 comment
Open

Possibly escape apostrophes differently #33

araichev opened this issue May 23, 2018 · 1 comment

Comments

@araichev
Copy link

Hi there,

I'm using your library to produce HTML tables to good effect in most cases.
However, when the dictionary input involves apostrophes, the output fails to play nice with Folium popups.
Using html.escape seems to fix that.
For example, running

from json2html import json2html as jh
import html


d = {'stop_name': "Ponsonby Rd & K'Rd"}
dd = {}
for k, v in d.items():
    try:
        vv = html.escape(v)
    except AttributeError:
        vv = v
    dd[k] = vv

jh.convert(d), jh.convert(dd)

produces

'<table border="1"><tr><th>stop_name</th><td>Ponsonby Rd &amp; K\'Rd</td></tr></table>',
 '<table border="1"><tr><th>stop_name</th><td>Ponsonby Rd &amp;amp; K&amp;#x27;Rd</td></tr></table>'

The first output crashes Folium popups, but the second works with them.
So would it be better for json2html to escape apostrophes in the same way that html.escape does?

@ljluestc
Copy link

import html
from json2html import json2html as jh

def convert_with_escape(data):
    # Create a new dictionary to store the escaped values
    escaped_data = {}
    for key, value in data.items():
        if isinstance(value, str):
            # Escape the value to handle special characters
            escaped_value = html.escape(value)
            escaped_data[key] = escaped_value
        else:
            escaped_data[key] = value  # Keep non-string values unchanged
    
    # Use the json2html conversion method on the escaped data
    return jh.convert(escaped_data)

# Example usage
d = {'stop_name': "Ponsonby Rd & K'Rd"}
html_output = convert_with_escape(d)
print(html_output)

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

No branches or pull requests

2 participants