| 
									
										
										
										
											2010-10-15 15:57:45 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | General functions for HTML manipulation. | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # NB: this is a candidate for a bytes/string polymorphic interface | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def escape(s, quote=True): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Replace special characters "&", "<" and ">" to HTML-safe sequences. | 
					
						
							|  |  |  |     If the optional flag quote is true (the default), the quotation mark | 
					
						
							| 
									
										
										
										
											2011-09-13 07:14:13 +08:00
										 |  |  |     characters, both double quote (") and single quote (') characters are also | 
					
						
							|  |  |  |     translated. | 
					
						
							| 
									
										
										
										
											2010-10-15 15:57:45 +00:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2013-07-07 11:11:24 +02:00
										 |  |  |     s = s.replace("&", "&") # Must be done first! | 
					
						
							|  |  |  |     s = s.replace("<", "<") | 
					
						
							|  |  |  |     s = s.replace(">", ">") | 
					
						
							| 
									
										
										
										
											2010-10-15 15:57:45 +00:00
										 |  |  |     if quote: | 
					
						
							| 
									
										
										
										
											2013-07-07 11:11:24 +02:00
										 |  |  |         s = s.replace('"', """) | 
					
						
							|  |  |  |         s = s.replace('\'', "'") | 
					
						
							|  |  |  |     return s |