Browse Source

implement gemtext code format

main
yggverse 5 months ago
parent
commit
675cdf3ac4
  1. 4
      README.md
  2. 85
      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
) )

85
src/Pango.php

@ -10,39 +10,77 @@ 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 = [];
// H1 // Code
foreach ($body->getH1() as $index => $value) $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( $lines[$index] = sprintf(
'<span size="xx-large">%s</span>', '<b>%s</b><tt>',
self::escape( self::escape(
$value $capture
) )
); );
}
else
{
$lines[$index] = '<tt>';
}
$offset = $index + 1;
}
$escaped[] = $index; $escaped[] = $index;
$i++;
}
} }
// H2 // H1
foreach ($body->getH2() as $index => $value) foreach ($body->getH1() as $index => $value)
{
if (!isset($raw[$index]))
{ {
$lines[$index] = sprintf( $lines[$index] = sprintf(
'<span size="x-large">%s</span>', '<span size="xx-large">%s</span>',
self::escape( self::escape(
$value $value
) )
@ -50,12 +88,15 @@ class Pango
$escaped[] = $index; $escaped[] = $index;
} }
}
// H3 // H2
foreach ($body->getH3() as $index => $value) foreach ($body->getH2() as $index => $value)
{
if (!isset($raw[$index]))
{ {
$lines[$index] = sprintf( $lines[$index] = sprintf(
'<span size="large">%s</span>', '<span size="x-large">%s</span>',
self::escape( self::escape(
$value $value
) )
@ -63,9 +104,12 @@ class Pango
$escaped[] = $index; $escaped[] = $index;
} }
}
// H3 // H3
foreach ($body->getH3() as $index => $value) foreach ($body->getH3() as $index => $value)
{
if (!isset($raw[$index]))
{ {
$lines[$index] = sprintf( $lines[$index] = sprintf(
'<span size="large">%s</span>', '<span size="large">%s</span>',
@ -76,9 +120,12 @@ class Pango
$escaped[] = $index; $escaped[] = $index;
} }
}
// Quote // Quote
foreach ($body->getQuote() as $index => $value) foreach ($body->getQuote() as $index => $value)
{
if (!isset($raw[$index]))
{ {
$lines[$index] = sprintf( $lines[$index] = sprintf(
'<i>%s</i>', '<i>%s</i>',
@ -89,8 +136,20 @@ class Pango
$escaped[] = $index; $escaped[] = $index;
} }
}
// @TODO links
// @TODO links, code // 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( return implode(
PHP_EOL, PHP_EOL,

Loading…
Cancel
Save