Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

AttachIcons2

Summary: Add icon images to Attach file links according to file extension
Version:
Prerequisites:
Status:
Maintainer: Pierre Racine
Categories: Images, Uploads

Questions answered by this recipe

How can I automatically display an icon image specific to a file extension beside an attach link?

Description

The following markup adds an icon for .pdf and .doc files (but not others). Add the markup definition to config.php or another local customisation file. Extend and adjust the definition for other file extensions according to your needs. The link will open in a new window. Put the following code in your config.php. You also need to create icon images php.pdf and doc.gif and put them in a pub/icons/ directory.

This recipe is different from AttachIcons in that it works for [[Attach:file.pdf]], [[Attach:file.pdf|text]] and [[http://www.site.com/file.pdf]] links.

Markup('[[ext|','<[[|',"/(?>\\[\\[([^|\\]]*.([Pp][Dd][Ff]|[Dd][Oo][Cc]))\\s*\\|\\s*)(.*?)\\s*\\]\\]($SuffixPattern)/e", 
"MakeLinkWithIcon(\$pagename, PSS('$1'), PSS('$2'), PSS('$3'),'$4')");

Markup('[[ext','<[[', "/(?>\\[\\[\\s*(.*?.([Pp][Dd][Ff]|[Dd][Oo][Cc]))\\s*\\]\\])($SuffixPattern)/e", 
"MakeLinkWithIcon(\$pagename, PSS('$1'), PSS('$2'), NULL, '$3')");

function MakeLinkWithIcon($pagename, $target, $ext, $text, $suffix)
{
    global $UrlLinkFmt;

    $fmt = $UrlLinkFmt."&nbsp;<img src=/pub/icons/".$ext.".gif>";
    if (strtolower($ext) != "zip")
        $fmt = "%newwin%".$fmt;

    if (is_null($text))
        return Keep(MakeLink($pagename, PSS($target), NULL, $suffix, $fmt), 'L');
    else 
        return Keep(MakeLink($pagename, PSS($target), PSS($text), $suffix, $fmt), 'L');
}

Get your doctype icons from here: www.stylegala.com/features/bulletmadness/ - link doesn't work

Rename them to be named like this:
pdf.gif
doc.gif
.....
and place them in the icons directory.

Notes

  • I could not make this to work with bare hyperlink like http://www.site.com/file.pdf
  • I had to use complete url <img src=http://mysite/pmwiki/pub/icons/(approve links)".$ext.".gif> instead of relative <img src=/pub/icons/".$ext.".gif> to make it work. gb?

Release Notes

If the recipe has multiple releases, then release notes can be placed here. Note that it's often easier for people to work with "release dates" instead of "version numbers".

Comments

See Also

Contributors

Pierre Racine

Edit - History - Print - Recent Changes - Search
Page last modified on December 27, 2008, at 04:58 PM