Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextHandling: implement a tool to solve string-based text handling problems #8096

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions components/ILIAS/Data/src/TextHandling/Markup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace ILIAS\Data\TextHandling;

interface Markup
{
}
27 changes: 27 additions & 0 deletions components/ILIAS/Data/src/TextHandling/Markup/Markdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace ILIAS\Data\TextHandling\Markup;

use ILIAS\Data\TextHandling\Markup;

class Markdown implements Markup
{
}
51 changes: 51 additions & 0 deletions components/ILIAS/Data/src/TextHandling/Shape.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace ILIAS\Data\TextHandling;

use ILIAS\Data\TextHandling\Text\HTML;
use ILIAS\Data\TextHandling\Text\PlainText;

/**
* Methods in this interface should mostly be called by the according methods
* on `Text` instances, most consumer code shouldn't need to bother with `Shape`
* directly.
*
* Only exception is `fromString` which acts as an entrypoint into the logic
* of TextHandling.
*/
interface Shape
{
/**
* @throws \InvalidArgumentException if $text is not compliant.
*/
public function fromString(string $text): Text;

public function isRawStringCompliant(string $text): bool;
/**
* @throws \InvalidArgumentException if $text does not match format.
*/
public function toHTML(Text $text): HTML;
/**
* @throws \InvalidArgumentException if $text does not match format.
*/
public function toPlainText(Text $text): PlainText;
public function getMarkup(): Markup;
}
69 changes: 69 additions & 0 deletions components/ILIAS/Data/src/TextHandling/Shape/Markdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace ILIAS\Data\TextHandling\Shape;

use ILIAS\Data\TextHandling\Shape;
use ILIAS\Data\TextHandling\Markup;
use ILIAS\Data\TextHandling\Text;
use ILIAS\Refinery;

class Markdown implements Shape
{
public function __construct(
protected Refinery\Transformation $markdown_to_html_transformation,
) {
}

public function toHTML(Text $text): Text\HTML
{
if (!$text instanceof Text\Markdown) {
throw new \InvalidArgumentException("Text does not match format.");
}
return new Text\HTML(
$this->markdown_to_html_transformation->transform(
$text->getRawRepresentation()
)
);
}

public function toPlainText(Text $text): Text\PlainText
{
if (!$text instanceof Text\Markdown) {
throw new \InvalidArgumentException("Text does not match format.");
}
return new Text\PlainText($text->getRawRepresentation());
}

public function getMarkup(): Markup\Markdown
{
return new Markup\Markdown();
}

public function fromString(string $text): Text\Markdown
{
return new Text\Markdown($this, $text);
}

public function isRawStringCompliant(string $text): bool
{
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace ILIAS\Data\TextHandling\Shape;

use ILIAS\Data\TextHandling\Structure;
use ILIAS\Data\TextHandling\Text;

class SimpleDocumentMarkdown extends Markdown
{
/**
* @return mixed[] consts from Structure
*/
public function getSupportedStructure(): array
{
return [
Structure::BOLD,
Structure::ITALIC,
Structure::HEADING_1,
Structure::HEADING_2,
Structure::HEADING_3,
Structure::HEADING_4,
Structure::HEADING_5,
Structure::HEADING_6,
Structure::UNORDERED_LIST,
Structure::ORDERED_LIST,
Structure::PARAGRAPH,
Structure::LINK,
Structure::BLOCKQUOTE,
Structure::CODE
];
}

public function fromString(string $text): Text\SimpleDocumentMarkdown
{
return new Text\SimpleDocumentMarkdown($this, $text);
}

public function isRawStringCompliant(string $text): bool
{
$structure_patterns = [
'\!\[(.)*\]\((.)+\)', // images ![](url)
'\!\[(.)+\]', // images ![][url]
'\[(.)*\]\:(.)+' // images []:url
];

foreach ($structure_patterns as $pattern) {
if (mb_ereg_match($pattern, $text)) {
return false;
}
}

return true;
}
}
72 changes: 72 additions & 0 deletions components/ILIAS/Data/src/TextHandling/Shape/WordOnlyMarkdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace ILIAS\Data\TextHandling\Shape;

use ILIAS\Data\TextHandling\Structure;
use ILIAS\Data\TextHandling\Text;

class WordOnlyMarkdown extends SimpleDocumentMarkdown
{
/**
* @return mixed[] consts from Structure
*/
public function getSupportedStructure(): array
{
return [
Structure::BOLD,
Structure::ITALIC
];
}

public function fromString(string $text): Text\WordOnlyMarkdown
{
return new Text\WordOnlyMarkdown($this, $text);
}

public function isRawStringCompliant(string $text): bool
{
$structure_patterns = [
'^(\#){1,6}(\ )', // headings 1 - 6
'^(\- )', // unordered list
'^(\* )', // unordered list
'^(\+ )', // unordered list
'^([0-9]+)(\.\ )', // ordered list
'(\ ){2}', // paragraph via space
'(\\\)', // paragraph
'\[(.)*\]\((.)+\)', // link [title](url)
'\[(.)*\]\([.]+\)', // link [id][url]
'\[(.)*\]\:(.)+', // link [id]:url
'\!\[(.)*\]\((.)+\)', // images ![](url)
'\!\[(.)+\]', // images ![][url]
'\[(.)*\]\:(.)+', // images []:url
'^(\>)+', // blockquote
'(\`)' // code only
];

foreach ($structure_patterns as $pattern) {
if (mb_ereg_match($pattern, $text)) {
return false;
}
}

return true;
}
}
40 changes: 40 additions & 0 deletions components/ILIAS/Data/src/TextHandling/Structure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace ILIAS\Data\TextHandling;

enum Structure
{
// heading 1-6 are cases for <h1> to <h6>
case HEADING_1;
case HEADING_2;
case HEADING_3;
case HEADING_4;
case HEADING_5;
case HEADING_6;
case BOLD;
case ITALIC;
case UNORDERED_LIST;
case ORDERED_LIST;
case LINK;
case PARAGRAPH;
case BLOCKQUOTE;
case CODE;
}
34 changes: 34 additions & 0 deletions components/ILIAS/Data/src/TextHandling/Text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace ILIAS\Data\TextHandling;

interface Text
{
public function getShape(): Shape;
public function getMarkup(): Markup;
/**
* @return Structure[]
*/
public function getSupportedStructure(): array;
public function toHTML(): Text\HTML;
public function toPlainText(): Text\PlainText;
public function getRawRepresentation(): string;
}
Loading
Loading