Browse Source

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

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

22
distribusi/cli.py

@ -2,7 +2,6 @@ import argparse
from distribusi.distribusi import distribusify from distribusi.distribusi import distribusify
def build_argparser(): def build_argparser():
parser = argparse.ArgumentParser(""" parser = argparse.ArgumentParser("""
distbusi is a content management system for the web that produces static 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" help="Select which directory to distribute"
) )
parser.add_argument(
'-s',
'--style',
help="Select a CSS style to include"
)
parser.add_argument( parser.add_argument(
'-v', '-v',
'--verbose', '--verbose',
@ -40,12 +45,27 @@ def build_argparser():
action="store_true" 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 return parser
def cli_entrypoint(): def cli_entrypoint():
parser = build_argparser() parser = build_argparser()
args = parser.parse_args() args = parser.parse_args()
directory = '.'
if args.directory: if args.directory:
if args.verbose: if args.verbose:

57
distribusi/distribusi.py

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

5
distribusi/page_template.py

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

Loading…
Cancel
Save