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

JJ <[email protected]> Wed, 19 Nov 2008 16:17:24 -0600
Newsgroups gmane.comp.version-control.cvs.viewcvs.devel
Message-ID <eed30ed90811191417v6711c6dfx22ca70df2d56beda__17043.158124625$1227133128$gmane$org@mail.gmail.com>
>
>      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.
>>
>>
>
> Here's another patch (including the last patch in it) that adds a config
> option prefer_markup_no_mime_type.  You can rename the variable, but this
> allows someone to decide whether files should be marked up if no mime type
> can be found.  For example, Java .properties files won't have a mime type,
> but we still want them marked up.
>
>

Sorry, my last patch sometimes used prefer_markup_no_mimetype and sometimes
prefer_markup_no_mime_type.  I changed it to prefer_markup_no_mime_type.
Again, feel free to choose a friendlier variable name.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
patch.txt (text/plain, 2.7 KB)
Index: viewvc.conf.dist
===================================================================
--- viewvc.conf.dist	(revision 2056)
+++ viewvc.conf.dist	(working copy)
@@ -339,6 +339,12 @@
 # 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
+
+# Should prefer_markup be true if no mime type can be found for the file?
+prefer_markup_no_mime_type = 1
+
 # 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,20 @@
 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
+  else:
+    return cfg.options.prefer_markup_no_mime_type
+  return False
+
 def is_cvsroot_path(roottype, path_parts):
   return roottype == 'cvs' and path_parts and path_parts[0] == 'CVSROOT'
 
@@ -988,7 +999,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,8 @@
     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.prefer_markup_no_mime_type = 1
     self.options.use_rcsparse = 0
     self.options.sort_by = 'file'
     self.options.sort_group_dirs = 1