001package org.hl7.fhir.utilities;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033
034import java.util.Collections;
035import java.util.Set;
036import java.util.regex.Matcher;
037import java.util.regex.Pattern;
038
039import org.commonmark.Extension;
040import org.commonmark.ext.gfm.tables.TablesExtension;
041import org.commonmark.node.Node;
042import org.commonmark.parser.Parser;
043import org.commonmark.renderer.html.HtmlRenderer;
044import org.commonmark.renderer.text.TextContentRenderer;
045
046import com.github.rjeschke.txtmark.Configuration;
047import com.github.rjeschke.txtmark.Processor;
048
049public class MarkDownProcessor {
050
051  public enum Dialect {DARING_FIREBALL, COMMON_MARK};
052  
053  private Dialect dialect;
054  
055  
056  public MarkDownProcessor(Dialect dialect) {
057    super();
058    this.dialect = dialect;
059  }
060
061
062  public String process(String source, String context) {
063    if (source == null) {
064      return null;
065    }
066    if ("".equals(source)) {
067      return "";
068    }
069    switch (dialect) {
070    case DARING_FIREBALL : 
071      return Processor.process(source, Configuration.builder().enableSafeMode().enablePanicMode().build()); 
072    case COMMON_MARK : return processCommonMark(preProcess(source)); 
073    default: throw new Error("Unknown Markdown Dialect: "+dialect.toString()+" at "+context); 
074    }
075  }
076
077  /**
078   * Returns true if this is intended to be processed as markdown
079   * 
080   * this is guess, based on textual analysis of the content. 
081   * 
082   * Uses of this routine:
083   *   In general, the main use of this is to decide to escape the string so erroneous markdown processing doesn't munge characters
084   *   If it's a plain string, and it's being put into something that's markdown, then you should escape the content
085   *   If it's markdown, but you're not sure whether to process it as markdown
086   *   
087   * The underlying problem is that markdown processing plain strings is problematic because some technical characters might 
088   * get lost. So it's good to escape them... but if it's meant to be markdown, then it'll get trashed. 
089   * 
090   * This method works by looking for character patterns that are unlikely to occur outside markdown - but it's still only unlikely
091   *  
092   * @param content
093   * @return
094   */
095  // todo: dialect dependency?
096  public boolean isProbablyMarkdown(String content, boolean mdIfParagrapghs) {
097    if (content == null) {
098      return false;
099    }
100    if (mdIfParagrapghs && content.contains("\n")) {
101      return true;
102    }
103    String[] lines = content.split("\\r?\\n");
104    for (String s : lines) {
105      if (s.startsWith("* ") || isHeading(s) || s.startsWith("1. ") || s.startsWith("    ")) {
106        return true;
107      }
108      if (s.contains("```") || s.contains("~~~") || s.contains("[[[")) {
109        return true;
110      }
111      if (hasLink(s)) {
112        return true;
113      }
114      if (hasTextSpecial(s, '*') || hasTextSpecial(s, '_') ) {
115        return true;
116      }
117    }
118      
119    return false;
120  }
121  
122  private boolean isHeading(String s) {
123    if (s.length() > 7 && s.startsWith("###### ") && !Character.isWhitespace(s.charAt(7))) {
124      return true;
125    }
126    if (s.length() > 6 && s.startsWith("##### ") && !Character.isWhitespace(s.charAt(6))) {
127      return true;
128    }
129    if (s.length() > 5 && s.startsWith("#### ") && !Character.isWhitespace(s.charAt(5))) {
130      return true;
131    }
132    if (s.length() > 4 && s.startsWith("### ") && !Character.isWhitespace(s.charAt(4))) {
133      return true;
134    }
135    if (s.length() > 3 && s.startsWith("## ") && !Character.isWhitespace(s.charAt(3))) {
136      return true;
137    }
138    //
139    // not sure about this one. # [string] is something that could easily arise in non-markdown, 
140    // so this appearing isn't enough to call it markdown
141    //
142//    if (s.length() > 2 && s.startsWith("# ") && !Character.isWhitespace(s.charAt(2))) {
143//      return true;
144//    }
145    return false;
146  }
147
148
149  private boolean hasLink(String s) {
150    int left = -1;
151    int mid = -1;
152    for (int i = 0; i < s.length(); i++) {
153      char c = s.charAt(i);
154      if (c == '[') {
155        mid = -1;
156        left = i;
157      } else if (left > -1 && i < s.length()-1 && c == ']' && s.charAt(i+1) == '(') {
158        mid = i;
159      } else if (left > -1 && c == ']') {
160        left = -1;
161      } else if (left > -1 && mid > -1 && c == ')') {
162        return true;
163      } else if (mid > -1 && c == '[' || c == ']' || (c == '(' && i > mid+1)) {
164        left = -1;
165        mid = -1;
166      }
167    }
168
169    // Detect autolinks, which should start with a scheme, followed by a colon, followed by some content. Whitespace
170    // is not allowed and for practical purposes, the scheme is considered to consist of lowercase ASCII characters
171    // only.
172    Pattern autolinkPattern = Pattern.compile("<[a-z]+:[^\\s]+>");
173    Matcher autolinkMatcher = autolinkPattern.matcher(s);
174    return autolinkMatcher.find();
175  }
176
177
178  private boolean hasTextSpecial(String s, char c) {
179    boolean second = false;
180    for (int i = 0; i < s.length(); i++) {
181      char prev = i == 0 ? ' ' : s.charAt(i-1);
182      char next = i < s.length() - 1 ? s.charAt(i+1) : ' ';
183      if (s.charAt(i) != c) {
184        // nothing
185      } else if (second) {
186        if (Character.isWhitespace(next) && (isPunctation(prev) || Character.isLetterOrDigit(prev))) {
187          return true;
188        }
189        second = false;        
190      } else {
191        if (Character.isWhitespace(prev) && (isPunctation(next) || Character.isLetterOrDigit(next))) {
192          second = true;
193        }            
194      }
195    }
196    return false;
197  }
198
199
200  private boolean isPunctation(char ch) {
201    return Utilities.existsInList(ch, '.', ',', '!', '?');
202  }
203
204
205  /**
206   * This deals with a painful problem created by the intersection of previous publishing processes 
207   * and the way commonmark specifies that < is handled in content. For control reasons, the FHIR specification does 
208   * not allow raw html tags in the markdown 
209   * 
210   * This check finds any raw html tag and prepends \ to it so that it renders as a < (e.g. gets escaped in the output
211   * HTML)
212   * 
213   * This is public to enable testing (not for direct use otherwise)
214   * 
215   * @param source
216   * @return
217   */
218  public static String preProcess(String source) {
219    // Escape all unescaped open and closing tags ('<' or '</', followed by an ASCII letter, followed by ASCII 
220    // letters, digits and/or hyphens).
221    String processed = source.replaceAll("(?<!\\\\)<(\\/)?([A-Za-z][A-Za-z0-9-]*[\\s>])", "\\\\<$1$2");
222
223    // Escape all other HTML tags: HTML comments, processing instructions, declarations and CDATA sections -- 
224    // everything starting with '<?' or '<!'.
225    processed = processed.replaceAll("<(!|\\?)", "\\\\<$1");
226
227    return processed;
228  }
229
230  private String processCommonMark(String source) {
231    Set<Extension> extensions = Collections.singleton(TablesExtension.create());
232    Parser parser = Parser.builder().extensions(extensions).build();
233    Node document = parser.parse(source);
234    HtmlRenderer renderer = HtmlRenderer.builder().escapeHtml(true).extensions(extensions).build();
235    String html = renderer.render(document);
236    html = html.replace("<table>", "<table class=\"grid\">");
237    return html;  
238  }
239
240
241  public static boolean isSimpleMarkdown(String description) {
242    return !description.contains("\n");
243  }
244  
245  public static String makeStringSafeAsMarkdown(String content) {
246    StringBuilder b = new StringBuilder();
247    for (char c : content.toCharArray()) {
248      if (isEscableMarkdownChar(c)) {
249        b.append('\\');
250      }
251      b.append(c);
252    }
253    return b.toString();
254  }
255  
256  private static boolean isEscableMarkdownChar(char c) {
257    switch (c) {
258    case '*':
259    case '&':
260    case '#':
261    case '[':
262    case '>':
263    case '<':
264    case '`':
265      return true;  
266    default: 
267      return false;
268    }
269  }
270
271  public static String makeMarkdownForString(String content) {
272    StringBuilder b = new StringBuilder();
273    for (int i = 0; i < content.length(); i++) {
274      char c = content.charAt(i);
275      if (c != '\\' || i == content.length() - 1 || !isEscableMarkdownChar(content.charAt(i+1))) {
276        b.append(c);
277      }
278    }
279    return b.toString();
280  }
281
282  public static String markdownToPlainText(String source) {
283    if (source == null) {
284      return "";
285    }
286    Set<Extension> extensions = Collections.singleton(TablesExtension.create());
287    Parser parser = Parser.builder().extensions(extensions).build();
288    Node document = parser.parse(source);
289    TextContentRenderer renderer = TextContentRenderer.builder().stripNewlines(true).extensions(extensions).build();
290    String text = renderer.render(document);
291    return text;  
292  }
293}