/[drupal]/contributions/sandbox/jjeff/codepress/codepress.module
ViewVC logotype

Contents of /contributions/sandbox/jjeff/codepress/codepress.module

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


Revision 1.1 - (show annotations) (download) (as text)
Mon Jul 9 00:26:03 2007 UTC (2 years, 4 months ago) by jjeff
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/x-php
Non-functional beginning stages of CodePress (syntax hilighted code editing) module for Drupal. Also some good code in here for textarea discovery for WYSIWYG-type modules.
1 <?php
2
3 define('CODEPRESS_DEFAULTS', "");
4
5 function codepress_menu($may_cache) {
6 $items = array();
7 if ($may_cache) {
8 $items[] = array(
9 'title' => t('CodePress'),
10 'path' => 'admin/settings/codepress',
11 'description' => t('Settings for the CodePress module.'),
12 'access' => user_access('administer site configuration'),
13 'callback' => 'drupal_get_form',
14 'callback arguments' => array('codepress_settings_form'),
15 );
16 }
17 return $items;
18 }
19
20 function codepress_settings_form_submit($form_id, $form) {
21 variable_set('codepress_textareas', $form['textareas']);
22 }
23
24
25 function codepress_settings_form() {
26 $forms = variable_get('codepress_textareas', array());
27 $form['textareas']['#tree'] = TRUE;
28 foreach($forms as $form_id => $textareas) {
29 $form['textareas'][] = array(
30 '#value' => $form_id,
31 );
32 foreach ($textareas as $id => $foo) {
33 $form['textareas'][$form_id][$id]['name'] = array(
34 '#value' => $id,
35 );
36 $form['textareas'][$form_id][$id]['syntax'] = array(
37 '#type' => 'select',
38 '#options' => array(
39 '' => '<none>',
40 'html' => 'HTML',
41 'php' => 'PHP',
42 'css' => 'CSS',
43 'javascript' => "Javascript",
44 'sql' => 'SQL',
45 'java' => 'Java',
46 'perl' => 'Perl',
47 ),
48 '#default_value' => $forms[$form_id][$id]['syntax'] ? $forms[$form_id][$id]['syntax'] : '',
49 );
50 $form['textareas'][$form_id][$id]['linenumbers'] = array(
51 '#type' => 'checkbox',
52 '#attributes' => array('title' => t('line numbers')),
53 '#default_value' => isset($forms[$form_id][$id]['linenumbers']) ? $forms[$form_id][$id]['linenumbers'] : 1,
54 //'#title' => t('line numbers'),
55 );
56 $form['textareas'][$form_id][$id]['autocomplete'] = array(
57 '#type' => 'checkbox',
58 '#attributes' => array('title' => t('autocomplete')),
59 '#default_value' => isset($forms[$form_id][$id]['autocomplete']) ? $forms[$form_id][$id]['autocomplete'] : 1,
60 //'#title' => t('autocomplete'),
61 );
62 $form['textareas'][$form_id][$id]['filtered'] = array(
63 '#type' => 'value',
64 '#value' => $forms[$form_id][$id]['filtered'] ? $forms[$form_id][$id]['filtered'] : 0,
65 );
66 }
67 }
68
69 $form['submit'] = array(
70 '#type' => 'submit',
71 '#value' => t('Submit'),
72 );
73
74 return $form;
75 }
76
77 function theme_codepress_settings_form(&$form) {
78 $output = '';
79 $header = array(
80 array('data' => t('field')),
81 array('data' => t('syntax')),
82 array('data' => t('line numbers'), 'align' => 'center'),
83 array('data' => t('autocomplete'), 'align' => 'center'),
84 );
85 $span = count($header);
86 foreach(element_children($form['textareas']) as $name) {
87 $element = &$form['textareas'][$name];
88 //dsm($element);
89 if ($element['#value'] && !$element['#type']) {
90 // we've got a form name
91 $rows[] = array(array('data' => '<strong>'. drupal_render($element) .'</strong>', 'colspan' => $span));
92 }
93 else {
94 foreach(element_children($element) as $fid) {
95 if (is_array($element[$fid])) {
96 $rows[] = array(
97 array('data' => drupal_render($element[$fid]['name'])),
98 array('data' => drupal_render($element[$fid]['syntax'])),
99 array('data' => drupal_render($element[$fid]['linenumbers']), 'align' => 'center'),
100 array('data' => drupal_render($element[$fid]['autocomplete']), 'align' => 'center'),
101 );
102 }
103 }
104 }
105 }
106 $output .= theme_table($header, $rows);
107 $output .= drupal_render($form);
108 return $output;
109 }
110
111 function codepress_form_alter($form_id, &$form) {
112 $site_textareas = variable_get('codepress_textareas', array());
113 $site_textareas_alter = $site_textareas;
114 // find the textareas in this form
115 $textareas = &codepress_find_textareas($form);
116 foreach ($textareas as $id => $element) {
117 if (!isset($site_textareas[$form_id][$id])) {
118 // check to see if this is a textarea that hasn't been seen before
119 $info = array();
120 if (isset($element['#format'])) {
121 $info[] = 'filtered';
122 }
123 $site_textareas_alter[$form_id][$id] = $info;
124 if (user_access('administer site configuration')) {
125 drupal_set_message(t('Found new %filtered textarea %name in form %form',
126 array(
127 '%name' => $id,
128 '%form' => $form_id,
129 '%filtered' => $element['#filtered'] ? t('filtered') : t('unfiltered')
130 )
131 ));
132 }
133 }
134 else {
135 // see if this textarea should be get a codepress treatment
136 $settings = $site_textareas[$form_id][$id];
137 if ($settings['syntax']) {
138 codepress_add_js();
139 $textareas[$id]['#resizable'] = FALSE;
140 $classes = array();
141 if (isset($textareas[$id]['#attributes']['class'])) {
142 $textareas[$id]['#attributes']['class'] .= ' ';
143 }
144 $classes[] = 'codepress';
145 $classes[] = $settings['syntax'];
146 if (!$settings['linenumbers']) {
147 $classes[] = 'linenumbers-off';
148 }
149 if (!$settings['autocomplete']) {
150 $classes[] = 'autocomplete-off';
151 }
152 $textareas[$id]['#attributes']['class'] .= implode(' ', $classes);
153 }
154 }
155 }
156 if ($site_textareas_alter != $site_textareas) {
157 variable_set('codepress_textareas', $site_textareas_alter);
158 }
159 }
160
161 function codepress_add_js() {
162 static $added;
163 if (!$added) {
164 global $base_url;
165 $path = drupal_get_path('module', 'codepress');
166 //drupal_add_js($path .'/jquery.Codepress.js');
167 // $(function(){ $.codepress.start({path: '/fwx/js/codepress/'}); });
168 //drupal_add_js("$(function(){ $.codepress.start({path: '". $path ."/codepress/'}); });", 'inline');
169 drupal_add_js($path .'/codepress/codepress.js');
170 $added = TRUE;
171 }
172 }
173
174 /**
175 * Recurse through a form and find the textareas
176 *
177 * @return
178 * an array referencing the original form elements
179 */
180 function codepress_find_textareas(&$form, &$accumulator = array()) {
181 foreach (element_children($form) as $name) {
182 $element = &$form[$name];
183 if ($element['#type'] == 'textarea') {
184 if (isset($form['format']['#validate']['filter_form_validate'])) {
185 //$element['#filtered'] = TRUE;
186 $element['#format'] = &$form['format'];
187 }
188 $accumulator[$name] = &$element;
189 }
190 codepress_find_textareas($element, $accumulator);
191 }
192 return $accumulator;
193 }

  ViewVC Help
Powered by ViewVC 1.1.2