use of com.vladsch.flexmark.ext.jekyll.tag.JekyllTag in project flexmark-java by vsch.
the class JekyllIncludeFileSample method commonMark.
static String commonMark(String markdown, Map<String, String> included) {
MutableDataHolder options = new MutableDataSet();
options.set(Parser.EXTENSIONS, Arrays.asList(AutolinkExtension.create(), JekyllTagExtension.create()));
// change soft break to hard break
options.set(HtmlRenderer.SOFT_BREAK, "<br/>");
Parser parser = Parser.builder(options).build();
HtmlRenderer renderer = HtmlRenderer.builder(options).build();
Node document = parser.parse(markdown);
// see if document has includes
if (document instanceof Document) {
Document doc = (Document) document;
if (doc.contains(JekyllTagExtension.TAG_LIST)) {
List<JekyllTag> tagList = JekyllTagExtension.TAG_LIST.getFrom(doc);
Map<String, String> includeHtmlMap = new HashMap<String, String>();
for (JekyllTag tag : tagList) {
String includeFile = tag.getParameters().toString();
if (tag.getTag().equals("include") && !includeFile.isEmpty() && !includeHtmlMap.containsKey(includeFile)) {
// see if it exists
if (included.containsKey(includeFile)) {
// have the file
String text = included.get(includeFile);
if (includeFile.endsWith(".md")) {
Node includeDoc = parser.parse(text);
String includeHtml = renderer.render(includeDoc);
includeHtmlMap.put(includeFile, includeHtml);
if (includeDoc instanceof Document) {
// copy any definition of reference elements from included file to our document
parser.transferReferences(doc, (Document) includeDoc);
}
} else {
includeHtmlMap.put(includeFile, text);
}
}
}
if (!includeHtmlMap.isEmpty()) {
doc.set(JekyllTagExtension.INCLUDED_HTML, includeHtmlMap);
}
}
}
}
final String html = renderer.render(document);
return html;
}
use of com.vladsch.flexmark.ext.jekyll.tag.JekyllTag in project flexmark-java by vsch.
the class JekyllTagInlineParserExtension method parse.
@Override
public boolean parse(final InlineParser inlineParser) {
if (inlineParser.peek(1) == '%' && (inlineParser.peek(2) == ' ' || inlineParser.peek(2) == '\t')) {
BasedSequence input = inlineParser.getInput();
Matcher matcher = inlineParser.matcher(parsing.MACRO_TAG);
if (matcher != null) {
BasedSequence tag = input.subSequence(matcher.start(), matcher.end());
BasedSequence tagName = input.subSequence(matcher.start(1), matcher.end(1));
BasedSequence parameters = input.subSequence(matcher.end(1), matcher.end() - 2).trim();
JekyllTag macro = new JekyllTag(tag.subSequence(0, 2), tagName, parameters, tag.endSequence(2));
macro.setCharsFromContent();
if (!listIncludesOnly || tagName.equals("includes")) {
List<JekyllTag> tagList = JekyllTagExtension.TAG_LIST.getFrom(inlineParser.getDocument());
tagList.add(macro);
}
inlineParser.flushTextNode();
inlineParser.getBlock().appendChild(macro);
return true;
}
}
return false;
}
Aggregations