adding support for expanding plain text, html, code and pdf

This commit is contained in:
RRA 2017-03-26 18:56:41 +02:00
parent 16a0577026
commit 0f8b7e7187
2 changed files with 17 additions and 5 deletions

View File

@ -23,9 +23,12 @@ else:
mime_type = magic.Magic(mime=True)
file_types = {'image':'<img class="image" src="{}"></img>', 'pdf':'<a href="{}" class="pdf">{}</a>',
file_types = {'image':'<img class="image" src="{}"></img>', 'pdf':'<object data="{}" class="pdf" type="application/pdf"><embed src="{}" type="application/pdf" /></object>',
'text':'<a href="{}" class="text">{}</a>', 'video':'<video class="video" controls><source src="{}"></source></video>', 'audio':'<audio controls class="audio"> <source src="{}"></source></audio>','html':'<a href="{}">{}</a>'}
code_types = ['x-c', 'html']
def dict_by_value(dictionary, value):
return(list(dictionary.keys())[list(dictionary.values()).index(value)]) # returns the key for the given value
def div(mime, tag, *values):
@ -33,6 +36,9 @@ def div(mime, tag, *values):
class_name = values[0].split('.')[0].replace(' ', '_')
if 'image' in mime:
html ='<div class="{}">'.format(class_name)+tag+'</br><span class="filename">{}</span></div>'.format(values[0])
elif 'pdf' in format:
html ='<div class="{}">'.format(class_name)+tag+'</br><class="filename">{}</span></div>'.format(values[0])
else:
html = '<div class="{}">'.format(class_name)+tag+'</div>'.format(values[0])
return html
@ -52,9 +58,14 @@ for root, dirs, files in os.walk(directory):
if args.verbose:
print(mime, format)
if mime in file_types:
if name.endswith('.html'):
a = open(full_path).read()
if mime in file_types: #expansion for different kind of textfiles
if mime == 'text':
if name.endswith('.html') or name.endswith('.txt'): #what types of text files to expand
a = open(full_path).read()
elif format in code_types: #if the plain text is code, which types do we wrap in pre-tags?
a = "<pre>"+open(full_path).read()+"</pre>"
else:
a = file_types[mime]
else:
a = file_types[mime]

View File

@ -8,7 +8,8 @@ html_head = """
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style>
.image{max-width: 100%;}
div{width: 640px;float:left;}
.pdf {width:100%;}
div{width: 640px;float:left;padding:1em;}
video {width:640px;}
.dir::before{content:"📁";font-size:18px;}
</style>