raw
mp-wp_add-footnot...    1 <?php
mp-wp_add-footnot... 2 /*
mp-wp_add-embedda... 3 Plugin Name: MP-WP Content Processing
mp-wp_add-embedda... 4 Plugin URI: http://billymg.com/category/mp-wp/
mp-wp_add-embedda... 5 Description: Allows for the custom processing of article content. Currently supports footnotes and embedded vpatch snippets.
mp-wp_add-embedda... 6 Author: billymg
mp-wp_add-embedda... 7 Author URI: http://billymg.com
mp-wp_add-footnot... 8 */
mp-wp_add-footnot... 9
mp-wp_add-footnot... 10 // Instantiate the class
mp-wp_add-embedda... 11 $mp_wp_content_processing = new mp_wp_content_processing();
mp-wp_add-footnot... 12
mp-wp_add-footnot... 13 // Encapsulate in a class
mp-wp_add-embedda... 14 class mp_wp_content_processing {
mp-wp_add-embedda... 15 const MP_WP_FOOTNOTES_OPEN = " ((";
mp-wp_add-embedda... 16 const MP_WP_FOOTNOTES_CLOSE = "))";
mp-wp_add-embedda... 17 const MP_WP_CODEBLOCKS_PRIORITY = 9; // highest value that comes before wpautop filter
mp-wp_add-embedda... 18 const MP_WP_FOOTNOTES_PRIORITY = 11;
mp-wp_add-embedda... 19
mp-wp_add-embedda... 20 var $options;
mp-wp_add-footnot... 21 var $default_options;
mp-wp_add-footnot... 22
mp-wp_add-footnot... 23 /**
mp-wp_add-footnot... 24 * Constructor.
mp-wp_add-footnot... 25 */
mp-wp_add-embedda... 26 function mp_wp_content_processing() {
mp-wp_add-footnot... 27 // Define the implemented option styles
mp-wp_add-footnot... 28 $this->styles = array(
mp-wp_add-footnot... 29 'decimal' => '1,2...10',
mp-wp_add-footnot... 30 'decimal-leading-zero' => '01, 02...10',
mp-wp_add-footnot... 31 'lower-alpha' => 'a,b...j',
mp-wp_add-footnot... 32 'upper-alpha' => 'A,B...J',
mp-wp_add-footnot... 33 'lower-roman' => 'i,ii...x',
mp-wp_add-footnot... 34 'upper-roman' => 'I,II...X',
mp-wp_add-footnot... 35 'symbol' => 'Symbol'
mp-wp_add-footnot... 36 );
mp-wp_add-footnot... 37
mp-wp_add-footnot... 38 // Define default options
mp-wp_add-embedda... 39 $this->default_options = array(
mp-wp_add-embedda... 40 'pre_backlink'=>' [',
mp-wp_add-embedda... 41 'backlink'=>'&#8617;',
mp-wp_add-embedda... 42 'post_backlink'=>']',
mp-wp_add-embedda... 43 'pre_identifier'=>'',
mp-wp_add-embedda... 44 'list_style_type'=>'decimal',
mp-wp_add-embedda... 45 'list_style_symbol'=>'&dagger;',
mp-wp_add-embedda... 46 'post_identifier'=>'',
mp-wp_add-embedda... 47 'pre_footnotes'=>'',
mp-wp_add-embedda... 48 'post_footnotes'=>''
mp-wp_add-embedda... 49 );
mp-wp_add-footnot... 50
mp-wp_add-footnot... 51 // Get the current settings or setup some defaults if needed
mp-wp_add-embedda... 52 $this->options = get_option('mp_wp_cpp_options');
mp-wp_add-embedda... 53 if (!$this->options) {
mp-wp_add-embedda... 54 $this->options = $this->default_options;
mp-wp_add-embedda... 55 update_option('mp_wp_cpp_options', $this->options);
mp-wp_add-embedda... 56 } else {
mp-wp_add-footnot... 57 // Set any unset options
mp-wp_add-embedda... 58 $updated = false;
mp-wp_add-embedda... 59 foreach ($this->default_options as $key => $value) {
mp-wp_add-embedda... 60 if (!isset($this->options[$key])) {
mp-wp_add-embedda... 61 $this->options[$key] = $value;
mp-wp_add-embedda... 62 $updated = true;
mp-wp_add-footnot... 63 }
mp-wp_add-embedda... 64 }
mp-wp_add-embedda... 65 if ($updated) {
mp-wp_add-embedda... 66 update_option('mp_wp_cpp_options', $this->options);
mp-wp_add-footnot... 67 }
mp-wp_add-footnot... 68 }
mp-wp_add-footnot... 69
mp-wp_add-footnot... 70 // Hook me up
mp-wp_add-embedda... 71 add_action('the_content', array($this, 'process_codeblocks'), self::MP_WP_CODEBLOCKS_PRIORITY);
mp-wp_add-embedda... 72 add_action('the_content', array($this, 'process_footnotes'), self::MP_WP_FOOTNOTES_PRIORITY);
mp-wp_add-embedda... 73 add_filter('the_content', array($this, 'server_side_selection'));
mp-wp_add-footnot... 74 add_action('wp_head', array($this, 'insert_styles'));
mp-wp_add-footnot... 75 }
mp-wp_add-footnot... 76
mp-wp_add-embedda... 77
mp-wp_add-embedda... 78 /**
mp-wp_add-embedda... 79 * Finds the selection from the query params.
mp-wp_add-embedda... 80 * @param $data string The content of the post.
mp-wp_add-embedda... 81 * @return string The new content with the highlighted selection.
mp-wp_add-embedda... 82 */
mp-wp_add-embedda... 83 function server_side_selection($data) {
mp-wp_add-embedda... 84 //bookend code goes here
mp-wp_add-embedda... 85 $b_code = '<span class="mp-wp-selection" id="select">';
mp-wp_add-embedda... 86 $b_code .= $_GET["b"];
mp-wp_add-embedda... 87 $e_code = $_GET["e"].'</span>';
mp-wp_add-embedda... 88
mp-wp_add-embedda... 89 //change page ; last to first to preserve indexes.
mp-wp_add-embedda... 90 $b_pos = strpos($data,$_GET["b"]);
mp-wp_add-embedda... 91 $e_pos = strpos($data,$_GET["e"], $b_pos);
mp-wp_add-embedda... 92 if ($e_pos>0)
mp-wp_add-embedda... 93 $data = substr_replace($data, $e_code, $e_pos, strlen($_GET["e"]));
mp-wp_add-embedda... 94 if ($b_pos>0)
mp-wp_add-embedda... 95 $data = substr_replace($data, $b_code, $b_pos, strlen($_GET["b"]));
mp-wp_add-embedda... 96 return $data;
mp-wp_add-embedda... 97 }
mp-wp_add-embedda... 98
mp-wp_add-embedda... 99 /**
mp-wp_add-embedda... 100 * Searches the text and apply markup to codeblocks.
mp-wp_add-embedda... 101 * Adds line number links and diff syntax highlighting.
mp-wp_add-embedda... 102 * @param $data string The content of the post.
mp-wp_add-embedda... 103 * @return string The new content with formatted codeblocks.
mp-wp_add-embedda... 104 */
mp-wp_add-embedda... 105 function process_codeblocks($data) {
mp-wp_add-embedda... 106 global $post;
mp-wp_add-embedda... 107
mp-wp_add-embedda... 108 // Regex extraction of all codeblocks (or return if there are none)
mp-wp_add-embedda... 109 if ( !preg_match_all("/\[([a-z]+)\[(.*)(\]\])/Us", $data, $codeblocks, PREG_SET_ORDER) ) {
mp-wp_add-embedda... 110 return $data;
mp-wp_add-embedda... 111 }
mp-wp_add-embedda... 112
mp-wp_add-embedda... 113 for ($i = 0; $i < count($codeblocks); $i++) {
mp-wp_add-embedda... 114 $codeblocks[$i]['snippet'] = $this->format_snippet($codeblocks[$i][2], $codeblocks[$i][1], $i+1);
mp-wp_add-embedda... 115 }
mp-wp_add-embedda... 116
mp-wp_add-embedda... 117 foreach ($codeblocks as $key => $value) {
mp-wp_add-embedda... 118 $data = substr_replace($data, $value['snippet'], strpos($data,$value[0]),strlen($value[0]));
mp-wp_add-embedda... 119 }
mp-wp_add-embedda... 120
mp-wp_add-embedda... 121 return $data;
mp-wp_add-embedda... 122 }
mp-wp_add-embedda... 123
mp-wp_add-embedda... 124 function format_snippet($snippet, $syntax, $snippet_number) {
mp-wp_add-embedda... 125 $highlighting_functions = array(
mp-wp_add-embedda... 126 'plaintext' => 'highlight_as_plain_text',
mp-wp_add-embedda... 127 'diff' => 'highlight_as_diff'
mp-wp_add-embedda... 128 );
mp-wp_add-embedda... 129
mp-wp_add-embedda... 130 if (is_null($highlighting_functions[$syntax])) {
mp-wp_add-embedda... 131 $syntax = 'plaintext';
mp-wp_add-embedda... 132 }
mp-wp_add-embedda... 133
mp-wp_add-embedda... 134 $code_lines = explode("\r\n", $snippet);
mp-wp_add-embedda... 135
mp-wp_add-embedda... 136 foreach ($code_lines as $idx => $line) {
mp-wp_add-embedda... 137 $line_number = sprintf('S%d-L%d', $snippet_number, $idx+1);
mp-wp_add-embedda... 138 $line_link = sprintf('<a href="#%s" name="%s">%d</a>', $line_number, $line_number, $idx+1);
mp-wp_add-embedda... 139 $line_open = sprintf('<tr><td class="line-number-column">%s</td><td class="content-column">', $line_link);
mp-wp_add-embedda... 140 $line_close = '</td></tr>';
mp-wp_add-embedda... 141
mp-wp_add-embedda... 142 $code_lines[$idx] = $this->$highlighting_functions[$syntax]($line_open, $line, $line_close);
mp-wp_add-embedda... 143 }
mp-wp_add-embedda... 144
mp-wp_add-embedda... 145 $formatted_snippet = implode("\n", $code_lines);
mp-wp_add-embedda... 146
mp-wp_add-embedda... 147 $formatted_snippet = sprintf(
mp-wp_add-embedda... 148 '%s%s%s',
mp-wp_add-embedda... 149 '<div class="mp-wp-codeblock"><table cellpadding="0" cellspacing="0"><tbody>',
mp-wp_add-embedda... 150 $formatted_snippet,
mp-wp_add-embedda... 151 '</tbody></table></div>'
mp-wp_add-embedda... 152 );
mp-wp_add-embedda... 153
mp-wp_add-embedda... 154 return $formatted_snippet;
mp-wp_add-embedda... 155 }
mp-wp_add-embedda... 156
mp-wp_add-embedda... 157 function highlight_as_plain_text($line_open, $line, $line_close) {
mp-wp_add-embedda... 158 return sprintf('%s<span class="line-default">%s</span>%s', $line_open, $line, $line_close);
mp-wp_add-embedda... 159 }
mp-wp_add-embedda... 160
mp-wp_add-embedda... 161 function highlight_as_diff($line_open, $line, $line_close) {
mp-wp_add-embedda... 162 if (substr($line, 0, 5) == 'diff ') {
mp-wp_add-embedda... 163 $highlighted_line = sprintf('%s<span class="line-filename">%s</span>%s', $line_open, $line, $line_close);
mp-wp_add-embedda... 164 } elseif (substr($line, 0, 4) == '--- ' || substr($line, 0, 4) == '+++ ' || substr($line, 0, 3) == '@@ ') {
mp-wp_add-embedda... 165 $highlighted_line = sprintf('%s<span class="line-meta">%s</span>%s', $line_open, $line, $line_close);
mp-wp_add-embedda... 166 } elseif (substr($line, 0, 1) == '-') {
mp-wp_add-embedda... 167 $highlighted_line = sprintf('%s<span class="line-removed">%s</span>%s', $line_open, $line, $line_close);
mp-wp_add-embedda... 168 } elseif (substr($line, 0, 1) == '+') {
mp-wp_add-embedda... 169 $highlighted_line = sprintf('%s<span class="line-added">%s</span>%s', $line_open, $line, $line_close);
mp-wp_add-embedda... 170 } else {
mp-wp_add-embedda... 171 $highlighted_line = sprintf('%s<span class="line-default">%s</span>%s', $line_open, $line, $line_close);
mp-wp_add-embedda... 172 }
mp-wp_add-embedda... 173
mp-wp_add-embedda... 174 return $highlighted_line;
mp-wp_add-embedda... 175 }
mp-wp_add-embedda... 176
mp-wp_add-footnot... 177 /**
mp-wp_add-footnot... 178 * Searches the text and extracts footnotes.
mp-wp_add-footnot... 179 * Adds the identifier links and creats footnotes list.
mp-wp_add-footnot... 180 * @param $data string The content of the post.
mp-wp_add-footnot... 181 * @return string The new content with footnotes generated.
mp-wp_add-footnot... 182 */
mp-wp_add-embedda... 183 function process_footnotes($data) {
mp-wp_add-footnot... 184 global $post;
mp-wp_add-footnot... 185
mp-wp_add-footnot... 186 // Check for and setup the starting number
mp-wp_add-footnot... 187 $start_number = (preg_match("|<!\-\-startnum=(\d+)\-\->|",$data,$start_number_array)==1) ? $start_number_array[1] : 1;
mp-wp_add-footnot... 188
mp-wp_add-footnot... 189 // Regex extraction of all footnotes (or return if there are none)
mp-wp_add-embedda... 190 if (!preg_match_all("/(".preg_quote(self::MP_WP_FOOTNOTES_OPEN)."|<footnote>)(.*)(".preg_quote(self::MP_WP_FOOTNOTES_CLOSE)."|<\/footnote>)/Us", $data, $identifiers, PREG_SET_ORDER)) {
mp-wp_add-footnot... 191 return $data;
mp-wp_add-footnot... 192 }
mp-wp_add-footnot... 193
mp-wp_add-footnot... 194 $footnotes = array();
mp-wp_add-footnot... 195
mp-wp_add-footnot... 196 // Check if this post is using a different list style to the settings
mp-wp_add-footnot... 197 if ( array_key_exists(get_post_meta($post->ID, 'footnote_style', true), $this->styles) ) {
mp-wp_add-footnot... 198 $style = get_post_meta($post->ID, 'footnote_style', true);
mp-wp_add-footnot... 199 } else {
mp-wp_add-embedda... 200 $style = $this->options['list_style_type'];
mp-wp_add-footnot... 201 }
mp-wp_add-footnot... 202
mp-wp_add-footnot... 203 // Create 'em
mp-wp_add-footnot... 204 for ($i=0; $i<count($identifiers); $i++){
mp-wp_add-footnot... 205 // Look for ref: and replace in identifiers array.
mp-wp_add-footnot... 206 if (substr($identifiers[$i][2],0,4) == 'ref:'){
mp-wp_add-footnot... 207 $ref = (int)substr($identifiers[$i][2],4);
mp-wp_add-footnot... 208 $identifiers[$i]['text'] = $identifiers[$ref-1][2];
mp-wp_add-footnot... 209 }else{
mp-wp_add-footnot... 210 $identifiers[$i]['text'] = $identifiers[$i][2];
mp-wp_add-footnot... 211 }
mp-wp_add-footnot... 212
mp-wp_add-embedda... 213 // Add footnote and record the key
mp-wp_add-embedda... 214 $identifiers[$i]['use_footnote'] = count($footnotes);
mp-wp_add-embedda... 215 $footnotes[$identifiers[$i]['use_footnote']]['text'] = $identifiers[$i]['text'];
mp-wp_add-embedda... 216 $footnotes[$identifiers[$i]['use_footnote']]['symbol'] = $identifiers[$i]['symbol'];
mp-wp_add-embedda... 217 $footnotes[$identifiers[$i]['use_footnote']]['identifiers'][] = $i;
mp-wp_add-footnot... 218 }
mp-wp_add-footnot... 219
mp-wp_add-footnot... 220 // Footnotes and identifiers are stored in the array
mp-wp_add-footnot... 221 $use_full_link = false;
mp-wp_add-footnot... 222
mp-wp_add-footnot... 223 if (is_feed()) $use_full_link = true;
mp-wp_add-footnot... 224
mp-wp_add-footnot... 225 if (is_preview()) $use_full_link = false;
mp-wp_add-footnot... 226
mp-wp_add-footnot... 227 // Display identifiers
mp-wp_add-footnot... 228 foreach ($identifiers as $key => $value) {
mp-wp_add-footnot... 229 $id_id = "identifier_".$key."_".$post->ID;
mp-wp_add-footnot... 230 $id_num = ($style == 'decimal') ? $value['use_footnote']+$start_number : $this->convert_num($value['use_footnote']+$start_number, $style, count($footnotes));
mp-wp_add-footnot... 231 $id_href = ( ($use_full_link) ? get_permalink($post->ID) : '' ) . "#footnote_".$value['use_footnote']."_".$post->ID;
mp-wp_add-footnot... 232 $id_title = str_replace('"', '`', strip_tags($value['text']));
mp-wp_add-embedda... 233 $id_replace = '<sup>'.$this->options['pre_identifier'].'<a href="'.$id_href.'" id="'.$id_id.'" class="footnote-link footnote-identifier-link" title="'.$id_title.'">'.$id_num.'</a>'.$this->options['post_identifier'].'</sup>';
mp-wp_add-embedda... 234 $data = substr_replace($data, $id_replace, strpos($data,$value[0]),strlen($value[0]));
mp-wp_add-footnot... 235 }
mp-wp_add-footnot... 236
mp-wp_add-footnot... 237 // Display footnotes
mp-wp_add-embedda... 238 $start = ($start_number != 1) ? 'start="'.$start_number.'" ' : '';
mp-wp_add-embedda... 239 $data = $data.$this->options['pre_footnotes'];
mp-wp_add-embedda... 240
mp-wp_add-embedda... 241 $data = $data . '<ol '.$start.'class="footnotes">';
mp-wp_add-embedda... 242 foreach ($footnotes as $key => $value) {
mp-wp_add-embedda... 243 $data = $data.'<li id="footnote_'.$key.'_'.$post->ID.'" class="footnote"';
mp-wp_add-embedda... 244 if ($style == 'symbol') {
mp-wp_add-embedda... 245 $data = $data . ' style="list-style-type:none;"';
mp-wp_add-embedda... 246 } elseif($style != $this->options['list_style_type']) {
mp-wp_add-embedda... 247 $data = $data . ' style="list-style-type:' . $style . ';"';
mp-wp_add-embedda... 248 }
mp-wp_add-embedda... 249 $data = $data . '>';
mp-wp_add-embedda... 250 if ($style == 'symbol') {
mp-wp_add-embedda... 251 $data = $data . '<span class="symbol">' . $this->convert_num($key+$start_number, $style, count($footnotes)) . '</span> ';
mp-wp_add-embedda... 252 }
mp-wp_add-embedda... 253 $data = $data.$value['text'];
mp-wp_add-embedda... 254 if (!is_feed()){
mp-wp_add-embedda... 255 foreach($value['identifiers'] as $identifier){
mp-wp_add-embedda... 256 $data = $data.$this->options['pre_backlink'].'<a href="'.( ($use_full_link) ? get_permalink($post->ID) : '' ).'#identifier_'.$identifier.'_'.$post->ID.'" class="footnote-link footnote-back-link">'.$this->options['backlink'].'</a>'.$this->options['post_backlink'];
mp-wp_add-footnot... 257 }
mp-wp_add-footnot... 258 }
mp-wp_add-embedda... 259 $data = $data . '</li>';
mp-wp_add-footnot... 260 }
mp-wp_add-embedda... 261 $data = $data . '</ol>' . $this->options['post_footnotes'];
mp-wp_add-footnot... 262
mp-wp_add-footnot... 263 return $data;
mp-wp_add-footnot... 264 }
mp-wp_add-footnot... 265
mp-wp_add-embedda... 266 function insert_styles() {
mp-wp_add-footnot... 267 ?>
mp-wp_add-footnot... 268 <style type="text/css">
mp-wp_add-embedda... 269 ol.footnotes { font-size: 0.8em; color: #666; }
mp-wp_add-embedda... 270 a.footnote-link,
mp-wp_add-embedda... 271 td.line-number-column {
mp-wp_add-embedda... 272 -moz-user-select: none;
mp-wp_add-embedda... 273 -webkit-user-select: none;
mp-wp_add-embedda... 274 user-select: none;
mp-wp_add-embedda... 275 }
mp-wp_add-embedda... 276 div.mp-wp-codeblock {
mp-wp_add-embedda... 277 background: none;
mp-wp_add-embedda... 278 font-family: monospace;
mp-wp_add-embedda... 279 color: #333;
mp-wp_add-embedda... 280 border: 1px solid #ddd;
mp-wp_add-embedda... 281 padding: 0;
mp-wp_add-embedda... 282 overflow: auto;
mp-wp_add-embedda... 283 }
mp-wp_add-embedda... 284 td.line-number-column { background: #f5f6f7; text-align: right; vertical-align: top; }
mp-wp_add-embedda... 285 td.line-number-column a { color: #555; padding: 0 5px; }
mp-wp_add-embedda... 286 td.content-column {
mp-wp_add-embedda... 287 padding-left: 10px;
mp-wp_add-embedda... 288 white-space: pre-wrap;
mp-wp_add-embedda... 289 word-break: break-word;
mp-wp_add-embedda... 290 tab-size: 4;
mp-wp_add-embedda... 291 -moz-tab-size: 4;
mp-wp_add-embedda... 292 max-width: 100%; /* adjust, if necessary, to fit your blog's viewport */
mp-wp_add-embedda... 293 }
mp-wp_add-embedda... 294 span.line-filename { font-weight: bold; }
mp-wp_add-embedda... 295 span.line-meta {
mp-wp_add-embedda... 296 display: block;
mp-wp_add-embedda... 297 color: #999;
mp-wp_add-embedda... 298 word-wrap: break-word;
mp-wp_add-embedda... 299 word-break: break-all;
mp-wp_add-embedda... 300 }
mp-wp_add-embedda... 301 span.line-added { color: green; }
mp-wp_remove-all-... 302 span.line-removed { color: red; }
mp-wp_add-embedda... 303
mp-wp_add-embedda... 304 <?php if ($this->options['list_style_type'] != 'symbol'): ?>
mp-wp_add-embedda... 305 ol.footnotes li { list-style-type: <?php echo $this->options['list_style_type']; ?>; }
mp-wp_add-footnot... 306 <?php endif; ?>
mp-wp_add-embedda... 307
mp-wp_add-embedda... 308 span.mp-wp-selection { background-color: #d3d3d3; }
mp-wp_add-footnot... 309 </style>
mp-wp_add-footnot... 310 <?php
mp-wp_add-footnot... 311 }
mp-wp_add-footnot... 312
mp-wp_add-footnot... 313
mp-wp_add-embedda... 314 function convert_num ($num, $style, $total) {
mp-wp_add-footnot... 315 switch ($style) {
mp-wp_add-footnot... 316 case 'decimal-leading-zero' :
mp-wp_add-footnot... 317 $width = max(2, strlen($total));
mp-wp_add-footnot... 318 return sprintf("%0{$width}d", $num);
mp-wp_add-footnot... 319 case 'lower-roman' :
mp-wp_add-footnot... 320 return $this->roman($num, 'lower');
mp-wp_add-footnot... 321 case 'upper-roman' :
mp-wp_add-footnot... 322 return $this->roman($num);
mp-wp_add-footnot... 323 case 'lower-alpha' :
mp-wp_add-footnot... 324 return $this->alpha($num, 'lower');
mp-wp_add-footnot... 325 case 'upper-alpha' :
mp-wp_add-footnot... 326 return $this->alpha($num);
mp-wp_add-footnot... 327 case 'symbol' :
mp-wp_add-footnot... 328 $sym = '';
mp-wp_add-footnot... 329 for ($i = 0; $i<$num; $i++) {
mp-wp_add-embedda... 330 $sym .= $this->options['list_style_symbol'];
mp-wp_add-footnot... 331 }
mp-wp_add-footnot... 332 return $sym;
mp-wp_add-footnot... 333 }
mp-wp_add-footnot... 334 }
mp-wp_add-footnot... 335
mp-wp_add-footnot... 336
mp-wp_add-footnot... 337 /**
mp-wp_add-footnot... 338 * Convert to a roman numeral.
mp-wp_add-footnot... 339 *
mp-wp_add-footnot... 340 * Thanks to Indi.in.the.Wired for the improved algorithm.
mp-wp_add-footnot... 341 * http://plugins.trac.wordpress.org/ticket/1177
mp-wp_add-footnot... 342 *
mp-wp_add-footnot... 343 * @param int $num The number to convert.
mp-wp_add-footnot... 344 * @param string $case Upper or lower case.
mp-wp_add-footnot... 345 * @return string The roman numeral
mp-wp_add-footnot... 346 */
mp-wp_add-embedda... 347 function roman($num, $case= 'upper') {
mp-wp_add-footnot... 348 $num = (int) $num;
mp-wp_add-footnot... 349 $conversion = array('M'=>1000, 'CM'=>900, 'D'=>500, 'CD'=>400, 'C'=>100, 'XC'=>90, 'L'=>50, 'XL'=>40, 'X'=>10, 'IX'=>9, 'V'=>5, 'IV'=>4, 'I'=>1);
mp-wp_add-footnot... 350 $roman = '';
mp-wp_add-footnot... 351
mp-wp_add-footnot... 352 foreach ($conversion as $r => $d){
mp-wp_add-footnot... 353 $roman .= str_repeat($r, (int)($num / $d));
mp-wp_add-footnot... 354 $num %= $d;
mp-wp_add-footnot... 355 }
mp-wp_add-footnot... 356
mp-wp_add-footnot... 357 return ($case == 'lower') ? strtolower($roman) : $roman;
mp-wp_add-footnot... 358 }
mp-wp_add-footnot... 359
mp-wp_add-embedda... 360 function alpha($num, $case='upper') {
mp-wp_add-footnot... 361 $j = 1;
mp-wp_add-footnot... 362 for ($i = 'A'; $i <= 'ZZ'; $i++){
mp-wp_add-footnot... 363 if ($j == $num){
mp-wp_add-footnot... 364 if ($case == 'lower')
mp-wp_add-footnot... 365 return strtolower($i);
mp-wp_add-footnot... 366 else
mp-wp_add-footnot... 367 return $i;
mp-wp_add-footnot... 368 }
mp-wp_add-footnot... 369 $j++;
mp-wp_add-footnot... 370 }
mp-wp_add-footnot... 371 }
mp-wp_add-footnot... 372 }