Skip to content

Commit

Permalink
Merge branch 'master' into libraries_update
Browse files Browse the repository at this point in the history
  • Loading branch information
gsantner authored Oct 26, 2024
2 parents 97f0336 + 46d0267 commit 6e6503a
Show file tree
Hide file tree
Showing 57 changed files with 361 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.gsantner.markor.format.plaintext.PlaintextActionButtons;
import net.gsantner.markor.format.todotxt.TodoTxtActionButtons;
import net.gsantner.markor.format.wikitext.WikitextActionButtons;
import net.gsantner.markor.format.orgmode.OrgmodeActionButtons;
import net.gsantner.opoc.util.GsCollectionUtils;

import java.util.ArrayList;
Expand Down Expand Up @@ -133,6 +134,8 @@ private void extractActionData() {
_textActions = new WikitextActionButtons(this, null);
} else if (documentType == R.string.pref_key__asciidoc__reorder_actions) {
_textActions = new AsciidocActionButtons(this, null);
} else if (documentType == R.string.pref_key__orgmode__reorder_actions) {
_textActions = new OrgmodeActionButtons(this, null);
} else { // Default to Plaintext
_textActions = new PlaintextActionButtons(this, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,26 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
}, 250);
return true;
}
case R.id.action_toggle_case:
if (_hlEditor != null) {
_hlEditor.toggleCase();
}
return true;
case R.id.action_switch_case:
if (_hlEditor != null) {
_hlEditor.switchCase();
}
return true;
case R.id.action_capitalize_words:
if (_hlEditor != null) {
_hlEditor.capitalizeWords();
}
return true;
case R.id.action_capitalize_sentences:
if (_hlEditor != null) {
_hlEditor.capitalizeSentences();
}
return true;
default: {
return super.onOptionsItemSelected(item);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ public List<ActionItem> getFormatActionList() {
new ActionItem(R.string.abid_common_deindent, R.drawable.ic_format_indent_decrease_black_24dp, R.string.deindent),
new ActionItem(R.string.abid_common_insert_link, R.drawable.ic_link_black_24dp, R.string.insert_link),
new ActionItem(R.string.abid_common_insert_image, R.drawable.ic_image_black_24dp, R.string.insert_image),
new ActionItem(R.string.abid_common_insert_audio, R.drawable.ic_keyboard_voice_black_24dp, R.string.audio)
new ActionItem(R.string.abid_common_insert_audio, R.drawable.ic_keyboard_voice_black_24dp, R.string.audio),
new ActionItem(R.string.abid_orgmode_bold, R.drawable.ic_format_bold_black_24dp, R.string.bold),
new ActionItem(R.string.abid_orgmode_italic, R.drawable.ic_format_italic_black_24dp, R.string.italic),
new ActionItem(R.string.abid_orgmode_strikeout, R.drawable.ic_format_strikethrough_black_24dp, R.string.strikeout),
new ActionItem(R.string.abid_orgmode_underline, R.drawable.ic_format_underlined_black_24dp, R.string.underline),
new ActionItem(R.string.abid_orgmode_code_inline, R.drawable.ic_code_black_24dp, R.string.inline_code)
);
}

Expand All @@ -45,4 +50,33 @@ protected void renumberOrderedList() {
// Use markdown format for orgmode too
AutoTextFormatter.renumberOrderedList(_hlEditor.getText(), MarkdownReplacePatternGenerator.formatPatterns);
}

