-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transformer.php
99 lines (91 loc) · 3.8 KB
/
Transformer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace Frontkom\FsInfoTransformer;
class Transformer
{
public function transform(string $data) : string
{
$data = $this->processWeblinks($data);
$data = $this->processBulletedLists($data);
$data = $this->replaceTags($data, 'bold', 'strong');
$data = $this->replaceTags($data, 'italic', 'em');
$data = $this->replaceEmptyParagraphs($data);
$data = $this->processNumberedLists($data);
return $data;
}
public function replaceEmptyParagraphs(string $data) : string
{
// From time to time we see these kinds of things:
// <p> </p> or <p></p> or <p />.
// We should replace them with nothing.
$variations_to_replace = [
'<p> </p>',
'<p></p>',
'<p />',
'<p/>',
];
return str_replace($variations_to_replace, '', $data);
}
public function processBulletedLists(string $data) : string
{
// The changes should be like this. An input would be
// <list listType="bulleted"><listItem>First bullet</listItem><listItem>Second bullet</listItem></list>.
// And then the output should become
// <ul><li>First bullet</li><li>Second bullet</li></ul>.
return $this->processListType($data, 'bulleted', 'ul');
}
public function processNumberedLists(string $data) : string
{
// The changes should be like this. An input would be
// <list listType="numbered"><listItem>First bullet</listItem><listItem>Second bullet</listItem></list>.
// And then the output should become
// <ol><li>First bullet</li><li>Second bullet</li></ol>.
return $this->processListType($data, 'numbered', 'ol');
}
protected function processListType(string $data, $list_type, $replacement_tag)
{
// First find the surrounding text of the list.
$regex = '<list listType="' . $list_type . '">([\S\s]*?)<\/list>';
$match = [];
preg_match_all('/' . $regex . '/', $data, $match);
if (empty($match[0])) {
return $data;
}
foreach ($match[0] as $key => $value) {
$inside = $this->replaceTags($match[1][$key], 'listItem', 'li');
$data = str_replace($value, '<' . $replacement_tag . '>' . $inside . '</' . $replacement_tag . '>', $data);
}
return $data;
}
public function replaceTags(string $data, $tag, $replacement_tag, $keep_self_closing_tags = false) : string
{
$data = str_replace(
['<' . $tag . '>', '</' . $tag . '>'],
['<' . $replacement_tag . '>', '</' . $replacement_tag . '>'],
$data
);
// Also self closing tags in a couple of variations.
$self_closing_replacement = '';
if ($keep_self_closing_tags) {
$self_closing_replacement = '<' . $replacement_tag . '>';
}
$data = str_replace(
[
'<' . $tag . ' />',
'<' . $tag . '/>'],
[$self_closing_replacement, $self_closing_replacement],
$data
);
return $data;
}
public function processWeblinks(string $data, bool $nofollow = true) : string
{
// The changes should be like this. An input would be
// <webLink><href>https://www.example.com</href><linkName>gjeldende forskrift og tilhørende retningslinjer.
// </linkName></webLink>.
// And then the output should become
// <a rel="nofollow" href="https://www.example.com">gjeldende forskrift og tilhørende retningslinjer.</a>
$regex = '<webLink><href>(.*?)<\/href>(<linkName>([\S\s]*?)<\/linkName>)?<\/webLink>';
$replace = $nofollow ? '<a rel="nofollow" href="$1">$3</a>' : '<a href="$1">$3</a>';
return preg_replace('/' . $regex . '/', $replace, $data);
}
}