1*635a8641SAndroid Build Coastguard Worker# -*- coding: utf-8 -*- 2*635a8641SAndroid Build Coastguard Worker""" 3*635a8641SAndroid Build Coastguard Worker markupsafe._native 4*635a8641SAndroid Build Coastguard Worker ~~~~~~~~~~~~~~~~~~ 5*635a8641SAndroid Build Coastguard Worker 6*635a8641SAndroid Build Coastguard Worker Native Python implementation the C module is not compiled. 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker :copyright: (c) 2010 by Armin Ronacher. 9*635a8641SAndroid Build Coastguard Worker :license: BSD, see LICENSE for more details. 10*635a8641SAndroid Build Coastguard Worker""" 11*635a8641SAndroid Build Coastguard Workerfrom markupsafe import Markup 12*635a8641SAndroid Build Coastguard Workerfrom markupsafe._compat import text_type 13*635a8641SAndroid Build Coastguard Worker 14*635a8641SAndroid Build Coastguard Worker 15*635a8641SAndroid Build Coastguard Workerdef escape(s): 16*635a8641SAndroid Build Coastguard Worker """Convert the characters &, <, >, ' and " in string s to HTML-safe 17*635a8641SAndroid Build Coastguard Worker sequences. Use this if you need to display text that might contain 18*635a8641SAndroid Build Coastguard Worker such characters in HTML. Marks return value as markup string. 19*635a8641SAndroid Build Coastguard Worker """ 20*635a8641SAndroid Build Coastguard Worker if hasattr(s, '__html__'): 21*635a8641SAndroid Build Coastguard Worker return s.__html__() 22*635a8641SAndroid Build Coastguard Worker return Markup(text_type(s) 23*635a8641SAndroid Build Coastguard Worker .replace('&', '&') 24*635a8641SAndroid Build Coastguard Worker .replace('>', '>') 25*635a8641SAndroid Build Coastguard Worker .replace('<', '<') 26*635a8641SAndroid Build Coastguard Worker .replace("'", ''') 27*635a8641SAndroid Build Coastguard Worker .replace('"', '"') 28*635a8641SAndroid Build Coastguard Worker ) 29*635a8641SAndroid Build Coastguard Worker 30*635a8641SAndroid Build Coastguard Worker 31*635a8641SAndroid Build Coastguard Workerdef escape_silent(s): 32*635a8641SAndroid Build Coastguard Worker """Like :func:`escape` but converts `None` into an empty 33*635a8641SAndroid Build Coastguard Worker markup string. 34*635a8641SAndroid Build Coastguard Worker """ 35*635a8641SAndroid Build Coastguard Worker if s is None: 36*635a8641SAndroid Build Coastguard Worker return Markup() 37*635a8641SAndroid Build Coastguard Worker return escape(s) 38*635a8641SAndroid Build Coastguard Worker 39*635a8641SAndroid Build Coastguard Worker 40*635a8641SAndroid Build Coastguard Workerdef soft_unicode(s): 41*635a8641SAndroid Build Coastguard Worker """Make a string unicode if it isn't already. That way a markup 42*635a8641SAndroid Build Coastguard Worker string is not converted back to unicode. 43*635a8641SAndroid Build Coastguard Worker """ 44*635a8641SAndroid Build Coastguard Worker if not isinstance(s, text_type): 45*635a8641SAndroid Build Coastguard Worker s = text_type(s) 46*635a8641SAndroid Build Coastguard Worker return s 47