@Override
public boolean onActionClick(final @StringRes int action) {
switch (action) {
case R.string.abid_orgmode_bold: {
runSurroundAction("*");
return true;
}
case R.string.abid_orgmode_italic: {
runSurroundAction("/");
return true;
}
case R.string.abid_orgmode_strikeout: {
runSurroundAction("+");
return true;
}
case R.string.abid_orgmode_underline: {
runSurroundAction("_");
return true;
}
case R.string.abid_orgmode_code_inline: {
runSurroundAction("=");
return true;
}
default: {
return runCommonAction(action);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package net.gsantner.markor.format.orgmode;

import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;

import net.gsantner.markor.frontend.textview.SyntaxHighlighterBase;
import net.gsantner.markor.model.AppSettings;

import java.util.regex.Pattern;

public class OrgmodeSyntaxHighlighter extends SyntaxHighlighterBase {

public final static String COMMON_EMPHASIS_PATTERN = "(?<=(\\n|^|\\s|\\{|\\())([%s])(?=\\S)(.*?)\\S\\2(?=(\\n|$|\\s|\\.|,|:|;|-|\\}|\\)))";
public final static Pattern BOLD = Pattern.compile(String.format(COMMON_EMPHASIS_PATTERN, "*"));
public final static Pattern ITALICS = Pattern.compile(String.format(COMMON_EMPHASIS_PATTERN, "/"));
public final static Pattern STRIKETHROUGH = Pattern.compile(String.format(COMMON_EMPHASIS_PATTERN, "+"));
public final static Pattern UNDERLINE = Pattern.compile(String.format(COMMON_EMPHASIS_PATTERN, "_"));
public final static Pattern CODE_INLINE = Pattern.compile(String.format(COMMON_EMPHASIS_PATTERN, "=~"));
public final static Pattern HEADING = Pattern.compile("(?m)^(\\*+)\\s(.*?)(?=\\n|$)");
public final static Pattern BLOCK = Pattern.compile("(?m)(?<=#\\+BEGIN_.{1,15}$\\s)[\\s\\S]*?(?=#\\+END)");
public final static Pattern PREAMBLE = Pattern.compile("(?m)^(#\\+)(.*?)(?=\\n|$)");
Expand Down Expand Up @@ -44,6 +51,12 @@ protected void generateSpans() {
createColorSpanForMatches(PREAMBLE, ORG_COLOR_DIM);
createColorSpanForMatches(COMMENT, ORG_COLOR_DIM);
createColorBackgroundSpan(BLOCK, ORG_COLOR_BLOCK);

createStyleSpanForMatches(BOLD, Typeface.BOLD);
createStyleSpanForMatches(ITALICS, Typeface.ITALIC);
createStrikeThroughSpanForMatches(STRIKETHROUGH);
createColoredUnderlineSpanForMatches(UNDERLINE, Color.BLACK);
createMonospaceSpanForMatches(CODE_INLINE);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class PlaintextTextConverter extends TextConverterBase {
private static final String HTML100_BODY_PRE_BEGIN = "<pre style='white-space: pre-wrap;font-family: " + TOKEN_FONT + "' >";
private static final String HTML101_BODY_PRE_END = "</pre>";
private static final List<String> EXT_TEXT = Arrays.asList(".txt", ".taskpaper", ".org", ".ldg", ".ledger", ".m3u", ".m3u8");
private static final List<String> EXT_TEXT = Arrays.asList(".txt", ".taskpaper", ".org", ".ldg", ".ledger", ".m3u", ".m3u8", ".lrc", ".fen");
private static final List<String> EXT_HTML = Arrays.asList(".html", ".htm");
private static final List<String> EXT_CODE_HL = Arrays.asList(".py", ".cpp", ".h", ".c", ".js", ".mjs", ".css", ".cs", ".kt", ".lua", ".perl", ".java", ".qml", ".diff", ".php", ".r", ".patch", ".rs", ".swift", ".ts", ".mm", ".go", ".sh", ".rb", ".tex", ".xml", ".xlf");
private static final List<String> EXT = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public static GsFileBrowserOptions.Options prepareFsViewerOpts(
opts.favouriteFiles = appSettings.getFavouriteFiles();
opts.recentFiles = appSettings.getRecentFiles();
opts.popularFiles = appSettings.getPopularFiles();
opts.storageMaps.clear();
opts.storageMaps.put(new File("/storage", cu.rstr(context, R.string.notebook)), appSettings.getNotebookDirectory());
opts.storageMaps.put(new File("/storage/Download"), new File("/storage/emulated/0/Download"));
};
opts.refresh.callback();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import net.gsantner.opoc.format.GsTextUtils;
import net.gsantner.opoc.wrapper.GsCallback;
import net.gsantner.opoc.wrapper.GsTextWatcherAdapter;
import net.gsantner.markor.util.TextCasingUtils;

import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
Expand Down Expand Up @@ -297,6 +299,60 @@ private int rowEnd(final int y) {
return layout.getLineEnd(line);
}

// Text-Casing
// ---------------------------------------------------------------------------------------------
public void toggleCase() {
String text = getSelectedText();
if (text.isEmpty()) {
text = Objects.requireNonNull(getText()).toString();
}
String newText = TextCasingUtils.toggleCase(text);
replaceSelection(newText);
}

public void switchCase() {
String text = getSelectedText();
if (text.isEmpty()) {
text = Objects.requireNonNull(getText()).toString();
}
String newText = TextCasingUtils.switchCase(text);
replaceSelection(newText);
}

public void capitalizeWords() {
String text = getSelectedText();
if (text.isEmpty()) {
text = Objects.requireNonNull(getText()).toString();
}
String newText = TextCasingUtils.capitalizeWords(text);
replaceSelection(newText);
}

public void capitalizeSentences() {
String text = getSelectedText();
if (text.isEmpty()) {
text = Objects.requireNonNull(getText()).toString();
}
String newText = TextCasingUtils.capitalizeSentences(text);
replaceSelection(newText);
}

private String getSelectedText() {
int start = Math.max(0, getSelectionStart());
int end = Math.max(0, getSelectionEnd());
return Objects.requireNonNull(getText()).toString().substring(start, end);
}

private void replaceSelection(String replacement) {
int start = Math.max(0, getSelectionStart());
int end = Math.max(0, getSelectionEnd());
if (start == end) { // If no selection is made, replace all the text in the document
setText(replacement);
} else { // Replace only the selected text
Objects.requireNonNull(getText()).replace(start, end, replacement);
}
}

// Various overrides
// ---------------------------------------------------------------------------------------------
public void setSaveInstanceState(final boolean save) {
Expand Down
42 changes: 15 additions & 27 deletions app/src/main/java/net/gsantner/markor/model/AppSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -746,59 +746,47 @@ public String getNavigationBarColor() {
return getString(R.string.pref_key__navigationbar_color, "#000000");
}

public @IdRes
Integer getAppStartupFolderMenuId() {
switch (getString(R.string.pref_key__app_start_folder, "notebook")) {
case "favourites":
return R.id.action_go_to_favourite_files;
case "internal_storage":
return R.id.action_go_to_storage;
case "appdata_public":
return R.id.action_go_to_appdata_public;
case "appdata_private":
return R.id.action_go_to_appdata_private;
case "popular_documents":
return R.id.action_go_to_popular_files;
case "recently_viewed_documents":
return R.id.action_go_to_recent_files;
}
return R.id.action_go_to_home;
public String getAppStartupFolderMenuId() {
return getString(R.string.pref_key__app_start_folder, "notebook");
}

public File getFolderToLoadByMenuId(int itemId) {
public File getFolderToLoadByMenuId(String itemId) {
List<Pair<File, String>> appDataPublicDirs = _cu.getAppDataPublicDirs(_context, false, true, false);
switch (itemId) {
case R.id.action_go_to_home: {
case "storage": {
return new File("/storage");
}
case "notebook": {
return getNotebookDirectory();
}
case R.id.action_go_to_popular_files: {
case "popular_documents": {
return GsFileBrowserListAdapter.VIRTUAL_STORAGE_POPULAR;
}
case R.id.action_go_to_recent_files: {
case "recently_viewed_documents": {
return GsFileBrowserListAdapter.VIRTUAL_STORAGE_RECENTS;
}
case R.id.action_go_to_favourite_files: {
case "favourites": {
return GsFileBrowserListAdapter.VIRTUAL_STORAGE_FAVOURITE;
}
case R.id.action_go_to_appdata_private: {
case "appdata_private": {
return _cu.getAppDataPrivateDir(_context);
}
case R.id.action_go_to_storage: {
case "internal_storage": {
return Environment.getExternalStorageDirectory();
}
case R.id.action_go_to_appdata_sdcard_1: {
case "appdata_sdcard_1": {
if (appDataPublicDirs.size() > 0) {
return appDataPublicDirs.get(0).first;
}
return Environment.getExternalStorageDirectory();
}
case R.id.action_go_to_appdata_sdcard_2: {
case "appdata_sdcard_2": {
if (appDataPublicDirs.size() > 1) {
return appDataPublicDirs.get(1).first;
}
return Environment.getExternalStorageDirectory();
}
case R.id.action_go_to_appdata_public: {
case "appdata_public": {
appDataPublicDirs = _cu.getAppDataPublicDirs(_context, true, false, false);
if (appDataPublicDirs.size() > 0) {
return appDataPublicDirs.get(0).first;
Expand Down
61 changes: 61 additions & 0 deletions app/src/main/java/net/gsantner/markor/util/TextCasingUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package net.gsantner.markor.util;

public class TextCasingUtils {
public static String toggleCase(String text) { // Toggle all text to Uppercase or Lowercase
if (text.equals(text.toUpperCase())) {
return text.toLowerCase();
} else {
return text.toUpperCase();
}
}

public static String switchCase(String text) { // Switch the text case of each character
StringBuilder result = new StringBuilder();
for (char c : text.toCharArray()) {
if (Character.isUpperCase(c)) {
result.append(Character.toLowerCase(c));
} else if (Character.isLowerCase(c)) {
result.append(Character.toUpperCase(c));
} else {
result.append(c);
}
}
return result.toString();
}

public static String capitalizeWords(String text) { // Capitalize the first letter of each word
StringBuilder result = new StringBuilder();
boolean capitalizeNext = true;
for (char c : text.toCharArray()) {
if (Character.isWhitespace(c)) {
capitalizeNext = true;
result.append(c);
} else if (capitalizeNext) {
result.append(Character.toUpperCase(c));
capitalizeNext = false;
} else {
result.append(Character.toLowerCase(c));
}
}
return result.toString();
}

public static String capitalizeSentences(String text) { // Capitalize the first letter of each sentence
StringBuilder result = new StringBuilder();
boolean capitalizeNext = true;
for (char c : text.toCharArray()) {
if (c == '.' || c == '!' || c == '?') {
capitalizeNext = true;
result.append(c);
} else if (Character.isWhitespace(c)) {
result.append(c);
} else if (capitalizeNext) {
result.append(Character.toUpperCase(c));
capitalizeNext = false;
} else {
result.append(c);
}
}
return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
}

List<Pair<File, String>> sdcardFolders = _cu.getAppDataPublicDirs(getContext(), false, true, true);
int[] sdcardResIds = {R.id.action_go_to_appdata_sdcard_1, R.id.action_go_to_appdata_sdcard_2};
int[] sdcardResIds = {};
for (int i = 0; i < sdcardResIds.length && i < sdcardFolders.size(); i++) {
item = menu.findItem(sdcardResIds[i]);
item.setTitle(item.getTitle().toString().replaceFirst("[)]\\s*$", " " + sdcardFolders.get(i).second) + ")");
Expand Down Expand Up @@ -432,16 +432,8 @@ public boolean onOptionsItemSelected(final MenuItem item) {
reloadCurrentFolder();
return true;
}
case R.id.action_go_to_home:
case R.id.action_go_to_popular_files:
case R.id.action_go_to_recent_files:
case R.id.action_go_to_favourite_files:
case R.id.action_go_to_appdata_private:
case R.id.action_go_to_storage:
case R.id.action_go_to_appdata_sdcard_1:
case R.id.action_go_to_appdata_sdcard_2:
case R.id.action_go_to_appdata_public: {
final File folder = _appSettings.getFolderToLoadByMenuId(_id);
case R.id.action_go_to: {
final File folder = new File("/storage");
_filesystemViewerAdapter.setCurrentFolder(folder);
Toast.makeText(getContext(), folder.getAbsolutePath(), Toast.LENGTH_SHORT).show();
return true;
Expand Down
Loading

0 comments on commit 6e6503a

Please sign in to comment.