Added support for tag clouds and search

This commit is contained in:
sørenpeter 2024-04-05 22:54:45 +02:00
parent 446d3b9317
commit c9f7d05017
7 changed files with 214 additions and 6 deletions

View file

@ -282,6 +282,24 @@ nav.pagnation {
border-radius: 0.25rem;
}
/* == Tag Cloud ===================== */
.tagcloud {
margin-top: 0.75rem;
border-top: thin solid var(--border);
padding: 0.2rem;
}
.tagcloud a,
.tagcloud a:visited {
text-decoration: none;
padding-right: 0.2rem;
color: var(--text-light);
}
.tagcloud a.active {
color: red;
}
/* === FOOTER === */

View file

@ -34,7 +34,8 @@ class Twt {
public $emoji;
public $nick;
public $mainURL;
public $images;
public $images = [];
public $tags = [];
}
# https://stackoverflow.com/a/39360281/13173382
@ -118,6 +119,24 @@ function getImagesFromTwt(string $twtString) {
return $result;
}
function getTagsFromTwt(string $twtString) {
//$pattern = '/(?<!\()\B#\w+(?!\))/iu';
$pattern = '/(?<=\B)#(\w+)/';
//$pattern = '/(?<=\s|^)#(\w+)/';
// TODO: Fix so it does not match with url#fragments (\B vs \s)
// But for some reason this does not work: '/(?<!\()\s#\w+(?!\))/iu';
preg_match_all($pattern, $twtString, $matches, PREG_SET_ORDER);
$result = array();
foreach ($matches as $match) {
$result[] = array($match[0]);
}
return $result;
}
function getMentionsFromTwt(string $twtString) {
$pattern = '/@<([^>]+)\s([^>]+)>/'; // Matches "@<nick url>"
preg_match_all($pattern, $twtString, $matches, PREG_SET_ORDER);
@ -187,8 +206,8 @@ function replaceImagesFromTwt(string $twtString) {
function replaceTagsFromTwt(string $twtString) {
$pattern = '/#(\w+)?/';
$replacement = '<a href="#">#\1</a>'; // Dummy link
//$replacement = '<a href="?tag=$1" class="tag">#${1}</a>';
//$replacement = '<a href="#">#\1</a>'; // Dummy link
$replacement = '<a href="?search=$1" class="tag">#${1}</a>';
$result = preg_replace($pattern, $replacement, $twtString);
return $result;
@ -376,7 +395,7 @@ function getTwtsFromTwtxtString($url) {
}
// TODO: Make ?tag= filtering feature
//$twtContent = replaceTagsFromTwt($twtContent);
$twtContent = replaceTagsFromTwt($twtContent);
// TODO: Get mentions
$mentions = getMentionsFromTwt($twtContent);

67
partials/__search.php Normal file
View file

@ -0,0 +1,67 @@
<?php
// Search / filter on tags (or anything within a twt actually)
// Base on code from: https://social.dfaria.eu/search
if (!empty($_GET['search'])) {
$searchfor = $_GET['search'];
//$file = 'twtxt.txt';
//$contents = file_get_contents($url);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/mi";
// 1. filter $twts for maches containg search string
/*
$twts_filtered = array_filter($twts, function($twt) {
return preg_match($pattern, $twt->content);
});
echo "<pre>";
print_r($twts[1711985096]);
*/
$twts_filtered = [];
//print_r($twts_filtered);
// 2. Repalce original $twts with new $twts_filtered
//$twts = $twts_filtered
foreach ($twts as $twt) {
if (preg_match_all($pattern, $twt->content, $matches)) {
echo "<hr>";
print_r($twt);
/*
$date = preg_filter('/^(?<date>[^\t]+)\t(?<entry>.+)/', '\2', $matches[0]);
$entry = preg_filter('/^(?<date>[^\t]+)\t(?<entry>.+)/', '\1', $matches[0]);
foreach ($date as $i => $tw) {
$post[$tw] = $entry[$i];
}
$post = array_reverse($post);
$perpage = 10;
if(isset($_GET['start'])) $start = $_GET['start']; else $start = 0;
$numposts = count($post);
$post = array_slice($post, $start, $perpage);
echo "<hr>";
foreach ($post as $tw => $data) {
echo $tw;
echo "<hr>";
}
*/
}
}
}

View file

@ -114,6 +114,23 @@ foreach ($parsedTwtxtFiles as $currentTwtFile) {
}
}
// Search / filter on tags (or anything within a twt actually)
// Base on hash filter below and on code from: https://social.dfaria.eu/search
// TODO: Move to after rendering of tag-cloud to get all tags rendered
/*
if (!empty($_GET['search'])) {
$search = $_GET['search'];
$pattern = preg_quote($search, '/');
$pattern = "/^.*$pattern.*\$/mi";
$twts = array_filter($twts, function ($twt) use ($pattern) {
return preg_match($pattern, $twt->content);
});
}
*/
if (!empty($_GET['hash'])) {
$hash = $_GET['hash'];
$twts = array_filter($twts, function ($twt) use ($hash) {

View file

@ -30,10 +30,14 @@ $textareaValue = "@<$profile->nick $profile->mainURL> ";
<span class="right">
<!-- <a href="following.php">Following <?php echo count($twtFollowingList); ?></a> | -->
<a target="_blank" href="<?=$profile->mainURL?>"></i><?=$profile->mainURL?></a>
<a target="_blank" href="<?=$profile->mainURL?>"><?=$profile->mainURL?></a>
(<a href="https://yarn.social">How to follow</a>)
</span>
<div class="tagcloud">
<?php include_once 'partials/tag_cloud.php'; ?>
</div>
</small>
</div>

65
partials/tag_cloud.php Normal file
View file

@ -0,0 +1,65 @@
<?php
// Tagcloud for twtxt
// Base on code from: https://social.dfaria.eu/search
// if(empty($_GET['search'])) {
// Add all tags to one array
foreach ($twts as $twt) {
$tag_array = getTagsFromTwt($twt->content);
foreach ($tag_array as $tag) {
$tags[] = $tag[0];
}
}
natcasesort($tags);
$tag_count = array_count_values($tags);
//arsort($tag_count, SORT_STRING);
//ksort($tag_count, SORT_STRING);
//strnatcasecmp($tag_count);
$max_count = max($tag_count);
$min_font_size = 10;
$max_font_size = 30;
$num_intermediate_levels = 1;
$font_size_interval = ($max_font_size - $min_font_size) / ($num_intermediate_levels + 1);
$uri = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
foreach ($tag_count as $tag => $count) {
$font_size = $min_font_size + ($count / $max_count) * $font_size_interval;
$tag = str_replace('#', '', $tag);
echo '<a href="'.$uri.'&search='.$tag.'" style="font-size: '.$font_size.'px;">#'. $tag .'</a> ';
//echo '<a href="?search='.$tag.'">#'. $tag .' ('.$count.')</a> ';
}
// Detail/summary with top tags and solo tags
/*
$top_tags = array_filter($tag_count, function($val){return ($val>1);});
$solo_tags = array_diff($tag_count, $top_tags);
krsort($solo_tags, SORT_STRING);
echo "<details><summary> Tags: ";
foreach ($top_tags as $tag => $count) {
$tag = str_replace('#', '', $tag);
echo '<a href="?search='.$tag.'">#'.$tag.'</a> ';
}
echo "</summary>";
foreach ($solo_tags as $tag => $count) {
$tag = str_replace('#', '', $tag);
echo '<a href="?search='.$tag.'">#'.$tag.'</a> ';
}
echo "</details>";
*/
// } else {
// echo "Showing posts with <code>".$_GET['search']."</code>";
// }

View file

@ -6,6 +6,24 @@
<?php }?>
-->
<?php
// Search / filter on tags (or anything within a twt actually)
// Base on hash filter below and on code from: https://social.dfaria.eu/search
if (!empty($_GET['search'])) {
$search = $_GET['search'];
$pattern = preg_quote($search, '/');
$pattern = "/^.*$pattern.*\$/mi";
$twts = array_filter($twts, function ($twt) use ($pattern) {
return preg_match($pattern, $twt->content);
});
}
?>
<?php foreach ($twts as $twt) {?>
<article class="post-entry" id="<?=$twt->hash?>">
<a href="<?=$baseURL?>/?profile=<?=$twt->mainURL?>">