Browse Source

add pango markup converter

main
yggverse 5 months ago
parent
commit
faac656ab1
  1. 22
      README.md
  2. 86
      src/Pango.php

22
README.md

@ -207,6 +207,28 @@ var_dump(
#### Link::getAlt #### 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 ## DokuWiki
Toolkit provides DokuWiki API for Gemini. Toolkit provides DokuWiki API for Gemini.

86
src/Pango.php

@ -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…
Cancel
Save