Skip to content

Commit

Permalink
Improve link parsing (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
bensku committed May 3, 2017
1 parent ffd14ae commit 119056a
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 12 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
groupid=ch.njol
name=skript
version=2.2-dev26f
version=2.2-dev27
24 changes: 20 additions & 4 deletions src/main/java/ch/njol/skript/SkriptConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import ch.njol.skript.util.Task;
import ch.njol.skript.util.Timespan;
import ch.njol.skript.util.chat.ChatMessages;
import ch.njol.skript.util.chat.LinkParseMode;
import ch.njol.util.Setter;

/**
Expand Down Expand Up @@ -190,12 +191,27 @@ public void set(Boolean t) {

});

public final static Option<Boolean> parseLinks = new Option<Boolean>("parse links in chat messages", false)
.setter(new Setter<Boolean>() {
public final static Option<String> parseLinks = new Option<String>("parse links in chat messages", "disabled")
.setter(new Setter<String>() {

@Override
public void set(Boolean t) {
ChatMessages.parseLinks = t;
public void set(String t) {
switch (t) {
case "false":
case "disabled":
ChatMessages.linkParseMode = LinkParseMode.DISABLED;
break;
case "true":
case "lenient":
ChatMessages.linkParseMode = LinkParseMode.LENIENT;
break;
case "strict":
ChatMessages.linkParseMode = LinkParseMode.STRICT;
break;
default:
ChatMessages.linkParseMode = LinkParseMode.DISABLED;
Skript.warning("Unknown link parse mode: " + t + ", please use disabled, strict or lenient");
}
}

});
Expand Down
49 changes: 45 additions & 4 deletions src/main/java/ch/njol/skript/util/chat/ChatMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@
public class ChatMessages {

/**
* If anything that starts with http(s):// should be interpreted
* as a link (for old script compatibility).
* Link parse mode.
*/
public static boolean parseLinks = false;
public static LinkParseMode linkParseMode = LinkParseMode.DISABLED;

/**
* Chat codes, see {@link ChatCode}.
Expand Down Expand Up @@ -181,7 +180,7 @@ public static List<MessageComponent> parse(String msg) {
VariableString varParam = null;

// Attempt link parsing
if (parseLinks && c == 'h') {
if (linkParseMode == LinkParseMode.STRICT && c == 'h') {
String rest = msg.substring(i); // Get rest of string

String link = null;
Expand Down Expand Up @@ -209,6 +208,48 @@ public static List<MessageComponent> parse(String msg) {

i += link.length() - 1; // Skip link for all other parsing

// Add one MORE component (this comes after the link)
current = new MessageComponent();
components.add(current);
continue;
}
} else if (linkParseMode == LinkParseMode.LENIENT && (i == 0 || chars[i - 1] == ' ')) {
// Lenient link parsing
String rest = msg.substring(i); // Get rest of string

String link = null;
if (rest.contains(".")) {
link = rest.split(" ", 2)[0];
}

// Link found
if (link != null && !link.isEmpty()) {
// Insert protocol (aka guess it) if it isn't there
String url;
if (!link.startsWith("http://") && !link.startsWith("https://")) {
url = "http://" + link; // Hope that http -> https redirect works on target site...
} else {
url = link;
}

// Take previous component, create new
String text = curStr.toString();
curStr = new StringBuilder();
assert text != null;
current.text = text;

MessageComponent old = current;
current = new MessageComponent();
copyStyles(old, current);

components.add(current);

// Make new component a link
ChatCode.open_url.updateComponent(current, url, null); // URL for client...
current.text = link; // ... and for player

i += link.length() - 1; // Skip link for all other parsing

// Add one MORE component (this comes after the link)
current = new MessageComponent();
components.add(current);
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/ch/njol/skript/util/chat/LinkParseMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* This file is part of Skript.
*
* Skript 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.
*
* Skript 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 Skript. If not, see <http://www.gnu.org/licenses/>.
*
*
* Copyright 2011-2017 Peter Güttinger and contributors
*/
package ch.njol.skript.util.chat;

/**
* Parse mode for links in chat messages.
*/
public enum LinkParseMode {

/**
* Parses nothing automatically as a link.
*/
DISABLED,

/**
* Parses everything that starts with http(s):// as a link.
*/
STRICT,

/**
* Parses everything with "." as a link.
*/
LENIENT
}
8 changes: 5 additions & 3 deletions src/main/resources/config.sk
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ enable timings: false
# Note that this requires Paper (https://paper.readthedocs.io/en/paper-1.11/) to work; on Bukkit/Spigot this option has no effect.
# When false, timings are not enabled for scripts even if you're running Paper.

parse links in chat messages: false
# If Skript should attempt to parse anything that begins with "http://" or "https://" as links
# when parsing chat messages. Turning this on might make parsing texts slightly slower.
parse links in chat messages: disabled
# Controls how Skript will try to parse links in chat messages.
# If 'disabled' or 'false', no links will be automatically parsed
# When 'strict', everything starting with http(s):// will be parsed as link
# When 'lenient', everything that contains a dot will be interpreted as a link

# ==== Variables ====

Expand Down

0 comments on commit 119056a

Please sign in to comment.