diff --git a/docs/image_information.rst b/docs/image_information.rst index 112f6b3..529b46c 100644 --- a/docs/image_information.rst +++ b/docs/image_information.rst @@ -1,20 +1,16 @@ =================== -Image information + Image information =================== -Additional information on an image can be given in a file using the `markdown`_ syntax, -named ``.md`` (example: IMG_5206.jpg.md): - -:: +Additional information on an image can be given in a file using the `markdown`_ +syntax, named ``.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 %} -

Location: {{ media.desc.meta.location[0] }} + {% if media.meta.location %} +

Location: {{ media.meta.location[0] }} {% endif %} .. _markdown: http://daringfireball.net/projects/markdown/ diff --git a/sigal/gallery.py b/sigal/gallery.py index f49e064..ba28c58 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -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.""" diff --git a/sigal/image.py b/sigal/image.py index 9d4e958..a5bf1cc 100644 --- a/sigal/image.py +++ b/sigal/image.py @@ -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 diff --git a/sigal/themes/colorbox/templates/index.html b/sigal/themes/colorbox/templates/index.html index a481c60..6b66965 100644 --- a/sigal/themes/colorbox/templates/index.html +++ b/sigal/themes/colorbox/templates/index.html @@ -90,7 +90,7 @@ {% if loop.index % nb_columns == 0 %}omega{% endif%}"> {{ media.filename }} + title="{{ media.title if media.title else media.filename }}" /> {% endif %} {% if media.type == "video" %} @@ -101,7 +101,7 @@ class="gallery" inline='yes' title="{{ media.filename }}" {% if media.big %} data-big="{{ media.big }}"{% endif %}> {{ media.filename }} + title="{{ media.title if media.title else media.filename }}" />

diff --git a/sigal/themes/galleria/templates/index.html b/sigal/themes/galleria/templates/index.html index 5c5e2b2..4bda703 100644 --- a/sigal/themes/galleria/templates/index.html +++ b/sigal/themes/galleria/templates/index.html @@ -60,8 +60,7 @@ {% if album.medias %} {% macro img_description(media) -%} {%- if media.big %}Full size{% endif %} - {% if media.title %}Title: {{ media.title }}
{% endif %} - {% if media.description %}Description: {{ media.description }}
{% endif %} + {% if media.description %}
{{ media.description }}{% endif %} {%- if media.exif %}
@@ -79,16 +78,18 @@ {% if media.type == "image" %} {{ media.filename }} {% endif %} {% if media.type == "video" %} {{ media.filename }} + data-title="{{ media.title if media.title else media.filename }}" + data-description="{{ img_description(media) }}" + data-layer="" /> {% endif %} {% endfor %} diff --git a/sigal/utils.py b/sigal/utils.py index 9bdedce..1ee3255 100644 --- a/sigal/utils.py +++ b/sigal/utils.py @@ -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() + } diff --git a/tests/sample/pictures/dir1/test1/11.jpg.md b/tests/sample/pictures/dir1/test1/11.md similarity index 100% rename from tests/sample/pictures/dir1/test1/11.jpg.md rename to tests/sample/pictures/dir1/test1/11.md diff --git a/tests/test_gallery.py b/tests/test_gallery.py index d9e60f2..71d3df7 100644 --- a/tests/test_gallery.py +++ b/tests/test_gallery.py @@ -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 == "

This is a funny description of this image

" assert repr(m) == "('{}')".format(file_path) assert str(m) == file_path diff --git a/tests/test_image.py b/tests/test_image.py index c693554..d50a1bd 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -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'] == "

This is a funny description of this image

" - \ No newline at end of file diff --git a/tests/test_utils.py b/tests/test_utils.py index 08e7e26..3d0227f 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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'] == \ + "

This is a funny description of this image

"