changeset 620:cb7deb83447c

introduce mimetype arg for show action, keep format action as compat. stub
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Fri, 05 May 2006 19:37:19 +0200
parents 0bb6b767baec
children d1e84cbab594
files MoinMoin/Page.py MoinMoin/action/links.py MoinMoin/theme/__init__.py MoinMoin/wikiaction.py
diffstat 4 files changed, 70 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/Page.py	Fri May 05 17:50:15 2006 +0200
+++ b/MoinMoin/Page.py	Fri May 05 19:37:19 2006 +0200
@@ -20,7 +20,7 @@
     # Header regular expression, used to get header boundaries
     header_re = r'(^#+.*(?:\n\s*)+)+'
 
-    def __init__(self, request, page_name, **keywords):
+    def __init__(self, request, page_name, **kw):
         """
         Create page object.
 
@@ -30,16 +30,16 @@
 
         @param page_name: WikiName of the page
         @keyword rev: number of older revision
-        @keyword formatter: formatter instance
+        @keyword formatter: formatter instance or mimetype str,
+                            None or no kw arg will use default formatter
         @keyword include_self: if 1, include current user (default: 0)
         """
-        self.rev = keywords.get('rev', 0) # revision of this page
-        self.is_rootpage = keywords.get('is_rootpage', 0) # is this __init__ of rootpage?
-        self.include_self = keywords.get('include_self', 0)
         self.request = request
         self.cfg = request.cfg
-
         self.page_name = page_name
+        self.rev = kw.get('rev', 0) # revision of this page
+        self.is_rootpage = kw.get('is_rootpage', 0) # is this __init__ of rootpage?
+        self.include_self = kw.get('include_self', 0)
 
         # XXX uncomment to see how many pages we create....
         #import sys, traceback
@@ -47,12 +47,21 @@
         #traceback.print_stack(limit=4, file=sys.stderr)
 
 
-        if keywords.has_key('formatter'):
-            self.formatter = keywords.get('formatter')
+        formatter = kw.get('formatter', None)
+        if isinstance(formatter, (str, unicode)): # mimetype given
+            mimetype = str(formatter)
+            self.output_mimetype = mimetype
+            self.default_formatter = mimetype == "text/html"
+        elif formatter is not None: # formatter instance given
+            self.formatter = formatter
             self.default_formatter = 0
+            self.output_mimetype = "text/todo" # TODO where do we get this value from?
         else:
             self.default_formatter = 1
+            self.output_mimetype = "text/html"
 
+        self.output_charset = config.charset # correct for wiki pages
+        
         self._raw_body = None
         self._raw_body_modified = 0
         self.hilite_re = None
@@ -992,12 +1001,25 @@
         # load the text
         body = self.get_raw_body()
 
-        # if necessary, load the default formatter
+        # if necessary, load the formatter
         if self.default_formatter:
             from MoinMoin.formatter.text_html import Formatter
             self.formatter = Formatter(request, store_pagelinks=1)
+        elif not self.formatter:
+            formatterName = self.output_mimetype.translate({ord('/'): '_', ord('.'): '_'}) # XXX use existing fn for that
+            try:
+                Formatter = wikiutil.importPlugin(request.cfg, "formatter", formatterName, "Formatter")
+                self.formatter = Formatter(request)
+            except wikiutil.PluginMissingError:
+                from MoinMoin.formatter.text_html import Formatter
+                self.formatter = Formatter(request, store_pagelinks=1)
+                self.output_mimetype = "text/html"
+            
+        request.http_headers(["Content-Type: %s; charset=%s" % (self.output_mimetype, self.output_charset)])
+
         self.formatter.setPage(self)
-        if self.hilite_re: self.formatter.set_highlight_re(self.hilite_re)
+        if self.hilite_re:
+            self.formatter.set_highlight_re(self.hilite_re)
         request.formatter = self.formatter
 
         # default is wiki markup
@@ -1173,7 +1195,6 @@
                         _('This page redirects to page "%(page)s"') % {'page': wikiutil.escape(pi_redirect)},
                         msg)
 
-
                 # Page trail
                 trail = None
                 if not print_mode:
@@ -1259,7 +1280,8 @@
                 cache.update('\n'.join(links) + '\n', True)
 
         request.clock.stop('send_page')
