Commit 1151a43e authored by Ubbo Veentjer's avatar Ubbo Veentjer
Browse files

display markdown from tgrep as html

parent 7bab98fe
Pipeline #136203 passed with stages
in 8 minutes and 13 seconds
package info.textgrid.rep.browse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
......@@ -16,10 +18,13 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import info.textgrid.clients.tgcrud.CrudClientException;
import info.textgrid.namespaces.middleware.tgsearch.ResultType;
import info.textgrid.rep.i18n.I18N;
import info.textgrid.rep.i18n.I18NProvider;
import info.textgrid.rep.markdown.MarkdownUtils;
import info.textgrid.rep.service.tgsearch.AggregatorClientService;
import info.textgrid.rep.service.tgsearch.TgcrudClientService;
import info.textgrid.rep.service.tgsearch.TgrepConfigurationService;
import info.textgrid.rep.service.tgsearch.TgsearchClientService;
import info.textgrid.rep.shared.Utils;
......@@ -28,15 +33,18 @@ import info.textgrid.rep.shared.ViewMode;
@Controller
public class BrowseController {
@Autowired
private TgsearchClientService tgsearchClientService;
@Autowired
private TgrepConfigurationService tgrepConfig;
@Autowired
private AggregatorClientService aggregatorClient;
@Autowired
private TgcrudClientService tgcrudClientService;
@Autowired
private TgsearchClientService tgsearchClientService;
@Autowired
private I18NProvider i18nProvider;
......@@ -100,6 +108,8 @@ public class BrowseController {
handleXml(model, i18n, id, metadata, fragment);
} else if (format.contains("image")) {
handleImages(model, id);
} else if (format.equals("text/markdown")) {
handleMarkdown(model, id);
}
return "browse";
......@@ -248,6 +258,27 @@ public class BrowseController {
model.addAttribute("tools", tools);
}
private void handleMarkdown(Model model, String id) {
InputStream contentStream;
String content;
try {
contentStream = this.tgcrudClientService.read(id);
content = MarkdownUtils.renderHtml(contentStream);
} catch (CrudClientException e) {
//TODO: nicer error strings
log.error("error reading data from crud", e);
content = "error reading data from crud";
} catch (IOException e) {
//TODO: nicer error strings
log.error("error parsing markdown", e);
content = "error parsing markdown";
}
model.addAttribute("htmlContent", content);
}
private boolean hasIiifManifest(String id) {
IIIFProjects iiip = ClientBuilder.newBuilder().build()
......
......@@ -51,11 +51,7 @@ public class DocController {
String content;
try {
InputStreamReader reader = new InputStreamReader(res.getInputStream());
Parser parser = Parser.builder().build();
Node document = parser.parseReader(reader);
HtmlRenderer renderer = HtmlRenderer.builder().build();
content = renderer.render(document);
content = MarkdownUtils.renderHtml(res.getInputStream());
} catch (IOException e) {
log.error("error rendering file " + doc, e);
content = "Sorry, there was an error opening file " + doc;
......
package info.textgrid.rep.markdown;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
// TODO: a service should be better to reuse the parser and renderer?
public class MarkdownUtils {
public static String renderHtml(InputStream in) throws IOException {
InputStreamReader reader = new InputStreamReader(in);
Parser parser = Parser.builder().build();
Node document = parser.parseReader(reader);
HtmlRenderer renderer = HtmlRenderer.builder().build();
return renderer.render(document);
}
}
package info.textgrid.rep.service.tgsearch;
import java.io.InputStream;
import javax.annotation.PostConstruct;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import info.textgrid.clients.CrudClient;
import info.textgrid.clients.tgcrud.CrudClientException;
@Service
public class TgcrudClientService {
@Autowired
private TgrepConfigurationService tgrepConfig;
private CrudClient crudClient;
private static final Log log = LogFactory.getLog(TgsearchClientService.class);
@PostConstruct
public void postConstruct() throws CrudClientException {
this.setupClient();
}
private void setupClient() throws CrudClientException {
log.info("setting up tgsearch client for host: " + tgrepConfig.getTextgridHost());
// Create CrudClient with GZIP compression enabled
crudClient = new CrudClient(tgrepConfig.getTextgridHost() + "/1.0/tgcrud-public/TGCrudService")
.enableGzipCompression();
}
public InputStream read(String textgridUri) throws CrudClientException {
return this.crudClient.read().setTextgridUri(textgridUri).execute().getData();
}
public CrudClient getClient() {
return crudClient;
}
}
......@@ -40,7 +40,7 @@ public class TgsearchClientService {
private void setupClient() {
log.info("setting up tgsearch client for host: " + tgrepConfig.getTextgridHost());
// Create Searchclient with GZIP Compression enabled
// Create Searchclient with GZIP compression enabled
searchClient = new SearchClient(tgrepConfig.getTextgridHost() + "/1.0/tgsearch-public")
.enableGzipCompression();
......
......@@ -243,7 +243,11 @@
</p>
<a target="_blank" href="${textgridHost}/1.0/tgcrud-public/rest/${metadata.object.generic.generated.textgridUri.value}/data">${i18n['open-html-in-new-window']}</a>
<iframe id="htmlIframe" width="560" height="600" frameborder="0" src="${textgridHost}/1.0/tgcrud-public/rest/${metadata.object.generic.generated.textgridUri.value}/data"></iframe>
</c:when>
</c:when>
<c:when test="${metadata.object.generic.provided.format eq 'text/markdown'}">
${htmlContent}
</c:when>
<c:otherwise>
<p>${i18n['dont-know-what-todo']} ${format}</p>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment