예제 #1
0
def convert_to_bookchapter(absolute_path, level):

    # album_title, album_description, album_cover, album_sorted_rev,
    # album_hide_cover, modified_albums_on_top, ignoreinbook)
    album_metadata = pyntrest_io.read_optional_album_metadata(
        absolute_path, pyntrest_config.META_INI_FILE_PATTERN)

    book_chapter = BookChapter()
    book_chapter.level = level
    book_chapter.title = album_metadata[0]
    book_chapter.subchapters = []
    book_chapter.ignore = album_metadata[6]

    subfiles = pyntrest_io.get_immediate_subfiles(absolute_path)
    content = []
    for subfile in subfiles:
        if re.match(pyntrest_config.INTRO_MD_FILE_PATTERN, subfile):
            html_content, _ = pyntrest_io.get_html_content(
                path.join(absolute_path, subfile))
            content.insert(0, html_content)
        if re.match(pyntrest_config.TEXT_MD_FILE_PATTERN, subfile):
            html_content, _ = pyntrest_io.get_html_content(
                path.join(absolute_path, subfile))
            content.append(html_content)

    book_chapter.html_content = u'\n'.join(content)

    # remove emoticons
    try:
        # Wide UCS-4 build
        myre = re.compile(
            u'['
            u'\U0001F300-\U0001F64F'
            u'\U0001F680-\U0001F6FF'
            u'\u2600-\u26FF\u2700-\u27BF]+', re.UNICODE)
    except re.error:
        # Narrow UCS-2 build
        myre = re.compile(
            u'('
            u'\ud83c[\udf00-\udfff]|'
            u'\ud83d[\udc00-\ude4f\ude80-\udeff]|'
            u'[\u2600-\u26FF\u2700-\u27BF])+', re.UNICODE)
    book_chapter.html_content = myre.sub(u'',
                                         book_chapter.html_content)  # no emoji

    for direct in pyntrest_io.get_immediate_subdirectories(
            absolute_path, True):
        book_chapter.subchapters.append(
            convert_to_bookchapter(path.join(absolute_path, direct),
                                   level + 1))

    return book_chapter
예제 #2
0
def convert_to_bookchapter( absolute_path, level ):

    # album_title, album_description, album_cover, album_sorted_rev,
    # album_hide_cover, modified_albums_on_top, ignoreinbook)
    album_metadata = pyntrest_io.read_optional_album_metadata (
         absolute_path, pyntrest_config.META_INI_FILE_PATTERN)

    book_chapter = BookChapter()
    book_chapter.level = level
    book_chapter.title = album_metadata[0]
    book_chapter.subchapters = []
    book_chapter.ignore = album_metadata[6]

    subfiles = pyntrest_io.get_immediate_subfiles(absolute_path)
    content = []
    for subfile in subfiles:
        if re.match(pyntrest_config.INTRO_MD_FILE_PATTERN, subfile):
            html_content, _ = pyntrest_io.get_html_content(
                path.join(absolute_path, subfile))
            content.insert(0, html_content)
        if re.match(pyntrest_config.TEXT_MD_FILE_PATTERN, subfile):
            html_content, _ = pyntrest_io.get_html_content(
                path.join(absolute_path, subfile))
            content.append(html_content)

    book_chapter.html_content = u'\n'.join(content)

    # remove emoticons
    try:
        # Wide UCS-4 build
        myre = re.compile(u'['
            u'\U0001F300-\U0001F64F'
            u'\U0001F680-\U0001F6FF'
            u'\u2600-\u26FF\u2700-\u27BF]+',
            re.UNICODE)
    except re.error:
        # Narrow UCS-2 build
        myre = re.compile(u'('
            u'\ud83c[\udf00-\udfff]|'
            u'\ud83d[\udc00-\ude4f\ude80-\udeff]|'
            u'[\u2600-\u26FF\u2700-\u27BF])+',
            re.UNICODE)
    book_chapter.html_content = myre.sub(
        u'', book_chapter.html_content) # no emoji

    for direct in pyntrest_io.get_immediate_subdirectories(absolute_path, True):
        book_chapter.subchapters.append(
            convert_to_bookchapter(path.join(absolute_path, direct), level+1))

    return book_chapter
