Skip to content
This repository was archived by the owner on Nov 21, 2019. It is now read-only.

Commit 0a20377

Browse files
nlisgothewilkybarkid
authored andcommitted
Add categories to content headers (#33)
* Add example categories * update patterns * Add categories visitor * Prepare categories from JATS to display * Prepare categories directly in FrontContentHeaderVisitor * Add FrontSubjectGroupContentVisitor * Remove categories from FrontContentHeaderVisitor * Add groups from JATS to content header * cs * cs * cs * cs * Convert children to support inline text elements * Add tests * Rename FrontContentHeaderVisitor to FrontArticleTitleContentHeaderVisitor * Improve code in FrontSubjectGroupContentHeaderVisitor * Use link pattern and add tests * Add label * Fix merge
1 parent b5a273c commit 0a20377

File tree

9 files changed

+378
-10
lines changed

9 files changed

+378
-10
lines changed

.docker/api/data/scholarly-articles/article1/1.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323

2424
</jats:title-group>
2525

26+
<jats:article-categories>
27+
<jats:subj-group subj-group-type="heading">
28+
<jats:subject>Evolutionary Biology</jats:subject>
29+
</jats:subj-group>
30+
</jats:article-categories>
31+
2632
<jats:kwd-group kwd-group-type="author-keywords">
2733
<jats:kwd>XML</jats:kwd>
2834
<jats:kwd>Housestyle</jats:kwd>

vendor-extra/JatsContentBundle/src/Resources/config/services.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,19 @@
6363
<tag name="libero.view_converter"/>
6464
</service>
6565

66-
<service id="Libero\JatsContentBundle\ViewConverter\FrontContentHeaderVisitor"
67-
class="Libero\JatsContentBundle\ViewConverter\FrontContentHeaderVisitor">
66+
<service id="Libero\JatsContentBundle\ViewConverter\FrontArticleTitleContentHeaderVisitor"
67+
class="Libero\JatsContentBundle\ViewConverter\FrontArticleTitleContentHeaderVisitor">
6868
<argument type="service" id="Libero\ViewsBundle\Views\ViewConverter"/>
6969
<tag name="libero.view_converter"/>
7070
</service>
7171

72+
<service id="Libero\JatsContentBundle\ViewConverter\FrontSubjectGroupContentHeaderVisitor"
73+
class="Libero\JatsContentBundle\ViewConverter\FrontSubjectGroupContentHeaderVisitor">
74+
<argument type="service" id="Libero\ViewsBundle\Views\ViewConverter"/>
75+
<argument type="service" id="translator"/>
76+
<tag name="libero.view_converter"/>
77+
</service>
78+
7279
<service id="Libero\JatsContentBundle\ViewConverter\ItalicVisitor"
7380
class="Libero\JatsContentBundle\ViewConverter\ItalicVisitor">
7481
<argument type="service" id="Libero\ViewsBundle\Views\ViewConverter"/>

vendor-extra/JatsContentBundle/src/ViewConverter/FrontContentHeaderVisitor.php renamed to vendor-extra/JatsContentBundle/src/ViewConverter/FrontArticleTitleContentHeaderVisitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Libero\ViewsBundle\Views\ViewConverter;
1212
use Libero\ViewsBundle\Views\ViewConverterVisitor;
1313

14-
final class FrontContentHeaderVisitor implements ViewConverterVisitor
14+
final class FrontArticleTitleContentHeaderVisitor implements ViewConverterVisitor
1515
{
1616
use SimplifiedVisitor;
1717

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Libero\JatsContentBundle\ViewConverter;
6+
7+
use DOMNodeList;
8+
use FluentDOM\DOM\Document;
9+
use FluentDOM\DOM\Element;
10+
use Libero\ViewsBundle\Views\ConvertsLists;
11+
use Libero\ViewsBundle\Views\SimplifiedVisitor;
12+
use Libero\ViewsBundle\Views\TranslatingVisitor;
13+
use Libero\ViewsBundle\Views\View;
14+
use Libero\ViewsBundle\Views\ViewConverter;
15+
use Libero\ViewsBundle\Views\ViewConverterVisitor;
16+
use Symfony\Contracts\Translation\TranslatorInterface;
17+
use function array_map;
18+
use function count;
19+
20+
final class FrontSubjectGroupContentHeaderVisitor implements ViewConverterVisitor
21+
{
22+
use ConvertsLists;
23+
use SimplifiedVisitor;
24+
use TranslatingVisitor;
25+
26+
private $converter;
27+
private $translator;
28+
29+
public function __construct(ViewConverter $converter, TranslatorInterface $translator)
30+
{
31+
$this->converter = $converter;
32+
$this->translator = $translator;
33+
}
34+
35+
protected function doVisit(Element $object, View $view, array &$context = []) : View
36+
{
37+
/** @var Document $document */
38+
$document = $object->ownerDocument;
39+
40+
/** @var DOMNodeList|Element[] $subjects */
41+
$subjects = $document->xpath()->evaluate(
42+
'jats:article-meta/jats:article-categories/jats:subj-group[@subj-group-type="heading"]/jats:subject',
43+
$object
44+
);
45+
46+
if (0 === count($subjects)) {
47+
return $view;
48+
}
49+
50+
return $view->withArgument(
51+
'categories',
52+
[
53+
'attributes' => [
54+
'aria-label' => $this->translate('libero.patterns.content_header.categories.label', $context),
55+
],
56+
'items' => array_map(
57+
function (View $link) : array {
58+
return ['content' => $link->getArguments()];
59+
},
60+
$this->convertList($subjects, '@LiberoPatterns/link.html.twig', $context)
61+
),
62+
]
63+
);
64+
}
65+
66+
protected function expectedTemplate() : string
67+
{
68+
return '@LiberoPatterns/content-header.html.twig';
69+
}
70+
71+
protected function expectedElement() : array
72+
{
73+
return ['{http://jats.nlm.nih.gov}front'];
74+
}
75+
76+
protected function unexpectedArguments() : array
77+
{
78+
return ['categories'];
79+
}
80+
}

vendor-extra/JatsContentBundle/src/ViewConverter/LinkVisitor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected function expectedElement() : array
3737
{
3838
return [
3939
'{http://jats.nlm.nih.gov}kwd',
40+
'{http://jats.nlm.nih.gov}subject',
4041
];
4142
}
4243

vendor-extra/JatsContentBundle/tests/ViewConverter/FrontContentHeaderVisitorTest.php renamed to vendor-extra/JatsContentBundle/tests/ViewConverter/FrontArticleTitleContentHeaderVisitorTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
namespace tests\Libero\JatsContentBundle\ViewConverter;
66

7-
use Libero\JatsContentBundle\ViewConverter\FrontContentHeaderVisitor;
7+
use Libero\JatsContentBundle\ViewConverter\FrontArticleTitleContentHeaderVisitor;
88
use Libero\ViewsBundle\Views\View;
99
use PHPUnit\Framework\TestCase;
1010
use tests\Libero\ContentPageBundle\ViewConvertingTestCase;
1111
use tests\Libero\ContentPageBundle\XmlTestCase;
1212

13-
final class FrontContentHeaderVisitorTest extends TestCase
13+
final class FrontArticleTitleContentHeaderVisitorTest extends TestCase
1414
{
1515
use ViewConvertingTestCase;
1616
use XmlTestCase;
@@ -21,7 +21,7 @@ final class FrontContentHeaderVisitorTest extends TestCase
2121
*/
2222
public function it_does_nothing_if_it_is_not_a_jats_front_element(string $xml) : void
2323
{
24-
$visitor = new FrontContentHeaderVisitor($this->createFailingConverter());
24+
$visitor = new FrontArticleTitleContentHeaderVisitor($this->createFailingConverter());
2525

2626
$element = $this->loadElement($xml);
2727

@@ -44,7 +44,7 @@ public function nodeProvider() : iterable
4444
*/
4545
public function it_does_nothing_if_is_not_the_content_header_template() : void
4646
{
47-
$visitor = new FrontContentHeaderVisitor($this->createFailingConverter());
47+
$visitor = new FrontArticleTitleContentHeaderVisitor($this->createFailingConverter());
4848

4949
$element = $this->loadElement(
5050
<<<XML
@@ -72,7 +72,7 @@ public function it_does_nothing_if_is_not_the_content_header_template() : void
7272
*/
7373
public function it_does_nothing_if_there_is_no_title_group() : void
7474
{
75-
$visitor = new FrontContentHeaderVisitor($this->createFailingConverter());
75+
$visitor = new FrontArticleTitleContentHeaderVisitor($this->createFailingConverter());
7676

7777
$element = $this->loadElement('<front xmlns="http://jats.nlm.nih.gov"><article-meta/></front>');
7878

@@ -93,7 +93,7 @@ public function it_does_nothing_if_there_is_no_title_group() : void
9393
*/
9494
public function it_does_nothing_if_there_is_already_a_content_title_set() : void
9595
{
96-
$visitor = new FrontContentHeaderVisitor($this->createFailingConverter());
96+
$visitor = new FrontArticleTitleContentHeaderVisitor($this->createFailingConverter());
9797

9898
$element = $this->loadElement(
9999
<<<XML
@@ -125,7 +125,7 @@ public function it_does_nothing_if_there_is_already_a_content_title_set() : void
125125
*/
126126
public function it_sets_the_text_argument() : void
127127
{
128-
$visitor = new FrontContentHeaderVisitor($this->createDumpingConverter());
128+
$visitor = new FrontArticleTitleContentHeaderVisitor($this->createDumpingConverter());
129129

130130
$element = $this->loadElement(
131131
<<<XML

0 commit comments

Comments
 (0)