-        request.theme.send_closing_html()
+        if not content_only and self.default_formatter:
+            request.theme.send_closing_html()
 
     def getFormatterName(self):
         """ Return a formatter name as used in the caching system
--- a/MoinMoin/action/links.py	Fri May 05 17:50:15 2006 +0200
+++ b/MoinMoin/action/links.py	Fri May 05 19:37:19 2006 +0200
@@ -19,7 +19,7 @@
     else:
         mimetype = "text/html"
 
-    request.http_headers(["Content-Type: %s; charset=%s" % (mimetype,config.charset)])
+    request.http_headers(["Content-Type: %s; charset=%s" % (mimetype, config.charset)])
 
     if mimetype == "text/html":
         request.theme.send_title(_('Full Link List for "%s"') % request.cfg.sitename)
--- a/MoinMoin/theme/__init__.py	Fri May 05 17:50:15 2006 +0200
+++ b/MoinMoin/theme/__init__.py	Fri May 05 19:37:19 2006 +0200
@@ -1430,7 +1430,7 @@
 
         # include charset information - needed for moin_dump or any other case
         # when reading the html without a web server
-        user_head.append('''<meta http-equiv="Content-Type" content="text/html;charset=%s">\n''' % config.charset)
+        user_head.append('''<meta http-equiv="Content-Type" content="%s;charset=%s">\n''' % (page.output_mimetype, page.output_charset))
 
         meta_keywords = request.getPragma('keywords')
         meta_desc = request.getPragma('description')
--- a/MoinMoin/wikiaction.py	Fri May 05 17:50:15 2006 +0200
+++ b/MoinMoin/wikiaction.py	Fri May 05 19:37:19 2006 +0200
@@ -51,28 +51,42 @@
 
     page.send_raw()
 
-def do_format(pagename, request):
-    """ send a page using a specific formatter given by mimetype form key """
-    # get the MIME type
-    if request.form.has_key('mimetype'):
-        mimetype = request.form['mimetype'][0]
-    else:
-        mimetype = u"text/plain"
+def do_show(pagename, request, count_hit=1, cacheable=1):
+    """ show a page, either current revision or the revision given by rev form value.
+        if count_hit is non-zero, we count the request for statistics.
+    """
+    # We must check if the current page has different ACLs.
+    if not request.user.may.read(pagename):
+        Page(request, pagename).send_page(request)
+        return
 
-    # try to load the formatter
-    formatterName = mimetype.translate({ord(u'/'): u'_', ord(u'.'): u'_'})
-    try:
-        Formatter = wikiutil.importPlugin(request.cfg, "formatter", formatterName, "Formatter")
-    except wikiutil.PluginMissingError:
-        # default to plain text formatter
-        mimetype = "text/plain"
-        from MoinMoin.formatter.text_plain import Formatter
+    mimetype = request.form.get('mimetype', [u"text/html"])[0]
+        
+    if request.form.has_key('rev'):
+        try:
+            rev = request.form['rev'][0]
+            try:
+                rev = int(rev)
+            except StandardError:
+                rev = 0
+        except KeyError:
+            rev = 0
+        Page(request, pagename, rev=rev, formatter=mimetype).send_page(request, count_hit=count_hit)
+    else:
+        request.cacheable = cacheable
+        Page(request, pagename, formatter=mimetype).send_page(request, count_hit=count_hit)
 
-    if "xml" in mimetype:
-        mimetype = "text/xml"
-
-    request.http_headers(["Content-Type: %s; charset=%s" % (mimetype, config.charset)])
-    Page(request, pagename, formatter=Formatter(request)).send_page(request)
+def do_format(pagename, request):
+    """ send a page using a specific formatter given by mimetype form key.
+        Since 5.5.2006 this functionality is also done by do_show, but do_format
+        has a default of text/plain when no format is given.
+        It also does not count in statistics and also does not set the cacheable flag.
+        TODO: remove this action when we don't need it any more for compatibility.
+    """
+    # get the MIME type
+    if not request.form.has_key('mimetype'):
+        request.form['mimetype'] = [u"text/plain"]
+    do_show(pagename, request, count_hit=0, cacheable=0)
 
 def do_content(pagename, request):
     """ same as do_show, but we only show the content """
@@ -89,29 +103,6 @@
     """ same as do_show, but never caches and never counts hits """
     do_show(pagename, request, count_hit=0, cacheable=0)
 
-def do_show(pagename, request, count_hit=1, cacheable=1):
-    """ show a page, either current revision or the revision given by rev form value.
-        if count_hit is non-zero, we count the request for statistics.
-    """
-    # We must check if the current page has different ACLs.
-    if not request.user.may.read(pagename):
-        Page(request, pagename).send_page(request)
-        return
-
-    if request.form.has_key('rev'):
-        try:
-            rev = request.form['rev'][0]
-            try:
-                rev = int(rev)
-            except StandardError:
-                rev = 0
-        except KeyError:
-            rev = 0
-        Page(request, pagename, rev=rev).send_page(request, count_hit=count_hit)
-    else:
-        request.cacheable = cacheable
-        Page(request, pagename).send_page(request, count_hit=count_hit)
-
 def do_refresh(pagename, request):
     """ Handle refresh action """
     # Without arguments, refresh action will refresh the page text_html cache.