50 lines
2.2 KiB
Bash
50 lines
2.2 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
# Create a Twitter summary (twitter "card") for the post
|
|
#
|
|
# $1 the post file
|
|
# $2 the title
|
|
twitter_card() {
|
|
declare global_twitter_username
|
|
declare template_tags_line_header
|
|
declare global_url
|
|
declare global_twitter_cookieless
|
|
declare template_twitter_button
|
|
declare template_twitter_comment
|
|
declare template_comments
|
|
[[ -z $global_twitter_username ]] && return
|
|
|
|
echo "<meta name='twitter:card' content='summary'>"
|
|
echo "<meta name='twitter:site' content='@$global_twitter_username'>"
|
|
echo "<meta name='twitter:title' content='$2'>" # Twitter truncates at 70 char
|
|
description=$(grep -v "^<p>$template_tags_line_header" "$1" | sed -e 's/<[^>]*>//g' | head -c 250 | tr '\n' ' ' | sed "s/\"/'/g")
|
|
echo "<meta name='twitter:description' content=\"$description\">"
|
|
image=$(sed -n 's/.*<img.*src="\([^"]*\)".*/\1/p' "$1" | head -n 1) # First image is fine
|
|
[[ -z $image ]] && return
|
|
[[ $image =~ ^https?:// ]] || image=$global_url/$image # Check that URL is absolute
|
|
echo "<meta name='twitter:image' content='$image'>"
|
|
}
|
|
|
|
# Adds the code needed by the twitter button
|
|
#
|
|
# $1 the post URL
|
|
twitter() {
|
|
[[ -z $global_twitter_username ]] && return
|
|
|
|
if [[ $global_twitter_cookieless == true ]]; then
|
|
id=$RANDOM
|
|
|
|
search_engine="https://twitter.com/search?q="
|
|
|
|
echo "<p id='twitter'><a href='http://twitter.com/intent/tweet?url=$1&text=$template_twitter_comment&via=$global_twitter_username'>$template_comments $template_twitter_button</a> "
|
|
echo "<a href='$search_engine""$1'><span id='count-$id'></span></a> </p>"
|
|
return;
|
|
else
|
|
echo "<p id='twitter'>$template_comments ";
|
|
fi
|
|
|
|
echo "<a href=\"https://twitter.com/share\" class=\"twitter-share-button\" data-text=\"$template_twitter_comment\" data-url=\"$1\""
|
|
echo " data-via=\"$global_twitter_username\""
|
|
echo ">$template_twitter_button</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=\"//platform.twitter.com/widgets.js\";fjs.parentNode.insertBefore(js,fjs);}}(document,\"script\",\"twitter-wjs\");</script>"
|
|
echo "</p>"
|
|
} |