Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/MacroReplacer/AdfPanelReplacer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Artemeon\Confluence\MacroReplacer;

/**
* Replace <ac:adf-node type="panel"> with <div class="panel panel-{type}">
*/
class AdfPanelReplacer implements MacroReplacerInterface
{
public function replace(string $haystack): string
{
$result = preg_replace_callback(
'/<ac:adf-node\s+type="panel"[^>]*>(.*?)<\/ac:adf-node>/is',
function ($match) {
$content = $match[1];

// Extract panel-type
preg_match('/<ac:adf-attribute key="panel-type">(.*?)<\/ac:adf-attribute>/is', $content, $typeMatch);
$panelType = strtolower($typeMatch[1] ?? 'custom');

// Extract inner content
preg_match('/<ac:adf-content[^>]*>(.*?)<\/ac:adf-content>/is', $content, $bodyMatch);
$body = $bodyMatch[1] ?? $content;

return sprintf('<div class="documentation-panel-%s"><div>%s</div></div>', $panelType, $body);
},
$haystack
);

$result = preg_replace(
'/<ac:adf-extension[^>]*>(.*?)<\/ac:adf-extension>/is',
'$1',
$result
);

return $result;
}
}
39 changes: 39 additions & 0 deletions src/MacroReplacer/GenericPanelReplacer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Artemeon\Confluence\MacroReplacer;

/**
* Replace <ac:structured-macro ac:name="panel"> (custom panels)
* with <div class="panel panel-custom" data-bgcolor="..." data-icon="...">
*/
class GenericPanelReplacer implements MacroReplacerInterface
{
public function replace(string $haystack): string
{
return preg_replace_callback(
'/<ac:structured-macro\s+ac:name="panel"[^>]*>(.*?)<\/ac:structured-macro>/is',
function ($match) {
$content = $match[1];

// Extract parameters
preg_match('/<ac:parameter ac:name="bgColor">#(.*?)<\/ac:parameter>/is', $content, $bgMatch);
preg_match('/<ac:parameter ac:name="panelIcon">:(.*?):<\/ac:parameter>/is', $content, $iconMatch);
preg_match('/<ac:rich-text-body[^>]*>(.*?)<\/ac:rich-text-body>/is', $content, $bodyMatch);

$bgColor = $bgMatch[1] ?? '';
$icon = $iconMatch[1] ?? '';
$body = $bodyMatch[1] ?? $content;

return sprintf(
'<div class="documentation-panel-custom" panel-color="%s" panel-icon="%s"><div>%s</div></div>',
htmlspecialchars($bgColor, ENT_QUOTES),
htmlspecialchars($icon, ENT_QUOTES),
$body
);
},
$haystack
);
}
}
53 changes: 29 additions & 24 deletions src/MacroReplacer/StructuredMacroReplacer.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
<?php

declare(strict_types=1);

namespace Artemeon\Confluence\MacroReplacer;

/**
* Replace <ac:structured-macro> with <div> with class
*/
class StructuredMacroReplacer implements MacroReplacerInterface
{
public function replace(string $haystack): string
{
return preg_replace_callback(
'/<ac:structured-macro\s+ac:name="([^"]+)"[^>]*>(.*?)<\/ac:structured-macro>/is',
function ($match) {
$macroName = $match[1];
$macroContent = $match[2];
return '<div class="' . $macroName . '">' . $macroContent . '</div>';
},
$haystack
);
}
}
<?php

declare(strict_types=1);

namespace Artemeon\Confluence\MacroReplacer;

/**
* Replace <ac:structured-macro> with <div> with class
*/
class StructuredMacroReplacer implements MacroReplacerInterface
{
public function replace(string $haystack): string
{
return preg_replace_callback(
'/<ac:structured-macro\s+ac:name="(info|note|warning|error|success)"[^>]*>(.*?)<\/ac:structured-macro>/is',
function ($match) {
$macroName = $match[1];
$macroContent = $match[2];

if (preg_match('/<ac:rich-text-body[^>]*>(.*?)<\/ac:rich-text-body>/is', $macroContent, $inner)) {
$macroContent = $inner[1];
}

return sprintf('<div class="documentation-panel-%s"><div>%s</div></div>', $macroName, $macroContent);
},
$haystack
);
}
}