From 5bcadb7e5e880535416b8e845624f897afcf3384 Mon Sep 17 00:00:00 2001 From: Skial Bainn Date: Wed, 7 May 2014 13:56:17 +0100 Subject: [PATCH] Updated SocialMetadata.hx to be smarter depending on the contents of the html file, to do with issue https://github.com/skial/haxe.io/issues/24 --- all.hxml | 1 + src/uhx/tuli/plugins/SocialMetadata.hx | 29 ++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/all.hxml b/all.hxml index 652ae8c..ec6c90d 100644 --- a/all.hxml +++ b/all.hxml @@ -4,6 +4,7 @@ -lib beanhx -lib tjson -lib selecthxml +-lib imagemagick -debug #-dce no -cp src diff --git a/src/uhx/tuli/plugins/SocialMetadata.hx b/src/uhx/tuli/plugins/SocialMetadata.hx index 20700d9..9b7e276 100644 --- a/src/uhx/tuli/plugins/SocialMetadata.hx +++ b/src/uhx/tuli/plugins/SocialMetadata.hx @@ -1,5 +1,6 @@ package uhx.tuli.plugins; +import haxe.imagemagick.Imagick; import sys.io.File; import uhx.sys.Tuli; @@ -60,8 +61,6 @@ class SocialMetadata { } } - var url = dom.find('meta[property="og:url"]'); - for (title in titles) { title.setAttr('content', dom.find('title').text()); } @@ -75,6 +74,32 @@ class SocialMetadata { } } + var img = 'img/skial.jpg'; + var alt = dom.find('img[alt*="social"]'); + var cardType = 'summary'; + + var images = []; + for (id in ['property', 'name']) { + for (meta in dom.find('meta[$id*="image"]')) { + if (meta.attr('content') == '') images.push( meta ); + } + } + + if (alt.length > 0) { + img = alt.first().attr('src'); + var data = new Imagick('${Tuli.config.input}/$img'.normalize()); + if (data.width >= 280 && data.height >= 150) { + dom.find('meta[name="twitter:image"]').setAttr('name', 'twitter:image:src'); + cardType = 'summary_large_image'; + } + } + + dom.find('meta[name="twitter:card"]').setAttr('content', cardType); + + for (image in images) image.setAttr('content', 'http://haxe.io/$img'.normalize()); + + var url = dom.find('meta[property="og:url"]'); + if (url.length > 0) { var path = 'http://haxe.io/$file'.normalize(); if (path.endsWith('index.html')) path = path.directory().addTrailingSlash();