59 lines
1.3 KiB
PHP
Executable File
59 lines
1.3 KiB
PHP
Executable File
#!/usr/bin/php
|
|
<?php
|
|
|
|
$VENDOR_DIR = "/../vendor/";
|
|
# ^^^^^^^ Path to the folder containing composer packages
|
|
|
|
require __DIR__ . $VENDOR_DIR . "autoload.php";
|
|
|
|
use andreskrey\Readability\Readability;
|
|
use andreskrey\Readability\Configuration;
|
|
use andreskrey\Readability\ParseException;
|
|
|
|
$readability = new Readability(new Configuration());
|
|
$target_url = urldecode(getenv("QUERY_STRING"));
|
|
|
|
|
|
if ($target_url == null) {
|
|
echo "10 Link an article to parse:\n";
|
|
die();
|
|
}
|
|
|
|
echo "20 text/gemini\n";
|
|
|
|
$html = file_get_contents($target_url);
|
|
|
|
try {
|
|
$readability->parse($html);
|
|
} catch (ParseException $e) {
|
|
echo "Could not parse " . $target_url . ".\n";
|
|
echo $ENDLINK;
|
|
die();
|
|
}
|
|
|
|
$title = $readability->getTitle();
|
|
$author = $readability->getAuthor();
|
|
|
|
if ($title === null) { $title = ""; }
|
|
else { $title = "# " . $title . "\n"; }
|
|
|
|
if ($author === null) { $author = "\n"; }
|
|
else { $author = "\n## By " . $author . "\n\n"; }
|
|
|
|
echo $title;
|
|
echo $author;
|
|
|
|
$dom = $readability->getDOMDocument();
|
|
$paragraphs = $dom->getElementsByTagName("p");
|
|
$images = $readability->getImages();
|
|
|
|
foreach($paragraphs as $paragraph) {
|
|
echo $paragraph->nodeValue . "\n\n";
|
|
}
|
|
|
|
echo "Images\n";
|
|
foreach($images as $image) {
|
|
# echo "=> mir?" . $image . " Image\n";
|
|
echo "=> " . $image . " Image\n";
|
|
}
|