mirror of
https://github.com/YGGverse/gemini-php.git
synced 2026-03-31 17:05:29 +00:00
add pango markup converter
This commit is contained in:
parent
fc3b82a052
commit
faac656ab1
2 changed files with 108 additions and 0 deletions
86
src/Pango.php
Normal file
86
src/Pango.php
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yggverse\Gemini;
|
||||
|
||||
class Pango
|
||||
{
|
||||
public static function fromGemtext(
|
||||
string $gemtext
|
||||
): string
|
||||
{
|
||||
return self::fromBody(
|
||||
new \Yggverse\Gemini\Gemtext\Body(
|
||||
$gemtext
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function fromBody(
|
||||
\Yggverse\Gemini\Gemtext\Body $body
|
||||
): string
|
||||
{
|
||||
// Format body
|
||||
$lines = $body->getLines();
|
||||
|
||||
$escaped = [];
|
||||
|
||||
/// Format H1
|
||||
foreach ($body->getH1() as $index => $h1)
|
||||
{
|
||||
$lines[$index] = sprintf(
|
||||
'<span size="xx-large">%s</span>',
|
||||
htmlentities(
|
||||
$h1
|
||||
)
|
||||
);
|
||||
|
||||
$escaped[] = $index;
|
||||
}
|
||||
|
||||
/// Format H2
|
||||
foreach ($body->getH2() as $index => $h2)
|
||||
{
|
||||
$lines[$index] = sprintf(
|
||||
'<span size="x-large">%s</span>',
|
||||
htmlentities(
|
||||
$h2
|
||||
)
|
||||
);
|
||||
|
||||
$escaped[] = $index;
|
||||
}
|
||||
|
||||
/// Format H3
|
||||
foreach ($body->getH3() as $index => $h3)
|
||||
{
|
||||
$lines[$index] = sprintf(
|
||||
'<span size="large">%s</span>',
|
||||
htmlentities(
|
||||
$h3
|
||||
)
|
||||
);
|
||||
|
||||
$escaped[] = $index;
|
||||
}
|
||||
|
||||
/// Escape entities
|
||||
foreach ($lines as $index => $line)
|
||||
{
|
||||
if (!in_array($index, $escaped))
|
||||
{
|
||||
$lines[$index] = htmlentities(
|
||||
$line
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// @TODO links, code, escape entities
|
||||
|
||||
return implode(
|
||||
PHP_EOL,
|
||||
$lines
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue