Skip to content

Commit

Permalink
Merge pull request #92 from mcruzdev/markdown
Browse files Browse the repository at this point in the history
Add markdown extension
  • Loading branch information
mkouba committed Jul 8, 2024
2 parents 7ffeed7 + 4068839 commit 05a10fa
Show file tree
Hide file tree
Showing 16 changed files with 618 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* xref:index.adoc[Quarkus Qute Web]
* xref:index.adoc[Quarkus Qute Markdown]
107 changes: 107 additions & 0 deletions docs/modules/ROOT/pages/markdown.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
= Quarkus Qute Markdown

include::./includes/attributes.adoc[]

The goal of this extension is to provide a simple way to render Markdown templates using Qute. It adds a new Qute section
named `markdown` or `md` that can be used to convert the content of the section to HTML using a Markdown processor.

== Installation

To install the extension, add the following dependency to your project:

[source,xml,subs=attributes+]
----
<dependency>
<groupId>io.quarkiverse.qute-markdown</groupId>
<artifactId>quarkus-qute-web-markdown</artifactId>
<version>999-SNAPSHOT</version>
</dependency>
----

== Usage

This extension can be used to render Markdown templates inside a Qute template or in a web application using the `quarkus-qute-web` extension.

=== Standalone Usage

Create a new Qute template with `foo.txt` name.

[source,html]
----
<!-- src/resources/templates/foo.txt -->
{#md}
# Hello World
{/md}
----

Create a new resource class and inject the template using the `@Inject` annotation. Then, use the `render` method to render the template.
[source,java]
----
package com.foo;
import io.quarkus.qute.Template;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
@Path("/hello")
public class GreetingResource {
@Inject
Template foo;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return foo.render();
}
}
----

Finally, start your application and open the following URL in your browser: `http://localhost:8080/hello`.

The output should be the following:

[source,html]
----
<h1>Hello World</h1>
----

=== Integration with Qute Web

This extension can be combined with the `quarkus-qute-web` extension to render Markdown templates in a web application.
Add the `quarkus-qute-web` extension to your project, inside the `pom.xml` file:

[source,xml,subs=attributes+]
----
<dependency>
<groupId>io.quarkiverse.qute.web</groupId>
<artifactId>quarkus-qute-web</artifactId>
<version>${project.version}</version>
</dependency>
----

Then, create a new Qute template with the `.txt` or `.html` extension.

[source,html]
----
<!-- src/resources/templates/pub/markdown.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Qute Page with Markdown</title>
</head>
<body>
<h1>Hello World!</h1>
{#md}
# This is a Markdown section
It will be converted to HTML using a Markdown processor.
{/md}
</body>
</html>
----

Finally, start your application and open the following URL in your browser: `http://localhost:8080/markdown`.
124 changes: 124 additions & 0 deletions examples/markdown-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>io.quarkiverse.qute.web</groupId>
<artifactId>quarkus-qute-web-restclient-example</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<compiler-plugin.version>3.10.1</compiler-plugin.version>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.version>3.6.6</quarkus.platform.version>
<qute.web.version>3.0.0</qute.web.version>
<qute.web.markdown.version>999-SNAPSHOT</qute.web.markdown.version>
<skipITs>true</skipITs>
<surefire-plugin.version>3.0.0-M7</surefire-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-reactive-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.qute.web</groupId>
<artifactId>quarkus-qute-web</artifactId>
<version>${qute.web.version}</version>
</dependency>
<dependency>
<groupId>io.quarkiverse.qute.web</groupId>
<artifactId>quarkus-qute-web-markdown</artifactId>
<version>${qute.web.markdown.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.platform.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>
org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>
${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>
org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<skipITs>false</skipITs>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
quarkus.tls.trust-all=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- src/resources/templates/pub/markdown.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Qute Page with Markdown</title>
</head>
<body>
<h1>Hello World!</h1>
{#md}
## This is a Markdown section
It will be converted to HTML using a Markdown processor.
{/md}
</body>
</html>
46 changes: 46 additions & 0 deletions markdown/deployment/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.quarkiverse.qute.web</groupId>
<artifactId>quarkus-qute-web-markdown-parent</artifactId>
<version>999-SNAPSHOT</version>
</parent>
<artifactId>quarkus-qute-web-markdown-deployment</artifactId>
<name>Quarkus Qute Web - Markdown - Deployment</name>

<dependencies>
<dependency>
<groupId>io.quarkiverse.qute.web</groupId>
<artifactId>quarkus-qute-web-markdown</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-qute-deployment</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${quarkus.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.quarkiverse.qute.web.markdown.deployment;

import io.quarkiverse.qute.web.markdown.runtime.MarkdownSectionHelperFactory;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.FeatureBuildItem;

class QuteWebMarkdownProcessor {

private static final String FEATURE = "qute-web-markdown";

@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}

@BuildStep
void process(BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
additionalBeans.produce(new AdditionalBeanBuildItem(MarkdownSectionHelperFactory.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.quarkiverse.qute.web.markdown.test;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import io.quarkiverse.qute.web.markdown.runtime.MarkdownSectionHelperFactory;
import io.quarkus.qute.Engine;

public class QuarkusMarkdownTest {

@Test
public void testMd() {
Engine engine = Engine.builder().addDefaults()
.addSectionHelper(new MarkdownSectionHelperFactory()).build();
assertEquals("<p>...</p>\n", engine.parse("{#md}...{/md}").render());
}

@Test
public void testMarkdown() {
Engine engine = Engine.builder().addDefaults()
.addSectionHelper(new MarkdownSectionHelperFactory()).build();
assertEquals("<p>...</p>\n", engine.parse("{#markdown}...{/markdown}").render());
}

@Test
public void testH1() {
Engine engine = Engine.builder().addDefaults()
.addSectionHelper(new MarkdownSectionHelperFactory()).build();
assertEquals("<h1>Quarkus and Roq</h1>\n", engine.parse("{#md}# Quarkus and Roq{/md}").render());
}

@Test
void testJsonObjectValueResolver() {

Engine engine = Engine.builder().addDefaults()
.addSectionHelper(new MarkdownSectionHelperFactory()).build();

String result = engine.parse("""
<h1>Quarkus and Qute</h1>
{#md}
# Qute and Roq
Here is a list:
{#for item in items}
- an {item} as a list item
{/for}
{/md}
""").data("items", List.of("apple", "banana", "cherry"))
.render();

Assertions.assertEquals("""
<h1>Quarkus and Qute</h1>
<h1>Qute and Roq</h1>
<p>Here is a list:</p>
<ul>
<li>an apple as a list item</li>
<li>an banana as a list item</li>
<li>an cherry as a list item</li>
</ul>
""", result);

}
}
Loading

0 comments on commit 05a10fa

Please sign in to comment.