def inline_css(html_message): """ Inlines all CSS in an HTML string Given an HTML document with CSS declared in the HEAD, inlines it into the applicable elements. Used primarily in the preparation of styled emails. Arguments: html_message -- a string of HTML, including CSS """ document = etree.HTML(html_message) converter = Conversion() converter.perform(document, html_message, '') return converter.convertedHTML
def convert(request): urllib._urlopener = MyURLopener() sourceHTML='' sourceURL='' if request.POST.has_key('source'): sourceHTML=request.POST['source'] if request.POST.has_key('source_url'): sourceURL=request.POST['source_url'].strip() if request.POST.has_key('returnraw'): outputTemplate='raw.html' else: outputTemplate='index.html' if request.method == 'POST': # form submitted #parse HTML try: if len(sourceURL): urlregexpt=re.compile('((https?):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)') #check for valid URL if(not urlregexpt.match(sourceURL)): return render_to_response(outputTemplate,{'sourceHTML':sourceHTML,'sourceURL':sourceURL,'error_message':'The source URL does not appear to be valid.'}) #if valid URL attempt to download try: f=urllib.urlopen(sourceURL) document = etree.HTML(f.read()) except: return render_to_response(outputTemplate,{'sourceHTML':sourceHTML,'sourceURL':sourceURL,'error_message':'The source URL could not be accessed.'}) else: try: document = etree.HTML(sourceHTML) except: return render_to_response(outputTemplate,{'sourceHTML':sourceHTML,'sourceURL':sourceURL,'error_message':'The source HTML does not appear to be valid.'}) except KeyError: return HttpResponseRedirect("/") # do conversion try: converter=Conversion() converter.perform(document,sourceHTML,sourceURL) except IOError: return render_to_response(outputTemplate,{'sourceHTML':sourceHTML, 'sourceURL':sourceURL, 'error_message': str(sys.exc_info()[1])}) return render_to_response(outputTemplate,{'sourceHTML':sourceHTML,'sourceURL':sourceURL,'convertedHTML':converter.convertedHTML, 'warnings':converter.CSSErrors, 'support':converter.CSSUnsupportErrors,'supportPercentage':converter.supportPercentage}) else: return HttpResponseRedirect("/")
def main(): (options, args) = parser.parse_args() if options.filename is None or options.css is None: raise Exception("Missing arguments filename and css") htmlFile = open(options.filename) htmlString = htmlFile.read() cssFile = open(options.css) css = cssFile.read() document = html.fromstring(htmlString) converter = Conversion() converter.perform(document, document, css) if options.output is not None: f = open(options.output, "w") f.write(converter.convertedHTML) f.close() else: print converter.convertedHTML
def convert(request): urllib._urlopener = MyURLopener() sourceHTML = '' sourceURL = '' if request.POST.has_key('source'): sourceHTML = request.POST['source'] if request.POST.has_key('source_url'): sourceURL = request.POST['source_url'].strip() if request.POST.has_key('returnraw'): outputTemplate = 'raw.html' else: outputTemplate = 'index.html' if request.method == 'POST': # form submitted #parse HTML try: if len(sourceURL): urlregexpt = re.compile( '((https?):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)') #check for valid URL if (not urlregexpt.match(sourceURL)): return render_to_response( outputTemplate, { 'sourceHTML': sourceHTML, 'sourceURL': sourceURL, 'error_message': 'The source URL does not appear to be valid.' }) #if valid URL attempt to download try: f = urllib.urlopen(sourceURL) document = etree.HTML(f.read()) except: return render_to_response( outputTemplate, { 'sourceHTML': sourceHTML, 'sourceURL': sourceURL, 'error_message': 'The source URL could not be accessed.' }) else: try: document = etree.HTML(sourceHTML) except: return render_to_response( outputTemplate, { 'sourceHTML': sourceHTML, 'sourceURL': sourceURL, 'error_message': 'The source HTML does not appear to be valid.' }) except KeyError: return HttpResponseRedirect("/") # do conversion try: converter = Conversion() converter.perform(document, sourceHTML, sourceURL) except IOError: return render_to_response( outputTemplate, { 'sourceHTML': sourceHTML, 'sourceURL': sourceURL, 'error_message': str(sys.exc_info()[1]) }) return render_to_response( outputTemplate, { 'sourceHTML': sourceHTML, 'sourceURL': sourceURL, 'convertedHTML': converter.convertedHTML, 'warnings': converter.CSSErrors, 'support': converter.CSSUnsupportErrors, 'supportPercentage': converter.supportPercentage }) else: return HttpResponseRedirect("/")