Adapt code to work with the latest changes on master.

This commit is contained in:
Simon Conseil
2014-07-17 00:32:42 +02:00
parent b8733c68ec
commit 71afbf5355
10 changed files with 73 additions and 97 deletions

View File

@@ -1,20 +1,16 @@
===================
Image information
Image information
===================
Additional information on an image can be given in a file using the `markdown`_ syntax,
named ``<imagename>.md`` (example: IMG_5206.jpg.md):
::
Additional information on an image can be given in a file using the `markdown`_
syntax, named ``<imagename>.md`` (example: ``IMG_5206.md``)::
Title: My awesome photo
And a description with *Markdown* syntax.
EXIF data is directly extracted
Some meta-data keys are used by Sigal to get the useful informations on the
gallery:
EXIF data is directly extracted, see :ref:`simple-exif-data`. Some meta-data
keys are used by Sigal to get the useful informations on the gallery:
- *Title*: the image title.
@@ -26,8 +22,8 @@ can be used in the template with:
.. code-block:: jinja
{% if media.desc.meta.location %}
<p>Location: {{ media.desc.meta.location[0] }}</>
{% if media.meta.location %}
<p>Location: {{ media.meta.location[0] }}</>
{% endif %}
.. _markdown: http://daringfireball.net/projects/markdown/

View File

