def add_plain_converter(self): """Adds a plain text converter.""" def plain_converter(file): text = file.data.decode("utf-8") return "<pre>{0}</pre>".format(text) def is_plain(file): return file.ext == "txt" or not file.ext File.add_html_converter(plain_converter, is_plain)
def add_markdown_converter(self): """Adds a markdown converter if :mod:`markdown` exists.""" try: from markdown import Markdown def markdown_converter(file): text = file.data.decode("utf-8") return Markdown().convert(text) for ext in ["markdown", "md"]: File.add_html_converter_by_ext(markdown_converter, ext) except ImportError: pass
def add_html_converter(self): """Adds a HTML converter.""" skip = lambda f: f.data.encode("utf-8") for ext in ["html", "htm", "xhtml"]: File.add_html_converter_by_ext(skip, ext)
('vcard.png', 'vcf'), ('page_white.png', Default) ] by_filename = [ ('page_white_gear.png', ['Makefile', 'Rakefile']) ] by_mimetype = [ ('page_white_text.png', 'text/*'), ('picture.png', 'image/*'), ('music.png', 'audio/*'), ('film.png', 'video/*') ] def to_list(val): if not getattr(val, '__iter__', False): return [val] else: return val for icon, exts in by_extension: for ext in to_list(exts): File.add_icon_rule_by_ext(icon, ext) for icon, filenames in by_filename: for name in to_list(filenames): File.add_icon_rule_by_name(icon, name) for icon, mimetypes in by_mimetype: for mimetype in to_list(mimetypes): File.add_icon_rule_by_mimetype(icon, mimetype)