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

Option to add HTML line breaks to multiline text #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ python:
- "3.5"
- "3.6"
- "3.7"
- "3.8-dev"
- "3.8"
- "3.9"
- "3.10-dev"

# command to run tests
script: cd test/ && python run_tests.py
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Argument Description
`encode` turn on/off[default] encoding of result to escaped html, compatible with any browser
--------------------- ----------------
`escape` turn on[default]/off escaping of html tags in text nodes (prevents XSS attacks in case you pass untrusted data to json2html)
--------------------- ----------------
`multiline` turn on/off[default] using HTML line breaks in multiline text nodes
===================== ================

Installation
Expand Down
11 changes: 7 additions & 4 deletions json2html/jsonconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@


class Json2Html:
def convert(self, json="", table_attributes='border="1"', clubbing=True, encode=False, escape=True):
def convert(self, json="", table_attributes='border="1"', clubbing=True, encode=False, escape=True, multiline=False):
"""
Convert JSON to HTML Table format
"""
Expand All @@ -42,6 +42,7 @@ def convert(self, json="", table_attributes='border="1"', clubbing=True, encode=
self.table_init_markup = "<table %s>" % table_attributes
self.clubbing = clubbing
self.escape = escape
self.multiline = multiline
json_input = None
if not json:
json_input = {}
Expand Down Expand Up @@ -91,10 +92,12 @@ def convert_json_node(self, json_input):
basic JSON types.
"""
if type(json_input) in text_types:
html_output = text(json_input)
if self.escape:
return html_escape(text(json_input))
else:
return text(json_input)
html_output = html_escape(html_output)
if self.multiline:
html_output = html_output.replace("\n", "<br/>")
return html_output
if hasattr(json_input, 'items'):
return self.convert_object(json_input)
if hasattr(json_input, '__iter__') and hasattr(json_input, '__getitem__'):
Expand Down
10 changes: 10 additions & 0 deletions test/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ def test_xss(self):
u"<script></script>"
)

def test_multiline(self):
self.assertEqual(
json2html.convert("a\nb"),
u"a\nb"
)
self.assertEqual(
json2html.convert("a\nb", multiline=True),
u"a<br/>b"
)

def test_all(self):
for test_definition in self.test_json:
_json = test_definition['json']
Expand Down