/[drupal]/contributions/sandbox/fgm/valid_node/valid_node.module
ViewVC logotype

Contents of /contributions/sandbox/fgm/valid_node/valid_node.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Fri Sep 29 21:38:34 2006 UTC (3 years, 1 month ago) by fgm
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/x-php
Initial version. See :

http://blog.riff.org/2006_09_29_force_valid_html_with_valid_node_module

for details.
1 <?php
2 /**
3 * Copyright (C) 2005-2006 Frederic G. MARAND
4 * Licensed under the CeCILL, version 2
5 * See LICENSE.txt for the license text
6 * $Id: $
7 */
8
9 define('VALIDNODEVERSION', '$Id: $');
10
11 /**
12 * Persistent variables
13 */
14 define ('VALIDNODEVARTIDY', 'valid_node_tidy');
15 define ('VALIDNODEVARHEAD', 'valid_node_head');
16 define ('VALIDNODEVARTAIL', 'valid_node_tail');
17
18 /**
19 * Default values for persistent variables
20 */
21 define ('VALIDNODEDEFTIDY', 'tidy -asxml');
22 define ('VALIDNODEDEFHEAD', 9);
23 define ('VALIDNODEDEFTAIL', 4);
24
25
26 function valid_node_help($section)
27 {
28 $ret = '';
29 switch ($section) {
30 case 'admin/modules#name':
31 $ret = 'valid_node';
32 break;
33 case 'admin/help#valid_node':
34 $ret = t('');
35 break;
36 case 'admin/modules#description':
37 $ret = t('valid_node automatically converts node content to XHTML fragments. This helps to maintain valid content on all pages, in spite of the contributors\' HTML quality or lack thereof.');
38 break;
39 default: // ignore, this hook is called all over the place in 4.7b1
40 //$ret = "g2_help($section)";
41 }
42 return $ret;
43 }
44
45 /**
46 * Act on nodes defined by other modules.
47 */
48 function valid_node_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
49 {
50 // dprint_r("vnna op = $op");
51 switch ($op)
52 {
53 case 'validate':
54 case 'view':
55 case 'update':
56 case 'insert':
57 case 'load':
58 case 'submit':
59 valid_node_validate($node);
60 break;
61 break;
62 }
63 }
64
65 function valid_node_validate(&$node)
66 {
67 $prolog = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
68 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">' . "\n";
69 $epilog = ' </html>';
70 $body = $prolog . $node->body . $epilog ;
71
72 $descriptorspec = array
73 (
74 0 => array("pipe", "r"), // stdin is a pipe that the child will read from
75 1 => array("pipe", "w"), // stdout is a pipe that the child will write to
76 2 => array("file", "e:/tmp/error-output.txt", "a"), // stderr is a file to write to
77 );
78 $process = proc_open("e:\\images\\w3c\\tidy.exe -asxml", $descriptorspec, $pipes);
79 if (is_resource($process))
80 {
81 // $pipes now looks like this:
82 // 0 => writeable handle connected to child stdin
83 // 1 => readable handle connected to child stdout
84 // Any error output will be appended to /tmp/error-output.txt
85 fwrite($pipes[0], $body);
86 fclose($pipes[0]);
87 $s = '';
88 while(!feof($pipes[1]))
89 {
90 $s .= fgets($pipes[1], 1024);
91 }
92 fclose($pipes[1]);
93
94 $ar = explode(chr(10), $s);
95
96 $head = variable_get(VALIDNODEVARHEAD, VALIDNODEDEFHEAD);
97 for ($i = 0 ; $i < $head ; $i++)
98 {
99 array_shift($ar);
100 }
101
102 $tail = variable_get(VALIDNODEVARTAIL, VALIDNODEDEFTAIL);
103 for ($i = 0 ; $i < $tail ; $i++)
104 {
105 array_pop($ar);
106 }
107
108 $s = implode($ar);
109
110 // It is important that you close any pipes before calling
111 // proc_close in order to avoid a deadlock
112 $return_value = proc_close($process);
113
114 $node->body = $s ;
115 }
116 }
117
118 function valid_node_settings()
119 {
120 $form [VALIDNODEVARTIDY] = array
121 (
122 '#type' => 'textfield',
123 '#title' => t('"%tidy" command, including path if necessary', array('%tidy' => 'tidy')),
124 '#default_value' => variable_get(VALIDNODEVARTIDY, VALIDNODEDEFTIDY),
125 '#description' => t('This command takes as input the unformatted content wrapped within a XHTML prolog and epilog, and returns them as a tidied XHTML document, which is then stripped from both prolog and epilog to return the body as an XHTML fragment.')
126 );
127
128 $form [VALIDNODEVARHEAD] = array
129 (
130 '#type' => 'textfield',
131 '#title' => t('Number of lines to strip from top of tidied content to remove the prolog'),
132 '#size' => 3,
133 '#maxlength' => 3,
134 '#default_value' => variable_get(VALIDNODEVARHEAD, VALIDNODEDEFHEAD),
135 );
136 $form [VALIDNODEVARTAIL] = array
137 (
138 '#type' => 'textfield',
139 '#title' => t('Number of lines to strip from bottom of tidied content to remove the epilog'),
140 '#size' => 3,
141 '#maxlength' => 3,
142 '#default_value' => variable_get(VALIDNODEVARTAIL, VALIDNODEDEFTAIL),
143 );
144 $form[VALIDNODEVERSION] = array
145 (
146 '#value' => '<p>'
147 . t('This site is running valid_node version %version.',
148 array('%version' => VALIDNODEVERSION)
149 )
150 . "</p>\n<p>"
151 . t('For information about tidy, see %tidy',
152 array('%tidy' => '<a href="http://www.w3.org/Markup" title="Markup activity at W3C">http://www.w3.org/Markup</a>')
153 )
154 . "</p>\n"
155 );
156
157
158 return $form;
159 }

  ViewVC Help
Powered by ViewVC 1.1.2