/[drupal]/contributions/modules/html2book/html2book.module
ViewVC logotype

Contents of /contributions/modules/html2book/html2book.module

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


Revision 1.2 - (show annotations) (download) (as text)
Sun Apr 5 13:35:56 2009 UTC (7 months, 3 weeks ago) by karens
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +39 -22 lines
File MIME type: text/x-php
#253587 Port to D6, patch by junedkazi.
1 <?php
2 //$Id$
3
4 /**
5 * Implementation of hook_help().
6 */
7 function html2book_help($path, $arg) {
8 switch ($path) {
9 case 'admin/help#html2book' :
10 $output = t('<p>Optionally create a new book page at each html heading and subheading (&lt;h1&gt;, &lt;h2&gt;, etc) in the body text. This makes it possible to use documents created in word processing programs to automatically create a multi-page Drupal book or book section in a single step. To use this feature, click on the input split collapsible box below the book body in the edit form.</p><p>All text before the first heading will be retained as the body of the original page. Subsequent pages will be added as children of that page, using the heading as their title and all text from that point to the next heading as their body. Child pages will be nested based on the subheadding number, i.e. h2 pages will be nested below the h1 page they follow, h3 pages will be nested below h2 pages, etc. Each new book page will have the same author, categories, and other settings selected in the original page. </p><p>If the source is a word processing document, be sure it has been saved as html rather than as a word processing document so the HTML2Book module can locate the headings in the text. Microsoft Word documents pasted into the body should be saved as \'Html, filtered\' for best results.</p>');
11 $output .= t('EXAMPLE:
12 <pre>
13 INPUT:
14
15 Node 1 Title: My Book
16 Node 1 Body:
17 &lt;div&gt;Here is my page.&lt;/div&gt;
18 &lt;h1&gt;Page 1&lt;/h1&gt;
19 &lt;p&gt;Here is my text for page 1.&lt;/p&gt;
20 &lt;h2&gt;Page 1a&lt;/h2&gt;
21 &lt;p&gt;This is page 1a.&lt;/p&gt;
22 &lt;h2&gt;Page 1b&lt;/h2&gt;
23 &lt;p&gt;This is page 1b.&lt;/p&gt;
24 &lt;h1&gt;Page 2&lt;/h1&gt;
25 &lt;p&gt;This is page 2.&lt;/p&gt;
26
27 CREATES:
28
29 Node 1 Title: My Book
30 Node 1 Body: &lt;div&gt;Here is my page.&lt;/div&gt;
31 Node 1 Parent: &lt;top level&gt;
32 Node 1 Weight: -15
33
34 Node 2 Title: Page 1
35 Node 2 Body: &lt;p&gt;Here is my text for page 1.&lt;/p&gt;
36 Node 2 Parent: Node 1
37 Node 2 Weight: -15
38
39 Node 3 Title: Page 1a
40 Node 3 Body: &lt;p&gt;This is page 1a.&lt;/p&gt;
41 Node 3 Parent: Node 2
42 Node 3 Weight: -15
43
44 Node 4 Title: Page 1b
45 Node 4 Body: &lt;p&gt;This is page 1b.&lt;/p&gt;
46 Node 4 Parent: Node 2
47 Node 4 Weight: -14
48
49 Node 5 Title: Page 2
50 Node 5 Body: &lt;p&gt;This is page 2.&lt;/p&gt;
51 Node 5 Parent: Node 1
52 Node 5 Weight: -14
53 </pre>
54 ');
55 return $output;
56 break;
57 }
58 }
59
60 /**
61 * Implementation of hook_perm().
62 */
63 function html2book_perm() {
64 return array('use html2book');
65 }
66
67 /**
68 * Implementation of hook_form_alter().
69 */
70 function html2book_form_alter(&$form, $form_state, $form_id) {
71 if ($form_id == 'book_node_form' && user_access('use html2book')) {
72 $form['html2book_group'] = array(
73 '#type' => 'fieldset',
74 '#title' => t('Input split'),
75 '#collapsible' => TRUE,
76 '#collapsed' => FALSE,
77 );
78 $form['html2book_group']['html2book'] = array(
79 '#type' => 'radios',
80 '#title' => t('Heading treatment'),
81 '#description' => t('Should the input text be !help at each HTML heading?', array('!help' => l(t('split into new book pages'), 'admin/help/html2book'))),
82 '#default_value' => $form['html2book'] ? $form['html2book'] : 0,
83 '#options' => array(0 => t('Do not split input text.'), 1 => t('Create a new book page at each HTML heading.')),
84 );
85 }
86 }
87
88 /**
89 * Implementation of hook_nodeapi().
90 */
91 function html2book_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
92 if ($node->type == 'book' && $node->html2book && user_access('use html2book')) {
93 switch ($op) {
94 case 'presave':
95 $items = preg_split('/<h(.*)>/U', $node->body, -1, PREG_SPLIT_DELIM_CAPTURE );
96 $node->temp = $node->body;
97 $node->body = $items[0];
98 $node->teaser = node_teaser($node->body);
99 break;
100 case 'insert':
101 html2book_split($node);
102 unset($node->temp);
103 break;
104 }
105 }
106 }
107
108 /**
109 * html2book operation.
110 */
111 function html2book_split(&$node) {
112 // This may take some time since new nodes are created.
113 $original_time = ini_get('max_execution_time');
114 ini_set('max_execution_time', 0);
115
116 // Break body into separate pages wherever there is a heading.
117 $items = preg_split('/<h(.*)>/U', $node->temp, -1, PREG_SPLIT_DELIM_CAPTURE );
118 // Track heading levels, weights, and page parents and children.
119 $istag = FALSE;
120 $weight[0] = -15;
121 $prev_level = 0;
122 foreach ($items as $item) {
123 if ($istag) {
124 $parts = explode(' ', $item);
125 $level = $parts[0];
126 }
127 else {
128 if (isset($level)) {
129 $string = '<h'. $level . '>'. $item;
130 $split = explode('</h'. $level .'>', $string);
131 $title = trim(str_replace('&nbsp;', ' ', strip_tags($split[0])));
132 $body = trim($split[1]);
133 }
134 else {
135 // If $level isn't set, this is the body text that preceeds the first heading.
136 // This will become the parent node's body text.
137 $title = '';
138 $body = $item;
139 }
140 if (!empty($title) || !empty($body) || !isset($parent)) {
141 if (!isset($parent)) {
142 // The first time through, make sure we have a nid for the parent
143 // and adjust the parent body and teaser.
144 $parent = array(0 => $node->nid);
145 $book = array(0 => $node);
146 $page = 1;
147 }
148 else {
149 // After the first time through, clone the parent as a starting point for child nodes.
150 $child_node = drupal_clone($node);
151 // Set the child nid and vid to zero so new nodes are created.
152 $child_node->nid = 0;
153 $child_node->vid = 0;
154 // Make sure the title is not empty and create the child's body, teaser, and weight.
155 $child_node->title = !empty($title) ? $title : t('Page !$page', array('!page' => $page));
156 $child_node->body = $body;
157 $child_node->teaser = node_teaser($body);
158
159 if ($level > $prev_level || !isset($weight[$level])) {
160 $weight[$level] = -15;
161 }
162 else {
163 $weight[$level]++;
164 }
165 //$child_node->weight = $weight[$level];
166 $child_node->book['weight'] = $weight[$level];
167
168 // Find the parent id for this node using the value stored for the
169 // next higher level.
170 $parent_level = intval($level - 1);
171 $child_node->parent = !empty($parent[$parent_level]) ? $parent[$parent_level] : $node->nid;
172
173 $child_node->book['bid'] = $parent[0];
174 $child_node->book['menu_name'] = $book[0]->book['menu_name'];
175 $child_node->book['plid'] = $book[$parent_level]->book['mlid'];
176
177 // Make sure html2book is not run again on the child nodes that are created.
178 $child_node->html2book = 0;
179 // Save the child node.
180 node_save($child_node);
181 // Set a parent nid for this level that can be used by later children.
182 $parent[$level] = $child_node->nid;
183 $book[$level] = $child_node;
184 $page++;
185 }
186 }
187 }
188 $istag = !$istag;
189 $prev_level = $level;
190 }
191 // Make sure html2book isn't run more than once on the same text.
192 $node->html2book = 0;
193 // Reset max_execution_time
194 ini_set('max_execution_time', $original_time);
195 }

  ViewVC Help
Powered by ViewVC 1.1.2