Skip to content

Commit

Permalink
Fixed + improved isFileOutOfThisFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Jun 4, 2024
1 parent e947739 commit ddfdbb2
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,11 @@ public Format(@StringRes final int a_format, @StringRes final int a_name, final
public static boolean isFileSupported(final File file, final boolean... textOnly) {
final boolean textonly = textOnly != null && textOnly.length > 0 && textOnly[0];
if (file != null) {
final String filepath = file.getAbsolutePath().toLowerCase(Locale.ROOT);
for (final Format format : FORMATS) {
if (textonly && format.converter instanceof EmbedBinaryTextConverter) {
continue;
}
if (format.converter != null && format.converter.isFileOutOfThisFormat(filepath)) {
if (format.converter != null && format.converter.isFileOutOfThisFormat(file)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.text.format.DateFormat;
import android.webkit.WebView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import net.gsantner.markor.ApplicationObject;
Expand Down Expand Up @@ -199,11 +200,11 @@ protected String getContentType() {
return CONTENT_TYPE_HTML;
}

public boolean isFileOutOfThisFormat(String filepath) {
String extWithDot = filepath.replace(JavaPasswordbasedCryption.DEFAULT_ENCRYPTION_EXTENSION, "").replaceAll(".*\\.", ".").toLowerCase();
extWithDot = TextUtils.isEmpty(extWithDot) || extWithDot.startsWith(".") ? extWithDot : "";
return isFileOutOfThisFormat(filepath, extWithDot);
public boolean isFileOutOfThisFormat(final @NonNull File file) {
final String name = file.getName().toLowerCase().replace(JavaPasswordbasedCryption.DEFAULT_ENCRYPTION_EXTENSION, "");
final String ext = name.replaceAll(".*\\.", ".");
return isFileOutOfThisFormat(file, name, ext);
}

protected abstract boolean isFileOutOfThisFormat(String filepath, String extWithDot);
protected abstract boolean isFileOutOfThisFormat(final File file, final String name, final String ext);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public String convertMarkup(String markup, Context context, boolean lightMode, b
}

@Override
protected boolean isFileOutOfThisFormat(String filepath, String extWithDot) {
return EXT.contains(extWithDot);
protected boolean isFileOutOfThisFormat(final File file, final String name, final String ext) {
return EXT.contains(ext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ protected String getContentType() {
}

@Override
protected boolean isFileOutOfThisFormat(String filepath, String extWithDot) {
return EXT.contains(extWithDot);
protected boolean isFileOutOfThisFormat(final File file, final String name, final String ext) {
return EXT.contains(ext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.List;

/**
Expand All @@ -41,16 +42,18 @@
*/
@SuppressWarnings("WeakerAccess")
public class CsvTextConverter extends MarkdownTextConverter {

final List<String> EXT = Arrays.asList(".csv", ".tsv", ".tab", ".psv");

@Override
public String convertMarkup(String csvMarkup, Context context, boolean lightMode, boolean lineNum, File file) {
String mdMarkup = Csv2MdTable.toMdTable(csvMarkup);
return super.convertMarkup(mdMarkup, context, lightMode, lineNum, file);
}

@Override
protected boolean isFileOutOfThisFormat(String filepath, String extWithDot) {
final List<String> assignedExtensions = GsCollectionUtils.asList(".csv", ".tsv", ".tab", ".psv");
return assignedExtensions.contains(extWithDot);
protected boolean isFileOutOfThisFormat(final File file, final String name, final String ext) {
return EXT.contains(ext);
}

protected static class Csv2MdTable implements Closeable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import net.gsantner.markor.format.plaintext.PlaintextTextConverter;

import java.io.File;
import java.util.Arrays;
import java.util.List;

Expand All @@ -17,7 +18,7 @@ public class KeyValueTextConverter extends PlaintextTextConverter {
private static final List<String> EXT = Arrays.asList(".yml", ".yaml", ".toml", ".vcf", ".ics", ".ini", ".json", ".zim");

@Override
public boolean isFileOutOfThisFormat(String filepath, String extWithDot) {
return EXT.contains(extWithDot);
protected boolean isFileOutOfThisFormat(final File file, final String name, final String ext) {
return EXT.contains(ext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,8 @@ private String getViewHlPrismIncludes(final String themeName, final boolean enab
}

@Override
protected boolean isFileOutOfThisFormat(String filepath, String extWithDot) {
filepath = filepath.replace(JavaPasswordbasedCryption.DEFAULT_ENCRYPTION_EXTENSION, "");
return (MarkdownTextConverter.PATTERN_HAS_FILE_EXTENSION_FOR_THIS_FORMAT.matcher(filepath).matches() && !filepath.toLowerCase().endsWith(".txt")) || filepath.toLowerCase().endsWith(".md.txt");
protected boolean isFileOutOfThisFormat(final File file, final String name, final String ext) {
return (MarkdownTextConverter.PATTERN_HAS_FILE_EXTENSION_FOR_THIS_FORMAT.matcher(name).matches() && !name.endsWith(".txt")) || name.endsWith(".md.txt");
}

private Map<String, List<String>> extractYamlAttributes(final String markup) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,19 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@SuppressWarnings("WeakerAccess")
public class OrgmodeTextConverter extends TextConverterBase {
private static final List<String> EXT_ORG = Arrays.asList(".org");
private static final List<String> EXT = new ArrayList<>();
private static final List<String> EXT = Collections.singletonList(".org");

/**
* this file is exported by browserify from <a href="https://github.com/mooz/org-js">org-js</a>
*/
public static final String HTML_ORG_JS_INCLUDE = "<script src='file:///android_asset/orgmode/org-bundle.js'></script>\n";
public static final String HTML_ORG_CSS_INCLUDE = "<link href='file:///android_asset/orgmode/org.css' type='text/css' rel='stylesheet'/>\n";


static {
EXT.addAll(EXT_ORG);
}

//########################
//## Methods
//########################
Expand Down Expand Up @@ -61,8 +56,9 @@ protected String getContentType() {
return CONTENT_TYPE_HTML;
}


@Override
protected boolean isFileOutOfThisFormat(String filepath, String extWithDot) {
return EXT.contains(extWithDot);
protected boolean isFileOutOfThisFormat(final File file, final String name, final String ext) {
return EXT.contains(ext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected String getContentType() {
}

@Override
protected boolean isFileOutOfThisFormat(String filepath, String extWithDot) {
return EXT.contains(extWithDot) || _appSettings.isExtOpenWithThisApp(extWithDot) || GsFileUtils.isTextFile(new File(filepath));
protected boolean isFileOutOfThisFormat(final File file, final String name, final String ext) {
return EXT.contains(ext) || _appSettings.isExtOpenWithThisApp(ext) || GsFileUtils.isTextFile(file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class TodoTxtTask {
// Static memebers
//

public static final Pattern TODOTXT_FILE_PATTERN = Pattern.compile("(?i)(^todo[-.]?.*)|(.*[-.]todo\\.((txt)|(text))$)");
public static final SimpleDateFormat DATEF_YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT);
public static final int DATEF_YYYY_MM_DD_LEN = "yyyy-MM-dd".length();
public static final String PT_DATE = "\\d{4}-\\d{2}-\\d{2}";
Expand Down Expand Up @@ -65,11 +64,6 @@ public static String getToday() {
return DATEF_YYYY_MM_DD.format(new Date());
}

public static boolean isTodoFile(String filepath) {
return filepath != null && (filepath.endsWith("/todo.txt")
|| TODOTXT_FILE_PATTERN.matcher(filepath).matches() && (filepath.endsWith(".txt") || filepath.endsWith(".text")));
}

public static List<TodoTxtTask> getTasks(final CharSequence text, final int selStart, final int selEnd) {
final String[] lines = text.subSequence(
TextViewUtils.getLineStart(text, selStart),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.gsantner.markor.format.TextConverterBase;

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

import other.de.stanetz.jpencconverter.JavaPasswordbasedCryption;

Expand All @@ -22,6 +23,7 @@ public class TodoTxtTextConverter 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>";
public static final Pattern TODOTXT_FILE_PATTERN = Pattern.compile("(?i)(^todo[-.]?.*)|(.*[-.]todo\\.((txt)|(text))$)");


//########################
Expand All @@ -48,7 +50,8 @@ protected String getContentType() {
}

@Override
protected boolean isFileOutOfThisFormat(String filepath, String extWithDot) {
return TodoTxtTask.isTodoFile(filepath.replace(JavaPasswordbasedCryption.DEFAULT_ENCRYPTION_EXTENSION, ""));
protected boolean isFileOutOfThisFormat(final File file, final String name, final String ext) {
return name.equals("todo.txt") ||
(TODOTXT_FILE_PATTERN.matcher(name).matches() && (name.endsWith(".txt") || name.endsWith(".text")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.gsantner.markor.format.TextConverterBase;
import net.gsantner.markor.model.AppSettings;
import net.gsantner.opoc.format.GsTextUtils;
import net.gsantner.opoc.util.GsFileUtils;

import org.apache.commons.io.FilenameUtils;

Expand Down Expand Up @@ -158,31 +159,15 @@ private String convertImage(File file, String fullMatch) {
return "![" + file.getName() + "](" + markdownPathToImage + ")";
}

/**
* NOTE: This method only works if the full file path is specified.
*
* @param filepath of a file
* @param extWithDot
* @return true if the file extension is .txt and the file contains a zim header; false otherwise
*/
@Override
protected boolean isFileOutOfThisFormat(String filepath, String extWithDot) {
if (extWithDot.equals(".txt")) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(new File(filepath)));
protected boolean isFileOutOfThisFormat(final File file, final String name, final String ext) {
if (ext.equals(".txt")) {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
return WikitextSyntaxHighlighter.ZIMHEADER_CONTENT_TYPE_ONLY.matcher(reader.readLine()).find();
} catch (Exception ignored) {
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (Exception ignored) {
}
}
}
return Arrays.asList(new String[]{".wikitext"}).contains(extWithDot);
return Arrays.asList(new String[]{".wikitext"}).contains(ext);
}

/*
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/net/gsantner/markor/model/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ public Document(@NonNull final File file) {
_fileExtension = GsFileUtils.getFilenameExtension(_file);

// Set initial format
final String fnlower = _file.getName().toLowerCase();
for (final FormatRegistry.Format format : FormatRegistry.FORMATS) {
if (format.converter == null || format.converter.isFileOutOfThisFormat(fnlower)) {
if (format.converter == null || format.converter.isFileOutOfThisFormat(_file)) {
setFormat(format.format);
break;
}
Expand Down Expand Up @@ -190,7 +189,7 @@ public static boolean isEncrypted(File file) {
}

public boolean isBinaryFileNoTextLoading() {
return _file != null && FormatRegistry.CONVERTER_EMBEDBINARY.isFileOutOfThisFormat(_file.getAbsolutePath());
return _file != null && FormatRegistry.CONVERTER_EMBEDBINARY.isFileOutOfThisFormat(_file);
}

public boolean isEncrypted() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class TodoTxtFileRecognitionTests {

@Test
public void checkTodoTxtFileRecognition() {
Pattern p = TodoTxtTask.TODOTXT_FILE_PATTERN;
Pattern p = TodoTxtTextConverter.TODOTXT_FILE_PATTERN;
assertThat(ispm(p, "todo.txt")).isEqualTo(true);
assertThat(ispm(p, "ToDO.txt")).isEqualTo(true);
assertThat(ispm(p, "todo.archive.txt")).isEqualTo(true);
Expand Down

0 comments on commit ddfdbb2

Please sign in to comment.