| 1 |
<?php |
<?php |
| 2 |
// $Id$ |
// $Id: citation_filter.module,v 1.3 2007/12/15 20:23:14 cwgordon7 Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* Implementation of hook_filter. |
* Implementation of hook_flexifilter_components() |
| 6 |
|
* |
| 7 |
|
* @return An array of components to be used by the Flexifilter module. The keys of this array |
| 8 |
|
* are unique identifiers for the component (called the component class), and the values of the |
| 9 |
|
* array are again arrays, with the following keys: |
| 10 |
|
* - label : A human readable name for the component |
| 11 |
|
* - description (optional) : A human readable description of what the component does (defaults to none) |
| 12 |
|
* - is_container (optional) : TRUE if any of the #contains_ fields are TRUE (set automatically)\ |
| 13 |
|
* - is_advanced (optional) : TRUE if the component should only be shown in component dropdown lists in advanced editing mode (defaults to FALSE) |
| 14 |
|
* - contains_condition (optional) : TRUE if the component has a condition associated with it (defaults to FALSE) |
| 15 |
|
* - contains_components (optional) : TRUE if the component has children components (defaults to FALSE) |
| 16 |
|
* - callback : A callback function which implements the component |
| 17 |
|
* - callback_arguments (optional) : An array of arguments to pass to the callback function (defaults to none) |
| 18 |
|
* - group (optional) : A human readable name of the group that the component belongs to (defaults to "Other") |
| 19 |
*/ |
*/ |
| 20 |
function citation_filter_filter($op, $delta = 0, $format = -1, $text = '') { |
function citation_filter_flexifilter_components() { |
| 21 |
switch ($op) { |
$components = array(); |
| 22 |
case 'list': |
// If, While, etc. |
| 23 |
return array(0 => t('Reference filter')); |
$components['flexifilter_citation_filter'] = array( |
| 24 |
|
'label' => t('Citation / Reference filter'), |
| 25 |
case 'description': |
'description' => t('Citation filter for flexifilter module. Turns citations into references with links at the bottom.'), |
| 26 |
return t('Allows users to post references in the form of [ref:reference], which is replaced by [1] with a reference at the bottom of the page in the format of [1] reference.'); |
'callback' => 'citation_filter_flexifilter_component', |
| 27 |
|
'group' => t('Citation filter'), |
| 28 |
case 'prepare': |
'contains_components' => TRUE, |
| 29 |
return $text; |
'step' => 'either', |
| 30 |
|
); |
| 31 |
case "process": |
return $components; |
|
$info = citation_filter_replace($text, $format); |
|
|
$append = citation_filter_append($info['refs'], $format); |
|
|
$text = $info['text']; |
|
|
return $text . $append; |
|
|
|
|
|
case 'settings': |
|
|
return citation_filter_settings($format); |
|
|
|
|
|
default: |
|
|
return $text; |
|
|
} |
|
| 32 |
} |
} |
| 33 |
|
|
| 34 |
/** |
/** |
| 35 |
* Helper function for citation_filter_filter. |
* Component callback. |
| 36 |
*/ |
*/ |
| 37 |
function citation_filter_replace($text, $format) { |
function citation_filter_flexifilter_component($op, $settings, $text) { |
| 38 |
$sup = variable_get("citation_filter_superscript_$format", TRUE); |
switch ($op) { |
| 39 |
$pos = TRUE; |
case 'settings': |
| 40 |
$postitions = array(); |
$form = array(); |
| 41 |
$return = ''; |
$form['is_append'] = array( |
| 42 |
$references = array(); |
'#type' => 'checkbox', |
| 43 |
$tmp = $text; |
'#title' => t('Is this the componenet where the text should be appended?'), |
| 44 |
$refnum = 1; |
'#default_value' => isset($settings['is_append']) ? $settings['is_append'] : FALSE, |
| 45 |
$links = array(); |
'#description' => t('Check this box if this is the component at which text should be appended.'), |
| 46 |
while ($pos !== FALSE) { |
); |
| 47 |
$pos = stripos($tmp, '[ref:'); |
$form['link_begin'] = array( |
| 48 |
if ($pos !== FALSE) { |
'#type' => 'textfield', |
| 49 |
$return .= substr($tmp, 0, $pos); |
'#title' => t('Link prefix'), |
| 50 |
|
'#default_value' => isset($settings['link_begin']) ? $settings['link_begin'] : '<sup>[', |
| 51 |
|
'#description' => t('What to begin the link to the reference at the bottom of the page with.'), |
| 52 |
|
); |
| 53 |
|
$form['sequence'] = array( |
| 54 |
|
'#type' => 'radios', |
| 55 |
|
'#title' => t('Sequence type'), |
| 56 |
|
'#options' => module_invoke_all('flexifilter_sequences'), |
| 57 |
|
'#default_value' => isset($settings['sequence']) ? $settings['sequence'] : 'flexifilter_sequence_numbers', |
| 58 |
|
); |
| 59 |
|
$form['link_end'] = array( |
| 60 |
|
'#type' => 'textfield', |
| 61 |
|
'#title' => t('Link suffix'), |
| 62 |
|
'#default_value' => isset($settings['link_end']) ? $settings['link_end'] : ']</sup>', |
| 63 |
|
'#description' => t('What to end the link to the reference at the bottom of the page with.'), |
| 64 |
|
); |
| 65 |
|
return $form; |
| 66 |
|
|
| 67 |
$ref = substr($tmp, $pos + 5, (strpos($tmp, ']') - 5) - $pos); |
case 'prepare': |
| 68 |
$pos2 = strpos($ref, '|'); |
case 'process': |
| 69 |
if ($pos2) { |
if (!$settings['is_append']) { |
| 70 |
$link = substr($ref, 0, $pos2); |
static $_flexifilter_sequence_count = 0; |
| 71 |
|
static $_citation_filter_text = array(); |
| 72 |
|
$_citation_filter_text[$_flexifilter_sequence_count] = $text; |
| 73 |
|
$text = '<a href="#citation-'. $_flexifilter_sequence_count .'" rel="nofollow">'. $settings['link_begin'] . call_user_func($settings['sequence'], $_flexifilter_sequence_count) . $settings['link_end'] . '</a>'; |
| 74 |
|
$_flexifilter_sequence_count++; |
| 75 |
|
return $text; |
| 76 |
} |
} |
| 77 |
else { |
else { |
| 78 |
$link = $ref; |
static $_citation_filter_text = array(); |
| 79 |
} |
foreach ($_citation_filter_text as $num => $chunk) { |
| 80 |
|
$chunk = flexifilter_invoke_components($settings['components'], $op, $chunk); |
| 81 |
$keys = array_keys($links); |
$text .= '<br /><a href="#citation-'. $num .'" name="citation-'. $num .'" rel="nofollow">'. $settings['link_begin'] . call_user_func($settings['sequence'], $num) . $settings['link_end'] . '</a>'. $chunk; |
|
if (!in_array($link, $keys)) { |
|
|
$links[$link] = $refnum; |
|
|
$class = '#citation-'. $refnum; |
|
|
if ($sup) { |
|
|
$return .= '<sup><a href="'. $class .'" rel="nofollow">['. $refnum .']</a></sup>'; |
|
| 82 |
} |
} |
| 83 |
else { |
return $text; |
|
$return .= '<a href="'. $class .'" rel="nofollow">['. $refnum .']</a>'; |
|
|
} |
|
|
$refnum++; |
|
| 84 |
} |
} |
|
else { |
|
|
$tmprefnum = $links[$link]; |
|
|
$class = '#citation-'. $tmprefnum; |
|
|
if ($sup) { |
|
|
$return .= '<sup><a href="'. $class .'" rel="nofollow">['. $tmprefnum .']</a></sup>'; |
|
|
} |
|
|
else { |
|
|
$return .= '<a href="'. $class .'" rel="nofollow">['. $tmprefnum .']</a>'; |
|
|
} |
|
|
} |
|
|
|
|
|
$tmp = substr($tmp, $pos + 1); |
|
|
$end = stripos($tmp, ']'); |
|
|
$references[] = substr($tmp, 0, $end); |
|
|
$tmp = substr($tmp, $end + 1); |
|
|
} |
|
|
} |
|
|
$return .= $tmp; |
|
|
return array('text' => $return, 'refs' => $references); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Helper function for citation_filter_filter. |
|
|
*/ |
|
|
function citation_filter_append($refs, $format) { |
|
|
$sup = variable_get("citation_filter_superscript_$format", TRUE); |
|
|
$refnum = 1; |
|
|
$return = '<br />'. variable_get("citation_filter_title_$format", t('<h3>References</h3>')); |
|
|
$links[] = array(); |
|
|
foreach ($refs as $ref) { |
|
|
$pos = strpos($ref, '|'); |
|
|
if ($pos) { |
|
|
$link = substr($ref, 4, $pos - 4); |
|
|
$title = substr($ref, $pos + 1); |
|
|
} |
|
|
else { |
|
|
$ref = substr($ref, 4); |
|
|
$link = $ref; |
|
|
$title = $ref; |
|
|
} |
|
|
// Only add citation if doesn't already exist. |
|
|
if (!in_array($link, $links)) { |
|
|
$links[] = $link; |
|
|
$class = '#citation-'. $refnum; |
|
|
if ($sup) { |
|
|
$return .= '<br /><sup><a href="'. $class .'" name="'. substr($class, 1) .'" rel="nofollow">['. $refnum .']</a></sup>'. l($title, $link); |
|
|
} |
|
|
else { |
|
|
$return .= '<br /><a href="'. $class .'" name="'. substr($class, 1) .'" rel="nofollow">['. $refnum .']</a>'. l($title, $link); |
|
|
} |
|
|
} |
|
|
$refnum++; |
|
| 85 |
} |
} |
|
return $return; |
|
| 86 |
} |
} |
| 87 |
|
|
| 88 |
/** |
/** |
| 89 |
* Return settings form for citation filter. |
* Implementation of hook_flexifilters(). |
| 90 |
*/ |
*/ |
| 91 |
function citation_filter_settings($format) { |
function citation_filter_flexifilters() { |
| 92 |
$form['citation_filter'] = array( |
return array( |
| 93 |
'#type' => 'fieldset', |
array( |
| 94 |
'#title' => t('Citation filter'), |
'label' => 'Citation Filter', |
| 95 |
'#collapsible' => TRUE, |
'description' => 'You may add citations in the form of [ref:link|title].', |
| 96 |
); |
'id' => '15', |
| 97 |
$form['citation_filter']["citation_filter_superscript_$format"] = array( |
'enabled' => true, |
| 98 |
'#type' => 'checkbox', |
'advanced' => false, |
| 99 |
'#title' => t('Use superscript for references.'), |
'delta' => '8', |
| 100 |
'#default_value' => variable_get("citation_filter_superscript_$format", TRUE), |
'components' => array( |
| 101 |
'#description' => t('Whether or not to use superscript for the references. If unchecked, references will appear as [1]; if checked, they will appear as <sup>[1]</sup>.'), |
array( |
| 102 |
); |
'class' => 'flexifilter_chunk_grab', |
| 103 |
$form['citation_filter']["citation_filter_title_$format"] = array( |
'settings' => array( |
| 104 |
'#type' => 'textfield', |
'starts' => '[ref:', |
| 105 |
'#title' => t('The title of the citation section.'), |
'ends' => ']', |
| 106 |
'#default_value' => variable_get("citation_filter_title_$format", t('<h3>References</h3>')), |
'pass_limits' => 0, |
| 107 |
'#description' => t('This will be the title over the references at the bottom of the page. HTML can be used. Use [none] to omit the title.'), |
'case_sensitive' => 0, |
| 108 |
|
'include_rest' => 1, |
| 109 |
|
'step' => 'process', |
| 110 |
|
'components' => array( |
| 111 |
|
array( |
| 112 |
|
'class' => 'flexifilter_citation_filter', |
| 113 |
|
'settings' => array( |
| 114 |
|
'is_append' => 0, |
| 115 |
|
'link_begin' => '<sup>', |
| 116 |
|
'sequence' => 'flexifilter_sequence_numbers', |
| 117 |
|
'link_end' => ']</sup>', |
| 118 |
|
'step' => 'process', |
| 119 |
|
'components' => array( |
| 120 |
|
), |
| 121 |
|
), |
| 122 |
|
'id' => '239', |
| 123 |
|
), |
| 124 |
|
), |
| 125 |
|
), |
| 126 |
|
'id' => '238', |
| 127 |
|
), |
| 128 |
|
array( |
| 129 |
|
'class' => 'flexifilter_citation_filter', |
| 130 |
|
'settings' => array( |
| 131 |
|
'is_append' => 1, |
| 132 |
|
'link_begin' => '<sup>[', |
| 133 |
|
'sequence' => 'flexifilter_sequence_numbers', |
| 134 |
|
'link_end' => ']</sup>', |
| 135 |
|
'step' => 'process', |
| 136 |
|
'components' => array( |
| 137 |
|
array( |
| 138 |
|
'class' => 'flexifilter_link_component', |
| 139 |
|
'settings' => array( |
| 140 |
|
'ordering' => '0', |
| 141 |
|
'divider' => '|', |
| 142 |
|
'no_links' => 1, |
| 143 |
|
'check_path' => 0, |
| 144 |
|
'prefix' => '', |
| 145 |
|
'step' => 'process', |
| 146 |
|
), |
| 147 |
|
'id' => '241', |
| 148 |
|
), |
| 149 |
|
), |
| 150 |
|
), |
| 151 |
|
'id' => '240', |
| 152 |
|
), |
| 153 |
|
'id_next' => 242, |
| 154 |
|
'id_prefix' => 'flexifilter_component_', |
| 155 |
|
), |
| 156 |
|
'fid' => 'new', |
| 157 |
|
), |
| 158 |
); |
); |
|
return $form; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Implementation of hook_filter_tips. |
|
|
*/ |
|
|
function citation_filter_filter_tips($delta, $format, $long = FALSE) { |
|
|
if ($long) { |
|
|
return t('You may post references in the form of [ref:link] or [ref:linkdestination|linktitle]. It will be replaced by a reference in the form of [1], with a reference at the bottom of the page in the form of [1]ClickableCitation.'); |
|
|
} |
|
|
else { |
|
|
return t('You may post references in the form of [ref:link] or [ref:linkdestination|linktitle].'); |
|
|
} |
|
| 159 |
} |
} |