Extensions Wiki » Extensions » Tag Transformation

Tag Transformation

Last modified by Vincent Massol on 2013/03/11 09:36
cogCreates a rendering transformation in Groovy which generates in view mode links to the page that lists all the documents tagged with a tag word
TypeSnippet
Developed by

Oana Tabaranu

LicenseGNU Lesser General Public License 2.1

Table of contents

Description

{{groovy}}
import org.xwiki.component.descriptor.*;
import org.xwiki.script.service.*;
import org.xwiki.context.*;
import com.xpn.xwiki.web.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.xwiki.rendering.block.Block;
import org.xwiki.rendering.block.LinkBlock;
import org.xwiki.rendering.block.WordBlock;
import org.xwiki.rendering.internal.block.ProtectedBlockFilter;
import org.xwiki.rendering.listener.reference.DocumentResourceReference;
import org.xwiki.rendering.listener.reference.ResourceReference;
import org.xwiki.rendering.transformation.AbstractTransformation;
import org.xwiki.rendering.transformation.TransformationContext;
import org.xwiki.rendering.transformation.TransformationException;
import org.xwiki.rendering.transformation.Transformation;

class TagTransformation extends AbstractTransformation {
  org.xwiki.context.Execution e;
 
   private ProtectedBlockFilter filter = new ProtectedBlockFilter();

   public void transform(Block block, TransformationContext transformationContext) throws TransformationException
   {
       def context = e.getContext().getProperty("xwikicontext");
       def xwikiApi = new com.xpn.xwiki.api.XWiki(context.getWiki(), context);
       def tagCount = xwikiApi.tag.getTagCount("");
      // Find all Word blocks and for each of them check if they're a wiki word or not
      for (WordBlock wordBlock : this.filter.getChildrenByType(block, WordBlock.class, true)) {
           // check if the word is a tag
          // if (tagCount.keySet().contains(wordBlock.getWord())) {
          if (!(wordBlock.getParent() instanceof LinkBlock) && tagCount.keySet().contains(wordBlock.getWord())) {
                ResourceReference linkReference = new DocumentResourceReference("Main.Tags");
                linkReference.setQueryString("do=viewTag&tag=${wordBlock}");
                Block linkBlock = new LinkBlock([new WordBlock(wordBlock.getWord())], linkReference, false);
                wordBlock.getParent().replaceChild(linkBlock, wordBlock);
           }
       }
   }
}
DefaultComponentDescriptor descriptor = new DefaultComponentDescriptor(implementation: TagTransformation.class, role: Transformation.class, roleHint: 'tag', instantiationStrategy: ComponentInstantiationStrategy.SINGLETON);
DefaultComponentDependency dep = new DefaultComponentDependency(name: 'e', role: Execution.class, roleHint: 'default');
descriptor.addComponentDependency(dep);
Utils.getComponentManager().unregisterComponent(Transformation.class, 'tag');
Utils.getComponentManager().registerComponent(descriptor);

{{/groovy}}

The rendering transformation needs to be added in xwiki.properties:

#-# [Since 2.6RC1]
#-# Controls what transformations will be executed when rendering content.
#-# A transformation modifies the parsed content. For example the Icon transformation replaces some characters with
#-# icons, a WikiWord transformation will automatically create links when it finds wiki words, etc.
#-# Note that the Macro transformation is a special transformation that replaces macro markers by the result of the
#-# macro execution. If you don't list it, macros won't get executed.
#-# The default value is: rendering.transformations = macro, icon
rendering.transformations = macro, icon, tag

Example

For the example text below the transformation generates links to existing tags in the wiki like test:
transformation.png
tags.png

Tags:
Created by Oana Tabaranu on 2011/08/03 11:19

Download XWiki