Re: [viewvc-dev] Re: [viewvc-users] Don't display binary files

JJ <[email protected]> Wed, 19 Nov 2008 15:36:56 -0600
Newsgroups gmane.comp.version-control.cvs.viewcvs.devel
Message-ID <[email protected]>
>
> JJ wrote:
> > What do you of this idea?
> >
> > Add a new file sitting at the same level as viewvc.conf (perhaps called
> > text-mime.types).  It contains a list of mime types that contain text,
> > and thus that should have prefer_markup set to true.  We can try to
> > populate it with a list of text mime types, but since it comes with
> > ViewVC anyone can add missing or custom mime types to it as needed.
> >
> > Add a new variable to viewvc.conf called text_mime_types_file.  By
> > default it will be commented out, but if uncommented, the is_text
> > function in viewvc.py will use the mime types listed in this file to
> > determine if it is text, instead of just checking that the mime type
> > begins with text/.
> >
> > This approach is less expensive than opening files and checking their
> > contents to determine if they are binary.  It may require tweaking of
> > the text file as new types are discovered, but that should be pretty
> > straight forward and people don't have to use the functionality if they
> > don't want.
> >
> > What do you think?
>
> I like this, but I wouldn't add a new config file.  I'd just add a new
> configuration option that follows the format of our existing multi-value
> options.  And I'd let the values take wildcards (text/*, application/*-xml,
> etc.), too, so you wouldn't have to specify every single little thing.
>
>

Patch attached.  I also need to clean up the default templates to respect
prefer_markup values.

JJ

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
patch.txt (text/plain, 2.5 KB)
Index: viewvc.conf.dist
===================================================================
--- viewvc.conf.dist	(revision 2056)
+++ viewvc.conf.dist	(working copy)
@@ -339,6 +339,9 @@
 # by browsers).
 svn_ignore_mimetype = 0
 
+# List of text file mime types.  These files will have prefer_markup set.
+text_mime_types = text/*, */xml, application/*-xml
+
 # svn_config_dir: Path of the Subversion runtime configuration
 # directory ViewVC should consult for various things, including cached
 # remote authentication credentials.  If unset, Subversion will use
Index: lib/viewvc.py
===================================================================
--- lib/viewvc.py	(revision 2056)
+++ lib/viewvc.py	(working copy)
@@ -969,9 +969,18 @@
 def is_viewable_image(mime_type):
   return mime_type and mime_type in ('image/gif', 'image/jpeg', 'image/png')
 
-def is_text(mime_type):
-  return not mime_type or mime_type[:5] == 'text/'
+def is_text(mime_type, cfg):
+  if mime_type:
+    for m in cfg.options.text_mime_types:
+      # Make regex valid
+      mime_type_re = re.sub('\*', '.*', m)
 
+      # If mimetype matches...
+      if re.match(mime_type_re, mime_type):
+        # then it's a text file!
+        return True
+  return False
+
 def is_cvsroot_path(roottype, path_parts):
   return roottype == 'cvs' and path_parts and path_parts[0] == 'CVSROOT'
 
@@ -988,7 +997,7 @@
   # we encounter an unrecognized file extension) we also view it through
   # the markup page since that's better than sending it text/plain.
   if ('markup' in cfg.options.allowed_views and 
-      (is_viewable_image(mime_type) or is_text(mime_type))):
+      (is_viewable_image(mime_type) or is_text(mime_type, cfg))):
     return view_markup
   return view_checkout
 
Index: lib/config.py
===================================================================
--- lib/config.py	(revision 2056)
+++ lib/config.py	(working copy)
@@ -40,7 +40,7 @@
 class Config:
   _sections = ('general', 'utilities', 'options', 'cvsdb', 'templates')
   _force_multi_value = ('cvs_roots', 'svn_roots', 'languages', 'kv_files',
-                        'root_parents', 'allowed_views')
+                        'root_parents', 'allowed_views', 'text_mime_types')
 
   def __init__(self):
     for section in self._sections:
@@ -221,6 +221,7 @@
     self.options.generate_etags = 1
     self.options.svn_ignore_mimetype = 0
     self.options.svn_config_dir = None
+    self.options.text_mime_types = ['text/*']
     self.options.use_rcsparse = 0
     self.options.sort_by = 'file'
     self.options.sort_group_dirs = 1