All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xwiki.rendering.internal.macro.figure.FigureCaptionMacro Maven / Gradle / Ivy

There is a newer version: 15.3
Show newest version
/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.rendering.internal.macro.figure;

import java.util.Collections;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.xwiki.component.annotation.Component;
import org.xwiki.rendering.block.Block;
import org.xwiki.rendering.block.FigureBlock;
import org.xwiki.rendering.block.FigureCaptionBlock;
import org.xwiki.rendering.block.MetaDataBlock;
import org.xwiki.rendering.block.XDOM;
import org.xwiki.rendering.macro.AbstractNoParameterMacro;
import org.xwiki.rendering.macro.MacroContentParser;
import org.xwiki.rendering.macro.MacroExecutionException;
import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor;
import org.xwiki.rendering.transformation.MacroTransformationContext;

/**
 * Provides a caption inside the Figure Macro content. Needs to be used as the first or last block.
 *
 * @version $Id: 744128b1616cfd9af3bc565a9c73c5cad3ee854e $
 * @since 10.2
 */
@Component
@Named("figureCaption")
@Singleton
public class FigureCaptionMacro extends AbstractNoParameterMacro
{
    /**
     * The description of the macro.
     */
    private static final String DESCRIPTION = "Provide a figure caption when used inside the Figure macro.";

    /**
     * The description of the macro content.
     */
    private static final String CONTENT_DESCRIPTION = "Caption content";

    @Inject
    private MacroContentParser contentParser;

    /**
     * Create and initialize the descriptor of the macro.
     */
    public FigureCaptionMacro()
    {
        super("Figure Caption", DESCRIPTION,
            new DefaultContentDescriptor(CONTENT_DESCRIPTION, true, Block.LIST_BLOCK_TYPE));
        setDefaultCategories(Set.of(DEFAULT_CATEGORY_DEVELOPMENT));
    }

    @Override
    public boolean supportsInlineMode()
    {
        return false;
    }

    @Override
    public List execute(Object unusedParameters, String content, MacroTransformationContext context)
        throws MacroExecutionException
    {
        List result = Collections.emptyList();

        // If we're not inside a FigureBlock then don't do anything.
        Block parent = context.getCurrentMacroBlock().getParent();
        if (parent instanceof FigureBlock
            || (parent instanceof MetaDataBlock && parent.getParent() instanceof FigureBlock))
        {
            XDOM xdom = this.contentParser.parse(content, context, false, false);
            List figureCaptionChildren = xdom.getChildren();

            // Mark the macro content as being content that has not been transformed (so that it can editable inline)
            figureCaptionChildren = Collections.singletonList(new MetaDataBlock(figureCaptionChildren,
                getNonGeneratedContentMetaData()));
            result = Collections.singletonList(new FigureCaptionBlock(figureCaptionChildren));
        }

        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy