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