-
Notifications
You must be signed in to change notification settings - Fork 13
/
extension.driver.php
110 lines (88 loc) · 3.97 KB
/
extension.driver.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
100
101
102
103
104
105
106
107
108
109
110
<?php
class extension_html5_doctype extends Extension{
private $_trigger;
private static $_name = 'HTML5 Doctype';
public function getSubscribedDelegates(){
return array(
array(
'page' => '/frontend/',
'delegate' => 'FrontendOutputPostGenerate',
'callback' => 'parse_html'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendPageResolved',
'callback' => 'setRenderTrigger'
),
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'appendPreferences'
),
);
}
/**
* append the preferences field
* @return void
*/
public function appendPreferences($context, $errors = null) {
$fieldset = new XMLElement('fieldset', null, array('class' => 'settings'));
$legend = new XMLElement('legend', __(self::$_name));
$div = new XMLElement('div');
$label = Widget::Label(__('Exclude Types'),
Widget::Input(
'settings[html5_doctype][exclude_pagetypes]',
Symphony::Configuration()->get('exclude_pagetypes', 'html5_doctype')
));
$tags = new XMLElement('ul', null, array('class' => 'tags', 'data-interactive' => 'data-interactive'));
$types = PageManager::fetchAvailablePageTypes();
foreach($types as $type) {
$tags->appendChild(new XMLElement('li', $type));
}
$div->appendChild($label);
$div->appendChild($tags);
$fieldset->appendChild($legend);
$fieldset->appendChild($div);
$context['wrapper']->appendChild($fieldset);
}
public function setRenderTrigger($context)
{
$this->_trigger = true;
$conf = preg_split(
'~,~',
preg_replace('/\s+/', '',
Symphony::Configuration()->get('exclude_pagetypes', 'html5_doctype')),
-1,
PREG_SPLIT_NO_EMPTY
);
$types = $context['page_data']['type'];
if (!empty($types) && !empty($conf)) {
foreach($conf as $type) {
if (in_array($type, $types)) {
$this->_trigger = false;
break;
}
}
}
}
public function parse_html($context) {
if (!$this->_trigger) return;
// Parse only if $context['output'] exists and it's an HTML document
if(substr($context['output'], 0, 14) == '<!DOCTYPE html') {
$html = $context['output'];
// Split the HTML output into two variables:
// $html_doctype contains the first fifteen lines of the HTML document
// $html_doc contains the rest of the HTML document
$html_array = explode("\n", $html, 15);
$html_doc = array_pop($html_array);
$html_doctype = implode("\n", $html_array);
// Parse the doctype to convert XHTML syntax to HTML5
$html_doctype = preg_replace("/<!DOCTYPE [^>]+>/", "<!DOCTYPE html>", $html_doctype);
$html_doctype = preg_replace('/ xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"| xml:lang="[^\"]*\"/', '', $html_doctype);
$html_doctype = preg_replace('/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=(.*[a-z0-9-])\"( \/)?>/i', '<meta charset="\1"\2>', $html_doctype);
// Concatenate the fragments into a complete HTML5 document
$html = $html_doctype . "\n" . $html_doc;
$context['output'] = $html;
}
}
}