예제 #3
0
    def process_subimage (self, image_name, local_albumpath_rel,
        local_albumpath_abs):
        """Uses the given sub-image path and creates a AlbumImage model for it,
        including the creation of all necessary thumbnails and the check for
        whether the file is a Youtube video hook. Finally it appends the
        AlbumImg to the provided image list."""

        local_imagepath_abs = path.join(local_albumpath_abs, image_name)
        modified, _ = is_modified(
            local_imagepath_abs, True,
            pyntrest_config.MAX_AGE_OF_NEW_IMAGES_H,
            pyntrest_config.HIGHLIGHT_NEW_IMAGES)

        #######################################################################

        if pyntrest_config.IMAGE_FILE_PATTERN.match(
                                        local_imagepath_abs.lower()):

            static_fullsize_path_abs = path.join (
                    self.static_fullsize_path, local_albumpath_rel, image_name)
            static_thumb_path_abs = path.join (
                    self.static_ithumbs_path, local_albumpath_rel, image_name)

            # copy full size image to static folder
            if not path.exists(static_fullsize_path_abs):
                self.pil_handler.copy_with_rotation(local_imagepath_abs,
                    static_fullsize_path_abs)

            # calculate image size for masonry and copy to static folder
            width, height = (
                self.pil_handler.create_image_thumbnail_if_not_present(
                static_fullsize_path_abs, static_thumb_path_abs))

            latitude, longitude = (
                self.pil_handler.get_geo_coordinates( local_imagepath_abs ))
            geocoord = None
            if latitude and longitude:
                #print '{0}: {1}, {2}'.format(image_name, latitude, longitude)
                geocoord = '{0}, {1}'.format(latitude, longitude)

            # add image to template context
            albumimage = AlbumImage(type='img',
                location=path.join(local_albumpath_rel, image_name),
                title=image_name, width=width, height=height,
                modified=modified, geocoord=geocoord)
            return albumimage

        #######################################################################

        elif pyntrest_config.YOUTUBE_INI_FILE_PATTERN.match(
                        local_imagepath_abs):

            youtube_id = read_youtube_ini_file (local_imagepath_abs)
            # .75 is the fixed Youtube thumbnail width to height ratio
            thumb_height = int(
                float (pyntrest_config.IMAGE_THUMB_WIDTH) * 0.75)
            albumimage = AlbumImage(type='you', location=path.join(
                        local_albumpath_rel, image_name), title=image_name,
                        width=pyntrest_config.IMAGE_THUMB_WIDTH,
                        height=thumb_height, youtubeid=youtube_id,
                        modified=modified)
            return albumimage

        #######################################################################

        elif (pyntrest_config.TEXT_MD_FILE_PATTERN.match(
                                local_imagepath_abs.lower()) and
              not (pyntrest_config.INTRO_MD_FILE_PATTERN.match(
                                local_imagepath_abs.lower()))):

            html_content, title = get_html_content(local_imagepath_abs)

            divid="".join(choice('abcdefghijklmnopqrstuvwxyz') for _ in range(16))

            albumimage = AlbumImage(type='txt', location=path.join(
                        local_albumpath_rel, image_name), title=title,
                        width=pyntrest_config.IMAGE_THUMB_WIDTH,
                        height=pyntrest_config.IMAGE_THUMB_HEIGHT/2,
                        modified=modified, text_content=html_content,
                        divid=divid)
            return albumimage
