001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2017-2018 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.webconsole.provider.markdownit;
020
021import freemarker.core.ParseException;
022import freemarker.template.MalformedTemplateNameException;
023import freemarker.template.TemplateNotFoundException;
024import java.io.IOException;
025import java.util.List;
026import org.jgrapes.core.Channel;
027import org.jgrapes.core.Event;
028import org.jgrapes.core.Manager;
029import org.jgrapes.core.annotation.Handler;
030import org.jgrapes.webconsole.base.ConsoleConnection;
031import org.jgrapes.webconsole.base.PageResourceProvider;
032import org.jgrapes.webconsole.base.events.AddPageResources;
033import org.jgrapes.webconsole.base.events.AddPageResources.ScriptResource;
034import org.jgrapes.webconsole.base.events.ConsoleReady;
035
036/**
037 * A provider for the [Markdownit](https://github.com/markdown-it/markdown-it)
038 * library.
039 */
040public class MarkdownItProvider extends PageResourceProvider {
041
042    /**
043     * Creates a new component with its channel set to the given 
044     * channel.
045     * 
046     * @param componentChannel the channel that the component's 
047     * handlers listen on by default and that 
048     * {@link Manager#fire(Event, Channel...)} sends the event to 
049     */
050    public MarkdownItProvider(Channel componentChannel) {
051        super(componentChannel);
052    }
053
054    /**
055     * On {@link ConsoleReady}, fire the appropriate {@link AddPageResources}.
056     *
057     * @param event the event
058     * @param connection the web console connection
059     * @throws TemplateNotFoundException the template not found exception
060     * @throws MalformedTemplateNameException the malformed template name exception
061     * @throws ParseException the parse exception
062     * @throws IOException Signals that an I/O exception has occurred.
063     */
064    @Handler(priority = 100)
065    @SuppressWarnings({ "PMD.AvoidDuplicateLiterals",
066        "PMD.AvoidInstantiatingObjectsInLoops" })
067    public void onConsoleReady(ConsoleReady event,
068            ConsoleConnection connection)
069            throws TemplateNotFoundException, MalformedTemplateNameException,
070            ParseException, IOException {
071        String minExt = event.renderSupport()
072            .useMinifiedResources() ? ".min" : "";
073        var pageResources = new AddPageResources()
074            .addScriptResource(new ScriptResource()
075                .setProvides(new String[] { "markdown-it" })
076                .setScriptUri(event.renderSupport().pageResource(
077                    "markdown-it/markdown-it" + minExt + ".js")));
078        for (var pkg : List.of("abbr", "container", "deflist", "emoji",
079            "footnote", "ins", "mark", "sub", "sup")) {
080            pageResources.addScriptResource(new ScriptResource()
081                .setProvides(new String[] { "markdown-it-" + pkg })
082                .setScriptUri(event.renderSupport().pageResource(
083                    "markdown-it/markdown-it-" + pkg + minExt + ".js")));
084        }
085        connection.respond(pageResources);
086    }
087}