Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Z
Zope
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
Zope
Commits
d804ff41
Commit
d804ff41
authored
Dec 16, 2005
by
Andreas Jung
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored content type guessing
parent
00eb6822
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
7 deletions
+15
-7
lib/python/Products/PageTemplates/ZopePageTemplate.py
lib/python/Products/PageTemplates/ZopePageTemplate.py
+15
-7
No files found.
lib/python/Products/PageTemplates/ZopePageTemplate.py
View file @
d804ff41
...
...
@@ -36,6 +36,7 @@ from AccessControl.Permissions import view, ftp_access, change_page_templates, v
from
webdav.Lockable
import
ResourceLockedError
from
webdav.WriteLockInterface
import
WriteLockInterface
from
zope.pagetemplate.pagetemplate
import
PageTemplate
from
zope.pagetemplate.pagetemplatefile
import
sniff_type
# regular expression to extract the encoding from the XML preamble
encoding_reg
=
re
.
compile
(
'<
\
?xml.*?e
n
coding="(.*?)".*?
\
?>
'
, re.M)
...
...
@@ -69,13 +70,22 @@ class Src(Acquisition.Explicit):
def sniffEncoding(text, default_encoding='
utf
-
8
'):
""" try to determine the encoding from html or xml """
# XXX: look at pagetemplates.py (BOM!!!)
if text.startswith('
<
?
xml
'):
mo = encoding_reg.search(text)
if mo:
return mo.group(1)
return default_encoding
def guess_type(filename, text):
content_type, dummy = guess_content_type(filename, text)
if content_type in ('
text
/
html
', '
text
/
xml
'):
return content_type
return sniff_type(text) or '
text
/
html
'
_default_content_fn = os.path.join(package_home(globals()), '
pt
', '
default
.
html
')
...
...
@@ -120,10 +130,9 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable,
security.declareProtected(change_page_templates, '
pt_encoding
')
def pt_encoding(self):
encoding = sniffEncoding(self.read())
print encoding
return encoding
# Use the encoding of the document as encoding for the ZMI to
# avoid any kind of encoding troubles
from ComputedAttribute import ComputedAttribute
management_page_charset = ComputedAttribute(pt_encoding, 1)
...
...
@@ -185,7 +194,7 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable,
filename = file.filename
text = file.read()
content_type
, dummy = guess_content
_type(filename, text)
content_type
= guess
_type(filename, text)
if not content_type in ('
text
/
html
', '
text
/
xml
'):
raise ValueError('
Unsupported
mimetype
:
%
s
' % content_type)
...
...
@@ -413,11 +422,10 @@ def manage_addPageTemplate(self, id, title='', text=None, encoding='utf-8', subm
if headers and headers.has_key('content_type'):
content_type = headers['content_type']
else:
content_type
, dummy = guess_content
_type(filename, text)
content_type
= guess
_type(filename, text)
encoding = sniffEncoding(text)
else:
if hasattr(text, 'read'):
filename = getattr(text, 'filename', '')
headers = getattr(text, 'headers', None)
...
...
@@ -425,7 +433,7 @@ def manage_addPageTemplate(self, id, title='', text=None, encoding='utf-8', subm
if headers and headers.has_key('content_type'):
content_type = headers['content_type']
else:
content_type
, dummy = guess_content
_type(filename, text)
content_type
= guess
_type(filename, text)
if not text:
text = open(_default_content_fn).read()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment