From 1a894737b39f6ba1e03f565bb5560ed23eb1619d Mon Sep 17 00:00:00 2001 From: David Russell Date: Sat, 29 Apr 2017 22:37:13 +0700 Subject: [PATCH] Added support for markdown fragment shortcut syntax. --- app/Module.java | 1 + app/com/gitpitch/models/MarkdownModel.java | 10 ++- .../gitpitch/services/ShortcutsService.java | 71 +++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 app/com/gitpitch/services/ShortcutsService.java diff --git a/app/Module.java b/app/Module.java index 2948ac8..d901ee9 100644 --- a/app/Module.java +++ b/app/Module.java @@ -51,6 +51,7 @@ public class Module extends AbstractModule { bind(ImageService.class).asEagerSingleton(); bind(VideoService.class).asEagerSingleton(); bind(GISTService.class).asEagerSingleton(); + bind(ShortcutsService.class).asEagerSingleton(); bind(WebService.class).asEagerSingleton(); bind(ComposableService.class).asEagerSingleton(); bind(GRSManager.class).asEagerSingleton(); diff --git a/app/com/gitpitch/models/MarkdownModel.java b/app/com/gitpitch/models/MarkdownModel.java index 39fc231..bd0267a 100644 --- a/app/com/gitpitch/models/MarkdownModel.java +++ b/app/com/gitpitch/models/MarkdownModel.java @@ -28,6 +28,7 @@ import com.gitpitch.utils.*; import com.gitpitch.services.ImageService; import com.gitpitch.services.VideoService; import com.gitpitch.services.GISTService; +import com.gitpitch.services.ShortcutsService; import org.apache.commons.io.FilenameUtils; import com.google.inject.assistedinject.Assisted; import javax.annotation.Nullable; @@ -52,6 +53,7 @@ public class MarkdownModel implements Markdown { private final ImageService imageService; private final VideoService videoService; private final GISTService gistService; + private final ShortcutsService shortcutsService; private final MarkdownRenderer mrndr; private final String markdown; private String hSlideDelim = HSLIDE_DELIM_DEFAULT; @@ -61,11 +63,13 @@ public class MarkdownModel implements Markdown { public MarkdownModel(ImageService imageService, VideoService videoService, GISTService gistService, + ShortcutsService shortcutsService, @Nullable @Assisted MarkdownRenderer mrndr) { this.imageService = imageService; this.videoService = videoService; this.gistService = gistService; + this.shortcutsService = shortcutsService; this.mrndr = mrndr; String consumed = null; @@ -238,7 +242,7 @@ public class MarkdownModel implements Markdown { } - } if(imageService.tagFound(md)) { + } else if(imageService.tagFound(md)) { String tagLink = imageService.tagLink(md); if (linkAbsolute(tagLink)) { @@ -247,6 +251,8 @@ public class MarkdownModel implements Markdown { String absTagLink = gitRawBase + tagLink; return md.replace(tagLink, absTagLink); } + } else if(shortcutsService.fragmentFound(md)) { + return shortcutsService.expandFragment(md); } else { /* @@ -604,6 +610,8 @@ public class MarkdownModel implements Markdown { public static final String MD_CLOSER = "\" -->"; public static final String MD_SPACER = "\n"; public static final String DATA_IMAGE_ATTR = "data-background-image="; + public static final String MD_FRAG_OPEN = "- "; + public static final String MD_FRAG_CLOSE = "|"; private static final String MD_HSLIDE_IMAGE = "?image="; private static final String MD_VSLIDE_IMAGE = "?image="; diff --git a/app/com/gitpitch/services/ShortcutsService.java b/app/com/gitpitch/services/ShortcutsService.java new file mode 100644 index 0000000..f5e98ff --- /dev/null +++ b/app/com/gitpitch/services/ShortcutsService.java @@ -0,0 +1,71 @@ +/* + * MIT License + * + * Copyright (c) 2016 David Russell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.gitpitch.services; + +import com.gitpitch.models.MarkdownModel; +import com.gitpitch.utils.PitchParams; +import com.gitpitch.utils.YAMLOptions; +import org.apache.commons.io.FilenameUtils; +import java.util.*; +import javax.inject.*; +import play.Logger; +import play.Logger.ALogger; + +/* + * PITCHME.md feature-shortcuts support service. + */ +@Singleton +public class ShortcutsService { + + private final Logger.ALogger log = Logger.of(this.getClass()); + + public boolean fragmentFound(String md) { + boolean found = false; + if(md != null) { + String trimmed = md.trim(); + if(trimmed.startsWith(MarkdownModel.MD_FRAG_OPEN) && + trimmed.endsWith(MarkdownModel.MD_FRAG_CLOSE)) { + found = true; + } + } + return found; + } + + /* + * Expand shortcut syntax for fragment with fully expanded + * HTML comment syntax. + */ + public String expandFragment(String md) { + int fragCloseIdx = md.lastIndexOf(MarkdownModel.MD_FRAG_CLOSE); + return md.substring(0, fragCloseIdx) + FRAGMENT; + } + + /* + * Note, the space before opening bracket is intentional + * so tag injection can happen directly alongside other + * text in the markdown fragment. + */ + private static final String FRAGMENT = + " "; +}