Skip to content

Commit

Permalink
Improvements to templating and link handling (PR #2145 @harshad1)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 authored Nov 14, 2023
1 parent 6ffe87d commit fc7983e
Show file tree
Hide file tree
Showing 14 changed files with 231 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,26 @@ protected void onPreferenceChanged(final SharedPreferences prefs, final String k
@Override
@SuppressWarnings({"ConstantConditions", "ConstantIfStatement", "StatementWithEmptyBody"})
public Boolean onPreferenceClicked(Preference preference, String key, int keyResId) {
final FragmentManager fragManager = getActivity().getSupportFragmentManager();
switch (keyResId) {
case R.string.pref_key__snippet_directory_path: {
MarkorFileBrowserFactory.showFolderDialog(new GsFileBrowserOptions.SelectionListenerAdapter() {
@Override
public void onFsViewerSelected(String request, File file, final Integer lineNumber) {
_appSettings.setSnippetDirectory(file);
doUpdatePreferences();
}

@Override
public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
dopt.titleText = R.string.snippet_directory;
dopt.rootFolder = _appSettings.getNotebookDirectory();
}
}, fragManager, getActivity());
return true;
}

case R.string.pref_key__notebook_directory: {
FragmentManager fragManager = getActivity().getSupportFragmentManager();
MarkorFileBrowserFactory.showFolderDialog(new GsFileBrowserOptions.SelectionListenerAdapter() {
@Override
public void onFsViewerSelected(String request, File file, final Integer lineNumber) {
Expand All @@ -256,7 +272,6 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
return true;
}
case R.string.pref_key__quicknote_filepath: {
FragmentManager fragManager = getActivity().getSupportFragmentManager();
MarkorFileBrowserFactory.showFileDialog(new GsFileBrowserOptions.SelectionListenerAdapter() {
@Override
public void onFsViewerSelected(String request, File file, final Integer lineNumber) {
Expand All @@ -274,7 +289,6 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
return true;
}
case R.string.pref_key__todo_filepath: {
FragmentManager fragManager = getActivity().getSupportFragmentManager();
MarkorFileBrowserFactory.showFileDialog(new GsFileBrowserOptions.SelectionListenerAdapter() {
@Override
public void onFsViewerSelected(String request, File file, final Integer lineNumber) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,14 +599,17 @@ protected final boolean runCommonAction(final @StringRes int action) {
}
case R.string.abid_common_insert_snippet: {
MarkorDialogFactory.showInsertSnippetDialog(_activity, (snip) -> {
_hlEditor.insertOrReplaceTextOnCursor(TextViewUtils.interpolateEscapedDateTime(snip));
_hlEditor.insertOrReplaceTextOnCursor(TextViewUtils.interpolateSnippet(snip, _document.getTitle(), TextViewUtils.getSelectedText(_hlEditor)));
_lastSnip = snip;
});
return true;
}
case R.string.abid_common_open_link_browser: {
String url;
if ((url = GsTextUtils.tryExtractUrlAroundPos(text.toString(), _hlEditor.getSelectionStart())) != null) {
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);
if (url != null) {
if (url.endsWith(")")) {
url = url.substring(0, url.length() - 1);
}
Expand Down Expand Up @@ -689,7 +692,7 @@ protected final boolean runCommonLongPressAction(@StringRes int action) {
}
case R.string.abid_common_insert_snippet: {
if (!TextUtils.isEmpty(_lastSnip)) {
_hlEditor.insertOrReplaceTextOnCursor(TextViewUtils.interpolateEscapedDateTime(_lastSnip));
_hlEditor.insertOrReplaceTextOnCursor(TextViewUtils.interpolateSnippet(_lastSnip, _document.getTitle(), TextViewUtils.getSelectedText(_hlEditor)));
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,31 @@
import androidx.annotation.StringRes;

import net.gsantner.markor.R;
import net.gsantner.markor.activity.DocumentActivity;
import net.gsantner.markor.format.ActionButtonBase;
import net.gsantner.markor.frontend.MarkorDialogFactory;
import net.gsantner.markor.frontend.textview.AutoTextFormatter;
import net.gsantner.markor.frontend.textview.TextViewUtils;
import net.gsantner.markor.model.Document;
import net.gsantner.opoc.util.GsContextUtils;
import net.gsantner.opoc.util.GsFileUtils;

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

public class MarkdownActionButtons extends ActionButtonBase {

private Set<Integer> _disabledHeadings = new HashSet<>();
// 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 Set<Integer> _disabledHeadings = new HashSet<>();

public MarkdownActionButtons(@NonNull Context context, Document document) {
super(context, document);
Expand Down Expand Up @@ -139,6 +149,11 @@ public boolean onActionClick(final @StringRes int action) {
MarkorDialogFactory.showInsertTableRowDialog(getActivity(), false, this::insertTableRow);
return true;
}
case R.string.abid_common_open_link_browser: {
if (followLinkUnderCursor()) {
return true;
}
}
default: {
return runCommonAction(action);
}
Expand Down Expand Up @@ -168,6 +183,34 @@ public boolean onActionLongClick(final @StringRes int action) {
}
}

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

final String line = TextViewUtils.getSelectedLines(_hlEditor, sel);
final int cursor = sel - TextViewUtils.getLineStart(_hlEditor.getText(), sel);

final Matcher m = MARKDOWN_LINK.matcher(line);
while (m.find()) {
final String group = m.group(2);
if (m.start() <= cursor && m.end() > cursor && group != null) {
if (WEB_URL.matcher(group).matches()) {
GsContextUtils.instance.openWebpageInExternalBrowser(getActivity(), group);
return true;
} else {
final File f = GsFileUtils.makeAbsolute(group, _document.getFile().getParentFile());
if (GsFileUtils.canCreate(f)) {
DocumentActivity.handleFileClick(getActivity(), f, null);
return true;
}
}
}
}
return false;
}

private void insertTableRow(int cols, boolean isHeaderEnabled) {
StringBuilder sb = new StringBuilder();
_hlEditor.requestFocus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ public static void showInsertSnippetDialog(final Activity activity, final GsCall
dopt.data = data;
dopt.isSearchEnabled = true;
dopt.titleText = R.string.insert_snippet;
dopt.messageText = Html.fromHtml("<small><small>" + as().getSnippetsFolder().getAbsolutePath() + "</small></small>");
dopt.messageText = Html.fromHtml("<small><small>" + as().getSnippetsDirectory().getAbsolutePath() + "</small></small>");
dopt.positionCallback = (ind) -> callback.callback(GsFileUtils.readTextFileFast(texts.get(data.get(ind.get(0)))).first);
GsSearchOrCustomTextDialog.showMultiChoiceDialogWithSearchFilterUI(activity, dopt);
}
Expand Down
Loading

0 comments on commit fc7983e

Please sign in to comment.