예제 #4
0
    def generate_view_context(self, request_path):
        """The core "magic" method that reads the requested album filesystem
        path related to the virtual album path from the request and obtains all
        necessary information to create a neat web photo album"""

        local_albumpath_abs = convert_url_path_to_local_filesystem_path(
                  self.main_images_path, request_path)
        local_albumpath_rel = convert_url_path_to_local_filesystem_path(
                                                     '', request_path)
        mkdirs(path.join (self.static_fullsize_path, local_albumpath_rel))
        mkdirs(path.join (self.static_ithumbs_path, local_albumpath_rel))

        (album_title, album_description, album_cover, reversed_sorting,
         hide_cover, mods_on_top, _) = read_optional_album_metadata (
            local_albumpath_abs, pyntrest_config.META_INI_FILE_PATTERN)

        # setup sub albums
        subalbums = []
        for subalbum_name in get_immediate_subdirectories(local_albumpath_abs):
            self.process_subalbum (subalbums, subalbum_name,
            local_albumpath_rel, local_albumpath_abs)

        # sort subalbums by path
        if (mods_on_top):
            subalbums = sorted(subalbums, key=lambda subalbum:
                subalbum.last_modified, reverse=True)
        else:
            subalbums = sorted(subalbums, key=lambda subalbum:
                subalbum.path, reverse=False)

        # setup images
        images = []
        for image_name in listdir(local_albumpath_abs):
            subimage = self.process_subimage(image_name,
                                    local_albumpath_rel, local_albumpath_abs)
            if subimage is None:
                continue
            if (album_cover is None or hide_cover is False):
                images.append(subimage)
            else:
                if album_cover == image_name:
                    pass # ignore
                else:
                    images.append(subimage)

        # update image descriptions
        image_descriptions = read_optional_image_metadata(
                    local_albumpath_abs, pyntrest_config.META_INI_FILE_PATTERN)
        for image in images:
            basename = path.basename(image.location).lower()
            try:
                image_description = image_descriptions[basename]
                image.description = image_description
            except KeyError:
                pass # Ignore

        # sort images by path
        images = sorted(images, key=lambda albumimage: albumimage.location,
                        reverse=reversed_sorting)

        # setup breadcrumb
        breadcrumbs = []
        breadcrumb_paths = get_absolute_breadcrumb_filesystem_paths (
            request_path)
        local_albumpath_abs = self.main_images_path
        path_string = ''
        for breadcrumb_path in breadcrumb_paths:
            local_albumpath_abs = path.join(local_albumpath_abs,
                breadcrumb_path)
            path_string = path_string + '/' + breadcrumb_path
            path_string = sub ('[/]+' , '/', path_string)
            album_title, album_description, _, _, _, _, _= (
                read_optional_album_metadata (local_albumpath_abs,
                    pyntrest_config.META_INI_FILE_PATTERN))
            url_path = path_string
            if pyntrest_config.EXTERNAL_BASE_URL is not None:
                url_path  = pyntrest_config.EXTERNAL_BASE_URL + url_path

            web_path = WebPath(title=album_title, path=url_path)
            breadcrumbs.append(web_path)
        page_title = breadcrumbs[0].title
        breadcrumbs[0].title = pyntrest_config.WORDING_HOME

        # check for intro text
        intro_content = None
        intro_file = None
        subfiles = get_immediate_subfiles(local_albumpath_abs)
        for subfile in subfiles:
            matching = match(pyntrest_config.INTRO_MD_FILE_PATTERN,
                             subfile.lower())
            if matching != None:
                intro_file = path.join(local_albumpath_abs, subfile)

        if intro_file and path.isfile(intro_file):
            intro_content, _ = get_html_content(intro_file)

        # show header?
        show_breadcrumb = True
        first_page = len(breadcrumbs) == 1
        try:
            if first_page and pyntrest_config.SUPPRESS_BREADCRUMB_ON_HOME:
                show_breadcrumb = False
        except AttributeError:
            pass # just show it.

        context = { 'page_title': page_title,
                    'col_width': pyntrest_config.IMAGE_THUMB_WIDTH,
                    'col_height' : pyntrest_config.IMAGE_THUMB_HEIGHT,
                    'images': images,
                    'show_breadcrum': False,
                    'first_page': first_page,
                    'subalbums': subalbums,
                    'album_title' : album_title,
                    'album_description' : album_description,
                    'lang_images' : pyntrest_config.WORDING_IMAGES,
                    'lang_albums' : pyntrest_config.WORDING_ALBUM,
                    'breadcrumbs' : breadcrumbs,
                    'show_breadcrumb': show_breadcrumb,
                    'show_headings' :
                        pyntrest_config.SHOW_ALBUM_IMAGES_WORDINGS,
                   'intro_content' : intro_content}

        return context