From 632ba2dd1be605fcae2571f7e7dfcb7884f6ac7d Mon Sep 17 00:00:00 2001 From: David Russell Date: Fri, 17 Nov 2017 07:58:02 +0700 Subject: [PATCH] Added support for FontAwesome shortcuts. --- app/com/gitpitch/models/MarkdownModel.java | 6 ++ .../gitpitch/services/ShortcutsService.java | 90 +++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/app/com/gitpitch/models/MarkdownModel.java b/app/com/gitpitch/models/MarkdownModel.java index 68376c7..12798cd 100644 --- a/app/com/gitpitch/models/MarkdownModel.java +++ b/app/com/gitpitch/models/MarkdownModel.java @@ -109,6 +109,8 @@ public class MarkdownModel implements Markdown { consumed = stream.map(md -> { return process(md, pp, yOpts, gitRawBase); + }).map(md -> { + return shortcutsService.process(md, pp, yOpts); }).collect(Collectors.joining("\n")); consumed = postProcess(consumed, pp, yOpts, gitRawBase); @@ -658,6 +660,10 @@ public class MarkdownModel implements Markdown { public static final String MD_CODE_BLOCK_CLOSE = "```"; public static final String MD_TITLE_HINT_OPEN = "@title["; public static final String MD_TITLE_HINT_CLOSE = "]"; + public static final String MD_FA_OPEN = "@fa["; + public static final String MD_FA_CLOSE = "]"; + public static final String MD_FA_NOTE_OPEN = "("; + public static final String MD_FA_NOTE_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 index fe0e021..9e58cac 100644 --- a/app/com/gitpitch/services/ShortcutsService.java +++ b/app/com/gitpitch/services/ShortcutsService.java @@ -40,6 +40,22 @@ public class ShortcutsService { private final Logger.ALogger log = Logger.of(this.getClass()); + public String process(String md, PitchParams pp, YAMLOptions yOpts) { + + String processed = md; + + try { + int faCycles = 5; // Prevent infinite loop. + while(fontAwesomeFound(processed) && faCycles > 0) { + String expanded = expandFontAwesome(processed); + faCycles = processed.equals(expanded) ? 0 : (faCycles-1); + processed = expanded; + } + } catch(Exception ex) {} + + return processed; + } + public boolean listFragmentFound(String md) { boolean found = false; if(md != null) { @@ -72,6 +88,16 @@ public class ShortcutsService { return found; } + public boolean fontAwesomeFound(String md) { + boolean found = false; + if(md != null) { + if(md.contains(MarkdownModel.MD_FA_OPEN)) { + found = true; + } + } + return found; + } + /* * Expand shortcut syntax for list fragment with fully expanded * HTML comment syntax. @@ -149,6 +175,53 @@ public class ShortcutsService { } } + /* + * Expand shortcut syntax for font-awesome with fully expanded + * HTML span syntax with optional text. + */ + public String expandFontAwesome(String md) { + + String mdLeft = ""; + String faName = ""; + String faNote = ""; + String mdRight = ""; + String expandedFA = ""; + + try { + int faOpen = md.indexOf(MarkdownModel.MD_FA_OPEN); + int faClose = md.indexOf(MarkdownModel.MD_FA_CLOSE); + + if(faClose > faOpen) { /* faName exists */ + + mdLeft = md.substring(0, faOpen); + faName = + md.substring(faOpen + MarkdownModel.MD_FA_OPEN.length(), + faClose); + + String rmd = md.substring(faClose + 1); + + int faNoteOpen = rmd.indexOf(MarkdownModel.MD_FA_NOTE_OPEN); + int faNoteClose = rmd.indexOf(MarkdownModel.MD_FA_NOTE_CLOSE); + + if(faNoteOpen == 0) { /* faNote immediately follows faName */ + + faNote = + rmd.substring(MarkdownModel.MD_FA_NOTE_OPEN.length(), + faNoteClose); + mdRight = rmd.substring(faNoteClose + 1); + } else { + mdRight = rmd; + } + + expandedFA = buildFontAwesome(faName, faNote); + } + + } catch(Exception ex) { + } finally { + return mdLeft + expandedFA + mdRight; + } + } + /* * Construct a HTML fragment for a code fragment based on the * reveal-code-focus plugin syntax: @@ -164,6 +237,19 @@ public class ShortcutsService { .toString(); } + /* + * Construct a HTML fragment for an FA icon. + */ + private String buildFontAwesome(String font, String text) { + if(text == null) text = ""; + return new StringBuffer(HTML_FONT_FRAG_OPEN) + .append(font) + .append(HTML_FONT_FRAG_CLOSE) + .append(text) + .append(HTML_FONT_FRAG_TEXT_CLOSE) + .toString(); + } + /* * Note, the space before opening bracket is intentional * so tag injection can happen directly alongside other @@ -178,4 +264,8 @@ public class ShortcutsService { private static final String TITLE_HINT_SPAN_OPEN = ""; private static final String TITLE_HINT_SPAN_CLOSE = ""; + private static final String HTML_FONT_FRAG_OPEN = " "; + private static final String HTML_FONT_FRAG_TEXT_CLOSE = ""; + }