def pt_edit(self, text, content_type, keep_output_encoding=False): text = text.strip() is_unicode = isinstance(text, unicode) encoding = None output_encoding = None if content_type.startswith('text/xml'): if is_unicode: encoding = None output_encoding = 'utf-8' else: encoding = encodingFromXMLPreamble(text) output_encoding = 'utf-8' elif content_type.startswith('text/html'): charset = charsetFromMetaEquiv(text) if is_unicode: if charset: encoding = None output_encoding = charset else: encoding = None output_encoding = 'iso-8859-15' else: if charset: encoding = charset output_encoding = charset else: encoding = 'iso-8859-15' output_encoding = 'iso-8859-15' else: utext, encoding = convertToUnicode(text, content_type, preferred_encodings) output_encoding = encoding # for content updated through WebDAV, FTP if not keep_output_encoding: self.output_encoding = output_encoding if not is_unicode: text = unicode(text, encoding) self.ZCacheable_invalidate() super(ZopePageTemplate, self).pt_edit(text, content_type)
def _readFile(self, reparse): """Read the data from the filesystem. """ if reparse: file = open(self._filepath, 'rU') # not 'rb', as this is a text file! try: data = file.read() finally: file.close() # If we already have a content_type set it must come from a # .metadata file and we should always honor that. The content # type is initialized as text/html by default, so we only # attempt further detection if the default is encountered. # One previous misbehavior remains: It is not possible to # force a text./html type if parsing detects it as XML. encoding = None if getattr(self, 'content_type', 'text/html') == 'text/html': xml_info = xml_detect_re.match(data) if xml_info: # Smells like xml # set "content_type" from the XML declaration encoding = xml_info.group(1) or 'utf-8' self.content_type = 'text/xml; charset=%s' % encoding if encoding is None: charset = getattr(self, 'charset', None) if charset is None: if self.content_type.startswith('text/html'): charset = charsetFromMetaEquiv(data) or 'iso-8859-15' elif self.content_type.startswith('text/xml'): charset = encodingFromXMLPreamble(data) else: raise ValueError('Unsupported content-type: %s' % self.content_type) if not isinstance(data, unicode): data = unicode(data, charset) else: if not isinstance(data, unicode): data = unicode(data, encoding) self.write(data)
def _readFile(self, reparse): """Read the data from the filesystem. """ file = open(self._filepath, 'rU') # not 'rb', as this is a text file! try: data = file.read() finally: file.close() if reparse: # If we already have a content_type set it must come from a # .metadata file and we should always honor that. The content # type is initialized as text/html by default, so we only # attempt further detection if the default is encountered. # One previous misbehavior remains: It is not possible to # force a text./html type if parsing detects it as XML. encoding = None if getattr(self, 'content_type', 'text/html') == 'text/html': xml_info = xml_detect_re.match(data) if xml_info: # Smells like xml # set "content_type" from the XML declaration encoding = xml_info.group(1) or 'utf-8' self.content_type = 'text/xml; charset=%s' % encoding if encoding is None: charset = getattr(self, 'charset', None) if charset is None: if self.content_type.startswith('text/html'): charset = charsetFromMetaEquiv(data) or 'iso-8859-15' elif self.content_type.startswith('text/xml'): charset = encodingFromXMLPreamble(data) else: raise ValueError('Unsupported content-type: %s' % self.content_type) if not isinstance(data, unicode): data = unicode(data, charset) else: if not isinstance(data, unicode): data = unicode(data, encoding) self.write(data)
def _prepare_xml(self, text): if not isinstance(text, six.text_type): encoding = encodingFromXMLPreamble(text, default=self.encoding) text = text.decode(encoding) return text, 'text/xml'
def _readFile(self, reparse): """Read the data from the filesystem. """ if reparse: if six.PY2: # not 'rb', as this is a text file! file = open(self._filepath, 'rU') else: file = open(self._filepath, 'br') try: data = file.read() if not six.PY2: data = data.replace(b'\r\n', b'\n').replace(b'\r', b'\n') finally: file.close() # If we already have a content_type set it must come from a # .metadata file and we should always honor that. The content # type is initialized as text/html by default, so we only # attempt further detection if the default is encountered. # One previous misbehavior remains: It is not possible to # force a text/html type if parsing detects it as XML. encoding = None preferred = preferred_encodings[:] if getattr(self, 'content_type', 'text/html') == 'text/html': xml_info = xml_detect_re.match(data) if xml_info: # Smells like xml # set "content_type" from the XML declaration encoding = xml_info.group(1) or 'utf-8' self.content_type = 'text/xml; charset=%s' % encoding if not isinstance(data, six.text_type): if encoding is None: charset = getattr(self, 'charset', None) if charset is None: if self.content_type.startswith('text/html'): mo = charset_re.search(self.content_type) if mo: charset = mo.group(1).lower() if charset is None: charset = charsetFromMetaEquiv(data) elif self.content_type.startswith('text/xml'): charset = encodingFromXMLPreamble(data) else: raise ValueError('Unsupported content_type: %s' % self.content_type) if charset is not None: preferred.insert(0, charset) else: preferred.insert(0, encoding) for enc in preferred: try: data = six.text_type(data, enc) if isinstance(data, six.text_type): break except UnicodeDecodeError: continue else: data = six.text_type(data) self.write(data)