Skip to content

Commit

Permalink
Added metadata tag for Twitter Cards, summaries, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Mar 14, 2016
1 parent 2f14084 commit e1cdf8a
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 44 deletions.
95 changes: 75 additions & 20 deletions IcculusFinger_archiveplans.pl
Expand Up @@ -87,30 +87,36 @@
my $force_archive = undef;
my $replace_archive = 0;

sub strip_tags {
my $t = shift;
1 while ($t =~ s/\&(?!amp)/&/s);
1 while ($t =~ s/</&lt;/s);
1 while ($t =~ s/>/&gt;/s);
1 while ($t =~ s/\[title\](.*?)\[\/title\](\n|\r\n|\b)//is);
1 while ($t =~ s/\[wittyremark\](.*?)\[\/wittyremark\](\n|\r\n|\b)//is);
1 while ($t =~ s/\[b\](.*?)\[\/b\]/<b>$1<\/b>/is);
1 while ($t =~ s/\[u\](.*?)\[\/u\]/<u>$1<\/u>/is);
1 while ($t =~ s/\[i\](.*?)\[\/i\]/<i>$1<\/i>/is);
1 while ($t =~ s/\[center\](.*?)\[\/center\]/<center>$1<\/center>/is);
1 while ($t =~ s/\[img=(".*?")\].*?\[\/img\]/<img src=$1>/is);
1 while ($t =~ s/\[link=(".*?")\](.*?)\[\/link\]/<a href=$1>$2<\/a>/is);
1 while ($t =~ s/\[defaultsection=".*?"\](\n|\r\n|\b)//is);
1 while ($t =~ s/\[section=".*?"\](\n|\r\n|\b)(.*?)\[\/section\](\n|\r\n|\b)/$2/is);
1 while ($t =~ s/\[font(.*?)\](.*?)\[\/font\]/<font $1>$2<\/font>/is);
1 while ($t =~ s/\[noarchive\](.*?)\[\/noarchive\]/$1/is);
1 while ($t =~ s/\[markdown\](.*?)\[\/markdown\]/$1/is);
1 while ($t =~ s/\[metadata=\].*?\[\/metadata\]//is);
return $t;
}

sub run_external_updater {
my $u = shift;
my $d = shift;
my $t = shift;

if ($post_to_icculusnews) {
print(" posting to IcculusNews's submission queue...\n") if $debug;
1 while ($t =~ s/\&(?!amp)/&amp;/s);
1 while ($t =~ s/</&lt;/s);
1 while ($t =~ s/>/&gt;/s);
1 while ($t =~ s/\[title\](.*?)\[\/title\](\n|\r\n|\b)//is);
1 while ($t =~ s/\[wittyremark\](.*?)\[\/wittyremark\](\n|\r\n|\b)//is);
1 while ($t =~ s/\[b\](.*?)\[\/b\]/<b>$1<\/b>/is);
1 while ($t =~ s/\[u\](.*?)\[\/u\]/<u>$1<\/u>/is);
1 while ($t =~ s/\[i\](.*?)\[\/i\]/<i>$1<\/i>/is);
1 while ($t =~ s/\[center\](.*?)\[\/center\]/<center>$1<\/center>/is);
1 while ($t =~ s/\[img=(".*?")\].*?\[\/img\]/<img src=$1>/is);
1 while ($t =~ s/\[link=(".*?")\](.*?)\[\/link\]/<a href=$1>$2<\/a>/is);
1 while ($t =~ s/\[defaultsection=".*?"\](\n|\r\n|\b)//is);
1 while ($t =~ s/\[section=".*?"\](\n|\r\n|\b)(.*?)\[\/section\](\n|\r\n|\b)/$2/is);
1 while ($t =~ s/\[font(.*?)\](.*?)\[\/font\]/<font $1>$2<\/font>/is);
1 while ($t =~ s/\[noarchive\](.*?)\[\/noarchive\]/$1/is);
1 while ($t =~ s/\[markdown\](.*?)\[\/markdown\]/$1/is);

$t = strip_tags($t);
print(" parsed markup tags...\n") if $debug;
my $newssubj = "Notable .plan update from $u";
$ENV{'ICCNEWS_POST_PASS'} = $newspass if defined $newspass;
Expand Down Expand Up @@ -275,6 +281,55 @@ sub update_planfile {
}

$plantext = read_plantext($link, $filename) if (not defined $plantext);

my $metadatastr = $plantext;
my %metadata = ();
1 while ($metadatastr =~ s/\r\n/\n/s);
1 while ($metadatastr =~ s/\r/\n/s);
while ($metadatastr =~ s/\[metadata=\"(.*?)\"\](\n|\b)(.*?)\[\/metadata\](\n|\b)//) {
my $k = $1;
my $v = $3;
$k =~ tr/A-Z/a-z/;
$metadata{$k} = $v;
}

my $summary = $metadata{'summary'};
if (not defined $summary) {
my $str = $metadatastr;
1 while ($str =~ s/\[title\](.*?)\[\/title\](\n|\r\n|\b)//is);
1 while ($str =~ s/\[wittyremark\](.*?)\[\/wittyremark\](\n|\r\n|\b)//is);
1 while ($str =~ s/\[b\](.*?)\[\/b\]/$1/is);
1 while ($str =~ s/\[u\](.*?)\[\/u\]/$1/is);
1 while ($str =~ s/\[i\](.*?)\[\/i\]/$1/is);
1 while ($str =~ s/\[center\](.*?)\[\/center\]/$1/is);
1 while ($str =~ s/\[img=(".*?")\].*?\[\/img\]//is);
1 while ($str =~ s/\[link=(".*?")\](.*?)\[\/link\]/$2/is);
1 while ($str =~ s/\[defaultsection=".*?"\](\n|\r\n|\b)//is);
1 while ($str =~ s/\[section=".*?"\](\n|\r\n|\b)(.*?)\[\/section\](\n|\r\n|\b)/$2/is);
1 while ($str =~ s/\[font(.*?)\](.*?)\[\/font\]/$2/is);
1 while ($str =~ s/\[noarchive\](.*?)\[\/noarchive\]/$1/is);
1 while ($str =~ s/\[markdown\](.*?)\[\/markdown\]/$1/is);
1 while ($str =~ s/\[metadata=".*?"\](\n|\r\n|\b).*?\[\/metadata\](\n|\r\n|\b)//is);

$str =~ s/\A\s+//;
$str =~ s/\s+\Z//;
$str =~ s/\A(.*?)[\r\n].*\Z/$1/s;

$str =~ s/\A\s+//;
$str =~ s/\s+\Z//;

$summary = $str;
}

1 while ($summary =~ s/\r\n/ /s);
1 while ($summary =~ s/\r/ /s);
1 while ($summary =~ s/\n/ /s);
if (length($summary) > 80) {
$summary =~ s/\A(.{15,}?[.?!])(.*?)\Z/$1/; # gross.
}

$summary = $link->quote($summary);

my $ftext = $link->quote($plantext);

my $lastpost = undef;
Expand All @@ -295,13 +350,13 @@ sub update_planfile {
}

if ($replace) {
$sql = "update $dbtable_archive set text=$ftext" .
$sql = "update $dbtable_archive set text=$ftext, summary=$summary" .
" where $user=$user and postdate='$lastpost'";
$lastpost =~ s/\d\d\d\d\-(\d\d)-(\d\d) (\d\d)\:(\d\d)\:(\d\d)/$1$2$3$4.$5/;
`touch -c -m -t '$lastpost' '$filename'`; # force file to this date.
} else {
$sql = "insert into $dbtable_archive (username, postdate, text)" .
" values ($user, '$fdate', $ftext)";
$sql = "insert into $dbtable_archive (username, postdate, summary, text)" .
" values ($user, '$fdate', $summary, $ftext)";
}

$link->do($sql) or die "can't execute the query: $link->errstr";
Expand Down
113 changes: 89 additions & 24 deletions IcculusFinger_daemon.pl
Expand Up @@ -84,6 +84,7 @@
# 2.1.25: IPv6 support, use https:// URLs where appropriate.
# 2.1.26: Added Markdown support, removed <pre> tags and workarounds for it.
# A few other output fixes.
# 2.1.27: Added metadata support for Twitter Cards and summaries.
#-----------------------------------------------------------------------------

# !!! TODO: If an [img] isn't in a link tag, make it link to the image.
Expand All @@ -99,7 +100,7 @@


# Version of IcculusFinger. Change this if you are forking the code.
my $version = 'v2.1.26';
my $version = 'v2.1.27';


#-----------------------------------------------------------------------------#
Expand Down Expand Up @@ -659,6 +660,9 @@ sub do_rss_digest {
close(RSS_DIGESTH);
}

my $linkcount = 0;
my @link_digest;
my %metadata;

my $did_output_start = 0;
sub output_start {
Expand All @@ -675,12 +679,23 @@ sub output_start {
"<link rel=\"alternate\" title=\"$digest_rss_title\" href=\"$digest_rss_about\" type=\"application/rss+xml\" />\n";
}

my $twitter = '';
if (defined $metadata{'twitter'}) {
my $uname = $metadata{'twitter'};
my $summary = $metadata{'summary'};
$twitter .= "<meta name='twitter:card' content='summary' />\n";
$twitter .= "<meta name='twitter:site' content='\@$uname' />\n";
$twitter .= "<meta name='twitter:title content='$title' />\n";
$twitter .= "<meta name='twitter:description' content='$summary' />\n";
}

print <<__EOF__ if not $embed;
<html>
<head>
<title> $title </title>
$rssdigest
$twitter
__EOF__

print "<link rel=\"stylesheet\" href=\"$style\"
Expand Down Expand Up @@ -854,37 +869,38 @@ sub load_archive_list {
return($err) if defined $err;

my $u = $link->quote($user);
my $sql = "select postdate from $dbtable_archive where username=$u" .
my $sql = "select postdate, summary from $dbtable_archive where username=$u" .
" order by postdate desc";
my $sth = $link->prepare($sql);
if (not $sth->execute()) {
$link->disconnect();
return "can't execute the query: $sth->errstr";
}

my @archivelist;
$output_text = "Available archives:\n";
my $had_archives = 0;
while (my @row = $sth->fetchrow_array()) {
push @archivelist, $row[0];
}

if ($#archivelist < 0) {
$output_text = ''; # will use $no_report_string.
} else {
$output_text = "Available archives:\n";
foreach (@archivelist) {
my ($d, $t) = /(\d\d\d\d-\d\d-\d\d) (\d\d:\d\d:\d\d)/;
$t =~ s/(..):(..):(..)/$1-$2-$3/;
if ($do_html_formatting) {
my $url = "$base_url?user=$user&date=$d&time=$t";
$output_text .= " \[link=\"$url\"\]$_\[/link\]\n";
} else {
$output_text .= " finger '$user?date=$d&time=$t\@$host'\n";
}
my ($dt, $summary) = @row;
my ($d, $t) = $dt =~ /(\d\d\d\d-\d\d-\d\d) (\d\d:\d\d:\d\d)/;
$t =~ s/(..):(..):(..)/$1-$2-$3/;
if ($do_html_formatting) {
my $url = "$base_url?user=$user&date=$d&time=$t";
$summary = " -- $summary" if ($summary ne '');
$output_text .= " \[link=\"$url\"\]$dt\[/link\]$summary\n";
} else {
$summary = " # $summary" if ($summary ne '');
$output_text .= " finger '$user?date=$d&time=$t\@$host'$summary\n";
}
$had_archives = 1;
}

$sth->finish();
$link->disconnect();

if (not $had_archives) {
$output_text = ''; # will use $no_report_string.
}

return(undef);
}

Expand Down Expand Up @@ -1128,9 +1144,6 @@ sub verify_and_load_request {
return(1);
}

my $linkcount = 0;
my @link_digest;

sub process_tags {
my $text = shift;

Expand Down Expand Up @@ -1253,7 +1266,11 @@ sub process_tags {

sub do_fingering {
my ($query_string, $user, $block_output) = @_;

my $orig_text = $output_text;

@link_digest = ();
%metadata = ();
$linkcount = $#link_digest + 2; # start at one.

if ($debug) {
Expand All @@ -1278,6 +1295,9 @@ sub do_fingering {
1 while ($output_text =~ s/\r\n/\n/s);
1 while ($output_text =~ s/\r/\n/s);

# !!! FIXME: need to handle these in process_tags() so we ignore them
# !!! FIXME: inside [markdown] blocks.

# Change [style][/style] tags.
while ($output_text =~ s/\[style\](.*?)\[\/style\](\n|\b)//is) {
push @style_array, $1 if $permit_user_styles;
Expand All @@ -1293,6 +1313,14 @@ sub do_fingering {
push @wittyremark_array, $1 if $permit_user_wittyremarks;
}

# Change [metadata][/metadata] tags.
while ($output_text =~ s/\[metadata=\"(.*?)\"\](\n|\b)(.*?)\[\/metadata\](\n|\b)//) {
my $k = $1;
my $v = $3;
$k =~ tr/A-Z/a-z/;
$metadata{$k} = $v;
}

# !!! FIXME: Make this a separate subroutine?
if ($list_sections) {
my @sectionlist;
Expand Down Expand Up @@ -1339,6 +1367,7 @@ sub do_fingering {
$final .= process_tags($output_text);
$output_text = $final;

# Pick a random title...
if ($#title_array >= 0) {
$title = $title_array[int(rand($#title_array + 1))];
}
Expand All @@ -1353,7 +1382,38 @@ sub do_fingering {
$wittyremark = $wittyremark_array[int(rand($#wittyremark_array + 1))];
}

# Pick a random title...
if (not defined $metadata{'summary'}) {
my $str = $orig_text;
1 while ($str =~ s/\[title\](.*?)\[\/title\](\n|\r\n|\b)//is);
1 while ($str =~ s/\[wittyremark\](.*?)\[\/wittyremark\](\n|\r\n|\b)//is);
1 while ($str =~ s/\[b\](.*?)\[\/b\]/$1/is);
1 while ($str =~ s/\[u\](.*?)\[\/u\]/$1/is);
1 while ($str =~ s/\[i\](.*?)\[\/i\]/$1/is);
1 while ($str =~ s/\[center\](.*?)\[\/center\]/$1/is);
1 while ($str =~ s/\[img=(".*?")\].*?\[\/img\]//is);
1 while ($str =~ s/\[link=(".*?")\](.*?)\[\/link\]/$2/is);
1 while ($str =~ s/\[defaultsection=".*?"\](\n|\r\n|\b)//is);
1 while ($str =~ s/\[section=".*?"\](\n|\r\n|\b)(.*?)\[\/section\](\n|\r\n|\b)/$2/is);
1 while ($str =~ s/\[font(.*?)\](.*?)\[\/font\]/$2/is);
1 while ($str =~ s/\[noarchive\](.*?)\[\/noarchive\]/$1/is);
1 while ($str =~ s/\[markdown\](.*?)\[\/markdown\]/$1/is);
1 while ($str =~ s/\[metadata=".*?"\](\n|\r\n|\b).*?\[\/metadata\](\n|\r\n|\b)//is);

$str =~ s/\A\s+//;
$str =~ s/\s+\Z//;
$str =~ s/\A(.*?)[\r\n].*\Z/$1/s;
$str =~ s/\A\s+//;
$str =~ s/\s+\Z//;
$metadata{'summary'} = $str;
}

1 while ($metadata{'summary'} =~ s/\r\n/ /s);
1 while ($metadata{'summary'} =~ s/\r/ /s);
1 while ($metadata{'summary'} =~ s/\n/ /s);
if (length($metadata{'summary'}) > 80) {
$metadata{'summary'} =~ s/\A(.{15,}?[.?!])(.*?)\Z/$1/; # gross.
}

if ($debug) {
my $x;
$x = $#title_array + 1;
Expand Down Expand Up @@ -1383,7 +1443,12 @@ sub do_fingering {
print("Chosen: [$wittyremark].\n");

$x = $#link_digest + 1;
print("Items in link digest: $x ... \n");
print("Items in link digest: $x ...\n");

foreach (sort keys %metadata) {
my $k = $_;
print("metadata{'$k'} = '" . $metadata{$k} . "' ...\n");
}

print("\n");
print("Actual finger output begins below line...\n");
Expand Down

0 comments on commit e1cdf8a

Please sign in to comment.