add: show tags list when searching tags
add: show preview when searching in content
change: delete previous upon rename
change: various improvments
This commit is contained in:
quenousimporte 2024-02-19 10:18:10 +01:00
parent ac6c01bd46
commit 6e85a1336b
1 changed files with 87 additions and 27 deletions

114
index.php
View File

@ -3,9 +3,9 @@
<head>
<title>
<?php
if (isset($_GET['openaction']))
if (isset($_GET['action']) && $_GET['action'] == 'open')
{
echo $_GET['userdata'];
echo $_GET['param'];
}
?>
</title>
@ -23,13 +23,8 @@
font-family: inherit;
font-size: inherit;
}
.title {
width: 100%;
border: none;
outline: none;
font-family: inherit;
font-size: inherit;
font-weight: bold;
.grey {
color: lightgrey;
}
</style>
</head>
@ -44,8 +39,6 @@
exit;
}
echo '<div><a accesskey="h" href="' . $root . '">home</a></div><br>';
$action = '';
if (isset($_POST['action'])) $action = $_POST['action'];
else if (isset($_GET['action'])) $action = $_GET['action'];
@ -53,11 +46,19 @@
$param = '';
if (isset($_GET['param'])) $param = $_GET['param'];
$content = '';
if ($action == 'save')
{
$title = $_POST['title'];
$content = $_POST['content'];
file_put_contents($dir . '/' . $title, $content);
$previoustitle = $_POST['previoustitle'];
if ($title != $previoustitle)
{
rename($dir . '/' . $previoustitle, $dir . '/' . $previoustitle . '.del');
}
$action = 'open';
$param = $title;
}
@ -69,47 +70,82 @@
if ($action == 'open')
{
$now = date("Y-m-d H.i.s", time());
if (!$param)
{
$param = date("Y-m-d H.i.s", time());
$param = $now;
}
$content = '';
if (!file_exists($dir . '/' . $param))
{
file_put_contents($dir . '/' . $param, $content);
file_put_contents($dir . '/' . $param, '');
}
else
else if (!$content)
{
$content = file_get_contents($dir . '/' . $param);
}
$nblines = max(20, substr_count($content, "\r\n") + 1) * 2;
$lines = substr_count($content, "\r\n") + 1;
$words = $lines + substr_count($content, " ");
$rows = max(20, $lines) + 10;
$length = strlen($content);
echo '<form action="index.php" method="POST">
<div>
<a accesskey="h" href="' . $root . '">home</a>
<select name="action">
<option value="save">save</option>
<option value="delete">delete</option>
</select>
<input class="title" name="title" value="' . $param . '">
<input type="submit" name="go" value="go" accesskey="s">
</div>
<br>
<div><input class="title" name="title" value="' . $param . '"></div><br>
<div>
<textarea rows="' . $nblines. '" autofocus name="content" spellcheck="false">' . $content . '</textarea>
<textarea rows="' . $rows. '" autofocus name="content" spellcheck="false">' . $content . '</textarea>
</div>
<input hidden name="previoustitle" value="' . $param . '">
</form>';
$links = array();
if (preg_match_all('/\[\[(.*)\]\]/', $content, $links, PREG_SET_ORDER)
|| preg_match_all('/http(.*)[ \r]/', $content, $links, PREG_SET_ORDER))
{
echo '<div>Internal links:</div>';
foreach($links as $link)
{
echo '<div>
<a href="index.php?action=open&param=' . urlencode($link[1]) . '">' . $link[1] . '</a>
</div>';
}
echo '<br>';
}
$links = array();
if (preg_match_all('/http(.*)[ \r]/', $content, $links, PREG_SET_ORDER))
{
echo '<div>External links:</div>';
foreach($links as $link)
{
echo '<div>
<a target="_blank" href="http' . $link[1] . '">http' . $link[1] . '</a>
</div>';
}
echo '<br>';
}
echo '<span class="grey">' . $param . ' - ' . $now . ' - ' . $length . 'c - ' . $words . 'w - ' . $lines . 'l </span>';
}
else
{
echo '<form action="index.php" method="GET">
<input name="param" value="' . $param . '">
<a accesskey="h" href="' . $root . '">home</a>
<select name="action">
<option value="open">open</option>
<option value="filter">filter</option>
<option value="search">search</option>
<option ' . ($action == 'open' ? 'selected' : '') . ' value="open">open</option>
<option ' . ($action == 'filter' ? 'selected' : '') . ' value="filter">filter</option>
<option ' . ($action == 'search' ? 'selected' : '') . ' value="search">search</option>
<option ' . ($action == 'tags' ? 'selected' : '') . ' value="tags">tags</option>
</select>
<input autofocus name="param" value="' . $param . '">
<input accesskey="s" type="submit" value="go">
</form>';
echo '<br>';
@ -122,19 +158,43 @@
foreach($files as $path)
{
$tags = '';
$name = basename($path);
if (!str_ends_with($name, '.del')
&& ($action != 'filter' || str_contains($name, $param)))
if (!str_ends_with($name, '.del') && ($action != 'filter' || str_contains($name, $param)))
{
if ($action == 'search')
if ($action == 'search' || $action == 'tags')
{
$content = file_get_contents($path);
if (!str_contains($content, $param))
if (($action == 'search' && !str_contains($content, $param)) ||
($action == 'tags' && !preg_match('/tags:.*' . $param . '/', $content)))
{
continue;
}
}
echo '<div><a href=index.php?action=open&param=' . urlencode($name) . '>' . $name .'</a></div>';
echo '<div><a href=index.php?action=open&param=' . urlencode($name) . '>' . $name .'</a>';
if ($action == 'tags')
{
$tags = array();
$hastags = preg_match_all('/tags:(.*)/', $content, $tags, PREG_SET_ORDER);
if ($hastags)
{
$tagslist = explode(',', $tags[0][1]);
echo '<span class="grey"> ';
foreach ($tagslist as $tag)
{
$tag = trim($tag);
echo('<a class="grey" href="index.php?action=tags&param=' . $tag . '">' . $tag . '</a> ');
}
echo '</span>';
}
}
else if ($action == 'search')
{
$pos = strpos($content, $param);
echo '<span class="grey"> <b>' . $param . '</b>' . substr($content, $pos + strlen($param), 42) . '</span>';
}
echo'</div>';
}
}
echo '<br>';