Added delimiter parameter processor.

This commit is contained in:
David Russell 2017-09-02 11:45:41 +07:00
parent 134dcb7f15
commit 9615356d40
6 changed files with 167 additions and 174 deletions

View File

@ -2,17 +2,17 @@
* 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
@ -30,6 +30,7 @@ import com.gitpitch.services.VideoService;
import com.gitpitch.services.GISTService;
import com.gitpitch.services.CodeService;
import com.gitpitch.services.ShortcutsService;
import com.gitpitch.git.GRSManager;
import org.apache.commons.io.FilenameUtils;
import com.google.inject.assistedinject.Assisted;
import javax.annotation.Nullable;
@ -56,6 +57,7 @@ public class MarkdownModel implements Markdown {
private final GISTService gistService;
private final CodeService codeService;
private final ShortcutsService shortcutsService;
private final GRSManager grsManager;
private final MarkdownRenderer mrndr;
private final String markdown;
private String hSlideDelim = HSLIDE_DELIM_DEFAULT;
@ -67,6 +69,7 @@ public class MarkdownModel implements Markdown {
GISTService gistService,
CodeService codeService,
ShortcutsService shortcutsService,
GRSManager grsManager,
@Nullable @Assisted MarkdownRenderer mrndr) {
this.imageService = imageService;
@ -74,6 +77,7 @@ public class MarkdownModel implements Markdown {
this.gistService = gistService;
this.codeService = codeService;
this.shortcutsService = shortcutsService;
this.grsManager = grsManager;
this.mrndr = mrndr;
String consumed = null;
@ -140,12 +144,13 @@ public class MarkdownModel implements Markdown {
/*
* Clean only delimiter fragments of whitespace
* noise before processing. Preserve end-of-line
* noise before processing. Preserve end-of-line
* characters on all non-delimiter fragments.
*/
md = md.trim();
DelimParams dp = DelimParams.build(md);
if (videoDelimFound(md)) {
if (videoDelimFound(dp)) {
/*
* Inject slide specific video background:
@ -153,13 +158,12 @@ public class MarkdownModel implements Markdown {
* <!-- .slide: data-background-video="vidUrl" -->
*/
String videoBgUrl =
videoService.extractBgUrl(md, gitRawBase, this);
return new StringBuffer(delimiter(md))
.append(videoService.buildBackground(videoBgUrl))
.append(videoService.buildBackground(md,
dp, pp, this))
.toString();
} else if (imageDelimFound(md)) {
} else if (imageDelimFound(dp)) {
/*
* Inject slide specific image background:
@ -167,21 +171,18 @@ public class MarkdownModel implements Markdown {
* <!-- .slide: data-background-image="imgUrl" -->
*/
String imageBgUrl =
imageService.extractBgUrl(md, gitRawBase, this);
String bgSize = (yOpts != null) ?
String defaultBgSize = (yOpts != null) ?
yOpts.fetchImageBgSize(pp) : YAMLOptions.DEFAULT_BG_SIZE;
return new StringBuffer(delimiter(md))
.append(imageService.buildBackground(pp,
imageBgUrl, bgSize))
.append(imageService.buildBackground(md,
dp, pp, defaultBgSize, this))
.toString();
} else if (gistDelimFound(md)) {
return gistService.build(md, pp, yOpts, this);
} else if(codeDelimFound(md)) {
return codeService.build(md, pp, yOpts, gitRawBase, this);
} else if (gistDelimFound(dp)) {
return gistService.build(md, dp, pp, yOpts, this);
} else if(codeDelimFound(dp)) {
return codeService.build(md, dp, pp, yOpts, this);
}
if (yOpts != null && yOpts.hasImageBg()) {
@ -394,20 +395,20 @@ public class MarkdownModel implements Markdown {
}
}
private boolean imageDelimFound(String md) {
return md.startsWith(horizImageDelim()) || md.startsWith(vertImageDelim());
private boolean imageDelimFound(DelimParams dp) {
return dp.get(DELIM_QUERY_IMAGE) != null;
}
private boolean videoDelimFound(String md) {
return md.startsWith(horizVideoDelim()) || md.startsWith(vertVideoDelim());
private boolean videoDelimFound(DelimParams dp) {
return dp.get(DELIM_QUERY_VIDEO) != null;
}
private boolean gistDelimFound(String md) {
return md.startsWith(horizGISTDelim()) || md.startsWith(vertGISTDelim());
private boolean gistDelimFound(DelimParams dp) {
return dp.get(DELIM_QUERY_GIST) != null;
}
private boolean codeDelimFound(String md) {
return md.startsWith(horizCodeDelim()) || md.startsWith(vertCodeDelim());
private boolean codeDelimFound(DelimParams dp) {
return dp.get(DELIM_QUERY_CODE) != null;
}
private String delimiter(String md) {
@ -570,11 +571,27 @@ public class MarkdownModel implements Markdown {
public boolean isHorizontal(String md) {
return (md.startsWith(horizDelim())) ? true : false;
}
public boolean linkAbsolute(String link) {
return link.startsWith(MD_LINK_ABS);
}
public String linkLive(PitchParams pp, String linkBase) {
try {
if (linkAbsolute(linkBase)) {
return linkBase;
} else {
return grsManager.getService(grsManager.get(pp))
.raw(pp, linkBase);
}
} catch (Exception lex) {
log.warn("linkLive: ex={}", lex);
return "#";
}
}
public String extractImageDelim(String md) {
return isHorizontal(md) ? horizImageDelim() : vertImageDelim();
}
@ -684,6 +701,13 @@ public class MarkdownModel implements Markdown {
public static final String HSLIDE_DELIM_BACK_COMPAT = "#HSLIDE";
public static final String VSLIDE_DELIM_BACK_COMPAT = "#VSLIDE";
public static final String DELIM_QUERY_IMAGE = "image";
public static final String DELIM_QUERY_VIDEO = "video";
public static final String DELIM_QUERY_GIST = "gist";
public static final String DELIM_QUERY_CODE = "code";
public static final String DELIM_QUERY_LANG = "lang";
public static final String DELIM_QUERY_SIZE = "size";
public static final String MD_LINK_OPEN = "![";
public static final String MD_ANCHOR_OPEN = "[";
public static final String MD_IMAGE_OPEN =

View File

@ -2,17 +2,17 @@
* 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
@ -29,6 +29,7 @@ import com.gitpitch.git.GRSService;
import com.gitpitch.git.GRSManager;
import com.gitpitch.services.DiskService;
import com.gitpitch.utils.PitchParams;
import com.gitpitch.utils.DelimParams;
import com.gitpitch.utils.YAMLOptions;
import java.util.*;
import java.nio.file.*;
@ -56,19 +57,17 @@ public class CodeService {
}
public String build(String md,
DelimParams dp,
PitchParams pp,
YAMLOptions yOpts,
String gitRawBase,
MarkdownModel mdm) {
String codeBlock = mdm.extractCodeDelim(md);
try {
String codePath = extractCodePath(md, gitRawBase, mdm);
String langHint = getLangHintOptionFromPath(codePath);
codePath = cleanOptionsFromPath(codePath);
String codePath = dp.get(MarkdownModel.DELIM_QUERY_CODE);
String langHint = dp.get(MarkdownModel.DELIM_QUERY_LANG);
GRS grs = grsManager.get(pp);
GRSService grsService = grsManager.getService(grs);
@ -88,23 +87,6 @@ public class CodeService {
return codeBlock;
}
public String extractCodePath(String md,
String gitRawBase,
MarkdownModel mdm) {
String codePath = null;
try {
String delim = mdm.extractCodeDelim(md);
codePath = md.substring(delim.length());
} catch (Exception pex) {
log.warn("extractCodePath: ex={}", pex);
}
return codePath;
}
private String extractPath(String md, MarkdownModel mdm) {
String path = null;
@ -142,32 +124,6 @@ public class CodeService {
.toString();
}
private String getLangHintOptionFromPath(String codePath) {
String extractedHint = NO_LANG_HINT;
try {
int hintOptIdx = codePath.indexOf(LANG_HINT_OPTION);
if(hintOptIdx != -1) {
int hintOffset = hintOptIdx + LANG_HINT_OPTION.length();
extractedHint = codePath.substring(hintOffset);
}
} catch(Exception ex) {}
return extractedHint;
}
private String cleanOptionsFromPath(String codePath) {
String cleanedPath = codePath;
try {
int hintOptIdx = codePath.indexOf(LANG_HINT_OPTION);
if(hintOptIdx != -1) {
cleanedPath = codePath.substring(0, hintOptIdx);
}
} catch(Exception cex) {}
return cleanedPath;
}
private static final String SOURCE_CODE = "PITCHME.code";
private static final String SOURCE_CODE_DELIMITER =
"### Code Block Delimiter";

View File

@ -2,17 +2,17 @@
* 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
@ -26,6 +26,7 @@ package com.gitpitch.services;
import com.gitpitch.models.MarkdownModel;
import com.gitpitch.services.ImageService;
import com.gitpitch.utils.PitchParams;
import com.gitpitch.utils.DelimParams;
import com.gitpitch.utils.YAMLOptions;
import java.util.*;
import javax.inject.*;
@ -65,14 +66,14 @@ public class GISTService {
}
public String build(String md,
DelimParams dp,
PitchParams pp,
YAMLOptions yOpts,
MarkdownModel mdm) {
try {
String gid =
md.substring(mdm.horizGISTDelim().length());
String gid = dp.get(MarkdownModel.DELIM_QUERY_GIST);
String gistCallback =
com.gitpitch.controllers.routes.PitchController.gist(gid)

View File

@ -2,17 +2,17 @@
* 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
@ -25,6 +25,7 @@ package com.gitpitch.services;
import com.gitpitch.models.MarkdownModel;
import com.gitpitch.utils.PitchParams;
import com.gitpitch.utils.DelimParams;
import com.gitpitch.utils.YAMLOptions;
import org.apache.commons.io.FilenameUtils;
import java.util.*;
@ -41,16 +42,24 @@ public class ImageService {
private final Logger.ALogger log = Logger.of(this.getClass());
public String buildBackground(PitchParams pp,
YAMLOptions yOpts) {
YAMLOptions yOpts) {
return buildBackground(pp, yOpts.fetchImageBg(pp), yOpts.fetchImageBgSize(pp));
return buildBackground(yOpts.fetchImageBg(pp), yOpts.fetchImageBgSize(pp));
}
public String buildBackground(PitchParams pp,
String imageBgUrl, String imageBgSize) {
public String buildBackground(String md,
DelimParams dp,
PitchParams pp,
String defaultSize,
MarkdownModel mdm) {
String bgSize = getSizeOptionFromUrl(imageBgUrl, imageBgSize);
String bgUrl = cleanOptionsFromUrl(imageBgUrl);
String bgUrl = dp.get(MarkdownModel.DELIM_QUERY_IMAGE);
bgUrl = mdm.linkLive(pp, bgUrl);
String bgSize = dp.get(MarkdownModel.DELIM_QUERY_SIZE, defaultSize);
return buildBackground(bgUrl, bgSize);
}
private String buildBackground(String bgUrl, String bgSize) {
return new StringBuffer(MarkdownModel.MD_SPACER)
.append(MarkdownModel.MD_IMAGE_OPEN)
@ -107,31 +116,6 @@ public class ImageService {
return md.contains(MarkdownModel.DATA_IMAGE_ATTR);
}
public String extractBgUrl(String md,
String gitRawBase,
MarkdownModel mdm) {
try {
String delim = mdm.extractImageDelim(md);
String imageBgUrl = md.substring(delim.length());
if (mdm.linkAbsolute(imageBgUrl)) {
return imageBgUrl;
} else {
return new StringBuffer(gitRawBase).append(imageBgUrl)
.toString();
}
} catch (Exception pex) {
log.warn("extractImageBgUrl: ex={}", pex);
/*
* Invalid bg syntax, return clean slide delimiter.
*/
return mdm.isHorizontal(md) ? mdm.horizDelim() : mdm.vertDelim();
}
}
/*
* Return true is HTML image tag found.
*/
@ -143,7 +127,7 @@ public class ImageService {
int linkTagStart = md.indexOf(IMG_TAG_SRC_OPEN);
int linkStart = linkTagStart + IMG_TAG_SRC_OPEN.length();
int linkEnd =
int linkEnd =
md.indexOf(IMG_TAG_SRC_CLOSE, linkStart);
String tagLink = IMG_TAG_LINK_UNKNOWN;
@ -154,33 +138,6 @@ public class ImageService {
return tagLink;
}
private String getSizeOptionFromUrl(String url, String defaultSize) {
String extractedSize = defaultSize;
try {
int sizeOptIdx = url.indexOf(IMG_CUSTOM_SIZE_OPTION);
if(sizeOptIdx != -1) {
int sizeOffset = sizeOptIdx + IMG_CUSTOM_SIZE_OPTION.length();
extractedSize = url.substring(sizeOffset);
}
} catch(Exception ex) {}
return extractedSize;
}
private String cleanOptionsFromUrl(String url) {
String cleanedUrl = url;
try {
int sizeOptIdx = url.indexOf(IMG_CUSTOM_SIZE_OPTION);
if(sizeOptIdx != -1) {
cleanedUrl = url.substring(0, sizeOptIdx);
}
} catch(Exception cex) {}
return cleanedUrl;
}
private static final String IMG_OFFLINE_DIR = "./assets/md/assets/";
private static final String IMG_INLINE_OPEN = "![Image](" + IMG_OFFLINE_DIR;
private static final String IMG_INLINE_CLOSE = ")";

View File

@ -2,17 +2,17 @@
* 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
@ -24,6 +24,8 @@
package com.gitpitch.services;
import com.gitpitch.models.MarkdownModel;
import com.gitpitch.utils.PitchParams;
import com.gitpitch.utils.DelimParams;
import java.util.*;
import javax.inject.*;
import play.Logger;
@ -118,32 +120,13 @@ public class VideoService {
}
public String extractBgUrl(String md,
String gitRawBase,
MarkdownModel mdm) {
public String buildBackground(String md,
DelimParams dp,
PitchParams pp,
MarkdownModel mdm) {
try {
String delim = mdm.extractVideoDelim(md);
String videoBgUrl = md.substring(delim.length());
if (mdm.linkAbsolute(videoBgUrl)) {
return videoBgUrl;
} else {
return new StringBuffer(gitRawBase).append(videoBgUrl)
.toString();
}
} catch (Exception pex) {
log.warn("processVideoBg: ex={}", pex);
/*
* Invalid bg syntax, return clean slide delimiter.
*/
return mdm.isHorizontal(md) ? mdm.horizDelim() : mdm.vertDelim();
}
}
public String buildBackground(String bgUrl) {
String bgUrl = dp.get(MarkdownModel.DELIM_QUERY_VIDEO, "#");
bgUrl = mdm.linkLive(pp, bgUrl);
return new StringBuffer(MarkdownModel.MD_SPACER)
.append(MarkdownModel.MD_SPACER)

View File

@ -0,0 +1,72 @@
/*
* MIT License
*
* Copyright (c) 2017 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.utils;
import java.util.List;
import java.util.Collections;
import java.util.Optional;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import java.nio.charset.StandardCharsets;
/*
* Markdown Delimiter Parameter Parser.
*/
public class DelimParams {
private String delimiter;
private List<NameValuePair> params = Collections.emptyList();
private DelimParams(String delimiter) {
this.delimiter = delimiter;
if(delimiter != null && delimiter.contains(QUERY_ON_DELIM)) {
int queryIndex = delimiter.indexOf(QUERY_ON_DELIM) + 1;
String delimQuery = delimiter.substring(queryIndex);
this.params =
URLEncodedUtils.parse(delimQuery, StandardCharsets.UTF_8);
}
}
public static DelimParams build(String delimiter) {
return new DelimParams(delimiter);
}
public String get(String param) {
return get(param, null);
}
public String get(String param, String defaultValue) {
Optional<NameValuePair> match =
params.stream()
.filter(nvp -> nvp.getName().equals(param))
.findFirst();
return match.isPresent() ? match.get().getValue() : defaultValue;
}
public static final String QUERY_ON_DELIM = "?";
}