Browse Source

added arguments for including exif metadata, including external css, not including filenames of pictures

pull/3/head
lowrussia 5 years ago
parent
commit
7687aa96a2
  1. 22
      distribusi/cli.py
  2. 57
      distribusi/distribusi.py
  3. 7
      distribusi/page_template.py

22
distribusi/cli.py

@ -2,7 +2,6 @@ import argparse
from distribusi.distribusi import distribusify
def build_argparser():
parser = argparse.ArgumentParser("""
distbusi is a content management system for the web that produces static
@ -19,6 +18,12 @@ def build_argparser():
help="Select which directory to distribute"
)
parser.add_argument(
'-s',
'--style',
help="Select a CSS style to include"
)
parser.add_argument(
'-v',
'--verbose',
@ -40,12 +45,27 @@ def build_argparser():
action="store_true"
)
parser.add_argument(
'-nf',
'--no-filenames',
help="Don't use the template to ouput html",
action="store_true"
)
parser.add_argument(
'-c',
'--captions',
help="Print captions stored in exif metadata",
action="store_true"
)
return parser
def cli_entrypoint():
parser = build_argparser()
args = parser.parse_args()
directory = '.'
if args.directory:
if args.verbose:

57
distribusi/distribusi.py

@ -5,7 +5,7 @@ from io import BytesIO
import magic
from distribusi.page_template import html_footer, html_head
from PIL import Image
import subprocess
CODE_TYPES = [
'x-c',
@ -13,7 +13,7 @@ CODE_TYPES = [
]
FILE_TYPES = {
'image': '<img class="image" src="{}">',
'image': '<figure><img class="image" src="{}">{}</figure>',
'pdf': (
'<object data="{}" class="pdf" type="application/pdf">'
'<embed src="{}" type="application/pdf" /></object>'
@ -32,8 +32,16 @@ FILE_TYPES = {
MIME_TYPE = magic.Magic(mime=True)
def caption(image):
process = subprocess.Popen(['exiftool', '-Comment', image], stdout=subprocess.PIPE)
out, err = process.communicate()
try:
caption = out.decode("utf-8").split(": ", 1)[1]
except:
caption = ''
return caption
def thumbnail(image, name):
def thumbnail(image, name, args):
size = (450, 450)
im = Image.open(image)
im.thumbnail(size)
@ -41,19 +49,25 @@ def thumbnail(image, name):
im.save(output, format='JPEG')
im_data = output.getvalue()
data_url = base64.b64encode(im_data).decode()
cap = caption(image)
if cap and args.captions:
cap = "<figcaption>{}</figcaption>".format(cap)
else:
cap = ''
return (
"<a href='{}'><img class='thumbnail' "
"src='data:image/jpg;base64,{}'></a>"
).format(name, data_url)
"<figure><a href='{}'><img class='thumbnail' src='data:image/jpg;base64,{}'></a>{}</figure>"
).format(name, data_url, cap)
def div(mime, tag, *values):
def div(args, mime, tag, *values):
id_name = values[0].split('.')[0].replace(' ', '_')
if not args.no_filenames:
filename = '<br><span class="filename">{}</span>'
else:
filename = ''
if 'image' in mime:
html = '<div id="{}">{}<br><span class="filename">{}</span></div>'
html = '<div id="{}">{}' + filename + '</div>'
elif 'pdf' in mime:
html = '<div id="{}">{}<br><class="filename">{}</span></div>'
html = '<div id="{}">{}' + filename + '</div>'
else:
html = '<div id="{}">{}</div>'
@ -92,9 +106,14 @@ def distribusify(args, directory): # noqa
a = FILE_TYPES[mime]
if mime == 'image' and args.thumbnail:
a = thumbnail(full_path, name)
a = thumbnail(full_path, name, args)
else:
a = FILE_TYPES[mime]
cap = caption(full_path)
if cap and args.captions:
cap = "<figcaption>{}</figcaption>".format(cap)
else:
cap = ''
a = FILE_TYPES[mime].format(full_path, cap)
if format in FILE_TYPES:
a = FILE_TYPES[format]
@ -107,18 +126,24 @@ def distribusify(args, directory): # noqa
print(message, mime, format, name)
a = a.replace('{}', name)
html.append(div(mime, a, name))
html.append(div(args, mime, a, name))
if root != directory:
html.append('<a href="../">../</a>')
for name in dirs:
a = "<a href='{}' class='dir'>{}/</a>".replace('{}', name)
html.append(div('dir', a, 'folder'))
html.append(div(args, 'dir', a, 'folder'))
with open(os.path.join(root, 'index.html'), 'w') as f:
if not args.no_template:
f.write(html_head)
if args.style:
fs = open(os.path.join(root, args.style), "r")
style = fs.read()
styled_html_head = html_head % style
else:
styled_html_head = html_head % ''
f.write(styled_html_head)
for line in html:
f.write(line+'\n')

7
distribusi/page_template.py

@ -5,12 +5,13 @@ html_head = """
<!-- Generated with distribusi https://git.vvvvvvaria.org/rra/distribusi -->
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style>
.image{max-width: 100%;}
.pdf {width:100%;}
.image{max-width: 100%%;}
.pdf {width:100%%;}
div{width: 640px;float:left;padding:1em;}
video {width:640px;}
.dir::before{content:"📁";font-size:18px;}
</style>
%s
</style>
</head>
<body>
"""

Loading…
Cancel
Save