-
Notifications
You must be signed in to change notification settings - Fork 208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix default bookmarks activelinks #873
base: next
Are you sure you want to change the base?
Changes from all commits
4370631
ab5074b
24032c6
96408c6
5d68076
2d9d9e9
285262d
3eb09ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
import java.net.URI; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import freenet.client.ClientMetadata; | ||
import freenet.client.HighLevelSimpleClient; | ||
|
@@ -40,6 +42,12 @@ public class WelcomeToadlet extends Toadlet { | |
/** Suffix {@link #path()} with "#" + BOOKMARKS_ANCHOR to deep link to the bookmark list */ | ||
public static final String BOOKMARKS_ANCHOR = "bookmarks"; | ||
|
||
// Regex Patterns to use when generating keys for activelinks. | ||
// For USK and SSK we want to match from the start of the key to the first instance of -#/ or /#/. | ||
protected static final Pattern PATTERN_USK_SSK = Pattern.compile("^[^/]*/[^/]+/?[/-]?[0-9]+/"); | ||
// For bare SSK, CHK, and KSK we match from the start of the key to the first slash and append the activelink there. | ||
protected static final Pattern PATTERN_BARE_SSK_CHK_KSK = Pattern.compile("^[^/]*/"); | ||
|
||
final Node node; | ||
|
||
private static volatile boolean logMINOR; | ||
|
@@ -80,7 +88,42 @@ private void addCategoryToList(BookmarkCategory cat, HTMLNode list, boolean noAc | |
HTMLNode cell = row.addChild("td", "style", "border: none;"); | ||
if (item.hasAnActivelink() && !noActiveLinks) { | ||
String initialKey = item.getKey(); | ||
String key = '/' + initialKey + (initialKey.endsWith("/") ? "" : "/") + "activelink.png"; | ||
// extract the key type | ||
String keyType = initialKey.substring(1,3); | ||
String key = '/' + initialKey + (initialKey.endsWith("/") ? "" : "/"); | ||
Matcher match = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seeing that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but the switch branches share the scope, so that’s how it has to be to avoid inventing new names for the different branches. |
||
switch (keyType) { | ||
case "USK": | ||
case "SSK": | ||
match = PATTERN_USK_SSK.matcher(key); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this only matches when the key is already well-formed ⇒ ideally extract to a static method (uses no |
||
if (match.matches()) { | ||
Bombe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
key = match.group(1) + "activelink.png"; | ||
} | ||
else { // If no edition # exists, assume bare SSK and just append the activelink. | ||
match = PATTERN_BARE_SSK_CHK_KSK.matcher(key); | ||
if (match.matches()) { | ||
key = match.group(1) + "activelink.png"; | ||
} | ||
else { | ||
Logger.minor(this, "Impossible: Regex for USK/SSK didn't match. Key: "+ key); | ||
} | ||
} | ||
break; | ||
case "CHK": | ||
case "KSK": // This assumes the activelink is in the root of any one-shot directory upload. | ||
match = PATTERN_BARE_SSK_CHK_KSK.matcher(key); | ||
if (match.matches()) { | ||
key = match.group(1) + "activelink.png"; | ||
} | ||
else { // we append '/' to key at the start if it isn't already there, so this should never be reachable unless the regex is broken. | ||
Logger.minor(this, "Impossible: Regex for CHK/KSK didn't match. Key: "+ key); | ||
} | ||
break; | ||
default: | ||
Logger.minor(this, "Unknown key type: " + keyType + "! Activelink regex needs updating."); | ||
break; | ||
} | ||
|
||
cell.addChild("div", "style", "height: 36px; width: 108px;").addChild("a", "href", '/' + item.getKey()).addChild("img", new String[]{"src", "alt", "style", "title"}, | ||
new String[]{ key, "activelink", "height: 36px; width: 108px", item.getDescription()}); | ||
} else { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this has to go from 0 to 3 — Java is zero-indexed.