@@ -23,10 +23,8 @@
from __future__ import absolute_import, print_function
import codecs
import fnmatch
import logging
import markdown
import multiprocessing
import os
import sys
@@ -42,7 +40,7 @@ from .compat import UnicodeMixin, strxfrm, url_quote
from .image import process_image, get_exif_tags
from .log import colored, BLUE
from .settings import get_thumb
from .utils import copy, check_or_create_dir, url_from_path
from .utils import copy, check_or_create_dir, url_from_path, read_markdown
from .video import process_video
from .writer import Writer
@@ -80,6 +78,7 @@ class Media(UnicodeMixin):
self.raw_exif = None
self.exif = None
self.date = None
self._get_metadata()
signals.media_initialized.send(self)
def __repr__(self):
@@ -119,6 +118,18 @@ class Media(UnicodeMixin):
return
return url_from_path(self.thumb_name)
def _get_metadata(self):
""" Get image metadata from filename.md: title, description, meta."""
self.description = ''
self.meta = {}
self.title = ''
descfile = splitext(self.src_path)[0] + '.md'
if isfile(descfile):
meta = read_markdown(descfile)
for key, val in meta.items():
setattr(self, key, val)
class Image(Media):
"""Gather all informations on an image file."""
@@ -246,24 +257,16 @@ class Album(UnicodeMixin):
"""
descfile = join(self.src_path, self.description_file)
self.description = ''
self.meta = {}
# default: get title from directory name
self.title = os.path.basename(self.path if self.path != '.'
else self.src_path)
if isfile(descfile):
# Use utf-8-sig codec to remove BOM if it is present
with codecs.open(descfile, 'r', 'utf-8-sig') as f:
text = f.read()
md = markdown.Markdown(extensions=['meta'])
html = md.convert(text)
self.title = md.Meta.get('title', [''])[0]
self.description = html
self.meta = md.Meta.copy()
else:
self.description = ''
self.meta = {}
# default: get title from directory name
self.title = os.path.basename(self.path if self.path != '.'
else self.src_path)
meta = read_markdown(descfile)
for key, val in meta.items():
setattr(self, key, val)
def create_output_directories(self):
"""Create output directories for thumbnails and original images."""

View File

@@ -33,8 +33,6 @@ import logging
import os
import pilkit.processors
import sys
import codecs
import markdown
from copy import deepcopy
from datetime import datetime
@@ -43,7 +41,6 @@ from PIL import Image as PILImage
from PIL import ImageOps
from pilkit.processors import Transpose
from pilkit.utils import save_image
from os.path import join
from . import compat, signals
from .settings import get_thumb
@@ -253,39 +250,3 @@ def get_exif_tags(source):
}
return (data, simple)
def get_image_metadata(source, img):
"""
Get image metadata from filename.md:
- title
- description
return for usage in templates
"""
logger = logging.getLogger(__name__)
descfile = join(source, img + ".md")
if not os.path.isfile(descfile):
# set some defaults
meta = {
'title': '',
'description': '',
'meta': {}
}
logger.info(u'No markdown file named {0}'.format(descfile))
else:
with codecs.open(descfile, "r", "utf-8") as f:
text = f.read()
md = markdown.Markdown(extensions=['meta'])
html = md.convert(text)
meta = {
'title': md.Meta.get('title', [''])[0],
'description': html,
'meta': md.Meta.copy()
}
return meta

View File

@@ -90,7 +90,7 @@
{% if loop.index % nb_columns == 0 %}omega{% endif%}">
<a href="{{ media.filename }}" class="gallery" title="{{ media.filename }}" {{ img_description(media) }}>
<img src="{{ media.thumbnail }}" alt="{{ media.filename }}"
title="{{ media.filename }}" /></a>
title="{{ media.title if media.title else media.filename }}" /></a>
</div>
{% endif %}
{% if media.type == "video" %}
@@ -101,7 +101,7 @@
class="gallery" inline='yes' title="{{ media.filename }}"
{% if media.big %} data-big="{{ media.big }}"{% endif %}>
<img src="{{ media.thumbnail }}" alt="{{ media.filename }}"
title="{{ media.filename }}" /></a>
title="{{ media.title if media.title else media.filename }}" /></a>
</div>
<!-- This contains the hidden content for the video -->
<div style='display:none'>

View File

@@ -60,8 +60,7 @@
{% if album.medias %}
{% macro img_description(media) -%}
{%- if media.big %}<a href='{{ media.big }}'>Full size</a>{% endif %}
{% if media.title %}Title: {{ media.title }} </br>{% endif %}
{% if media.description %}Description: {{ media.description }}</br> {% endif %}
{% if media.description %}<br>{{ media.description }}{% endif %}
{%- if media.exif %}
<br>
@@ -79,16 +78,18 @@
{% if media.type == "image" %}
<a href="{{ media.filename }}">
<img src="{{ media.thumbnail }}" alt="{{ media.filename }}"
data-title="{{ media.filename }}"
data-title="{{ media.title if media.title else media.filename }}"
data-description="{{ img_description(media) }}"/>
</a>
{% endif %}
{% if media.type == "video" %}
<a href="{{ theme.url }}/img/empty.png">
<img src="{{ media.thumbnail }}" alt="{{ media.filename }}"
data-layer="<video controls>
<source src='{{ media.filename }}' type='video/webm' />
</video>" />
data-title="{{ media.title if media.title else media.filename }}"
data-description="{{ img_description(media) }}"
data-layer="<video controls>
<source src='{{ media.filename }}' type='video/webm' />
</video>" />
</a>
{% endif %}
{% endfor %}

View File

@@ -20,6 +20,8 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
import codecs
import markdown
import os
import shutil
@@ -46,3 +48,18 @@ def url_from_path(path):
return path
else:
return '/'.join(path.split(os.sep))
def read_markdown(filename):
# Use utf-8-sig codec to remove BOM if it is present
with codecs.open(filename, 'r', 'utf-8-sig') as f:
text = f.read()
md = markdown.Markdown(extensions=['meta'])
html = md.convert(text)
return {
'title': md.Meta.get('title', [''])[0],
'description': html,
'meta': md.Meta.copy()
}

View File

@@ -71,6 +71,8 @@ def test_media(settings):
assert m.dst_path == join(settings['destination'], file_path)
assert m.thumb_name == thumb
assert m.thumb_path == join(settings['destination'], path, thumb)
assert m.title == "Foo Bar"
assert m.description == "<p>This is a funny description of this image</p>"
assert repr(m) == "<Media>('{}')".format(file_path)
assert str(m) == file_path

View File

@@ -5,7 +5,7 @@ import pytest
from PIL import Image
from sigal import init_logging
from sigal.image import generate_image, generate_thumbnail, get_exif_tags, get_image_metadata
from sigal.image import generate_image, generate_thumbnail, get_exif_tags
from sigal.settings import create_settings
CURRENT_DIR = os.path.dirname(__file__)
@@ -90,16 +90,3 @@ def test_exif_gps(tmpdir):
assert abs(simple['gps']['lat'] - lat) < 0.0001
assert abs(simple['gps']['lon'] - lon) < 0.0001
def test_metadata(tmpdir):
"Test if metadata can be read for a given image."
test_image = '11.jpg'
src_path = os.path.join(CURRENT_DIR, 'sample', 'pictures', 'dir1', 'test1')
metadata = get_image_metadata(src_path, test_image)
assert metadata['title'] == "Foo Bar"
assert metadata['description'] == "<p>This is a funny description of this image</p>"

View File

@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import os
from sigal.utils import copy, check_or_create_dir, url_from_path
from sigal import utils
CURRENT_DIR = os.path.dirname(__file__)
SAMPLE_DIR = os.path.join(CURRENT_DIR, 'sample')
@@ -11,28 +11,37 @@ def test_copy(tmpdir):
filename = 'exo20101028-b-full.jpg'
src = os.path.join(SAMPLE_DIR, 'pictures', 'dir2', filename)
dst = str(tmpdir.join(filename))
copy(src, dst)
utils.copy(src, dst)
assert os.path.isfile(dst)
filename = 'm57_the_ring_nebula-587px.jpg'
src = os.path.join(SAMPLE_DIR, 'pictures', 'dir2', filename)
dst = str(tmpdir.join(filename))
copy(src, dst, symlink=True)
utils.copy(src, dst, symlink=True)
assert os.path.islink(dst)
assert os.readlink(dst) == src
filename = 'exo20101028-b-full.jpg'
src = os.path.join(SAMPLE_DIR, 'pictures', 'dir2', filename)
copy(src, dst, symlink=True)
utils.copy(src, dst, symlink=True)
assert os.path.islink(dst)
assert os.readlink(dst) == src
def test_check_or_create_dir(tmpdir):
path = str(tmpdir.join('new_directory'))
check_or_create_dir(path)
utils.check_or_create_dir(path)
assert os.path.isdir(path)
def test_url_from_path():
assert url_from_path(os.sep.join(['foo', 'bar'])) == 'foo/bar'
assert utils.url_from_path(os.sep.join(['foo', 'bar'])) == 'foo/bar'
def test_read_markdown():
src = os.path.join(SAMPLE_DIR, 'pictures', 'dir1', 'test1', '11.md')
m = utils.read_markdown(src)
assert m['title'] == "Foo Bar"
assert m['meta']['location'][0] == "Bavaria"
assert m['description'] == \
"<p>This is a funny description of this image</p>"