OSDN Git Service

BugTrack2/62: Do remove the whole design, 'Showing TrackBack-ping list by html'.
[pukiwiki/pukiwiki.git] / plugin / aname.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: aname.inc.php,v 1.23 2005/05/07 07:26:25 henoheno Exp $
4 // Copyright (C)
5 //   2002-2005 PukiWiki Developers Team
6 //   2001-2002 Originally written by yu-ji
7 // License: GPL v2 or (at your option) any later version
8 //
9 // aname plugin - Set various anchor tag
10 //   * A simple anchor <a id="key"></a>
11 //   * A clickable link to the anchor <a href="#key">string</a>
12 //   * Clickable anchor with the key itself <a id="key" href="#key">string</a>
13 //
14 // NOTE:
15 //   Use 'id="key"' instead of 'name="key"' at XHTML 1.1
16
17 // Check ID is unique or not (compatible: no-check)
18 define('PLUGIN_ANAME_ID_MUST_UNIQUE', 0);
19
20 // Max length of ID
21 define('PLUGIN_ANAME_ID_MAX',   40);
22
23 // Pattern of ID
24 define('PLUGIN_ANAME_ID_REGEX', '/^[A-Za-z][\w\-]*$/');
25
26
27 // #aname
28 function plugin_aname_convert()
29 {
30         $args = func_get_args(); // Zero or more
31         return plugin_aname_tag($args);
32 }
33
34 // &aname;
35 function plugin_aname_inline()
36 {
37         $args = func_get_args(); // ONE or more
38
39         // strip_htmltag() is just for avoiding AutoLink insertion
40         $body = strip_htmltag(array_pop($args));
41         array_push($args, $body);
42
43         return plugin_aname_tag($args, FALSE);
44 }
45
46 // Show usage
47 function plugin_aname_usage($convert = TRUE, $message = '')
48 {
49         if ($message == '') {
50                 if ($convert) {
51                         return '#aname(anchorID[[,super][,full][,noid],Link title])';
52                 } else {
53                         return '&amp;aname(anchorID[,super][,full][,noid]){[Link title]}';
54                 }
55         } else {
56                 if ($convert) {
57                         return '#aname: ' . $message;
58                 } else {
59                         return '&amp;aname: ' . $message . ';';
60                 }
61         }
62 }
63
64 // Aname plugin itself
65 function plugin_aname_tag($args = array(), $convert = TRUE)
66 {
67         global $vars;
68         static $_id = array();
69
70         if (empty($args) || $args[0] == '') return plugin_aname_usage($convert);
71         $id = array_shift($args);
72         $body = '';
73         if (! empty($args)) $body = array_pop($args);
74         $f_noid  = in_array('noid',  $args); // Option: Without id attribute
75         $f_super = in_array('super', $args); // Option: CSS class
76         $f_full  = in_array('full',  $args); // Option: With full(absolute) URI
77
78         if ($body == '') {
79                 if ($f_noid)  return plugin_aname_usage($convert, 'Meaningless(No link-title with \'noid\')');
80                 if ($f_super) return plugin_aname_usage($convert, 'Meaningless(No link-title with \'super\')');
81                 if ($f_full)  return plugin_aname_usage($convert, 'Meaningless(No link-title with \'full\')');
82         }
83
84         if (PLUGIN_ANAME_ID_MUST_UNIQUE && isset($_id[$id]) && ! $f_noid) {
85                 return plugin_aname_usage($convert, 'ID already used: '. $id);
86         } else {
87                 if (strlen($id) > PLUGIN_ANAME_ID_MAX)
88                         return plugin_aname_usage($convert, 'ID too long');
89                 if (! preg_match(PLUGIN_ANAME_ID_REGEX, $id))
90                         return plugin_aname_usage($convert, 'Invalid ID string: ' .
91                                 htmlspecialchars($id));
92                 $_id[$id] = TRUE; // Set
93         }
94
95         if ($convert) $body = htmlspecialchars($body);
96         $id = htmlspecialchars($id); // Insurance
97         $class   = $f_super ? 'anchor_super' : 'anchor';
98         $attr_id = $f_noid  ? '' : ' id="' . $id . '"';
99         $url     = $f_full  ? get_script_uri() . '?' . rawurlencode($vars['page']) : '';
100         if ($body != '') {
101                 $href  = ' href="' . $url . '#' . $id . '"';
102                 $title = ' title="' . $id . '"';
103         } else {
104                 $href = $title = '';
105         }
106
107         return '<a class="' . $class . '"' . $attr_id . $href . $title . '>' .
108                 $body . '</a>';
109 }
110 ?>