| 1 |
|
<?php |
| 2 |
|
|
| 3 |
|
function dialectic_menu() { |
| 4 |
|
$items = array(); |
| 5 |
|
|
| 6 |
|
$items['dialectic'] = array( |
| 7 |
|
'title' => 'Dialectic', |
| 8 |
|
'page callback' => 'drupal_get_form', |
| 9 |
|
'page arguments' => array('dialectic_form'), |
| 10 |
|
'access arguments' => array('access dialect translator'), |
| 11 |
|
); |
| 12 |
|
|
| 13 |
|
return $items; |
| 14 |
|
} |
| 15 |
|
|
| 16 |
|
function dialectic_perm() { |
| 17 |
|
return array('access dialect translator'); |
| 18 |
|
} |
| 19 |
|
|
| 20 |
|
function _dialectic_filters() { |
| 21 |
|
return array_flip(array( |
| 22 |
|
'Pirate' => 'pirate', |
| 23 |
|
'Swedish chef' => 'chef', |
| 24 |
|
'Elmer Fudd' => 'fudd', |
| 25 |
|
'Morse code' => 'morse', |
| 26 |
|
'Pig latin' => 'pig', |
| 27 |
|
'Rot13' => 'rot13', |
| 28 |
|
'Underwater' => 'ubby', |
| 29 |
|
'w4r3z d00dz' => 'warez', |
| 30 |
|
'Jive' => 'jive', |
| 31 |
|
'Upside-down' => 'upsidedown', |
| 32 |
|
)); |
| 33 |
|
} |
| 34 |
|
|
| 35 |
|
function dialectic_filter_tips($delta, $format, $long = FALSE) { |
| 36 |
|
if ($long) { |
| 37 |
|
return t("This site allows you to use tags like [chef] and [rot13] to automatically translate text. To use them when you write, surround the text you want to translate or alter [chef]like this.[/chef]. Valid dialects are: !dialects.", array('!dialects' => implode(', ', array_keys(_dialectic_filters())))); |
| 38 |
|
} |
| 39 |
|
else { |
| 40 |
|
return t('Text can be translated into novelty dialects using [chef]tags like this.[/chef]'); |
| 41 |
|
} |
| 42 |
|
} |
| 43 |
|
|
| 44 |
|
/** |
| 45 |
|
* Implementation of hook_filter(). |
| 46 |
|
*/ |
| 47 |
|
function dialectic_filter($op, $delta = 0, $format = -1, $text = '') { |
| 48 |
|
if ($op == 'list') { |
| 49 |
|
return array(0 => t('Dialectic filter')); |
| 50 |
|
} |
| 51 |
|
switch ($delta) { |
| 52 |
|
case 0: |
| 53 |
|
switch ($op) { |
| 54 |
|
case 'description': |
| 55 |
|
return t('Lets writers use a variety of BB-code style tags to translate their text into other dialects. Supported dialects include: !dialects.', array('!dialects' => implode(', ', array_values(_dialectic_filters())))); |
| 56 |
|
|
| 57 |
|
case 'prepare': |
| 58 |
|
return $text; |
| 59 |
|
|
| 60 |
|
case 'process': |
| 61 |
|
$filters = implode('|', array_keys(_dialectic_filters())); |
| 62 |
|
$pattern = "/\[(" . $filters .")\](.*)\[\/(" . $filters .")\]/e"; |
| 63 |
|
return preg_replace($pattern, "dialectic_replace('\\2', '\\1')", $text); |
| 64 |
|
} |
| 65 |
|
break; |
| 66 |
|
} |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
function dialectic_form($form_state) { |
| 70 |
|
$form['#cache'] = FALSE; |
| 71 |
|
|
| 72 |
|
if (!empty($form_state['values'])) { |
| 73 |
|
$form['results'] = array( |
| 74 |
|
'#type' => 'item', |
| 75 |
|
'#title' => t('Results'), |
| 76 |
|
'#value' => dialectic_replace(strip_tags($form_state['values']['text']), $form_state['values']['dialect']), |
| 77 |
|
); |
| 78 |
|
} |
| 79 |
|
|
| 80 |
|
$form['dialect'] = array( |
| 81 |
|
'#type' => 'select', |
| 82 |
|
'#title' => t('Dialect'), |
| 83 |
|
'#options' => _dialectic_filters(), |
| 84 |
|
'#default_value' => empty($form_state['values']['dialect']) ? '' : $form_state['values']['dialect'], |
| 85 |
|
); |
| 86 |
|
|
| 87 |
|
$form['text'] = array( |
| 88 |
|
'#type' => 'textarea', |
| 89 |
|
'#title' => t('Text to translate'), |
| 90 |
|
'#default_value' => empty($form_state['values']['text']) ? '' : $form_state['values']['text'], |
| 91 |
|
'#rows' => 10, |
| 92 |
|
); |
| 93 |
|
|
| 94 |
|
$form['submit'] = array( |
| 95 |
|
'#type' => 'submit', |
| 96 |
|
'#value' => 'Submit', |
| 97 |
|
); |
| 98 |
|
|
| 99 |
|
return $form; |
| 100 |
|
} |
| 101 |
|
|
| 102 |
|
function dialectic_form_submit($form, &$form_state) { |
| 103 |
|
$form_state['rebuild'] = TRUE; |
| 104 |
|
$form_state['redirect'] = FALSE; |
| 105 |
|
} |
| 106 |
|
|
| 107 |
|
|
| 108 |
|
function dialectic_replace($text, $type) { |
| 109 |
|
module_load_include('inc', 'dialectic'); |
| 110 |
|
$function = 'dialectic_' . $type; |
| 111 |
|
if (function_exists($function)) { |
| 112 |
|
return $function($text); |
| 113 |
|
} |
| 114 |
|
else { |
| 115 |
|
return $text; |
| 116 |
|
} |
| 117 |
|
} |