Skip to content

Commit

Permalink
Added logic to open any 'src' in a tag
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Apr 21, 2024
1 parent 2f66607 commit 1f19f0d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#########################################################*/
package net.gsantner.markor.format;

import static android.util.Patterns.WEB_URL;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
Expand Down Expand Up @@ -40,6 +42,7 @@

import net.gsantner.markor.ApplicationObject;
import net.gsantner.markor.R;
import net.gsantner.markor.activity.DocumentActivity;
import net.gsantner.markor.frontend.AttachLinkOrFileDialog;
import net.gsantner.markor.frontend.DatetimeFormatDialog;
import net.gsantner.markor.frontend.MarkorDialogFactory;
Expand All @@ -54,6 +57,7 @@
import net.gsantner.opoc.util.GsFileUtils;
import net.gsantner.opoc.wrapper.GsCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -696,7 +700,25 @@ protected final boolean runCommonAction(final @StringRes int action) {
final int sel = TextViewUtils.getSelection(_hlEditor)[0];
final String line = TextViewUtils.getSelectedLines(_hlEditor, sel);
final int cursor = sel - TextViewUtils.getLineStart(_hlEditor.getText(), sel);
String url = GsTextUtils.tryExtractUrlAroundPos(line, cursor);

// First try to pull a resource
String url = null;
final String resource = GsTextUtils.tryExtractResourceAroundPos(line, cursor);
if (resource != null) {
if (WEB_URL.matcher(resource).matches()) {
url = resource;
} else {
final File f = GsFileUtils.makeAbsolute(resource, _document.getFile().getParentFile());
if (f.canRead()) {
DocumentActivity.handleFileClick(getActivity(), f, null);
return true;
}
}

}

// Then try to pull a tag
url = url == null ? GsTextUtils.tryExtractUrlAroundPos(line, cursor) : url;
if (url != null) {
if (url.endsWith(")")) {
url = url.substring(0, url.length() - 1);
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/net/gsantner/opoc/format/GsTextUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings({"unused", "SpellCheckingInspection"})
public class GsTextUtils {
public static String UTF8 = "UTF-8";

// Regex patterns used for finding resources in tags
public final static Pattern SELF_CLOSING_TAG = Pattern.compile("<(\\w+)([^>]*?)src='([^']+)'([^>]*?)/>");
public final static Pattern REGULAR_TAG = Pattern.compile("<(\\w+)([^>]*?)src='([^']+)'([^>]*?)>(.*?)</\\1>");


/**
* This is a simple method that tries to extract an URL around a given index.
* It doesn't do any validation. Separation by whitespace or end. Detects http and https.
Expand All @@ -55,6 +62,32 @@ public static String tryExtractUrlAroundPos(final String text, int pos) {
return null;
}


/**
* This is a simple method that tries to extract the value of the 'src' attribute
* for any tag in the text which surrounds pos.
* It doesn't do any validation. Separation by whitespace or end.
*
* @param text Text to extract from
* @param pos Position to start searching from (backwards)
* @return Extracted resource path or {@code null} if none found
*/
public static String tryExtractResourceAroundPos(final String text, int pos) {

for (final Pattern pattern : Arrays.asList(SELF_CLOSING_TAG, REGULAR_TAG)) {
final Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
int start = matcher.start();
int end = matcher.end();
if (pos >= start && pos <= end) {
return matcher.group(3);
}
}
}

return null; // Return null if no enclosing tag with src attribute is found
}

/**
* find '\n' to the right and left of text[pos] .. text[posEnd].
* If left does not exist 0 (begin of text) is used.
Expand Down

0 comments on commit 1f19f0d

Please sign in to comment.