<s> from django import template <EOL> from subprocess import Popen , PIPE <EOL> from bs4 import BeautifulSoup <EOL> try : <EOL> unicode_type = unicode <EOL> except NameError : <EOL> unicode_type = str <EOL> register = template . Library ( ) <EOL> @ register . filter <EOL> def html2text ( value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> cmd = "<STR_LIT>" <EOL> proc = Popen ( cmd , shell = True , stdin = PIPE , stdout = PIPE , universal_newlines = True ) <EOL> return proc . communicate ( str ( value ) ) [ <NUM_LIT:0> ] <EOL> except OSError : <EOL> return value <EOL> @ register . filter <EOL> def extract_urllinks ( value , template = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> html = BeautifulSoup ( value ) <EOL> for link in html . findAll ( '<STR_LIT:a>' ) : <EOL> text = '<STR_LIT>' . join ( map ( unicode_type , link . contents ) ) . strip ( ) <EOL> if link . get ( '<STR_LIT>' ) and link . get ( '<STR_LIT>' ) != text : <EOL> result = template % { <EOL> '<STR_LIT:text>' : text , <EOL> '<STR_LIT:url>' : link [ '<STR_LIT>' ] . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) , <EOL> } <EOL> elif text : <EOL> result = text <EOL> link . replaceWith ( result ) <EOL> return html </s>
