Skip to content

Commit 2321d71

Browse files
committed
Move link adaption into its own method, adapt links in footnotes
1 parent 0d11888 commit 2321d71

1 file changed

Lines changed: 82 additions & 59 deletions

File tree

helper.php

Lines changed: 82 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -316,16 +316,9 @@ function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $inc
316316
$sect_title = false;
317317
$endpos = null; // end position of the raw wiki text
318318

319-
for($i=0; $i<$num; $i++) {
320-
// adjust links with image titles
321-
if (strpos($ins[$i][0], 'link') !== false && isset($ins[$i][1][1]) && is_array($ins[$i][1][1]) && $ins[$i][1][1]['type'] == 'internalmedia') {
322-
// resolve relative ids, but without cleaning in order to preserve the name
323-
$media_id = resolve_id($ns, $ins[$i][1][1]['src']);
324-
// make sure that after resolving the link again it will be the same link
325-
if ($media_id{0} != ':') $media_id = ':'.$media_id;
326-
$ins[$i][1][1]['src'] = $media_id;
327-
}
319+
$this->adapt_links($ins, $page, $included_pages);
328320

321+
for($i=0; $i<$num; $i++) {
329322
switch($ins[$i][0]) {
330323
case 'document_start':
331324
case 'document_end':
@@ -360,56 +353,8 @@ function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $inc
360353
if ($flags['inline'])
361354
unset($ins[$i]);
362355
break;
363-
case 'internallink':
364-
case 'internalmedia':
365-
// make sure parameters aren't touched
366-
$link_params = '';
367-
$link_id = $ins[$i][1][0];
368-
$link_parts = explode('?', $link_id, 2);
369-
if (count($link_parts) === 2) {
370-
$link_id = $link_parts[0];
371-
$link_params = $link_parts[1];
372-
}
373-
// resolve the id without cleaning it
374-
$link_id = resolve_id($ns, $link_id, false);
375-
// this id is internal (i.e. absolute) now, add ':' to make resolve_id work again
376-
if ($link_id{0} != ':') $link_id = ':'.$link_id;
377-
// restore parameters
378-
$ins[$i][1][0] = ($link_params != '') ? $link_id.'?'.$link_params : $link_id;
379-
if ($ins[$i][0] == 'internallink' && !empty($included_pages)) {
380-
// change links to included pages into local links
381-
$link_id = $ins[$i][1][0];
382-
$link_parts = explode('?', $link_id, 2);
383-
// only adapt links without parameters
384-
if (count($link_parts) === 1) {
385-
$link_parts = explode('#', $link_id, 2);
386-
$hash = '';
387-
if (count($link_parts) === 2) {
388-
list($link_id, $hash) = $link_parts;
389-
}
390-
$exists = false;
391-
resolve_pageid($ns, $link_id, $exists);
392-
if (array_key_exists($link_id, $included_pages)) {
393-
if ($hash) {
394-
// hopefully the hash is also unique in the including page (otherwise this might be the wrong link target)
395-
$ins[$i][0] = 'locallink';
396-
$ins[$i][1][0] = $hash;
397-
} else {
398-
// the include section ids are different from normal section ids (so they won't conflict) but this
399-
// also means that the normal locallink function can't be used
400-
$ins[$i][0] = 'plugin';
401-
$ins[$i][1] = array('include_locallink', array($included_pages[$link_id]['hid'], $ins[$i][1][1], $ins[$i][1][0]));
402-
}
403-
}
404-
}
405-
}
406-
break;
407-
case 'locallink':
408-
/* Convert local links to internal links if the page hasn't been fully included */
409-
if ($included_pages == null || !array_key_exists($page, $included_pages)) {
410-
$ins[$i][0] = 'internallink';
411-
$ins[$i][1][0] = ':'.$page.'#'.$ins[$i][1][0];
412-
}
356+
case 'nest':
357+
$this->adapt_links($ins[$i][1][0], $page, $included_pages);
413358
break;
414359
case 'plugin':
415360
// FIXME skip other plugins?
@@ -593,6 +538,84 @@ function _permalink(&$ins, $page, $sect, $flags) {
593538
$ins[1] = array('include_header', array($ins[1][0], $ins[1][1], $ins[1][2], $page, $sect, $flags));
594539
}
595540

541+
/**
542+
* Convert internal and local links depending on the included pages
543+
*
544+
* @param array $ins The instructions that shall be adapted
545+
* @param string $page The included page
546+
* @param array $included_pages The array of pages that are included
547+
*/
548+
private function adapt_links(&$ins, $page, $included_pages = null) {
549+
$num = count($ins);
550+
$ns = getNS($page);
551+
552+
for($i=0; $i<$num; $i++) {
553+
// adjust links with image titles
554+
if (strpos($ins[$i][0], 'link') !== false && isset($ins[$i][1][1]) && is_array($ins[$i][1][1]) && $ins[$i][1][1]['type'] == 'internalmedia') {
555+
// resolve relative ids, but without cleaning in order to preserve the name
556+
$media_id = resolve_id($ns, $ins[$i][1][1]['src']);
557+
// make sure that after resolving the link again it will be the same link
558+
if ($media_id{0} != ':') $media_id = ':'.$media_id;
559+
$ins[$i][1][1]['src'] = $media_id;
560+
}
561+
switch($ins[$i][0]) {
562+
case 'internallink':
563+
case 'internalmedia':
564+
// make sure parameters aren't touched
565+
$link_params = '';
566+
$link_id = $ins[$i][1][0];
567+
$link_parts = explode('?', $link_id, 2);
568+
if (count($link_parts) === 2) {
569+
$link_id = $link_parts[0];
570+
$link_params = $link_parts[1];
571+
}
572+
// resolve the id without cleaning it
573+
$link_id = resolve_id($ns, $link_id, false);
574+
// this id is internal (i.e. absolute) now, add ':' to make resolve_id work again
575+
if ($link_id{0} != ':') $link_id = ':'.$link_id;
576+
// restore parameters
577+
$ins[$i][1][0] = ($link_params != '') ? $link_id.'?'.$link_params : $link_id;
578+
579+
if ($ins[$i][0] == 'internallink' && !empty($included_pages)) {
580+
// change links to included pages into local links
581+
// only adapt links without parameters
582+
$link_id = $ins[$i][1][0];
583+
$link_parts = explode('?', $link_id, 2);
584+
if (count($link_parts) === 1) {
585+
$exists = false;
586+
resolve_pageid($ns, $link_id, $exists);
587+
588+
$link_parts = explode('#', $link_id, 2);
589+
$hash = '';
590+
if (count($link_parts) === 2) {
591+
list($link_id, $hash) = $link_parts;
592+
}
593+
if (array_key_exists($link_id, $included_pages)) {
594+
if ($hash) {
595+
// hopefully the hash is also unique in the including page (otherwise this might be the wrong link target)
596+
$ins[$i][0] = 'locallink';
597+
$ins[$i][1][0] = $hash;
598+
} else {
599+
// the include section ids are different from normal section ids (so they won't conflict) but this
600+
// also means that the normal locallink function can't be used
601+
$ins[$i][0] = 'plugin';
602+
$ins[$i][1] = array('include_locallink', array($included_pages[$link_id]['hid'], $ins[$i][1][1], $ins[$i][1][0]));
603+
}
604+
}
605+
}
606+
}
607+
break;
608+
case 'locallink':
609+
/* Convert local links to internal links if the page hasn't been fully included */
610+
if ($included_pages == null || !array_key_exists($page, $included_pages)) {
611+
$ins[$i][0] = 'internallink';
612+
$ins[$i][1][0] = ':'.$page.'#'.$ins[$i][1][0];
613+
}
614+
break;
615+
}
616+
}
617+
}
618+
596619
/**
597620
* Get a section including its subsections
598621
*

0 commit comments

Comments
 (0)