DescriptorListExporter.java

/*
 * Copyright 2018 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.genesys.server.service.worker;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.genesys.server.model.traits.DescriptorList;
import org.genesys.server.service.FreemarkerTemplating;
import org.genesys.server.service.FreemarkerTemplating.FreemarkerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension;
import com.vladsch.flexmark.ext.tables.TablesExtension;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.KeepType;
import com.vladsch.flexmark.util.data.MutableDataHolder;
import com.vladsch.flexmark.util.data.MutableDataSet;

/**
 * Export Descriptor list.
 */
@Component
public class DescriptorListExporter {

	@Autowired
	private FreemarkerTemplating freemarkerTemplating;

	@Value("${cdn.servers}")
	private String[] cdnServers;

	/** The Constant OPTIONS. */
	/*@formatter:off*/
	static final MutableDataHolder OPTIONS = new MutableDataSet()
			.set(Parser.REFERENCES_KEEP, KeepType.LAST)
			.set(Parser.HTML_BLOCK_PARSER, false)
//			.set(Parser.HTML_BLOCK_DEEP_PARSER, true)
//			.set(Parser.HTML_BLOCK_START_ONLY_ON_BLOCK_TAGS, true)
			.set(HtmlRenderer.INDENT_SIZE, 2)
			.set(HtmlRenderer.PERCENT_ENCODE_URLS, true)
//			.set(HtmlRenderer.ESCAPE_HTML, false)

			// for full GFM table compatibility add the following table extension options:
			.set(TablesExtension.COLUMN_SPANS, false)
			.set(TablesExtension.APPEND_MISSING_COLUMNS, true)
			.set(TablesExtension.DISCARD_EXTRA_COLUMNS, true)
			.set(TablesExtension.HEADER_SEPARATOR_COLUMN_MATCH, true)
			.set(Parser.EXTENSIONS, Arrays.asList(
				TablesExtension.create(),
				StrikethroughExtension.create()
			));
	/*@formatter:on*/

	/**
	 * Markdown descriptor list.
	 *
	 * @param descriptorList the descriptor list
	 * @return the string
	 * @throws FreemarkerException the freemarker exception
	 */
	public String markdownDescriptorList(final DescriptorList descriptorList) throws FreemarkerException {
		final Map<String, Object> root = new HashMap<>();
		root.put("descriptorList", descriptorList);
		return freemarkerTemplating.processTemplateResource("descriptorlist/booklet.ftl", root);
	}

	/**
	 * Html descriptor list.
	 *
	 * @param descriptorList the descriptor list
	 * @return the string
	 * @throws FreemarkerException the freemarker exception
	 */
	public String htmlDescriptorList(final DescriptorList descriptorList) throws FreemarkerException {
		final Map<String, Object> root = new HashMap<>();
		root.put("descriptorList", descriptorList);
		root.put("cdnServer", cdnServers[0]);
		final String markdown = freemarkerTemplating.processTemplateResource("descriptorlist/booklet.ftl", root);
		// System.err.println(markdown);

		final Parser parser = Parser.builder(OPTIONS).build();
		final Node document = parser.parse(markdown);
		final HtmlRenderer renderer = HtmlRenderer.builder(OPTIONS).build();
		final String html = renderer.render(document);
		// System.err.println(html);

		// HTML wrapper
		root.clear();
		root.put("title", descriptorList.getTitle());
		root.put("author", StringUtils.defaultIfEmpty(descriptorList.getPublisher(), descriptorList.getOwner().getName()));
		root.put("html", html);

		return freemarkerTemplating.processTemplateResource("html.ftl", root);
	}

}