AttachIcons2

Summary: Add icon images to Attach file links according to file extension
Version:
Prerequisites:
Status:
Maintainer: Pierre Racine
Discussion: AttachIcons2 -Talk
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/".$ext.".gif> instead of relative <img src=/pub/icons/".$ext.".gif> to make it work. gb
  • I tried adding a docx file (Word 2007) and it wouldn't display the docx.gif icon that I created. A fix would be terrific! mc?

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".

See Also

  • AttachIcons Add icon images to Attach file links according to file extension
  • ExternalLinks2 Add an icon to external links and make them automatically open in a new window
  • CustomBullets Enable custom bullets to distinguish document types
  • Link icons Add icons to your links according to their extensions.

Contributors

Pierre Racine

Comments

See discussion at AttachIcons2-Talk