Skip to content

Commit

Permalink
Add media reference adaption support for images inside other links
Browse files Browse the repository at this point in the history
  • Loading branch information
michitux committed Mar 29, 2013
1 parent 50d5c59 commit b4f6d8f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
36 changes: 36 additions & 0 deletions _test/mediamove.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* Test cases for the pagemove plugin
*/
class plugin_pagemove_mediamove_test extends DokuWikiTest {

public function setUp() {
$this->pluginsEnabled[] = 'pagemove';
parent::setUp();
}

public function test_movePageWithRelativeMedia() {
global $ID;

$ID = 'mediareltest:foo';
saveWikiText($ID,
'{{ myimage.png}} [[:start|{{ testimage.png?200x800 }}]] [[bar|{{testimage.gif?400x200}}]]
[[doku>wiki:dokuwiki|{{wiki:logo.png}}]] [[http://www.example.com|{{testimage.jpg}}]]
[[doku>wiki:foo|{{foo.gif?200x3000}}]]', 'Test setup');
idx_addPage($ID);

$opts = array();
$opts['ns'] = getNS($ID);
$opts['name'] = noNS($ID);
$opts['newns'] = '';
$opts['newname'] = 'foo';
/** @var helper_plugin_pagemove $pagemove */
$pagemove = plugin_load('helper', 'pagemove');
$pagemove->move_page($opts);

$this->assertEquals('{{ mediareltest:myimage.png}} [[:start|{{ mediareltest:testimage.png?200x800 }}]] [[mediareltest:bar|{{mediareltest:testimage.gif?400x200}}]]
[[doku>wiki:dokuwiki|{{wiki:logo.png}}]] [[http://www.example.com|{{mediareltest:testimage.jpg}}]]
[[doku>wiki:foo|{{mediareltest:foo.gif?200x3000}}]]', rawWiki('foo'));
}
}
31 changes: 23 additions & 8 deletions helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,14 @@ public function internallink($match, $state, $pos) {
$link = explode('|',$link,2);
if ( !isset($link[1]) ) {
$link[1] = NULL;
} else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) {
// If the title is an image, rewrite it
$old_title = $link[1];
$link[1] = $this->rewrite_media($link[1]);
// do a simple replace of the first match so really only the id is changed and not e.g. the alignment
$oldpos = strpos($match, $old_title);
$oldlen = strlen($old_title);
$match = substr_replace($match, $link[1], $oldpos, $oldlen);
}
$link[0] = trim($link[0]);

Expand Down Expand Up @@ -657,21 +665,28 @@ public function internallink($match, $state, $pos) {
* @return bool If parsing should be continued
*/
public function media($match, $state, $pos) {
$this->calls .= $this->rewrite_media($match);
return true;
}

/**
* Rewrite a media syntax
*
* @param string $match The text match of the media syntax
* @return string The rewritten syntax
*/
protected function rewrite_media($match) {
$p = Doku_Handler_Parse_Media($match);
if ($p['type'] == 'internalmedia') {
if ($p['type'] == 'internalmedia') { // else: external media
$new_src = $this->adaptRelativeId($p['src']);
if ($new_src == $p['src']) {
$this->calls .= $match;
} else {
if ($new_src !== $p['src']) {
// do a simple replace of the first match so really only the id is changed and not e.g. the alignment
$srcpos = strpos($match, $p['src']);
$srclen = strlen($p['src']);
$this->calls .= substr_replace($match, $new_src, $srcpos, $srclen);
return substr_replace($match, $new_src, $srcpos, $srclen);
}
} else { // external media
$this->calls .= $match;
}
return true;
return $match;
}

/**
Expand Down

0 comments on commit b4f6d8f

Please sign in to comment.