from __future__ import absolute_import

import re

from bs4 import BeautifulSoup

from six import StringIO


def _visible(elem):
    if elem.parent.name in ['style', 'script', '[document]', 'head']:
        return False

    elif re.match('<!--.*-->', str(elem)):
        return False

    return True


def _get_file(f, **kwargs):
    encoding, errors = kwargs['encoding'], kwargs['encoding_errors']
    data = f.read()
    data = data.decode(encoding, errors)
    text, bs = StringIO(), BeautifulSoup(data, 'lxml')

    for elem in filter(_visible, bs.findAll(text=True)):
        text.write(elem)
        text.write(u' ')

    return text.getvalue()

@@ -100,22 +100,45 @@
 tringIO%0A
+from six import PY3%0A
 %0A%0Adef 
+_is
 _visible
@@ -142,16 +142,34 @@
 ble(elem
+, encoding, errors
 ):%0A    i
@@ -259,18 +259,77 @@
 se%0A%0A    
-el
+if not PY3:%0A        elem = elem.encode(encoding, errors)%0A    
 if re.ma
@@ -349,18 +349,13 @@
 %3E', 
-str(
 elem)
-)
 :%0A  
@@ -625,25 +625,8 @@
 m in
- filter(_visible,
  bs.
@@ -643,19 +643,70 @@
 xt=True)
-)
 :%0A
+        if _is_visible(elem, encoding, errors):%0A    
         
@@ -722,16 +722,20 @@
 e(elem)%0A
+    
         

fix html backend on py 2