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 org.jgrapes.core.Channel;
026import org.jgrapes.core.Event;
027import org.jgrapes.core.Manager;
028import org.jgrapes.core.annotation.Handler;
029import org.jgrapes.webconsole.base.ConsoleSession;
030import org.jgrapes.webconsole.base.PageResourceProvider;
031import org.jgrapes.webconsole.base.events.AddPageResources;
032import org.jgrapes.webconsole.base.events.AddPageResources.ScriptResource;
033import org.jgrapes.webconsole.base.events.ConsoleReady;
034
035/**
036 * A provider for the [Markdownit](https://github.com/markdown-it/markdown-it)
037 * library.
038 */
039public class MarkdownItProvider extends PageResourceProvider {
040
041    /**
042     * Creates a new component with its channel set to the given 
043     * channel.
044     * 
045     * @param componentChannel the channel that the component's 
046     * handlers listen on by default and that 
047     * {@link Manager#fire(Event, Channel...)} sends the event to 
048     */
049    public MarkdownItProvider(Channel componentChannel) {
050        super(componentChannel);
051    }
052
053    /**
054     * On {@link ConsoleReady}, fire the appropriate {@link AddPageResources}.
055     *
056     * @param event the event
057     * @param consoleSession the web console session
058     * @throws TemplateNotFoundException the template not found exception
059     * @throws MalformedTemplateNameException the malformed template name exception
060     * @throws ParseException the parse exception
061     * @throws IOException Signals that an I/O exception has occurred.
062     */
063    @Handler(priority = 100)
064    @SuppressWarnings("PMD.AvoidDuplicateLiterals")
065    public void onConsoleReady(ConsoleReady event,
066            ConsoleSession consoleSession)
067            throws TemplateNotFoundException, MalformedTemplateNameException,
068            ParseException, IOException {
069        String minExt = event.renderSupport()
070            .useMinifiedResources() ? ".min" : "";
071        consoleSession.respond(new AddPageResources()
072            .addScriptResource(new ScriptResource()
073                .setProvides(new String[] { "markdown-it.github.io" })
074                .setScriptUri(event.renderSupport().pageResource(
075                    "markdown-it-8.0.4" + minExt + ".js")))
076            .addScriptResource(new ScriptResource()
077                .setRequires(new String[] { "markdown-it.github.io" })
078                .setProvides(
079                    new String[] { "github.com/markdown-it/markdown-it-abbr" })
080                .setScriptUri(event.renderSupport().pageResource(
081                    "markdown-it-abbr-1.0.4" + minExt + ".js")))
082            .addScriptResource(new ScriptResource()
083                .setRequires(new String[] { "markdown-it.github.io" })
084                .setProvides(new String[] {
085                    "github.com/markdown-it/markdown-it-container" })
086                .setScriptUri(event.renderSupport().pageResource(
087                    "markdown-it-container-2.0.0" + minExt + ".js")))
088            .addScriptResource(new ScriptResource()
089                .setRequires(new String[] { "markdown-it.github.io" })
090                .setProvides(new String[] {
091                    "github.com/markdown-it/markdown-it-deflist" })
092                .setScriptUri(event.renderSupport().pageResource(
093                    "markdown-it-deflist-2.0.3" + minExt + ".js")))
094            .addScriptResource(new ScriptResource()
095                .setRequires(new String[] { "markdown-it.github.io" })
096                .setProvides(
097                    new String[] { "github.com/markdown-it/markdown-it-emoji" })
098                .setScriptUri(event.renderSupport().pageResource(
099                    "markdown-it-emoji-1.4.0" + minExt + ".js")))
100            .addScriptResource(new ScriptResource()
101                .setRequires(new String[] { "markdown-it.github.io" })
102                .setProvides(new String[] {
103                    "github.com/markdown-it/markdown-it-footnote" })
104                .setScriptUri(event.renderSupport().pageResource(
105                    "markdown-it-footnote-3.0.1" + minExt + ".js")))
106            .addScriptResource(new ScriptResource()
107                .setRequires(new String[] { "markdown-it.github.io" })
108                .setProvides(
109                    new String[] { "github.com/markdown-it/markdown-it-ins" })
110                .setScriptUri(event.renderSupport().pageResource(
111                    "markdown-it-ins-2.0.0" + minExt + ".js")))
112            .addScriptResource(new ScriptResource()
113                .setRequires(new String[] { "markdown-it.github.io" })
114                .setProvides(
115                    new String[] { "github.com/markdown-it/markdown-it-mark" })
116                .setScriptUri(event.renderSupport().pageResource(
117                    "markdown-it-mark-2.0.0" + minExt + ".js")))
118            .addScriptResource(new ScriptResource()
119                .setRequires(new String[] { "markdown-it.github.io" })
120                .setProvides(
121                    new String[] { "github.com/markdown-it/markdown-it-sub" })
122                .setScriptUri(event.renderSupport().pageResource(
123                    "markdown-it-sub-1.0.0" + minExt + ".js")))
124            .addScriptResource(new ScriptResource()
125                .setRequires(new String[] { "markdown-it.github.io" })
126                .setProvides(
127                    new String[] { "github.com/markdown-it/markdown-it-sup" })
128                .setScriptUri(event.renderSupport().pageResource(
129                    "markdown-it-sup-1.0.0" + minExt + ".js"))));
130    }
131}