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; 044 045import com.github.rjeschke.txtmark.Configuration; 046import com.github.rjeschke.txtmark.Processor; 047 048public class MarkDownProcessor { 049 050 public enum Dialect {DARING_FIREBALL, COMMON_MARK}; 051 052 private Dialect dialect; 053 054 055 public MarkDownProcessor(Dialect dialect) { 056 super(); 057 this.dialect = dialect; 058 } 059 060 061 public String process(String source, String context) { 062 if (source == null) { 063 return null; 064 } 065 if ("".equals(source)) { 066 return ""; 067 } 068 switch (dialect) { 069 case DARING_FIREBALL : 070 return Processor.process(source, Configuration.builder().enableSafeMode().enablePanicMode().build()); 071 case COMMON_MARK : return processCommonMark(preProcess(source)); 072 default: throw new Error("Unknown Markdown Dialect: "+dialect.toString()+" at "+context); 073 } 074 } 075 076 /** 077 * Returns true if this is intended to be processed as markdown 078 * 079 * this is guess, based on textual analysis of the content. 080 * 081 * Uses of this routine: 082 * In general, the main use of this is to decide to escape the string so erroneous markdown processing doesn't munge characters 083 * If it's a plain string, and it's being put into something that's markdown, then you should escape the content 084 * If it's markdown, but you're not sure whether to process it as markdown 085 * 086 * The underlying problem is that markdown processing plain strings is problematic because some technical characters might 087 * get lost. So it's good to escape them... but if it's meant to be markdown, then it'll get trashed. 088 * 089 * This method works by looking for character patterns that are unlikely to occur outside markdown - but it's still only unlikely 090 * 091 * @param content 092 * @return 093 */ 094 // todo: dialect dependency? 095 public boolean isProbablyMarkdown(String content, boolean mdIfParagrapghs) { 096 if (content == null) { 097 return false; 098 } 099 if (mdIfParagrapghs && content.contains("\n")) { 100 return true; 101 } 102 String[] lines = content.split("\\r?\\n"); 103 for (String s : lines) { 104 if (s.startsWith("* ") || isHeading(s) || s.startsWith("1. ") || s.startsWith(" ")) { 105 return true; 106 } 107 if (s.contains("```") || s.contains("~~~") || s.contains("[[[")) { 108 return true; 109 } 110 if (hasLink(s)) { 111 return true; 112 } 113 if (hasTextSpecial(s, '*') || hasTextSpecial(s, '_') ) { 114 return true; 115 } 116 } 117 118 return false; 119 } 120 121 private boolean isHeading(String s) { 122 if (s.length() > 7 && s.startsWith("###### ") && !Character.isWhitespace(s.charAt(7))) { 123 return true; 124 } 125 if (s.length() > 6 && s.startsWith("##### ") && !Character.isWhitespace(s.charAt(6))) { 126 return true; 127 } 128 if (s.length() > 5 && s.startsWith("#### ") && !Character.isWhitespace(s.charAt(5))) { 129 return true; 130 } 131 if (s.length() > 4 && s.startsWith("### ") && !Character.isWhitespace(s.charAt(4))) { 132 return true; 133 } 134 if (s.length() > 3 && s.startsWith("## ") && !Character.isWhitespace(s.charAt(3))) { 135 return true; 136 } 137 // 138 // not sure about this one. # [string] is something that could easily arise in non-markdown, 139 // so this appearing isn't enough to call it markdown 140 // 141// if (s.length() > 2 && s.startsWith("# ") && !Character.isWhitespace(s.charAt(2))) { 142// return true; 143// } 144 return false; 145 } 146 147 148 private boolean hasLink(String s) { 149 int left = -1; 150 int mid = -1; 151 for (int i = 0; i < s.length(); i++) { 152 char c = s.charAt(i); 153 if (c == '[') { 154 mid = -1; 155 left = i; 156 } else if (left > -1 && i < s.length()-1 && c == ']' && s.charAt(i+1) == '(') { 157 mid = i; 158 } else if (left > -1 && c == ']') { 159 left = -1; 160 } else if (left > -1 && mid > -1 && c == ')') { 161 return true; 162 } else if (mid > -1 && c == '[' || c == ']' || (c == '(' && i > mid+1)) { 163 left = -1; 164 mid = -1; 165 } 166 } 167 168 // Detect autolinks, which should start with a scheme, followed by a colon, followed by some content. Whitespace 169 // is not allowed and for practical purposes, the scheme is considered to consist of lowercase ASCII characters 170 // only. 171 Pattern autolinkPattern = Pattern.compile("<[a-z]+:[^\\s]+>"); 172 Matcher autolinkMatcher = autolinkPattern.matcher(s); 173 return autolinkMatcher.find(); 174 } 175 176 177 private boolean hasTextSpecial(String s, char c) { 178 boolean second = false; 179 for (int i = 0; i < s.length(); i++) { 180 char prev = i == 0 ? ' ' : s.charAt(i-1); 181 char next = i < s.length() - 1 ? s.charAt(i+1) : ' '; 182 if (s.charAt(i) != c) { 183 // nothing 184 } else if (second) { 185 if (Character.isWhitespace(next) && (isPunctation(prev) || Character.isLetterOrDigit(prev))) { 186 return true; 187 } 188 second = false; 189 } else { 190 if (Character.isWhitespace(prev) && (isPunctation(next) || Character.isLetterOrDigit(next))) { 191 second = true; 192 } 193 } 194 } 195 return false; 196 } 197 198 199 private boolean isPunctation(char ch) { 200 return Utilities.existsInList(ch, '.', ',', '!', '?'); 201 } 202 203 204 /** 205 * This deals with a painful problem created by the intersection of previous publishing processes 206 * and the way commonmark specifies that < is handled in content. For control reasons, the FHIR specification does 207 * not allow raw html tags in the markdown 208 * 209 * This check finds any raw html tag and prepends \ to it so that it renders as a < (e.g. gets escaped in the output 210 * HTML) 211 * 212 * This is public to enable testing (not for direct use otherwise) 213 * 214 * @param source 215 * @return 216 */ 217 public static String preProcess(String source) { 218 // Escape all unescaped open and closing tags ('<' or '</', followed by an ASCII letter, followed by ASCII 219 // letters, digits and/or hyphens). 220 String processed = source.replaceAll("(?<!\\\\)<(\\/)?([A-Za-z][A-Za-z0-9-]*[\\s>])", "\\\\<$1$2"); 221 222 // Escape all other HTML tags: HTML comments, processing instructions, declarations and CDATA sections -- 223 // everything starting with '<?' or '<!'. 224 processed = processed.replaceAll("<(!|\\?)", "\\\\<$1"); 225 226 return processed; 227 } 228 229 private String processCommonMark(String source) { 230 Set<Extension> extensions = Collections.singleton(TablesExtension.create()); 231 Parser parser = Parser.builder().extensions(extensions).build(); 232 Node document = parser.parse(source); 233 HtmlRenderer renderer = HtmlRenderer.builder().escapeHtml(true).extensions(extensions).build(); 234 String html = renderer.render(document); 235 html = html.replace("<table>", "<table class=\"grid\">"); 236 return html; 237 } 238 239 240 public static boolean isSimpleMarkdown(String description) { 241 return !description.contains("\n"); 242 } 243 244 public static String makeStringSafeAsMarkdown(String content) { 245 StringBuilder b = new StringBuilder(); 246 for (char c : content.toCharArray()) { 247 if (isEscableMarkdownChar(c)) { 248 b.append('\\'); 249 } 250 b.append(c); 251 } 252 return b.toString(); 253 } 254 255 private static boolean isEscableMarkdownChar(char c) { 256 switch (c) { 257 case '*': 258 case '&': 259 case '#': 260 case '[': 261 case '>': 262 case '<': 263 case '`': 264 return true; 265 default: 266 return false; 267 } 268 } 269 270 public static String makeMarkdownForString(String content) { 271 StringBuilder b = new StringBuilder(); 272 for (int i = 0; i < content.length(); i++) { 273 char c = content.charAt(i); 274 if (c != '\\' || i == content.length() - 1 || !isEscableMarkdownChar(content.charAt(i+1))) { 275 b.append(c); 276 } 277 } 278 return b.toString(); 279 } 280 281}