-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditorJSConverter.php
84 lines (75 loc) · 3.04 KB
/
editorJSConverter.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
<?php
class editorJSConverter{
function jsonToHtml($data)
{
// Convert JSON string to JSON Object and get "blocks" child
$data = json_decode($data)->blocks;
// Loop through every block of JSON
$ret = '';
foreach ($data as $item)
{
// Create HTML for each Block depending on the Block Type
switch ($item->type)
{
case 'header':
// Header Block Type
$levelSize = $item->data->level;
$levelText = $item->data->text;
$ret .= "<h{$levelSize}> $levelText </h{$levelSize}> ";
break;
case 'paragraph':
// Paragraph Block Type
$levelText = $item->data->text;
$ret .= "<p>".$levelText."</p>";
break;
case 'list':
// List Block Type
$levelStyle = $item->data->style === 'unordered' ? 'ul' : 'ol';
$levelArr = $item->data->items;
$list = "<$levelStyle>";
$listItems = "";
foreach ($levelArr as $eleItem)
{
$listItems .= "<li> $eleItem </li>";
}
$list .= $listItems;
$list .= "</$levelStyle>";
$ret .= $list;
break;
case 'image':
// Image Block Type
$levelFilePath = $item->data->url;
$levelCaption = $item->data->caption;
$ret .= '<img style="max-width: 100%;vertical-align: bottom;display: block;" alt="' . $levelCaption . '" src="' . $levelFilePath . '"><center>' . $levelCaption . '</center>';
break;
case 'table':
// Table Block Type
$tableHtml = "<table>";
foreach ($item->data->content as $row)
{
$tableHtml .= "<tr>";
foreach ($row as $column){
$tableHtml .= "<td>$column</td>";
}
$tableHtml .= "</tr>";
}
$tableHtml .= "</table>";
$ret .= $tableHtml;
break;
case 'quote':
// Quote Block Type
$ret .= '<blockquote>
<p>';
$ret .= $item->data->text;
$ret .= "―".$item->data->caption;
$ret .= "</p></blockquote>";
break;
default:
// Unknown Block Type
throw new Exception("Unknown Block Type: '" . $item->type."'");
}
}
return $ret;
}
}
?>