mirror of
https://github.com/YGGverse/gemini-php.git
synced 2025-03-13 06:01:58 +00:00
implement gemtext code format
This commit is contained in:
parent
3265add74a
commit
675cdf3ac4
@ -221,10 +221,10 @@ $pango = \Yggverse\Gemini\Pango::fromGemtext(
|
||||
);
|
||||
```
|
||||
|
||||
#### Pango::fromBody
|
||||
#### Pango::fromGemtextBody
|
||||
|
||||
``` php
|
||||
$pango = \Yggverse\Gemini\Pango::fromBody(
|
||||
$pango = \Yggverse\Gemini\Pango::fromGemtextBody(
|
||||
new \Yggverse\Gemini\Gemtext\Body(
|
||||
$gemtext
|
||||
)
|
||||
|
147
src/Pango.php
147
src/Pango.php
@ -10,87 +10,146 @@ class Pango
|
||||
string $gemtext
|
||||
): string
|
||||
{
|
||||
return self::fromBody(
|
||||
return self::fromGemtextBody(
|
||||
new \Yggverse\Gemini\Gemtext\Body(
|
||||
$gemtext
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function fromBody(
|
||||
public static function fromGemtextBody(
|
||||
\Yggverse\Gemini\Gemtext\Body $body
|
||||
): string
|
||||
{
|
||||
$lines = $body->getLines();
|
||||
|
||||
$raw = [];
|
||||
|
||||
$escaped = [];
|
||||
|
||||
// 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++;
|
||||
}
|
||||
}
|
||||
|
||||
// H1
|
||||
foreach ($body->getH1() as $index => $value)
|
||||
{
|
||||
$lines[$index] = sprintf(
|
||||
'<span size="xx-large">%s</span>',
|
||||
self::escape(
|
||||
$value
|
||||
)
|
||||
);
|
||||
if (!isset($raw[$index]))
|
||||
{
|
||||
$lines[$index] = sprintf(
|
||||
'<span size="xx-large">%s</span>',
|
||||
self::escape(
|
||||
$value
|
||||
)
|
||||
);
|
||||
|
||||
$escaped[] = $index;
|
||||
$escaped[] = $index;
|
||||
}
|
||||
}
|
||||
|
||||
// H2
|
||||
foreach ($body->getH2() as $index => $value)
|
||||
{
|
||||
$lines[$index] = sprintf(
|
||||
'<span size="x-large">%s</span>',
|
||||
self::escape(
|
||||
$value
|
||||
)
|
||||
);
|
||||
if (!isset($raw[$index]))
|
||||
{
|
||||
$lines[$index] = sprintf(
|
||||
'<span size="x-large">%s</span>',
|
||||
self::escape(
|
||||
$value
|
||||
)
|
||||
);
|
||||
|
||||
$escaped[] = $index;
|
||||
$escaped[] = $index;
|
||||
}
|
||||
}
|
||||
|
||||
// H3
|
||||
foreach ($body->getH3() as $index => $value)
|
||||
{
|
||||
$lines[$index] = sprintf(
|
||||
'<span size="large">%s</span>',
|
||||
self::escape(
|
||||
$value
|
||||
)
|
||||
);
|
||||
if (!isset($raw[$index]))
|
||||
{
|
||||
$lines[$index] = sprintf(
|
||||
'<span size="large">%s</span>',
|
||||
self::escape(
|
||||
$value
|
||||
)
|
||||
);
|
||||
|
||||
$escaped[] = $index;
|
||||
}
|
||||
|
||||
// H3
|
||||
foreach ($body->getH3() as $index => $value)
|
||||
{
|
||||
$lines[$index] = sprintf(
|
||||
'<span size="large">%s</span>',
|
||||
self::escape(
|
||||
$value
|
||||
)
|
||||
);
|
||||
|
||||
$escaped[] = $index;
|
||||
$escaped[] = $index;
|
||||
}
|
||||
}
|
||||
|
||||
// Quote
|
||||
foreach ($body->getQuote() as $index => $value)
|
||||
{
|
||||
$lines[$index] = sprintf(
|
||||
'<i>%s</i>',
|
||||
self::escape(
|
||||
$value
|
||||
)
|
||||
);
|
||||
if (!isset($raw[$index]))
|
||||
{
|
||||
$lines[$index] = sprintf(
|
||||
'<i>%s</i>',
|
||||
self::escape(
|
||||
$value
|
||||
)
|
||||
);
|
||||
|
||||
$escaped[] = $index;
|
||||
$escaped[] = $index;
|
||||
}
|
||||
}
|
||||
|
||||
// @TODO links, code
|
||||
// @TODO links
|
||||
|
||||
// Escape special chars for non escaped lines
|
||||
foreach ($body->getLines() as $index => $value)
|
||||
{
|
||||
if (!in_array($index, $escaped))
|
||||
{
|
||||
$lines[$index] = self::escape(
|
||||
$value
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return implode(
|
||||
PHP_EOL,
|
||||
|
Loading…
x
Reference in New Issue
Block a user