This function mimics the behaviour of underscore js escape function The html escaped by jinja is not compatible for underscore unescape function :param text: input html text :return: escaped text
(text)
| 25 | |
| 26 | |
| 27 | def underscore_escape(text): |
| 28 | """ |
| 29 | This function mimics the behaviour of underscore js escape function |
| 30 | The html escaped by jinja is not compatible for underscore unescape |
| 31 | function |
| 32 | :param text: input html text |
| 33 | :return: escaped text |
| 34 | """ |
| 35 | html_map = { |
| 36 | '&': "&", |
| 37 | '<': "<", |
| 38 | '>': ">", |
| 39 | '"': """, |
| 40 | "'": "'" |
| 41 | } |
| 42 | |
| 43 | # always replace & first |
| 44 | if text: |
| 45 | for c, r in sorted(html_map.items(), |
| 46 | key=lambda x: 0 if x[0] == '&' else 1): |
| 47 | text = text.replace(c, r) |
| 48 | |
| 49 | return text |
| 50 | |
| 51 | |
| 52 | def underscore_unescape(text): |