mirror of
https://github.com/MarginaliaSearch/MarginaliaSearch.git
synced 2025-02-23 13:09:00 +00:00
(search) Fix broken !bang handling
!bang query handling seems to have fallen victim to an overzealous refactoring effort, and broken. It's now repaired, and a test is in place to ensure we know if it breaks again.
This commit is contained in:
parent
95d1bd98e4
commit
f2b39ad055
@ -21,6 +21,7 @@ public class BangCommand implements SearchCommandInterface {
|
||||
{
|
||||
bangsToPattern.put("!g", "https://www.google.com/search?q=%s");
|
||||
bangsToPattern.put("!ddg", "https://duckduckgo.com/search?q=%s");
|
||||
bangsToPattern.put("!w", "https://search.marginalia.nu/search?query=%s%20site:en.wikipedia.org&profile=wiki");
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -41,18 +42,25 @@ public class BangCommand implements SearchCommandInterface {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private Optional<String> matchBangPattern(String query, String bangKey) {
|
||||
/** If the query contains the bang pattern bangKey, return the query with the bang pattern removed. */
|
||||
Optional<String> matchBangPattern(String query, String bangKey) {
|
||||
var bm = new BangMatcher(query);
|
||||
|
||||
while (bm.findNext(bangKey)) {
|
||||
|
||||
if (bm.isRelativeSpaceOrInvalid(-1))
|
||||
if (!bm.isRelativeSpaceOrInvalid(-1))
|
||||
continue;
|
||||
if (bm.isRelativeSpaceOrInvalid(bangKey.length()))
|
||||
if (!bm.isRelativeSpaceOrInvalid(bangKey.length()))
|
||||
continue;
|
||||
|
||||
String queryWithoutBang = bm.prefix().trim() + " " + bm.suffix(bangKey.length()).trim();
|
||||
return Optional.of(queryWithoutBang);
|
||||
StringBuilder ret = new StringBuilder(bm.prefix().trim());
|
||||
|
||||
if (!ret.isEmpty())
|
||||
ret.append(" ");
|
||||
|
||||
ret.append(bm.suffix(bangKey.length()).trim());
|
||||
|
||||
return Optional.of(ret.toString());
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
|
@ -0,0 +1,37 @@
|
||||
package nu.marginalia.search.command.commands;
|
||||
|
||||
import nu.marginalia.client.Context;
|
||||
import nu.marginalia.search.command.SearchParameters;
|
||||
import nu.marginalia.search.exceptions.RedirectException;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class BangCommandTest {
|
||||
public BangCommand bangCommand = new BangCommand();
|
||||
|
||||
@Test
|
||||
public void testG() {
|
||||
try {
|
||||
bangCommand.process(Context.internal(),
|
||||
null,
|
||||
new SearchParameters(" !g test",
|
||||
null, null, null, null)
|
||||
);
|
||||
Assertions.fail("Should have thrown RedirectException");
|
||||
}
|
||||
catch (RedirectException ex) {
|
||||
assertEquals("https://www.google.com/search?q=test", ex.newUrl);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMatchPattern() {
|
||||
var match = bangCommand.matchBangPattern("!g test", "!g");
|
||||
|
||||
assertTrue(match.isPresent());
|
||||
assertEquals(match.get(), "test");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user