"Ensuring Your Web Presence Contributes"

Javascript code filter for Drupal

admin's picture
Posted by admin on Fri 27 Jun 2008

This was an explanation of how I wrote a Drupal5.n module to filter javascript code so that it displayed syntax highlighted. Drupal 7 has been released, this site has been upgraded and I decided to display programming code syntax highlighted using the GeShi PHP library. It caters for most languages and there is a geshifilter module to integrate the library into Drupal. Here are some examples:

Javascript

  1. /*
  2.  * comment block
  3.  *
  4.  */
  5. // single line comment
  6. function example(a,c) {
  7. return a * c * 59;
  8. }
  9. document.writeln("this is a text string");
  10. document.writeln('so is this, single quoted');
  11.  

CSS
  1. span.js_comment {
  2. color: Purple;
  3. font-style: italic;
  4. }
  5. span.js_string {
  6. color: Red;
  7. }
  8. span.js_keyword {
  9. color: Blue;
  10. }
  11. span.js_number {
  12. color: Green;
  13. }
PHP
  1. /**
  2.  * Implementation of hook_form_alter().
  3.  */
  4. function pos_form_alter(&$form, $form_state, $form_id) {
  5. if ($form_id == 'comment_node_blog_form') {
  6. // print '<pre>' . htmlspecialchars(print_r($form['author'], TRUE)) . '</pre>'; die;
  7. if (isset($form['author']['name']['#title'])) {
  8. $form['author']['name']['#prefix'] = '<div class="pos-comment-label">'. $form['author']['name']['#title'] .'</div><div class="pos-comment-input">';
  9. $form['author']['name']['#suffix'] = '</div>';
  10. $form['author']['name']['#title'] = '';
  11. }
  12. if (isset($form['author']['mail']['#title'])) {
  13. $form['author']['mail']['#prefix'] = '<div class="pos-comment-label">'. $form['author']['mail']['#title'] .'</div><div class="pos-comment-input">';
  14. $form['author']['mail']['#suffix'] = '</div>';
  15. $form['author']['mail']['#title'] = '';
  16. }
  17. if (isset($form['author']['homepage']['#title'])) {
  18. $form['author']['homepage']['#prefix'] = '<div class="pos-comment-label">'. $form['author']['homepage']['#title'] .'</div><div class="pos-comment-input">';
  19. $form['author']['homepage']['#suffix'] = '</div>';
  20. $form['author']['homepage']['#title'] = '';
  21. }
  22. if (isset($form['subject']['#title'])) {
  23. $form['subject']['#prefix'] = '<div class="pos-comment-label">'. $form['subject']['#title'] .'</div><div class="pos-comment-input">';
  24. $form['subject']['#suffix'] = '</div>';
  25. $form['subject']['#title'] = '';
  26. }
  27. if (isset($form['comment_body']['und']['0']['#title'])) {
  28. $form['comment_body']['und']['0']['#prefix'] = '<div class="pos-comment-label">'. $form['comment_body']['und']['0']['#title'] .'</div><div class="pos-comment-input">';
  29. $form['comment_body']['und']['0']['#suffix'] = '</div>';
  30. $form['comment_body']['und']['0']['#title'] = '';
  31. }
  32. } // comment_form
  33. }
Undefined

Add new comment