Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update gallery.html: read keywords from XPKeywords Exif tag #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion layouts/shortcodes/gallery.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@

{{ if not (eq $filterOptions "[]") }}
{{/* only include fields that are currently supported by the filter mechanism (in JS at the end of the file) */}}
data-meta="{{ (dict "Tags" $metadata.Tags "Rating" $metadata.Rating "ColorLabels" $metadata.ColorLabels "ImageDescription" $metadata.ImageDescription) | jsonify }}"
data-meta="{{ (dict "Tags" $metadata.Tags "Rating" $metadata.Rating "ColorLabels" $metadata.ColorLabels "ImageDescription" $metadata.ImageDescription "XPKeywords" $metadata.XPKeywords) | jsonify }}"
{{ end }}
{{ end }}
>
Expand Down Expand Up @@ -328,6 +328,35 @@
});
};

// XPKeywords use UCS-2 encoding. This function decodes it
function decodeUTF16LE(numbers) {
const uint8Array = new Uint8Array(numbers);
let decodedString = '';
for (let i = 0; i < uint8Array.length; i += 2) {
const charCode = uint8Array[i] + (uint8Array[i + 1] << 8);
if (charCode === 0x0000) {
break;
}
decodedString += String.fromCharCode(charCode);
}
return decodedString;
}

// this function returns a function that can be used by justifiedGallery
// for filtering images by their keywords
// read keywords from the XPKeywords Exif tag (0x9c9e), in UCS-2
// To convert IPTC keywords:
// exiftool -overwrite_original "-XPKeywords<Keywords" *.jpg
function createXPKeywordsFilter(tagsRegexString) {
const tagsRegex = RegExp(tagsRegexString);
return createMetadataFilter(meta => {
console.log(meta.XPKeywords);
let tags = decodeUTF16LE(meta.XPKeywords).split(",");
console.log(tags);
tags = tags ? tags : [];
return tags.some(tag => tagsRegex.test(tag));
});
};

const filterOptions = {{ $filterOptions | safeJS }};

Expand Down Expand Up @@ -402,6 +431,8 @@
filter = createColorLabelFilter(filterConfig.color_label);
} else if(filterConfig.description){
filter = createImageDescriptionFilter(filterConfig.description);
} else if(filterConfig.keywords){
filter = createXPKeywordsFilter(filterConfig.keywords);
} else {
// default to always true filter
filter = createMetadataFilter(meta => true);
Expand Down