gemini-php/src/Pango.php

219 lines
5.2 KiB
PHP
Raw Normal View History

2024-04-12 08:01:43 +03:00
<?php
declare(strict_types=1);
namespace Yggverse\Gemini;
class Pango
{
public static function fromGemtext(
string $gemtext
): string
{
2024-04-12 14:20:19 +03:00
return self::fromGemtextBody(
2024-04-12 08:01:43 +03:00
new \Yggverse\Gemini\Gemtext\Body(
$gemtext
)
);
}
2024-04-12 14:20:19 +03:00
public static function fromGemtextBody(
2024-04-12 08:01:43 +03:00
\Yggverse\Gemini\Gemtext\Body $body
): string
{
$lines = $body->getLines();
2024-04-12 14:20:19 +03:00
$raw = [];
2024-04-12 08:01:43 +03:00
$escaped = [];
2024-04-12 14:20:19 +03:00
// Code
$code = $body->getCode();
if (count($code) % 2 == 0) // make sure tags has pairs
{
$i = 1;
foreach ($code as $index => $capture)
{
// Replace code tags
if ($i % 2 == 0)
{
$lines[$index] = '</tt>';
// Skip code format inside the tags by raw registry
foreach (array_slice($lines, $offset, $index - $offset) as $start => $line)
{
$raw[$start + $offset] = $line;
}
}
else
{
if ($capture)
{
$lines[$index] = sprintf(
'<b>%s</b><tt>',
self::escape(
$capture
)
);
}
else
{
$lines[$index] = '<tt>';
}
$offset = $index + 1;
}
$escaped[] = $index;
$i++;
}
}
2024-04-12 09:46:12 +03:00
// H1
foreach ($body->getH1() as $index => $value)
2024-04-12 08:01:43 +03:00
{
2024-04-12 14:20:19 +03:00
if (!isset($raw[$index]))
{
$lines[$index] = sprintf(
'<span size="xx-large">%s</span>',
self::escape(
$value
)
);
$escaped[] = $index;
}
2024-04-12 08:01:43 +03:00
}
2024-04-12 09:46:12 +03:00
// H2
foreach ($body->getH2() as $index => $value)
2024-04-12 08:01:43 +03:00
{
2024-04-12 14:20:19 +03:00
if (!isset($raw[$index]))
{
$lines[$index] = sprintf(
'<span size="x-large">%s</span>',
self::escape(
$value
)
);
$escaped[] = $index;
}
2024-04-12 08:01:43 +03:00
}
2024-04-12 09:46:12 +03:00
// H3
foreach ($body->getH3() as $index => $value)
2024-04-12 08:01:43 +03:00
{
2024-04-12 14:20:19 +03:00
if (!isset($raw[$index]))
{
$lines[$index] = sprintf(
'<span size="large">%s</span>',
self::escape(
$value
)
);
$escaped[] = $index;
}
2024-04-12 08:01:43 +03:00
}
2024-04-12 14:20:19 +03:00
// Quote
foreach ($body->getQuote() as $index => $value)
2024-04-12 08:01:43 +03:00
{
2024-04-12 14:20:19 +03:00
if (!isset($raw[$index]))
{
$lines[$index] = sprintf(
'<i>%s</i>',
self::escape(
$value
)
);
$escaped[] = $index;
}
2024-04-12 09:46:12 +03:00
}
2024-04-14 07:50:59 +03:00
// Links
foreach ($body->getLinks() as $index => $line)
{
if (!isset($raw[$index]))
{
$link = new \Yggverse\Gemini\Gemtext\Link(
$line
);
if (!$address = $link->getAddress())
{
continue;
}
if (!$alt = $link->getAlt())
{
if (!$alt = $link->getDate())
{
$alt = $address;
}
}
if ($alt == $address)
{
$lines[$index] = sprintf(
'<a href="%s">%s</a>',
$address,
self::escape(
$alt
2024-04-16 19:46:39 +03:00
)
);
}
else
{
$lines[$index] = sprintf(
'<a href="%s" title="%s">%s</a>',
$address,
self::escape(
urldecode(
$address
)
),
self::escape(
$alt
)
);
}
2024-04-14 07:50:59 +03:00
$escaped[] = $index;
}
}
2024-04-12 14:20:19 +03:00
// Escape special chars for non escaped lines
foreach ($body->getLines() as $index => $value)
2024-04-12 09:46:12 +03:00
{
2024-04-12 14:20:19 +03:00
if (!in_array($index, $escaped))
{
$lines[$index] = self::escape(
2024-04-12 09:46:12 +03:00
$value
2024-04-12 14:20:19 +03:00
);
}
2024-04-12 08:01:43 +03:00
}
return implode(
PHP_EOL,
$lines
);
}
2024-04-12 09:36:27 +03:00
public static function escape(
string $value
): string
{
return htmlspecialchars(
$value
);
}
2024-04-12 08:01:43 +03:00
}