-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#178 add initial implementation for task lists
- Loading branch information
Thorsten Marx
committed
Mar 6, 2024
1 parent
7990d4b
commit 36d4e69
Showing
5 changed files
with
285 additions
and
14 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
cms-markdown/src/main/java/com/github/thmarx/cms/markdown/rules/block/TaskListBlockRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package com.github.thmarx.cms.markdown.rules.block; | ||
|
||
/*- | ||
* #%L | ||
* cms-markdown | ||
* %% | ||
* Copyright (C) 2023 - 2024 Marx-Software | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
* #L% | ||
*/ | ||
import com.github.thmarx.cms.markdown.Block; | ||
import com.github.thmarx.cms.markdown.BlockElementRule; | ||
import com.github.thmarx.cms.markdown.InlineRenderer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
*/ | ||
public class TaskListBlockRule implements BlockElementRule { | ||
|
||
private static final Pattern PATTERN = Pattern.compile( | ||
"(^- \\[([ x]?)\\] (.+?)\n)+(^\\n|\\Z)", | ||
Pattern.MULTILINE); | ||
|
||
private static final Pattern CHECKED_PATTERN = Pattern.compile( | ||
"- \\[(?<checked>[ x]?)\\] (?<content>.+?)"); | ||
|
||
@Override | ||
public Block next(String md) { | ||
Matcher matcher = PATTERN.matcher(md); | ||
if (matcher.find()) { | ||
var content = matcher.group(0).trim(); | ||
|
||
return new TaskListBlock(matcher.start(), matcher.end(), | ||
createTaskList(content) | ||
); | ||
} | ||
return null; | ||
} | ||
|
||
private TaskList createTaskList(String content) { | ||
var rows = content.split("\n"); | ||
List<Item> definitionLists = new ArrayList<>(); | ||
for (var row : rows) { | ||
if ("".equals(row.trim())) { | ||
continue; | ||
} | ||
var matcher = CHECKED_PATTERN.matcher(row); | ||
|
||
if (matcher.matches()) { | ||
definitionLists.add( | ||
new Item( | ||
matcher.group("content").trim(), | ||
"x".equalsIgnoreCase(matcher.group("checked")) | ||
) | ||
); | ||
} | ||
} | ||
|
||
return new TaskList(definitionLists); | ||
} | ||
|
||
static record TaskList(List<Item> items) { | ||
|
||
} | ||
|
||
static record Item(String title, boolean checked) { | ||
|
||
} | ||
|
||
public static record TaskListBlock(int start, int end, TaskList taskList) implements Block { | ||
|
||
@Override | ||
public String render(InlineRenderer inlineRenderer) { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("<ul>"); | ||
|
||
taskList.items().forEach(item -> { | ||
sb.append("<li>"); | ||
|
||
sb.append("<input disabled=\"\" type=\"checkbox\" "); | ||
if (item.checked()) { | ||
sb.append("checked=\"\""); | ||
} | ||
sb.append("/> "); | ||
|
||
sb.append(item.title()); | ||
sb.append("</li>"); | ||
}); | ||
|
||
sb.append("</ul>"); | ||
|
||
return sb.toString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...kdown/src/test/java/com/github/thmarx/cms/markdown/rules/block/TaskListBlockRuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.github.thmarx.cms.markdown.rules.block; | ||
|
||
/*- | ||
* #%L | ||
* cms-markdown | ||
* %% | ||
* Copyright (C) 2023 - 2024 Marx-Software | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
* #L% | ||
*/ | ||
import java.util.List; | ||
import java.util.Optional; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
*/ | ||
public class TaskListBlockRuleTest { | ||
|
||
TaskListBlockRule sut = new TaskListBlockRule(); | ||
|
||
@Test | ||
public void basic_test() { | ||
|
||
String input = """ | ||
- [ ] foo | ||
- [x] bar | ||
"""; | ||
|
||
String expected = """ | ||
<ul> | ||
<li><input disabled="" type="checkbox" /> foo</li> | ||
<li><input disabled="" type="checkbox" checked="" /> bar</li> | ||
</ul> | ||
"""; | ||
|
||
var next = sut.next(input); | ||
|
||
Assertions.assertThat(next) | ||
.isNotNull() | ||
.isInstanceOf(TaskListBlockRule.TaskListBlock.class); | ||
|
||
Assertions.assertThat(((TaskListBlockRule.TaskListBlock) next)) | ||
.hasFieldOrPropertyWithValue("taskList", | ||
new TaskListBlockRule.TaskList(List.of( | ||
new TaskListBlockRule.Item("foo", false), | ||
new TaskListBlockRule.Item("bar", true) | ||
)) | ||
); | ||
|
||
var rendered = next.render((md) -> md); | ||
Assertions.assertThat(rendered).isEqualToIgnoringWhitespace(expected); | ||
} | ||
|
||
@Test | ||
public void mulitple_test() { | ||
|
||
String input = """ | ||
some paragrahp before | ||
- [ ] foo | ||
- [x] bar | ||
some paragrahp after | ||
"""; | ||
|
||
String expected = """ | ||
<ul> | ||
<li><input disabled="" type="checkbox" /> foo</li> | ||
<li><input disabled="" type="checkbox" checked="" /> bar</li> | ||
</ul> | ||
"""; | ||
|
||
var next = sut.next(input); | ||
|
||
Assertions.assertThat(next) | ||
.isNotNull() | ||
.isInstanceOf(TaskListBlockRule.TaskListBlock.class); | ||
|
||
Assertions.assertThat(((TaskListBlockRule.TaskListBlock) next)) | ||
.hasFieldOrPropertyWithValue("taskList", | ||
new TaskListBlockRule.TaskList(List.of( | ||
new TaskListBlockRule.Item("foo", false), | ||
new TaskListBlockRule.Item("bar", true) | ||
)) | ||
); | ||
|
||
var rendered = next.render((md) -> md); | ||
Assertions.assertThat(rendered).isEqualToIgnoringWhitespace(expected); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
cms-markdown/src/test/resources/com/github/thmarx/cms/markdown/features.tasklist.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<!-- | ||
#%L | ||
cms-markdown | ||
%% | ||
Copyright (C) 2023 - 2024 Marx-Software | ||
%% | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as | ||
published by the Free Software Foundation, either version 3 of the | ||
License, or (at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public | ||
License along with this program. If not, see | ||
<http://www.gnu.org/licenses/gpl-3.0.html>. | ||
#L% | ||
--> | ||
<div> | ||
<h1>task list test</h1> | ||
<p></p> | ||
<ul> | ||
<li><input disabled="" type="checkbox" /> foo</li> | ||
<li><input disabled="" type="checkbox" checked="" /> bar</li> | ||
</ul> | ||
|
||
<p>a paragraph</p> | ||
</div> |
6 changes: 6 additions & 0 deletions
6
...markdown/src/test/resources/com/github/thmarx/cms/markdown/features.tasklist.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# task list test | ||
|
||
- [ ] foo | ||
- [x] bar | ||
|
||
a paragraph |