dokuwiki-plugin-dw2pdf icon indicating copy to clipboard operation
dokuwiki-plugin-dw2pdf copied to clipboard

div inside p

Open u07 opened this issue 3 years ago • 6 comments

Hi! There is a code in dw2pdf's renderer.php:

    /**
     * Wrap centered media in a div to center it
     */
    function _media($src, $title = NULL, $align = NULL, $width = NULL,
                    $height = NULL, $cache = NULL, $render = true) {

        $out = '';
        if($align == 'center') {
            $out .= '<div align="center" style="text-align: center">';
        }

        $out .= parent::_media($src, $title, $align, $width, $height, $cache, $render);

        if($align == 'center') {
            $out .= '</div>';
        }

        return $out;
    }

This results in html code like this:

<p>
<a href="1.jpg" class="media" title="1.jpg"><div align="center" style="text-align: center"><img src="1.jpg" class="mediacenter" title="1.jpg" alt="1.jpg" /></div></a>
</p>

Here we have a <DIV> tag inside <A> tag inside <P> tag, which does not comply HTML standards: a P tag should contain phrasing content only. Also it breaks mpdf logic and makes links in PDF broken, so, it's impossible to click on the image. You know, in DokuWiki images are links to their full versions. So, in PDF right- or left-aligned images are links, and center-aligned are not .

Is it possible to rewrite this function to align images somehow else, without introducing new DIVs inside <a>?

image

u07 avatar Oct 28 '20 13:10 u07

As I see it, a solution could be to place <a> inside <div>, not the other way around. That fixes everything. But I don't see a way to implement that in code (renderer.php)

u07 avatar Oct 29 '20 06:10 u07

Okay, I've managed to patch it. Do you accept patches or pull requests, what's the right way to send it? or just here, in comments? I've never made pull requests :)

u07 avatar Oct 29 '20 08:10 u07

Is this issue also connected to #275? Code example is welcome.

Pull request is welcome as well, but I'm wondering if it is worth to check more thoroughly the image handling.

Klap-in avatar Oct 29 '20 09:10 Klap-in

Is this issue also connected to #275?

They are not. That patch can be applied alongside with this one. Actually №275 helped to notice broken links. It made them visible, like this:

image

Code example

I moved <div> tag outside of <a>. To do so I moved the div wrapping code from _media() to internalmedia() and externalmedia(), cause they actually produce <a> tags.

    // Center-align images with their corresponding links
    function internalmedia($src, $title = null, $align = null, $width = null,
                           $height = null, $cache = null, $linking = null, $return = false) {
        
		$out = parent::internalmedia($src, $title, $align, $width, $height, $cache, $linking, true);
        
		if($align == 'center') {
			$out = '<div align="center" style="text-align: center">' .$out. '</div>';
        }
		
        if($return) return $out;
        else $this->doc .= $out;
    }	

It's impossible to show on a screenshot :) but both images are links now. And #275 produces a nice paragraph flow:

image

u07 avatar Oct 30 '20 04:10 u07

Here is a live example with both patches applied: https://petelinsasha.ru/wiki/playground/dw2pdf_test

u07 avatar Nov 03 '20 07:11 u07

If the html is not proper, that should be 'fixed' before we feed it to mpdf, or mpdf should be improved (then tiny example that exactly replicate the case should be created, and used to open an issue there), or DokuWiki should be changed as well, if it is not proper html.

Klap-in avatar Oct 12 '22 21:10 Klap-in