Skip to content

Commit

Permalink
Unified and cleaned up link formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed May 30, 2024
1 parent 7f35212 commit 58df0fa
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,8 @@ public boolean onOptionsItemSelected(@NonNull final MenuItem item) {
_document.setFormat(itemId);
applyTextFormat(itemId);
_appSettings.setDocumentFormat(_document.getPath(), _document.getFormat());
return true;
}
return true;
}
case R.id.action_search: {
setViewModeVisibility(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,13 @@
import net.gsantner.opoc.util.GsFileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MarkdownActionButtons extends ActionButtonBase {

// Group 1 matches text, group 2 matches path
private static final Pattern MARKDOWN_LINK = Pattern.compile("\\[([^\\]]*)\\]\\(([^)]+)\\)");

private static final Pattern WEB_URL = Pattern.compile("https?://[^\\s/$.?#].[^\\s]*");

private final MarkorDialogFactory.HeadlineDialogState _headlineDialogState = new MarkorDialogFactory.HeadlineDialogState();
Expand Down Expand Up @@ -232,40 +228,63 @@ public boolean onActionLongClick(final @StringRes int action) {
}
}

private boolean followLinkUnderCursor() {
final int sel = TextViewUtils.getSelection(_hlEditor)[0];
if (sel < 0) {
return false;
public static class Link {
public final String title, link;
public final boolean isImage;
public final int start, end;

private Link(String title, String link, boolean isImage, int start, int end) {
this.title = title;
this.link = link;
this.isImage = isImage;
this.start = start;
this.end = end;
}

final String line = TextViewUtils.getSelectedLines(_hlEditor, sel);
final int cursor = sel - TextViewUtils.getLineStart(_hlEditor.getText(), sel);
public boolean isValid() {
return !link.isEmpty() && start >= 0 && end >= 0;
}

final Matcher m = MARKDOWN_LINK.matcher(line);
public static Link extract(final CharSequence text, final int pos) {
final int[] sel = TextViewUtils.getLineSelection(text, pos);
if (sel != null && sel[0] != -1 && sel[1] != -1) {
final String line = text.subSequence(sel[0], sel[1]).toString();
final Matcher m = MarkdownSyntaxHighlighter.LINK.matcher(line);
final int po = pos - sel[0];

final ArrayList<String> linksUnderCursor = new ArrayList<>();
while (m.find()) {
final String group = m.group(2);
if (m.start() <= cursor && m.end() >= cursor && group != null) {
linksUnderCursor.add(group);
while (m.find()) {
final int start = m.start(), end = m.end();
if (start <= po && end >= po) {
final boolean isImage = m.group(1) != null;
return new Link(m.group(2), m.group(3), isImage, start, end);
}
}
}

return new Link("", "", false, -1, -1);
}
}

private boolean followLinkUnderCursor() {
final int sel = TextViewUtils.getSelection(_hlEditor)[0];
if (sel < 0) {
return false;
}

// We want to search the line backwards in order to find the link closest to the cursor
// This helps us to match a link right after the cursor when there is one right before
for (int i = linksUnderCursor.size() - 1; i >= 0; i--) {
final String group = linksUnderCursor.get(i);
if (WEB_URL.matcher(group).matches()) {
GsContextUtils.instance.openWebpageInExternalBrowser(getActivity(), group);
final Link link = Link.extract(_hlEditor.getText(), sel);
if (link.isValid()) {
if (WEB_URL.matcher(link.link).matches()) {
GsContextUtils.instance.openWebpageInExternalBrowser(getActivity(), link.link);
return true;
} else {
final File f = GsFileUtils.makeAbsolute(group, _document.getFile().getParentFile());
final File f = GsFileUtils.makeAbsolute(link.link, _document.getFile().getParentFile());
if (GsFileUtils.canCreate(f)) {
DocumentActivity.handleFileClick(getActivity(), f, null);
return true;
}
}
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ public class MarkdownSyntaxHighlighter extends SyntaxHighlighterBase {
public final static Pattern ITALICS = Pattern.compile("(?<=(\\n|^|\\s|\\[|\\{|\\())([*_])(?=((?!\\2)|\\2{2,}))(?=\\S)(.*?)\\S\\2(?=(\\n|$|\\s|\\.|,|:|;|-|\\]|\\}|\\)))");
public final static Pattern HEADING = Pattern.compile("(?m)((^#{1,6}[^\\S\\n][^\\n]+)|((\\n|^)[^\\s]+.*?\\n(-{2,}|={2,})[^\\S\\n]*$))");
public final static Pattern HEADING_SIMPLE = Pattern.compile("(?m)^(#{1,6}\\s.*$)");
public final static Pattern LINK = Pattern.compile("\\[([^\\[]+)\\]\\(([^\\)]+)\\)");
// Group 1 matches image, Group 2 matches text, group 3 matches path
public static final Pattern LINK = Pattern.compile("(?m)(!)?\\[([^\\]]*)\\]\\((.+?)\\)(:?\\s|$)");
public final static Pattern LIST_UNORDERED = Pattern.compile("(\\n|^)\\s{0,16}([*+-])( \\[[ xX]\\])?(?= )");
public final static Pattern LIST_ORDERED = Pattern.compile("(?m)^\\s{0,16}(\\d+)(:?\\.|\\))\\s");
public final static Pattern QUOTATION = Pattern.compile("(\\n|^)>");
public final static Pattern STRIKETHROUGH = Pattern.compile("~{2}(.*?)\\S~{2}");
public final static Pattern CODE = Pattern.compile("(?m)(`(?!`)(.*?)`)|(^[^\\S\\n]{4}(?![0-9\\-*+]).*$)");
public final static Pattern DOUBLESPACE_LINE_ENDING = Pattern.compile("(?m)(?<=\\S)([^\\S\\n]{2,})\\n");
public final static Pattern ACTION_LINK_PATTERN = Pattern.compile("(?m)\\[(.*?)\\]\\((.*?)\\)");
public final static Pattern ACTION_IMAGE_PATTERN = Pattern.compile("(?m)!\\[(.*?)\\]\\((.*?)\\)");

private static final int MD_COLOR_HEADING = 0xffef6D00;
private static final int MD_COLOR_LINK = 0xff1ea3fe;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import net.gsantner.markor.ApplicationObject;
import net.gsantner.markor.R;
import net.gsantner.markor.format.FormatRegistry;
import net.gsantner.markor.format.markdown.MarkdownSyntaxHighlighter;
import net.gsantner.markor.format.markdown.MarkdownActionButtons;
import net.gsantner.markor.frontend.filebrowser.MarkorFileBrowserFactory;
import net.gsantner.markor.frontend.filesearch.FileSearchDialog;
import net.gsantner.markor.frontend.filesearch.FileSearchEngine;
Expand All @@ -40,7 +40,6 @@
import net.gsantner.opoc.wrapper.GsCallback;

import java.io.File;
import java.util.regex.Matcher;

public class AttachLinkOrFileDialog {
public final static int IMAGE_ACTION = 2, FILE_OR_LINK_ACTION = 3, AUDIO_ACTION = 4;
Expand Down Expand Up @@ -103,21 +102,14 @@ public static void showInsertImageOrLinkDialog(
} else if (edit.length() == 0) {
inputPathName.setText("");
} else {
final int[] lineSel = TextViewUtils.getLineSelection(edit, sel);
final String line = edit.subSequence(lineSel[0], lineSel[1]).toString();
final Matcher m;
if (action == IMAGE_ACTION) {
m = MarkdownSyntaxHighlighter.ACTION_IMAGE_PATTERN.matcher(line);
} else if (action == FILE_OR_LINK_ACTION) {
m = MarkdownSyntaxHighlighter.ACTION_LINK_PATTERN.matcher(line);
} else {
m = null;
}
if (m != null && m.find() && sel[0] >= m.start() && sel[1] <= m.end()) {
inputPathName.setText(m.group(1));
inputPathUrl.setText((m.group(2)));
sel[0] = m.start() + lineSel[0];
sel[1] = m.end() + lineSel[0];
final MarkdownActionButtons.Link link = MarkdownActionButtons.Link.extract(edit, sel[0]);
final boolean isImage = action == IMAGE_ACTION && link.isImage;
final boolean isLink = action == FILE_OR_LINK_ACTION && !link.isImage;
if (link.isValid() && (isImage || isLink)) {
inputPathName.setText(link.title);
inputPathUrl.setText(link.link);
sel[0] = link.start;
sel[1] = link.end;
}
}
}
Expand Down Expand Up @@ -231,8 +223,9 @@ public static String formatLink(String title, String path, final int textFormatI
}

private static String formatLink(String title, String path, final int textFormatId, final InsertType action) {
title = title.trim().replace(")", "\\)");
path = path.trim().replace(")", "\\)")
title = title.trim().replace("|", "/");

path = path.trim()
// Workaround for parser - cannot deal with spaces and have other entities problems
.replace(" ", "%20")
// Disable space encoding for Jekyll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ private void updateHighlighting(final boolean recompute) {

public void setDynamicHighlightingEnabled(final boolean enable) {
_isDynamicHighlightingEnabled = enable;
updateHighlighting(true);
}

public boolean isDynamicHighlightingEnabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ public static int[] getLineSelection(final CharSequence text, final int[] sel) {
return sel != null && sel.length >= 2 ? new int[]{getLineStart(text, sel[0]), getLineEnd(text, sel[1])} : null;
}

public static int[] getLineSelection(final CharSequence text, final int sel) {
return getLineSelection(text, new int[]{sel, sel});
}

public static int[] getLineSelection(final TextView text) {
return getLineSelection(text.getText());
}
Expand Down

0 comments on commit 58df0fa

Please sign in to comment.