-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-footnotes.php
140 lines (111 loc) · 4.93 KB
/
web-footnotes.php
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/*
Plugin Name: Karoo Footnotes
Plugin URI: https://defomicron.net/projects/civil_footnotes
Version: 1.4.0
Description: Parses and displays footnotes.
Author: <a href="https://pixeline.be">Alexandre Plennevaux</a> - <a href="https://defomicron.net/colophon">Austin Sweeney</a>
*/
// If you’d like to edit the output, scroll down to the
// “Display the footnotes” section near the end of this file.
// Some important constants
define('WP_FOOTNOTES_OPEN', " ((");
define('WP_FOOTNOTES_CLOSE', "))");
define('WP_FOOTNOTES_VERSION', '1.3.1');
// Instantiate the class
$Web_Footnotes = new Web_Footnotes();
// Encapsulate in a class
class Web_Footnotes {
var $current_options;
var $default_options;
/**
* Constructor.
*/
function __construct() {
// Define the implemented option styles
$this->styles = array(
'decimal' => '1,2...10',
'decimal-leading-zero' => '01, 02...10',
'lower-alpha' => 'a,b...j',
'upper-alpha' => 'A,B...J',
'lower-roman' => 'i,ii...x',
'upper-roman' => 'I,II...X',
'symbol' => 'Symbol'
);
// Hook me up
add_action('the_content', array($this, 'process'), 11);
}
/**
* Searches the text and extracts footnotes.
* Adds the identifier links and creats footnotes list.
* @param $data string The content of the post.
* @return string The new content with footnotes generated.
*/
function process($data) {
global $post;
// Check for and setup the starting number
$start_number = (preg_match("|<!\-\-startnum=(\d+)\-\->|", $data, $start_number_array)==1) ? $start_number_array[1] : 1;
// Regex extraction of all footnotes (or return if there are none)
if (!preg_match_all("/(".preg_quote(WP_FOOTNOTES_OPEN)."|<footnote>)(.*)(".preg_quote(WP_FOOTNOTES_CLOSE)."|<\/footnote>)/Us", $data, $identifiers, PREG_SET_ORDER)) {
return $data;
}
$display = true;
$footnotes = array();
$style = 'decimal';
// Create 'em
for ($i=0; $i<count($identifiers); $i++){
// Look for ref: and replace in identifiers array.
if (substr($identifiers[$i][2], 0, 4) == 'ref:'){
$ref = (int)substr($identifiers[$i][2], 4);
$identifiers[$i]['text'] = $identifiers[$ref-1][2];
}else{
$identifiers[$i]['text'] = $identifiers[$i][2];
}
if (!isset($identifiers[$i]['use_footnote'])){
// Add footnote and record the key
$identifiers[$i]['use_footnote'] = count($footnotes);
$footnotes[$identifiers[$i]['use_footnote']]['text'] = $identifiers[$i]['text'];
$footnotes[$identifiers[$i]['use_footnote']]['symbol'] = ( array_key_exists( 'symbol', $identifiers[$i] ) ) ? $identifiers[$i]['symbol'] : ''; // Bugfix submitted by Greg Sullivan
$footnotes[$identifiers[$i]['use_footnote']]['identifiers'][] = $i;
}
}
// Footnotes and identifiers are stored in the array
$use_full_link = false;
if (is_feed()) $use_full_link = true;
if (is_preview()) $use_full_link = false;
// Display identifiers
$datanote = ''; // Bugfix submitted by Greg Sullivan
foreach ($identifiers as $key => $value) {
$id_num = ($style == 'decimal') ? $value['use_footnote']+$start_number : $this->convert_num($value['use_footnote']+$start_number, $style, count($footnotes));
$id_id = "rf".$id_num."-".$post->ID;
$id_href = ( ($use_full_link) ? get_permalink($post->ID) : '' ) . "#fn".$id_num."-".$post->ID;
$id_title = str_replace('"', """, htmlentities(html_entity_decode(strip_tags($value['text']), ENT_QUOTES, 'UTF-8'), ENT_QUOTES, 'UTF-8'));
$id_replace = '<sup id="'.$id_id.'"><a href="'.$id_href.'" title="'.$id_title.'" rel="footnote">'.$id_num.'</a></sup>';
if ($display) $data = substr_replace($data, $id_replace, strpos($data, $value[0]), strlen($value[0]));
else $data = substr_replace($data, '', strpos($data, $value[0]), strlen($value[0]));
// Display the footnotes (here is where you can change the output)
// Create each footnote
$datanote = $datanote.'<li id="fn'.$id_num.'-'.$post->ID.'">'; // You can add a class to the list item
$datanote = $datanote.'<p>'; // Before the footnote
$datanote = $datanote.$value['text'].' <a href="#'.$id_id.'"'; // The footnote (don't change this)
$datanote = $datanote.' class="backlink" title="Jump back to footnote '.$id_num.' in the text.">'; // You can change the class or hover text
$datanote = $datanote.'↩'; // The backlink character (↩)... ↲ (↲) is another common one
$datanote = $datanote.'</a></p></li>'; // After the footnote
}
// Create the footnotes
foreach ($footnotes as $key => $value) {
$data = $data.'<hr class="footnotes"><ol class="footnotes"'; // Before the footnotes
if ($start_number != '1') $data = $data.' start="'.$start_number.'"';
$data = $data.'>';
$data = $data.$datanote; // Don't change this
$data = $data.'</ol>'; // After the footnotes
return $data;
}
}
function upgrade_post($data){
$data = str_replace('<footnote>', WP_FOOTNOTES_OPEN, $data);
$data = str_replace('</footnote>', WP_FOOTNOTES_CLOSE, $data);
return $data;
}
}
?>