-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
586 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
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
51 changes: 51 additions & 0 deletions
51
src/main/java/io/github/gitbucket/markedj/extension/Extension.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,51 @@ | ||
/* | ||
* Copyright 2023 GitBucket. | ||
* | ||
* 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 io.github.gitbucket.markedj.extension; | ||
|
||
import io.github.gitbucket.markedj.Lexer; | ||
import io.github.gitbucket.markedj.Parser; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
*/ | ||
public interface Extension { | ||
|
||
public LexResult lex(String source, final Lexer.LexerContext context, final TokenConsumer consumer); | ||
|
||
public boolean handlesToken (final String token); | ||
|
||
public String parse (Parser.ParserContext context, Function<Parser.ParserContext, String> tok); | ||
|
||
public static class LexResult { | ||
private final String source; | ||
private final boolean matches; | ||
|
||
public LexResult(String source, boolean matches) { | ||
this.source = source; | ||
this.matches = matches; | ||
} | ||
|
||
public String getSource() { | ||
return source; | ||
} | ||
|
||
public boolean matches() { | ||
return matches; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/io/github/gitbucket/markedj/extension/TokenConsumer.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,27 @@ | ||
/* | ||
* Copyright 2023 GitBucket. | ||
* | ||
* 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 io.github.gitbucket.markedj.extension; | ||
|
||
import io.github.gitbucket.markedj.Lexer; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
*/ | ||
@FunctionalInterface | ||
public interface TokenConsumer { | ||
public void token (String src, boolean top, boolean bq, Lexer.LexerContext context); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/io/github/gitbucket/markedj/extension/notification/NotificationEndToken.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,29 @@ | ||
/* | ||
* Copyright 2023 GitBucket. | ||
* | ||
* 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 io.github.gitbucket.markedj.extension.notification; | ||
|
||
import io.github.gitbucket.markedj.token.Token; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
*/ | ||
public class NotificationEndToken implements Token { | ||
@Override | ||
public String getType() { | ||
return "NotificationEndToken"; | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
src/main/java/io/github/gitbucket/markedj/extension/notification/NotificationExtension.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,98 @@ | ||
/* | ||
* Copyright 2023 GitBucket. | ||
* | ||
* 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 io.github.gitbucket.markedj.extension.notification; | ||
|
||
import io.github.gitbucket.markedj.Lexer; | ||
import io.github.gitbucket.markedj.Parser; | ||
import io.github.gitbucket.markedj.extension.Extension; | ||
import io.github.gitbucket.markedj.extension.TokenConsumer; | ||
import io.github.gitbucket.markedj.rule.FindFirstRule; | ||
import io.github.gitbucket.markedj.rule.Rule; | ||
import io.github.gitbucket.markedj.token.Token; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.function.Function; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
*/ | ||
public class NotificationExtension implements Extension { | ||
|
||
public static String BLOCK_NOTIFICATION = "^((?:(!([xv!]?))[^\n]*(?!^!)\n?)+)"; | ||
|
||
private Rule notificationRule() { | ||
return new FindFirstRule(BLOCK_NOTIFICATION); | ||
} | ||
|
||
@Override | ||
public LexResult lex(String source, final Lexer.LexerContext context, final TokenConsumer consumer) { | ||
List<String> cap = notificationRule().exec(source); | ||
boolean tokenFound = false; | ||
if (!cap.isEmpty()) { | ||
// we have detected several contiguous lines of notifications | ||
// ensure that all are of same kind | ||
String allNotificationsLines = cap.get(0); | ||
|
||
// if other kind of notifications lines are detected | ||
// let's split them so that they are handled separately | ||
String findOtherLinesPattern = "(?m)^(!" + Notifications.exceptGivenNotificationClass(cap.get(3)) + " .*)"; | ||
Matcher otherLinesMatcher = Pattern.compile(findOtherLinesPattern).matcher(cap.get(1)); | ||
|
||
if (otherLinesMatcher.find()) { | ||
String otherLinesSeparated = otherLinesMatcher.replaceAll("\n$1\n"); | ||
|
||
// change the source to parse | ||
// replace all the notifications lines with separated notifications lines | ||
// and reparse the string | ||
source = otherLinesSeparated + source.substring(allNotificationsLines.length()); | ||
} else { | ||
source = source.substring(allNotificationsLines.length()); | ||
context.pushToken(new NotificationStartToken(cap.get(3))); | ||
consumer.token(allNotificationsLines.replaceAll("(?m)^" + cap.get(2) + "[ ]?", ""), false, false, context); | ||
context.pushToken(new NotificationEndToken()); | ||
} | ||
|
||
tokenFound = true; | ||
} | ||
return new LexResult(source, tokenFound); | ||
} | ||
|
||
@Override | ||
public boolean handlesToken(String token) { | ||
return NotificationStartToken.TYPE.equals(token); | ||
} | ||
|
||
@Override | ||
public String parse(Parser.ParserContext context, Function<Parser.ParserContext, String> tok) { | ||
NotificationStartToken t = (NotificationStartToken) context.currentToken(); | ||
StringBuilder body = new StringBuilder(); | ||
while (true) { | ||
Token n = context.nextToken(); | ||
if (n == null || n.getType().equals("NotificationEndToken")) { | ||
break; | ||
} | ||
body.append(tok.apply(context)); | ||
} | ||
return render(body.toString(), t.getNotification()); | ||
} | ||
|
||
private String render(String info, Notifications.Notification notification) { | ||
return String.format("<div class=\"notification_%s\">\n%s</div>\n", notification.name().toLowerCase(Locale.ENGLISH), info); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/io/github/gitbucket/markedj/extension/notification/NotificationStartToken.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,41 @@ | ||
/* | ||
* Copyright 2023 GitBucket. | ||
* | ||
* 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 io.github.gitbucket.markedj.extension.notification; | ||
|
||
import io.github.gitbucket.markedj.token.Token; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
*/ | ||
public class NotificationStartToken implements Token { | ||
protected static String TYPE = "NotificationStartToken"; | ||
|
||
private Notifications.Notification notification; | ||
|
||
public NotificationStartToken(String type) { | ||
this.notification = Notifications.Notification.fromString(type); | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return TYPE; | ||
} | ||
|
||
public Notifications.Notification getNotification() { | ||
return notification; | ||
} | ||
} |
Oops, something went wrong.