-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrhymes.module
76 lines (66 loc) · 1.57 KB
/
rhymes.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* @file
* Rhymes module.
*/
/**
* Implements hook_menu().
*/
function rhymes_menu() {
$items = array();
$items['rhymes'] = array(
'page callback' => 'rhymes_view',
'access callback' => TRUE,
);
$items['rhymes/%'] = array(
'page callback' => 'rhymes_rhyme_view',
'page arguments' => array(1),
'access callback' => TRUE,
);
return $items;
}
/**
* Page callback for /rhymes.
*/
function rhymes_view() {
$variables = array(
'items' => array(),
'title' => t('Rhymes'),
'type' => 'ul',
);
foreach (rhymes_get_rhymes() as $name => $filepath) {
$variables['items'][]['data'] = l($name, "rhymes/$name");
}
return theme('item_list', $variables);
}
/**
* Page callback for rhymes/%.
*/
function rhymes_rhyme_view($name) {
$rhymes = rhymes_get_rhymes();
// Error handling.
if (!isset($rhymes[$name])) {
drupal_set_message(t('Sorry. No rhyme by this name is available: !name',
array('!name' => $name)), 'warning');
return;
}
// Get rhyme. Format plain text.
$contents = file_get_contents($rhymes[$name]);
$contents = check_plain($contents);
$output = check_markup($contents, 'plain_text');
return $output;
}
/**
* Returns array of file paths from rhymes directory keyed by name.
*
* @return array
* Rhyme files keyed by file name.
*/
function rhymes_get_rhymes() {
$rhymes = array();
$directory = drupal_get_path('module', 'rhymes') . '/rhymes';
foreach (file_scan_directory($directory, '/.*/') as $file) {
$rhymes[$file->name] = "$directory/{$file->filename}";
}
return $rhymes;
}