Fix the filter to allow Instruction sections

Signed-off-by: Christian Wolf <github@christianwolf.email>
This commit is contained in:
Christian Wolf 2022-10-31 12:16:26 +01:00
Родитель 9ac1eefabd
Коммит ca595700aa
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 9FC3120E932F73F1
2 изменённых файлов: 34 добавлений и 1 удалений

Просмотреть файл

@ -94,7 +94,13 @@ class FixInstructionsFilter extends AbstractJSONFilter {
}
if ($this->jsonService->isSchemaObject($value, 'HowToStep', false)) {
$instructions[$key] = [$value['text']];
$instructions[$key] = [$this->extractHowToStep($value)];
continue;
}
if ($this->jsonService->isSchemaObject($value, 'HowToSection', false)) {
$newInstructions = $this->flattenHowToSection($value);
$instructions[$key] = $newInstructions;
continue;
}
@ -146,6 +152,11 @@ class FixInstructionsFilter extends AbstractJSONFilter {
continue;
}
if ($this->jsonService->isSchemaObject($element, 'HowToStep', false)) {
$newElements[$key] = [$this->extractHowToStep($element)];
continue;
}
throw new InvalidRecipeException($this->l->t('Cannot parse recipe: Unknown object found during flattening of instructions.'));
}
@ -197,4 +208,24 @@ class FixInstructionsFilter extends AbstractJSONFilter {
$pieces = array_filter($pieces);
return $pieces;
}
private function flattenHowToSection($item): array {
if ($item['name'] && is_string($item['name'])) {
$ret = ['## ' . $item['name']];
} else {
$ret = ['## HowToSection'];
}
$items = $this->flattenItemList($item);
return array_merge($ret, $items);
}
private function extractHowToStep($item) {
if (! $this->jsonService->isSchemaObject($item, 'HowToStep', false)) {
throw new InvalidRecipeException($this->l->t('Cannot parse recipe: Unknown object found during flattening of instructions.'));
}
return $item['text'];
}
}

Просмотреть файл

@ -239,6 +239,8 @@ class FixInstructionsFilterTest extends TestCase {
$original = json_decode($originalRaw, true);
$expected = json_decode($expectedRaw, true);
$this->textCleanupHelper->method('cleanUp')->willReturnArgument(0);
$ret = $this->dut->apply($original);
$this->assertEquals($expected, $original);