diff --git a/README.md b/README.md
index a03dfcb..a38d071 100644
--- a/README.md
+++ b/README.md
@@ -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
)
diff --git a/src/Pango.php b/src/Pango.php
index fc847b4..a3855db 100644
--- a/src/Pango.php
+++ b/src/Pango.php
@@ -10,88 +10,147 @@ 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] = '';
+
+ // 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(
+ '%s',
+ self::escape(
+ $capture
+ )
+ );
+ }
+
+ else
+ {
+ $lines[$index] = '';
+ }
+
+ $offset = $index + 1;
+ }
+
+ $escaped[] = $index;
+
+ $i++;
+ }
+ }
+
// H1
foreach ($body->getH1() as $index => $value)
{
- $lines[$index] = sprintf(
- '%s',
- self::escape(
- $value
- )
- );
-
- $escaped[] = $index;
+ if (!isset($raw[$index]))
+ {
+ $lines[$index] = sprintf(
+ '%s',
+ self::escape(
+ $value
+ )
+ );
+
+ $escaped[] = $index;
+ }
}
// H2
foreach ($body->getH2() as $index => $value)
{
- $lines[$index] = sprintf(
- '%s',
- self::escape(
- $value
- )
- );
-
- $escaped[] = $index;
+ if (!isset($raw[$index]))
+ {
+ $lines[$index] = sprintf(
+ '%s',
+ self::escape(
+ $value
+ )
+ );
+
+ $escaped[] = $index;
+ }
}
// H3
foreach ($body->getH3() as $index => $value)
{
- $lines[$index] = sprintf(
- '%s',
- self::escape(
- $value
- )
- );
-
- $escaped[] = $index;
+ if (!isset($raw[$index]))
+ {
+ $lines[$index] = sprintf(
+ '%s',
+ self::escape(
+ $value
+ )
+ );
+
+ $escaped[] = $index;
+ }
}
- // H3
- foreach ($body->getH3() as $index => $value)
+ // Quote
+ foreach ($body->getQuote() as $index => $value)
{
- $lines[$index] = sprintf(
- '%s',
- self::escape(
- $value
- )
- );
-
- $escaped[] = $index;
+ if (!isset($raw[$index]))
+ {
+ $lines[$index] = sprintf(
+ '%s',
+ self::escape(
+ $value
+ )
+ );
+
+ $escaped[] = $index;
+ }
}
- // Quote
- foreach ($body->getQuote() as $index => $value)
+ // @TODO links
+
+ // Escape special chars for non escaped lines
+ foreach ($body->getLines() as $index => $value)
{
- $lines[$index] = sprintf(
- '%s',
- self::escape(
+ if (!in_array($index, $escaped))
+ {
+ $lines[$index] = self::escape(
$value
- )
- );
-
- $escaped[] = $index;
+ );
+ }
}
- // @TODO links, code
-
return implode(
PHP_EOL,
$lines