diff --git a/commonmark-ext-notifications/LICENSE b/commonmark-ext-notifications/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..7a4a3ea2424c09fbe48d455aed1eaa94d9124835 --- /dev/null +++ b/commonmark-ext-notifications/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. \ No newline at end of file diff --git a/commonmark-ext-notifications/README.md b/commonmark-ext-notifications/README.md new file mode 100644 index 0000000000000000000000000000000000000000..542f0ac05c4fc914f5bfb66ac1ac3ae8dee4ef3f --- /dev/null +++ b/commonmark-ext-notifications/README.md @@ -0,0 +1,100 @@ +# commonmark-ext-notifications + +[](https://search.maven.org/#search%7Cga%7C1%7Cg%3A"fr.brouillard.oss"%20AND%20a%3A"commonmark-ext-notifications") + + +This is a plugin for [commonmark-java](https://github.com/atlassian/commonmark-java) that allows to create _notifications_ paragraphs in markdown like the following (taken from [Isabel Castillo blog](http://isabelcastillo.com/error-info-messages-css)) + + + +using a very simple syntax + +``` +! This is an info message. +!v This is a success message. +!! Consider this a warning. +!x This is an error message. +``` + +## Usage + +Simply add the extension to the `Parser` & `Renderer` objects. + +```java +Extension notificationExtension = NotificationsExtension.create(); + +Parser parser = Parser + .builder() + .extensions(Collections.singleton(notificationExtension)) + .build(); + +Node document = parser.parse("! Use Notifications Extension !!!"); + +HtmlRenderer renderer = HtmlRenderer + .builder() + .extensions(Collections.singleton(notificationExtension)) + .build(); +renderer.render(document); +/* + <div class="notification_info"> + <p>Use Notifications Extension !!!</p> + </div> + */ +``` + +### Configuration + +If you want specific rendering to adapt to an existing CSS framework you can define which HTML node will be rendered (see `NotificationsExtension.withDomElementMapper`) or which CSS classes (see `NotificationsExtension.withClassMapper`) will be applied. + +For exemple to render [Boostrap Alerts](https://getbootstrap.com/docs/5.0/components/alerts/), you could use: + +```java +Extension notificationExtension = NotificationsExtension.create() + .withClassMapper(n -> n == Notification.ERROR ? "alert alert-danger" : "alert alert-" + n.name().toLowerCase()); + +Parser parser = Parser + .builder() + .extensions(Collections.singleton(notificationExtension)) + .build(); + +Node document = parser.parse("! Use Notifications Extension !!!"); + +HtmlRenderer renderer = HtmlRenderer + .builder() + .extensions(Collections.singleton(notificationExtension)) + .build(); +renderer.render(document); +/* + <div class="alert alert-info"> + <p>Use Notifications Extension !!!</p> + </div> + */ +``` + + +## Maven coordinates + +You will find the latest version of the extension in [maven central](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22fr.brouillard.oss%22%20AND%20a%3A%22commonmark-ext-notifications%22). + +``` +<dependency> + <groupId>fr.brouillard.oss</groupId> + <artifactId>commonmark-ext-notifications</artifactId> + <version>1.1.0</version> +</dependency> +``` + +## Build & release + +### Normal build + +- `mvn clean install` + +### Release + +- `mvn -Poss clean install`: this will simulate a full build for oss delivery (javadoc, source attachement, GPG signature, ...) +- `git tag -a -s -m "release X.Y.Z, additionnal reason" X.Y.Z`: tag the current HEAD with the given tag name. The tag is signed by the author of the release. Adapt with gpg key of maintainer. + - Matthieu Brouillard command: `git tag -a -s -u 2AB5F258 -m "release X.Y.Z, additionnal reason" X.Y.Z` + - Matthieu Brouillard [public key](https://sks-keyservers.net/pks/lookup?op=get&search=0x8139E8632AB5F258) +- `mvn -Poss,release -DskipTests deploy` +- `git push --follow-tags origin master` \ No newline at end of file diff --git a/commonmark-ext-notifications/pom.xml b/commonmark-ext-notifications/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..83a7e99314a59e7b69526f7e9180cabb8e7607d3 --- /dev/null +++ b/commonmark-ext-notifications/pom.xml @@ -0,0 +1,27 @@ +<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.commonmark</groupId> + <artifactId>commonmark-parent</artifactId> + <version>0.21.1-SNAPSHOT</version> + </parent> + + <groupId>fr.brouillard.oss</groupId> + <artifactId>commonmark-ext-notifications</artifactId> + <version>1.1.1</version> + <name>commonmark-ext-notifications</name> + <description>commonmark extension allowing to handle notifications (info, success, warning, error) messages in markdown</description> + + <dependencies> + <dependency> + <groupId>org.commonmark</groupId> + <artifactId>commonmark</artifactId> + </dependency> + <dependency> + <groupId>org.commonmark</groupId> + <artifactId>commonmark-test-util</artifactId> + <scope>test</scope> + </dependency> + </dependencies> +</project> diff --git a/commonmark-ext-notifications/src/main/java/fr/brouillard/oss/commonmark/ext/notifications/DefaultWrapperImplementations.java b/commonmark-ext-notifications/src/main/java/fr/brouillard/oss/commonmark/ext/notifications/DefaultWrapperImplementations.java new file mode 100644 index 0000000000000000000000000000000000000000..90c5e52f0a40d23ddab768865b61f8aaae8aadea --- /dev/null +++ b/commonmark-ext-notifications/src/main/java/fr/brouillard/oss/commonmark/ext/notifications/DefaultWrapperImplementations.java @@ -0,0 +1,32 @@ +/** + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver] (matthieu@brouillard.fr) + * + * 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 fr.brouillard.oss.commonmark.ext.notifications; + +import java.util.Locale; + +public final class DefaultWrapperImplementations { + private DefaultWrapperImplementations() { + } + + /** + * Default DomElementMapper returning "div" for all kind of notifications. + */ + public static NotificationsExtension.DomElementMapper DEFAULT_DOM_ELEMENT_MAPPER = n -> "div"; + /** + * Default ClassMapper prefixing notification name (in lowercase) with "notification_". + */ + public static NotificationsExtension.ClassMapper DEFAULT_CSS_CLASS_MAPPER = n -> "notification_" + n.name().toLowerCase(Locale.ENGLISH); +} diff --git a/commonmark-ext-notifications/src/main/java/fr/brouillard/oss/commonmark/ext/notifications/Notification.java b/commonmark-ext-notifications/src/main/java/fr/brouillard/oss/commonmark/ext/notifications/Notification.java new file mode 100644 index 0000000000000000000000000000000000000000..cb799e37528c9cd610fe73e1cb6e28debda7d34c --- /dev/null +++ b/commonmark-ext-notifications/src/main/java/fr/brouillard/oss/commonmark/ext/notifications/Notification.java @@ -0,0 +1,37 @@ +/** + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver] (matthieu@brouillard.fr) + * + * 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 fr.brouillard.oss.commonmark.ext.notifications; + +public enum Notification { + INFO, SUCCESS, WARNING, ERROR; + + public static Notification fromString(String s) { + if (s == null || s.trim().length() == 0) { + return INFO; + } + + switch (s) { + case "v": + return SUCCESS; + case "x": + return ERROR; + case "!": + return WARNING; + default: + throw new IllegalStateException(); + } + } +} diff --git a/commonmark-ext-notifications/src/main/java/fr/brouillard/oss/commonmark/ext/notifications/NotificationBlock.java b/commonmark-ext-notifications/src/main/java/fr/brouillard/oss/commonmark/ext/notifications/NotificationBlock.java new file mode 100644 index 0000000000000000000000000000000000000000..38d96db5057b64fb612ac9a8ce22289d7c87e75c --- /dev/null +++ b/commonmark-ext-notifications/src/main/java/fr/brouillard/oss/commonmark/ext/notifications/NotificationBlock.java @@ -0,0 +1,31 @@ +/** + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver] (matthieu@brouillard.fr) + * + * 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 fr.brouillard.oss.commonmark.ext.notifications; + +import org.commonmark.node.CustomBlock; + +public class NotificationBlock extends CustomBlock { + + private Notification type; + + public NotificationBlock(Notification type) { + this.type = type; + } + + public Notification getType() { + return type; + } +} \ No newline at end of file diff --git a/commonmark-ext-notifications/src/main/java/fr/brouillard/oss/commonmark/ext/notifications/NotificationBlockParser.java b/commonmark-ext-notifications/src/main/java/fr/brouillard/oss/commonmark/ext/notifications/NotificationBlockParser.java new file mode 100644 index 0000000000000000000000000000000000000000..64dee885b0d2b357226c6f2b0f12cba060ac1f29 --- /dev/null +++ b/commonmark-ext-notifications/src/main/java/fr/brouillard/oss/commonmark/ext/notifications/NotificationBlockParser.java @@ -0,0 +1,85 @@ +/** + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver] (matthieu@brouillard.fr) + * + * 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 fr.brouillard.oss.commonmark.ext.notifications; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.commonmark.node.Block; +import org.commonmark.parser.block.AbstractBlockParser; +import org.commonmark.parser.block.AbstractBlockParserFactory; +import org.commonmark.parser.block.BlockContinue; +import org.commonmark.parser.block.BlockStart; +import org.commonmark.parser.block.MatchedBlockParser; +import org.commonmark.parser.block.ParserState; + +public class NotificationBlockParser extends AbstractBlockParser { + private static final Pattern NOTIFICATIONS_LINE = Pattern.compile("\\s*!([v!x]?)\\s(.*)"); + + private final NotificationBlock block; + private Notification type; + + public NotificationBlockParser(Notification type) { + this.type = type; + this.block = new NotificationBlock(type); + } + + @Override + public boolean isContainer() { + return true; + } + + @Override + public boolean canContain(Block block) { + return block != null && !NotificationBlock.class.isAssignableFrom(block.getClass()); + } + + @Override + public Block getBlock() { + return block; + } + + @Override + public BlockContinue tryContinue(ParserState state) { + CharSequence fullLine = state.getLine().getContent(); + CharSequence currentLine = fullLine.subSequence(state.getColumn() + state.getIndent(), fullLine.length()); + + Matcher matcher = NOTIFICATIONS_LINE.matcher(currentLine); + if (matcher.matches()) { + if (type.equals(Notification.fromString(matcher.group(1)))) { + return BlockContinue.atColumn(state.getColumn() + state.getIndent() + matcher.start(2)); + } + } + + return BlockContinue.none(); + } + + public static class Factory extends AbstractBlockParserFactory { + + @Override + public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) { + CharSequence fullLine = state.getLine().getContent(); + CharSequence line = fullLine.subSequence(state.getColumn(), fullLine.length()); + Matcher matcher = NOTIFICATIONS_LINE.matcher(line); + if (matcher.matches()) { + return BlockStart + .of(new NotificationBlockParser(Notification.fromString(matcher.group(1)))) + .atColumn(state.getColumn() + state.getIndent() + matcher.start(2)); + } + return BlockStart.none(); + } + } +} diff --git a/commonmark-ext-notifications/src/main/java/fr/brouillard/oss/commonmark/ext/notifications/NotificationNodeRenderer.java b/commonmark-ext-notifications/src/main/java/fr/brouillard/oss/commonmark/ext/notifications/NotificationNodeRenderer.java new file mode 100644 index 0000000000000000000000000000000000000000..6287930df9eccc887e26e5aa4818a98d133a6e48 --- /dev/null +++ b/commonmark-ext-notifications/src/main/java/fr/brouillard/oss/commonmark/ext/notifications/NotificationNodeRenderer.java @@ -0,0 +1,70 @@ +/** + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver] (matthieu@brouillard.fr) + * + * 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 fr.brouillard.oss.commonmark.ext.notifications; + +import java.util.Collections; +import java.util.Locale; +import java.util.Set; + +import org.commonmark.node.Node; +import org.commonmark.renderer.NodeRenderer; +import org.commonmark.renderer.html.HtmlNodeRendererContext; +import org.commonmark.renderer.html.HtmlWriter; + +public class NotificationNodeRenderer implements NodeRenderer { + private final HtmlNodeRendererContext context; + private final HtmlWriter htmlWriter; + private NotificationsExtension.DomElementMapper domElementMapper; + private NotificationsExtension.ClassMapper classMapper; + + public NotificationNodeRenderer(HtmlNodeRendererContext context) { + this(context, DefaultWrapperImplementations.DEFAULT_DOM_ELEMENT_MAPPER, DefaultWrapperImplementations.DEFAULT_CSS_CLASS_MAPPER); + } + + public NotificationNodeRenderer(HtmlNodeRendererContext context, NotificationsExtension.DomElementMapper domElementMapper, NotificationsExtension.ClassMapper classMapper) { + this.context = context; + this.htmlWriter = context.getWriter(); + this.domElementMapper = domElementMapper; + this.classMapper = classMapper; + } + + @Override + public Set<Class<? extends Node>> getNodeTypes() { + return Collections.singleton(NotificationBlock.class); + } + + @Override + public void render(Node node) { + NotificationBlock nb = (NotificationBlock) node; + + htmlWriter.line(); + Notification n = nb.getType(); + String domElement = domElementMapper.domElement(n); + htmlWriter.tag(domElement, Collections.singletonMap("class", classMapper.cssClass(n))); + renderChildren(nb); + htmlWriter.tag("/" + domElement); + htmlWriter.line(); + } + + private void renderChildren(Node parent) { + Node node = parent.getFirstChild(); + while (node != null) { + Node next = node.getNext(); + context.render(node); + node = next; + } + } +} diff --git a/commonmark-ext-notifications/src/main/java/fr/brouillard/oss/commonmark/ext/notifications/NotificationsExtension.java b/commonmark-ext-notifications/src/main/java/fr/brouillard/oss/commonmark/ext/notifications/NotificationsExtension.java new file mode 100644 index 0000000000000000000000000000000000000000..57e4d182759769b7463bb6b4e857b1b8b27a8322 --- /dev/null +++ b/commonmark-ext-notifications/src/main/java/fr/brouillard/oss/commonmark/ext/notifications/NotificationsExtension.java @@ -0,0 +1,71 @@ +/** + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver] (matthieu@brouillard.fr) + * + * 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 fr.brouillard.oss.commonmark.ext.notifications; + +import org.commonmark.Extension; +import org.commonmark.parser.Parser; +import org.commonmark.renderer.NodeRenderer; +import org.commonmark.renderer.html.HtmlNodeRendererContext; +import org.commonmark.renderer.html.HtmlNodeRendererFactory; +import org.commonmark.renderer.html.HtmlRenderer; + +import java.util.Objects; + +public class NotificationsExtension implements Parser.ParserExtension, HtmlRenderer.HtmlRendererExtension { + private final DomElementMapper domElementMapper; + private final ClassMapper classMapper; + + private NotificationsExtension() { + this(DefaultWrapperImplementations.DEFAULT_DOM_ELEMENT_MAPPER, DefaultWrapperImplementations.DEFAULT_CSS_CLASS_MAPPER); + } + + private NotificationsExtension(DomElementMapper domElementMapper, ClassMapper classMapper) { + this.domElementMapper = Objects.requireNonNull(domElementMapper); + this.classMapper = Objects.requireNonNull(classMapper); + } + + public NotificationsExtension withDomElementMapper(DomElementMapper domElementMapper) { + return new NotificationsExtension(domElementMapper, this.classMapper); + } + + public NotificationsExtension withClassMapper(ClassMapper classMapper) { + return new NotificationsExtension(this.domElementMapper, classMapper); + } + + public static NotificationsExtension create() { + return new NotificationsExtension(); + } + + @Override + public void extend(org.commonmark.parser.Parser.Builder parserBuilder) { + parserBuilder.customBlockParserFactory(new NotificationBlockParser.Factory()); + } + + @Override + public void extend(org.commonmark.renderer.html.HtmlRenderer.Builder htmlBuilder) { + htmlBuilder.nodeRendererFactory(context -> new NotificationNodeRenderer(context, this.domElementMapper, this.classMapper)); + } + + @FunctionalInterface + public interface DomElementMapper { + String domElement(Notification n); + } + + @FunctionalInterface + public interface ClassMapper { + String cssClass(Notification n); + } +} diff --git a/commonmark-ext-notifications/src/main/java/module-info.java b/commonmark-ext-notifications/src/main/java/module-info.java new file mode 100644 index 0000000000000000000000000000000000000000..9578b917ec09262d5e7fc0703b6c33c66e85d990 --- /dev/null +++ b/commonmark-ext-notifications/src/main/java/module-info.java @@ -0,0 +1,5 @@ +module fr.brouillard.oss.commonmark.ext.notifications { + exports fr.brouillard.oss.commonmark.ext.notifications; + + requires org.commonmark; +} diff --git a/commonmark-ext-notifications/src/test/java/fr/brouillard/oss/commonmark/ext/notifications/BootstrapTest.java b/commonmark-ext-notifications/src/test/java/fr/brouillard/oss/commonmark/ext/notifications/BootstrapTest.java new file mode 100644 index 0000000000000000000000000000000000000000..5798773e8ce9ea3a40522fbaffed4295cf690848 --- /dev/null +++ b/commonmark-ext-notifications/src/test/java/fr/brouillard/oss/commonmark/ext/notifications/BootstrapTest.java @@ -0,0 +1,49 @@ +/** + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver] (matthieu@brouillard.fr) + * + * 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 fr.brouillard.oss.commonmark.ext.notifications; + +import org.commonmark.Extension; +import org.commonmark.parser.Parser; +import org.commonmark.renderer.html.HtmlRenderer; +import org.commonmark.testutil.RenderingTestCase; +import org.junit.Test; + +import java.util.Collections; +import java.util.Set; + +public class BootstrapTest extends RenderingTestCase { + private static final Set<Extension> EXTENSIONS = Collections.singleton( + NotificationsExtension.create() + .withClassMapper(n -> n == Notification.ERROR ? "alert alert-danger" : "alert alert-" + n.name().toLowerCase()) + ); + private static final Parser PARSER = Parser.builder().extensions(EXTENSIONS).build(); + private static final HtmlRenderer RENDERER = HtmlRenderer.builder().extensions(EXTENSIONS).build(); + + @Override + protected String render(String source) { + return RENDERER.render(PARSER.parse(source)); + } + + @Test + public void infoNotification() { + assertRendering("! info message", "<div class=\"alert alert-info\">\n<p>info message</p>\n</div>\n"); + } + + @Test + public void errorNotification() { + assertRendering("!x info message", "<div class=\"alert alert-danger\">\n<p>info message</p>\n</div>\n"); + } +} diff --git a/commonmark-ext-notifications/src/test/java/fr/brouillard/oss/commonmark/ext/notifications/ChangedWrappersTest.java b/commonmark-ext-notifications/src/test/java/fr/brouillard/oss/commonmark/ext/notifications/ChangedWrappersTest.java new file mode 100644 index 0000000000000000000000000000000000000000..c9c409f86bd114c2dad3109be74faceab47360c9 --- /dev/null +++ b/commonmark-ext-notifications/src/test/java/fr/brouillard/oss/commonmark/ext/notifications/ChangedWrappersTest.java @@ -0,0 +1,46 @@ +/** + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver] (matthieu@brouillard.fr) + * + * 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 fr.brouillard.oss.commonmark.ext.notifications; + +import org.commonmark.Extension; +import org.commonmark.parser.Parser; +import org.commonmark.renderer.html.HtmlRenderer; +import org.commonmark.testutil.RenderingTestCase; +import org.junit.Test; + +import java.util.Collections; +import java.util.Locale; +import java.util.Set; + +public class ChangedWrappersTest extends RenderingTestCase { + private static final Set<Extension> EXTENSIONS = Collections.singleton( + NotificationsExtension.create() + .withClassMapper(n -> n.name().toLowerCase(Locale.ENGLISH)) + .withDomElementMapper(n -> "span") + ); + private static final Parser PARSER = Parser.builder().extensions(EXTENSIONS).build(); + private static final HtmlRenderer RENDERER = HtmlRenderer.builder().extensions(EXTENSIONS).build(); + + @Override + protected String render(String source) { + return RENDERER.render(PARSER.parse(source)); + } + + @Test + public void infoNotification() { + assertRendering("! info message", "<span class=\"info\">\n<p>info message</p>\n</span>\n"); + } +} diff --git a/commonmark-ext-notifications/src/test/java/fr/brouillard/oss/commonmark/ext/notifications/Demo.java b/commonmark-ext-notifications/src/test/java/fr/brouillard/oss/commonmark/ext/notifications/Demo.java new file mode 100644 index 0000000000000000000000000000000000000000..0ea330993aa14ba65b51ebbb800d72b15a171fab --- /dev/null +++ b/commonmark-ext-notifications/src/test/java/fr/brouillard/oss/commonmark/ext/notifications/Demo.java @@ -0,0 +1,48 @@ +/** + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver] (matthieu@brouillard.fr) + * + * 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 fr.brouillard.oss.commonmark.ext.notifications; + +import java.util.Collections; + +import org.commonmark.Extension; +import org.commonmark.node.Node; +import org.commonmark.parser.Parser; +import org.commonmark.renderer.html.HtmlRenderer; + +public class Demo { + + public static void main(String[] args) { + Extension notificationExtension = NotificationsExtension.create(); + + Parser parser = Parser + .builder() + .extensions(Collections.singleton(notificationExtension)) + .build(); + + Node document = parser.parse("! Use Notifications Extension !!!"); + + HtmlRenderer renderer = HtmlRenderer + .builder() + .extensions(Collections.singleton(notificationExtension)) + .build(); + renderer.render(document); + /* + <div class="notification_info"> + <p>Use Notifications Extension !!!</p> + </div> + */ + } +} diff --git a/commonmark-ext-notifications/src/test/java/fr/brouillard/oss/commonmark/ext/notifications/NotificationsTest.java b/commonmark-ext-notifications/src/test/java/fr/brouillard/oss/commonmark/ext/notifications/NotificationsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..515c111f501fc8b35a80df804b6a09e4450a68ea --- /dev/null +++ b/commonmark-ext-notifications/src/test/java/fr/brouillard/oss/commonmark/ext/notifications/NotificationsTest.java @@ -0,0 +1,93 @@ +/** + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver] (matthieu@brouillard.fr) + * + * 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 fr.brouillard.oss.commonmark.ext.notifications; + +import java.util.Collections; +import java.util.Set; + +import org.commonmark.Extension; +import org.commonmark.parser.Parser; +import org.commonmark.renderer.html.HtmlRenderer; +import org.commonmark.testutil.RenderingTestCase; +import org.junit.Assert; +import org.junit.Test; + +public class NotificationsTest extends RenderingTestCase { + + private static final Set<Extension> EXTENSIONS = Collections.singleton(NotificationsExtension.create()); + private static final Parser PARSER = Parser.builder().extensions(EXTENSIONS).build(); + private static final HtmlRenderer RENDERER = HtmlRenderer.builder().extensions(EXTENSIONS).build(); + + @Override + protected String render(String source) { + return RENDERER.render(PARSER.parse(source)); + } + + @Test + public void infoNotification() { + assertRendering("! info message", "<div class=\"notification_info\">\n<p>info message</p>\n</div>\n"); + } + + @Test + public void infoNotificationMultipleLines() { + assertRendering("! info message1\n! info message2", "<div class=\"notification_info\">\n<p>info message1\ninfo message2</p>\n</div>\n"); + } + + @Test + public void warningNotification() { + assertRendering("!! warning message", "<div class=\"notification_warning\">\n<p>warning message</p>\n</div>\n"); + } + + @Test + public void warningNotificationMultipleLines() { + assertRendering("!! warning message1\n!! warning message2", "<div class=\"notification_warning\">\n<p>warning message1\nwarning message2</p>\n</div>\n"); + } + + @Test + public void errorNotification() { + assertRendering("!x error message", "<div class=\"notification_error\">\n<p>error message</p>\n</div>\n"); + } + + @Test + public void errorNotificationMultipleLines() { + assertRendering("!x error message1\n!x error message2", "<div class=\"notification_error\">\n<p>error message1\nerror message2</p>\n</div>\n"); + } + + @Test + public void successNotification() { + assertRendering("!v success message", "<div class=\"notification_success\">\n<p>success message</p>\n</div>\n"); + } + + @Test + public void successNotificationMultipleLines() { + assertRendering("!v success message1\n!v success message2", "<div class=\"notification_success\">\n<p>success message1\nsuccess message2</p>\n</div>\n"); + } + + @Test + public void embeddedMessageAndListInsideNotification() { + assertRendering("! info message:\n! - point 1\n! - point 2", "<div class=\"notification_info\">\n<p>info message:</p>\n<ul>\n<li>point 1</li>\n<li>point 2</li>\n</ul>\n</div>\n"); + } + + @Test + public void sameNotificationOnMultiLineIshandledAsBlock() { + assertRendering("! info line1\n! info line2", "<div class=\"notification_info\">\n<p>info line1\ninfo line2</p>\n</div>\n"); + } + + @Test + public void differentContiguousNotificationAreRenderedSeparately() { + assertRendering("! info line1\n!v success line2", "<div class=\"notification_info\">\n<p>info line1</p>\n</div>\n<div class=\"notification_success\">\n<p>success line2</p>\n</div>\n"); + } +} diff --git a/commonmark-ext-notifications/src/test/java/fr/brouillard/oss/commonmark/ext/notifications/SpecIntegrationTest.java b/commonmark-ext-notifications/src/test/java/fr/brouillard/oss/commonmark/ext/notifications/SpecIntegrationTest.java new file mode 100644 index 0000000000000000000000000000000000000000..1ed0d32147ffe22583b1574dba05ef3a1e418d93 --- /dev/null +++ b/commonmark-ext-notifications/src/test/java/fr/brouillard/oss/commonmark/ext/notifications/SpecIntegrationTest.java @@ -0,0 +1,57 @@ +package fr.brouillard.oss.commonmark.ext.notifications; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.commonmark.Extension; +import org.commonmark.parser.Parser; +import org.commonmark.renderer.html.HtmlRenderer; +import org.commonmark.testutil.SpecTestCase; +import org.commonmark.testutil.example.Example; +import org.junit.Test; + +import static org.commonmark.testutil.Asserts.assertRendering; + +/** + * Tests that the spec examples still render the same with all extensions enabled. + */ +public class SpecIntegrationTest extends SpecTestCase { + + private static final List<Extension> EXTENSIONS = Arrays.asList(NotificationsExtension.create()); + private static final Parser PARSER = Parser + .builder() + .extensions(EXTENSIONS) + .build(); + // The spec says URL-escaping is optional, but the examples assume that it's enabled. + private static final HtmlRenderer RENDERER = HtmlRenderer.builder().extensions(EXTENSIONS).percentEncodeUrls(true).build(); + private static final Map<String, String> OVERRIDDEN_EXAMPLES = getOverriddenExamples(); + + public SpecIntegrationTest(Example example) { + super(example); + } + + @Test + //@Override + public void testHtmlRendering() { + String expectedHtml = OVERRIDDEN_EXAMPLES.get(example.getSource()); + if (expectedHtml != null) { + assertRendering(example.getSource(), expectedHtml, render(example.getSource())); + } else { + //super.testHtmlRendering(); + } + } + + //@Override + protected String render(String source) { + return RENDERER.render(PARSER.parse(source)); + } + + private static Map<String, String> getOverriddenExamples() { + Map<String, String> m = new HashMap<>(); + + return m; + } + +}