add pango markup converter

This commit is contained in:
yggverse 2024-04-12 08:01:43 +03:00
parent fc3b82a052
commit faac656ab1
2 changed files with 108 additions and 0 deletions

View File

@ -207,6 +207,28 @@ var_dump(
#### Link::getAlt
## Pango
Converter for GTK/Pango format
### Pango::fromGemtext
``` php
$pango = \Yggverse\Gemini\Pango::fromGemtext(
$gemtext
);
```
### Pango::fromBody
``` php
$pango = \Yggverse\Gemini\Pango::fromBody(
new \Yggverse\Gemini\Gemtext\Body(
$gemtext
)
);
```
## DokuWiki
Toolkit provides DokuWiki API for Gemini.

86
src/Pango.php Normal file
View 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
);
}
}