Skip to content

Commit

Permalink
HTML export
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienHarper committed Jun 29, 2022
1 parent 3cd2a68 commit 910bef0
Show file tree
Hide file tree
Showing 59 changed files with 1,539 additions and 22 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"symfony/var-dumper": "^4.0|^5.0|^6.0"
"symfony/var-dumper": "^5.0|^6.0"
},
"scripts": {
"test": "php -d pcov.enabled=1 ./vendor/bin/phpunit --colors=always",
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ parameters:
ignoreErrors:
# false positives
- '~Class DH\\Adf\\.* constructor invoked with \d parameters?, .* required\.~'
- '~Parameter \#1 \$.* of class DH\\Adf\\Node\\.* constructor expects .*, DH\\Adf\\Node\\BlockNode\|null given\.~'
- '~Parameter \#1 \$.* of class DH\\Adf\\.* constructor expects .*, DH\\Adf\\Node\\(Block|Inline)Node(\|null)? given\.~'
10 changes: 10 additions & 0 deletions src/Exporter/ExporterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace DH\Adf\Exporter;

interface ExporterInterface
{
public function export(): string;
}
17 changes: 17 additions & 0 deletions src/Exporter/Html/Block/BlockquoteExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace DH\Adf\Exporter\Html\Block;

use DH\Adf\Exporter\Html\HtmlExporter;
use DH\Adf\Node\Block\Blockquote;

class BlockquoteExporter extends HtmlExporter
{
public function __construct(Blockquote $node)
{
parent::__construct($node);
$this->tags = ['<blockquote>', '</blockquote>'];
}
}
17 changes: 17 additions & 0 deletions src/Exporter/Html/Block/BulletListExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace DH\Adf\Exporter\Html\Block;

use DH\Adf\Exporter\Html\HtmlExporter;
use DH\Adf\Node\Block\BulletList;

class BulletListExporter extends HtmlExporter
{
public function __construct(BulletList $node)
{
parent::__construct($node);
$this->tags = ['<ul>', '</ul>'];
}
}
17 changes: 17 additions & 0 deletions src/Exporter/Html/Block/CodeBlockExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace DH\Adf\Exporter\Html\Block;

use DH\Adf\Exporter\Html\HtmlExporter;
use DH\Adf\Node\Block\CodeBlock;

class CodeBlockExporter extends HtmlExporter
{
public function __construct(CodeBlock $node)
{
parent::__construct($node);
$this->tags = ['<pre class="'.$node->getLanguage().'">', '</pre>'];
}
}
17 changes: 17 additions & 0 deletions src/Exporter/Html/Block/DocumentExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace DH\Adf\Exporter\Html\Block;

use DH\Adf\Exporter\Html\HtmlExporter;
use DH\Adf\Node\Block\Document;

class DocumentExporter extends HtmlExporter
{
public function __construct(Document $node)
{
parent::__construct($node);
$this->tags = ['<div class="adf-container">', '</div>'];
}
}
17 changes: 17 additions & 0 deletions src/Exporter/Html/Block/ExpandExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace DH\Adf\Exporter\Html\Block;

use DH\Adf\Exporter\Html\HtmlExporter;
use DH\Adf\Node\Block\Expand;

class ExpandExporter extends HtmlExporter
{
public function __construct(Expand $node)
{
parent::__construct($node);
$this->tags = ['<div class="adf-expand"><div class="adf-expand-title">'.$node->getTitle().'</div><div class="adf-expand-body">', '</div></div>'];
}
}
20 changes: 20 additions & 0 deletions src/Exporter/Html/Block/HeadingExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace DH\Adf\Exporter\Html\Block;

use DH\Adf\Exporter\Html\HtmlExporter;
use DH\Adf\Node\Block\Heading;

class HeadingExporter extends HtmlExporter
{
public function __construct(Heading $node)
{
parent::__construct($node);
$this->tags = [
'<h'.$node->getLevel().'>',
'</h'.$node->getLevel().'>',
];
}
}
17 changes: 17 additions & 0 deletions src/Exporter/Html/Block/MediaGroupExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace DH\Adf\Exporter\Html\Block;

use DH\Adf\Exporter\Html\HtmlExporter;
use DH\Adf\Node\Block\MediaGroup;

class MediaGroupExporter extends HtmlExporter
{
public function __construct(MediaGroup $node)
{
parent::__construct($node);
$this->tags = ['<div class="adf-mediagroup">', '</div>'];
}
}
17 changes: 17 additions & 0 deletions src/Exporter/Html/Block/MediaSingleExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace DH\Adf\Exporter\Html\Block;

use DH\Adf\Exporter\Html\HtmlExporter;
use DH\Adf\Node\Block\MediaSingle;

class MediaSingleExporter extends HtmlExporter
{
public function __construct(MediaSingle $node)
{
parent::__construct($node);
$this->tags = ['<div class="adf-mediasingle">', '</div>'];
}
}
17 changes: 17 additions & 0 deletions src/Exporter/Html/Block/OrderedListExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace DH\Adf\Exporter\Html\Block;

use DH\Adf\Exporter\Html\HtmlExporter;
use DH\Adf\Node\Block\OrderedList;

class OrderedListExporter extends HtmlExporter
{
public function __construct(OrderedList $node)
{
parent::__construct($node);
$this->tags = ['<ol>', '</ol>'];
}
}
54 changes: 54 additions & 0 deletions src/Exporter/Html/Block/PanelExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace DH\Adf\Exporter\Html\Block;

use DH\Adf\Exporter\Html\HtmlExporter;
use DH\Adf\Node\Block\Panel;

class PanelExporter extends HtmlExporter
{
public function export(): string
{
\assert($this->node instanceof Panel);

switch ($this->node->getPanelType()) {
case Panel::ERROR:
$icon = '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" role="presentation"><path fill-rule="evenodd" clip-rule="evenodd" d="M13.8562 11.9112L16.5088 9.26C16.7433 9.02545 16.8751 8.70733 16.8751 8.37563C16.8751 8.04392 16.7433 7.7258 16.5088 7.49125C16.2742 7.2567 15.9561 7.12493 15.6244 7.12493C15.2927 7.12493 14.9746 7.2567 14.74 7.49125L12.09 10.1438L9.4375 7.49125C9.20295 7.25686 8.8849 7.12526 8.55331 7.12537C8.22172 7.12549 7.90376 7.25732 7.66937 7.49188C7.43499 7.72643 7.30338 8.04448 7.3035 8.37607C7.30361 8.70766 7.43545 9.02561 7.67 9.26L10.32 11.91L7.67 14.5625C7.4423 14.7983 7.31631 15.114 7.31916 15.4418C7.32201 15.7695 7.45347 16.083 7.68523 16.3148C7.91699 16.5465 8.2305 16.678 8.55825 16.6808C8.88599 16.6837 9.20175 16.5577 9.4375 16.33L12.0888 13.68L14.74 16.33C14.8561 16.4461 14.9939 16.5383 15.1455 16.6012C15.2972 16.664 15.4597 16.6964 15.6239 16.6965C15.7881 16.6966 15.9507 16.6643 16.1024 16.6015C16.2541 16.5387 16.392 16.4467 16.5081 16.3306C16.6243 16.2146 16.7164 16.0768 16.7793 15.9251C16.8422 15.7734 16.8746 15.6109 16.8746 15.4467C16.8747 15.2825 16.8424 15.1199 16.7796 14.9682C16.7168 14.8165 16.6248 14.6786 16.5088 14.5625L13.8562 11.9112V11.9112ZM12 22C9.34784 22 6.8043 20.9464 4.92893 19.0711C3.05357 17.1957 2 14.6522 2 12C2 9.34784 3.05357 6.8043 4.92893 4.92893C6.8043 3.05357 9.34784 2 12 2C14.6522 2 17.1957 3.05357 19.0711 4.92893C20.9464 6.8043 22 9.34784 22 12C22 14.6522 20.9464 17.1957 19.0711 19.0711C17.1957 20.9464 14.6522 22 12 22V22Z" fill="currentColor"></path></svg>';

break;

case Panel::WARNING:
$icon = '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" role="presentation"><path fill-rule="evenodd" clip-rule="evenodd" d="M13.4897 4.34592L21.8561 18.8611C21.9525 19.0288 22.0021 19.2181 21.9999 19.4101C21.9977 19.6021 21.9438 19.7903 21.8435 19.9559C21.7432 20.1215 21.6001 20.2588 21.4282 20.3542C21.2563 20.4497 21.0616 20.4999 20.8636 20.5H3.13707C2.93882 20.5 2.74401 20.4498 2.57196 20.3543C2.39992 20.2588 2.25663 20.1213 2.15631 19.9556C2.05598 19.7898 2.00212 19.6015 2.00006 19.4093C1.998 19.2171 2.04782 19.0278 2.14456 18.86L10.5121 4.34592C10.6602 4.08939 10.8762 3.87577 11.1377 3.72708C11.3993 3.57838 11.6971 3.5 12.0003 3.5C12.3036 3.5 12.6013 3.57838 12.8629 3.72708C13.1245 3.87577 13.3404 4.08939 13.4885 4.34592H13.4897ZM12.0003 7.82538C11.8232 7.82537 11.6482 7.86212 11.4869 7.93317C11.3257 8.00423 11.182 8.10793 11.0656 8.2373C10.9492 8.36668 10.8627 8.51872 10.8119 8.68321C10.7611 8.8477 10.7473 9.02083 10.7713 9.19093L11.3546 13.3416C11.3754 13.4933 11.4523 13.6326 11.5711 13.7334C11.6899 13.8343 11.8424 13.8899 12.0003 13.8899C12.1582 13.8899 12.3107 13.8343 12.4295 13.7334C12.5483 13.6326 12.6253 13.4933 12.6461 13.3416L13.2293 9.19093C13.2533 9.02083 13.2395 8.8477 13.1887 8.68321C13.138 8.51872 13.0515 8.36668 12.935 8.2373C12.8186 8.10793 12.6749 8.00423 12.5137 7.93317C12.3525 7.86212 12.1774 7.82537 12.0003 7.82538V7.82538ZM12.0003 17.3369C12.3395 17.3369 12.6649 17.2062 12.9047 16.9737C13.1446 16.7412 13.2793 16.4258 13.2793 16.0969C13.2793 15.7681 13.1446 15.4527 12.9047 15.2202C12.6649 14.9877 12.3395 14.857 12.0003 14.857C11.6611 14.857 11.3358 14.9877 11.0959 15.2202C10.8561 15.4527 10.7213 15.7681 10.7213 16.0969C10.7213 16.4258 10.8561 16.7412 11.0959 16.9737C11.3358 17.2062 11.6611 17.3369 12.0003 17.3369V17.3369Z" fill="currentColor"></path></svg>';

break;

case Panel::SUCCESS:
$icon = '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" role="presentation"><path fill-rule="evenodd" clip-rule="evenodd" d="M12 22C9.34784 22 6.8043 20.9464 4.92893 19.0711C3.05357 17.1957 2 14.6522 2 12C2 9.34784 3.05357 6.8043 4.92893 4.92893C6.8043 3.05357 9.34784 2 12 2C14.6522 2 17.1957 3.05357 19.0711 4.92893C20.9464 6.8043 22 9.34784 22 12C22 14.6522 20.9464 17.1957 19.0711 19.0711C17.1957 20.9464 14.6522 22 12 22V22ZM13.705 8.295L11.015 13.4325L9.08625 11.695C8.9642 11.5852 8.82172 11.5005 8.66694 11.4457C8.51216 11.391 8.3481 11.3672 8.18415 11.3759C8.0202 11.3845 7.85955 11.4254 7.71139 11.4961C7.56322 11.5669 7.43044 11.6661 7.32063 11.7881C7.21081 11.9102 7.1261 12.0527 7.07135 12.2074C7.0166 12.3622 6.99287 12.5263 7.00152 12.6902C7.01016 12.8542 7.05102 13.0148 7.12175 13.163C7.19248 13.3112 7.2917 13.4439 7.41375 13.5538L10.5388 16.3663C10.6803 16.4938 10.8492 16.5872 11.0325 16.6395C11.2157 16.6917 11.4085 16.7014 11.596 16.6678C11.7836 16.6341 11.9609 16.558 12.1146 16.4453C12.2682 16.3326 12.3941 16.1863 12.4825 16.0175L15.92 9.455C16.0738 9.16127 16.1047 8.81847 16.0057 8.502C15.9068 8.18553 15.6862 7.92133 15.3925 7.7675C15.0988 7.61367 14.756 7.58283 14.4395 7.68176C14.123 7.78068 13.8588 8.00127 13.705 8.295V8.295Z" fill="currentColor"></path></svg>';

break;

case Panel::NOTE:
$icon = '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" role="presentation"><path fill-rule="evenodd" clip-rule="evenodd" d="M7 2H17C17.663 2 18.2989 2.26339 18.7678 2.73223C19.2366 3.20107 19.5 3.83696 19.5 4.5V19.5C19.5 20.163 19.2366 20.7989 18.7678 21.2678C18.2989 21.7366 17.663 22 17 22H7C6.33696 22 5.70107 21.7366 5.23223 21.2678C4.76339 20.7989 4.5 20.163 4.5 19.5V4.5C4.5 3.83696 4.76339 3.20107 5.23223 2.73223C5.70107 2.26339 6.33696 2 7 2ZM8.875 7C8.70924 7 8.55027 7.06585 8.43306 7.18306C8.31585 7.30027 8.25 7.45924 8.25 7.625V8.875C8.25 9.04076 8.31585 9.19973 8.43306 9.31694C8.55027 9.43415 8.70924 9.5 8.875 9.5H15.125C15.2908 9.5 15.4497 9.43415 15.5669 9.31694C15.6842 9.19973 15.75 9.04076 15.75 8.875V7.625C15.75 7.45924 15.6842 7.30027 15.5669 7.18306C15.4497 7.06585 15.2908 7 15.125 7H8.875ZM8.875 12C8.70924 12 8.55027 12.0658 8.43306 12.1831C8.31585 12.3003 8.25 12.4592 8.25 12.625V13.875C8.25 14.0408 8.31585 14.1997 8.43306 14.3169C8.55027 14.4342 8.70924 14.5 8.875 14.5H12.625C12.7908 14.5 12.9497 14.4342 13.0669 14.3169C13.1842 14.1997 13.25 14.0408 13.25 13.875V12.625C13.25 12.4592 13.1842 12.3003 13.0669 12.1831C12.9497 12.0658 12.7908 12 12.625 12H8.875Z" fill="currentColor"></path></svg>';

break;

case Panel::INFO:
default:
$icon = '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" role="presentation"><path fill-rule="evenodd" clip-rule="evenodd" d="M12 22C9.34784 22 6.8043 20.9464 4.92893 19.0711C3.05357 17.1957 2 14.6522 2 12C2 9.34784 3.05357 6.8043 4.92893 4.92893C6.8043 3.05357 9.34784 2 12 2C14.6522 2 17.1957 3.05357 19.0711 4.92893C20.9464 6.8043 22 9.34784 22 12C22 14.6522 20.9464 17.1957 19.0711 19.0711C17.1957 20.9464 14.6522 22 12 22V22ZM12 11.375C11.6685 11.375 11.3505 11.5067 11.1161 11.7411C10.8817 11.9755 10.75 12.2935 10.75 12.625V15.75C10.75 16.0815 10.8817 16.3995 11.1161 16.6339C11.3505 16.8683 11.6685 17 12 17C12.3315 17 12.6495 16.8683 12.8839 16.6339C13.1183 16.3995 13.25 16.0815 13.25 15.75V12.625C13.25 12.2935 13.1183 11.9755 12.8839 11.7411C12.6495 11.5067 12.3315 11.375 12 11.375ZM12 9.96875C12.4558 9.96875 12.893 9.78767 13.2153 9.46534C13.5377 9.14301 13.7188 8.70584 13.7188 8.25C13.7188 7.79416 13.5377 7.35699 13.2153 7.03466C12.893 6.71233 12.4558 6.53125 12 6.53125C11.5442 6.53125 11.107 6.71233 10.7847 7.03466C10.4623 7.35699 10.2812 7.79416 10.2812 8.25C10.2812 8.70584 10.4623 9.14301 10.7847 9.46534C11.107 9.78767 11.5442 9.96875 12 9.96875Z" fill="currentColor"></path></svg>';

break;
}

$outputs = [];
foreach ($this->node->getContent() as $child) {
$class = self::NODE_MAPPING[$child::class];
$exporter = new $class($child);
$outputs[] = $exporter->export();
}
$output = implode('', $outputs);

return '<div class="adf-panel adf-panel-'.mb_strtolower($this->node->getPanelType()).'"><div class="adf-panel-icon"><span role="img" aria-label="Panel '.$this->node->getPanelType().'">'.$icon.'</span></div><div class="adf-panel-content">'.$output.'</div></div>';
}
}
17 changes: 17 additions & 0 deletions src/Exporter/Html/Block/ParagraphExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace DH\Adf\Exporter\Html\Block;

use DH\Adf\Exporter\Html\HtmlExporter;
use DH\Adf\Node\Block\Paragraph;

class ParagraphExporter extends HtmlExporter
{
public function __construct(Paragraph $node)
{
parent::__construct($node);
$this->tags = ['<p>', '</p>'];
}
}
17 changes: 17 additions & 0 deletions src/Exporter/Html/Block/RuleExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace DH\Adf\Exporter\Html\Block;

use DH\Adf\Exporter\Html\HtmlExporter;
use DH\Adf\Node\Block\Rule;

class RuleExporter extends HtmlExporter
{
public function __construct(Rule $node)
{
parent::__construct($node);
$this->tags = ['<hr/>'];
}
}
18 changes: 18 additions & 0 deletions src/Exporter/Html/Block/TableExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace DH\Adf\Exporter\Html\Block;

use DH\Adf\Exporter\Html\HtmlExporter;
use DH\Adf\Node\Block\Table;

class TableExporter extends HtmlExporter
{
public function __construct(Table $node)
{
parent::__construct($node);
// TODO: implement numbered rows
$this->tags = [sprintf('<table class="adf-table-%s"><tbody>', $node->getLayout()), '</tbody></table>'];
}
}
17 changes: 17 additions & 0 deletions src/Exporter/Html/Child/ListItemExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace DH\Adf\Exporter\Html\Child;

use DH\Adf\Exporter\Html\HtmlExporter;
use DH\Adf\Node\Child\ListItem;

class ListItemExporter extends HtmlExporter
{
public function __construct(ListItem $node)
{
parent::__construct($node);
$this->tags = ['<li>', '</li>'];
}
}
19 changes: 19 additions & 0 deletions src/Exporter/Html/Child/MediaExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace DH\Adf\Exporter\Html\Child;

use DH\Adf\Exporter\Html\HtmlExporter;
use DH\Adf\Node\Child\Media;

class MediaExporter extends HtmlExporter
{
public function __construct(Media $node)
{
parent::__construct($node);
// TODO: finalize HTML construct/support other types of media, only IMG supported as of now
$this->tags = ['<div class="adf-media"><!--'.$this->node->toJson().'--><p>Atlassian Media API is not publicly available at the moment.</p>', '</div>'];
// $this->tags = ['<div class="adf-media"><!-- '.$this->node->toJson().' --><img src="blob:https://odandb.atlassian.net/'.$this->node->getId().'">', '</div>'];
}
}
33 changes: 33 additions & 0 deletions src/Exporter/Html/Child/TableCellExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace DH\Adf\Exporter\Html\Child;

use DH\Adf\Exporter\Html\HtmlExporter;
use DH\Adf\Node\Child\TableCell;

class TableCellExporter extends HtmlExporter
{
public function __construct(TableCell $node)
{
parent::__construct($node);

$attributes = [];
if (null !== $node->getBackground()) {
$attributes[] = sprintf('style="background-color: %s"', $node->getBackground());
}
if (null !== $node->getColspan()) {
$attributes[] = sprintf('colspan="%s"', $node->getColspan());
}
if (null !== $node->getRowspan()) {
$attributes[] = sprintf('rowspan="%s"', $node->getRowspan());
}
// TODO: support colwidth

$this->tags = [
sprintf('<td%s>', implode(' ', $attributes)),
'</td>',
];
}
}
17 changes: 17 additions & 0 deletions src/Exporter/Html/Child/TableHeaderExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace DH\Adf\Exporter\Html\Child;

use DH\Adf\Exporter\Html\HtmlExporter;
use DH\Adf\Node\Child\TableHeader;

class TableHeaderExporter extends HtmlExporter
{
public function __construct(TableHeader $node)
{
parent::__construct($node);
$this->tags = ['<th>', '</th>'];
}
}
Loading

0 comments on commit 910bef0

Please sign in to comment.