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

org.spincast.website.pebble.AppPebbleExtension Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
package org.spincast.website.pebble;

import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.spincast.core.utils.SpincastStatics;

import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.extension.AbstractExtension;
import com.mitchellbosecke.pebble.extension.Filter;
import com.mitchellbosecke.pebble.template.EvaluationContext;
import com.mitchellbosecke.pebble.template.PebbleTemplate;

/**
 * Custom Pebble extension.
 */
public class AppPebbleExtension extends AbstractExtension {

    public static final String REMOVE_HTML_TAGS_FILTER_NAME = "strip";

    @Override
    public Map getFilters() {

        //==========================================
        // Pebble filter to remove the HTML tags.
        //==========================================
        Filter filter = new Filter() {

            @Override
            public List getArgumentNames() {
                return null;
            }

            @Override
            public Object apply(Object value,
                                Map args,
                                PebbleTemplate self,
                                EvaluationContext evaluationContext,
                                int lineNumber) throws PebbleException {
                if (value == null) {
                    return null;
                }
                String str = (String)value;

                return Jsoup.parse(str).text();
            }
        };

        return SpincastStatics.map(REMOVE_HTML_TAGS_FILTER_NAME, filter);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy