Permalink
Browse files

[transform] Add unwrap transform to remove newlines from a string

  • Loading branch information...
lovett committed Jan 9, 2019
1 parent 915780b commit 7fb89f5c2ae73408c1bfc039ffbe9766cabbc6fd
Showing with 27 additions and 2 deletions.
  1. +25 −0 apps/transform/main.py
  2. +1 −1 apps/transform/test_main.py
  3. +1 −1 apps/transform/transform.jinja.html
@@ -1,6 +1,7 @@
"""Convert a string to a different format."""

import urllib
import re
import cherrypy


@@ -19,8 +20,31 @@ def __init__(self):
"upper": lambda x: x.upper(),
"urldecode": urllib.parse.unquote_plus,
"urlencode": urllib.parse.quote_plus,
"unwrap": self.unwrap
}

@staticmethod
def unwrap(value):
"""Remove newlines and email quotes from a string.
Treat consecutive newlines as paragraph separators.
"""

value = value.replace("\r", "")
paragraphs = re.split("\n{2,}", value)

paragraphs = [
re.sub("[\r\n]?>[ ]+", " ", paragraph)
for paragraph in paragraphs
]

paragraphs = [
re.sub("[\r\n]", " ", paragraph)
for paragraph in paragraphs
]

return "\n\n".join(paragraphs)

def list_of_transforms(self):
"""Shape the list of transforms into a list of keys"""
return sorted(list(self.transforms.keys()))
@@ -52,6 +76,7 @@ def POST(self, transform, value):
"html": ("transform.jinja.html", {
"app_name": self.name,
"result": result,
"current_transform": transform,
"transforms": self.list_of_transforms(),
"value": value,
})
@@ -41,7 +41,7 @@ def test_lowercase_html(self):
transform="lower",
value="TEST")
self.assertTrue(helpers.response_is_html(response))
self.assertTrue("""<div id="result">test</div>""" in response.body)
self.assertTrue("test" in response.body)

def test_lowercase_json(self):
"""Input is converted to lowercase and returned as JSON"""
@@ -41,7 +41,7 @@
<p>
<clipboard-button target-id="result" label="Copy to clipboard" />
</p>
<div id="result">{{ result|escape()|nl2br() }}</div>
<div id="result" style="white-space: pre-line">{{ result|escape() }}</div>
</div>
{% endif %}
</form>

0 comments on commit 7fb89f5

Please sign in to comment.