Browse Source

implement gemtext code format

main
yggverse 7 months ago
parent
commit
675cdf3ac4
  1. 4
      README.md
  2. 153
      src/Pango.php

4
README.md

@ -221,10 +221,10 @@ $pango = \Yggverse\Gemini\Pango::fromGemtext(
); );
``` ```
#### Pango::fromBody #### Pango::fromGemtextBody
``` php ``` php
$pango = \Yggverse\Gemini\Pango::fromBody( $pango = \Yggverse\Gemini\Pango::fromGemtextBody(
new \Yggverse\Gemini\Gemtext\Body( new \Yggverse\Gemini\Gemtext\Body(
$gemtext $gemtext
) )

153
src/Pango.php

@ -10,88 +10,147 @@ class Pango
string $gemtext string $gemtext
): string ): string
{ {
return self::fromBody( return self::fromGemtextBody(
new \Yggverse\Gemini\Gemtext\Body( new \Yggverse\Gemini\Gemtext\Body(
$gemtext $gemtext
) )
); );
} }
public static function fromBody( public static function fromGemtextBody(
\Yggverse\Gemini\Gemtext\Body $body \Yggverse\Gemini\Gemtext\Body $body
): string ): string
{ {
$lines = $body->getLines(); $lines = $body->getLines();
$raw = [];
$escaped = []; $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 // H1
foreach ($body->getH1() as $index => $value) foreach ($body->getH1() as $index => $value)
{ {
$lines[$index] = sprintf( if (!isset($raw[$index]))
'<span size="xx-large">%s</span>', {
self::escape( $lines[$index] = sprintf(
$value '<span size="xx-large">%s</span>',
) self::escape(
); $value
)
$escaped[] = $index; );
$escaped[] = $index;
}
} }
// H2 // H2
foreach ($body->getH2() as $index => $value) foreach ($body->getH2() as $index => $value)
{ {
$lines[$index] = sprintf( if (!isset($raw[$index]))
'<span size="x-large">%s</span>', {
self::escape( $lines[$index] = sprintf(
$value '<span size="x-large">%s</span>',
) self::escape(
); $value
)
$escaped[] = $index; );
$escaped[] = $index;
}
} }
// H3 // H3
foreach ($body->getH3() as $index => $value) foreach ($body->getH3() as $index => $value)
{ {
$lines[$index] = sprintf( if (!isset($raw[$index]))
'<span size="large">%s</span>', {
self::escape( $lines[$index] = sprintf(
$value '<span size="large">%s</span>',
) self::escape(
); $value
)
$escaped[] = $index; );
$escaped[] = $index;
}
} }
// H3 // Quote
foreach ($body->getH3() as $index => $value) foreach ($body->getQuote() as $index => $value)
{ {
$lines[$index] = sprintf( if (!isset($raw[$index]))
'<span size="large">%s</span>', {
self::escape( $lines[$index] = sprintf(
$value '<i>%s</i>',
) self::escape(
); $value
)
$escaped[] = $index; );
$escaped[] = $index;
}
} }
// Quote // @TODO links
foreach ($body->getQuote() as $index => $value)
// Escape special chars for non escaped lines
foreach ($body->getLines() as $index => $value)
{ {
$lines[$index] = sprintf( if (!in_array($index, $escaped))
'<i>%s</i>', {
self::escape( $lines[$index] = self::escape(
$value $value
) );
); }
$escaped[] = $index;
} }
// @TODO links, code
return implode( return implode(
PHP_EOL, PHP_EOL,
$lines $lines

Loading…
Cancel
Save