Added support for markdown fragment shortcut syntax.

This commit is contained in:
David Russell 2017-04-29 22:37:13 +07:00
parent 912dd03796
commit 1a894737b3
3 changed files with 81 additions and 1 deletions

View File

@ -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();

View File

@ -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=";

View File

@ -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 =
" <!-- .element: class=\"fragment\" -->";
}