Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import net.gsantner.markor.R;
import net.gsantner.markor.format.ActionButtonBase;
import net.gsantner.markor.format.FormatRegistry;
import net.gsantner.markor.frontend.MarkorDialogFactory;
import net.gsantner.markor.frontend.textview.TextViewUtils;
import net.gsantner.markor.model.Document;
Expand Down Expand Up @@ -242,7 +243,7 @@ public void archiveDoneTasks() {
doneContents.append(TodoTxtTask.tasksToString(move)).append("\n");

// Write to done file
if (new Document(doneFile).saveContent(getActivity(), doneContents.toString())) {
if (new Document(doneFile, FormatRegistry.FORMAT_TODOTXT).saveContent(getActivity(), doneContents.toString())) {
final String tasksString = TodoTxtTask.tasksToString(keep);
_hlEditor.setText(tasksString);
TextViewUtils.setSelectionFromOffsets(_hlEditor, offsets);
Expand Down Expand Up @@ -514,4 +515,4 @@ public void onCreate(final Bundle savedInstanceState) {
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class WikitextSyntaxHighlighter extends SyntaxHighlighterBase {
public final static Pattern CHECKLIST_LEFT_ARROW = Pattern.compile("(?<=(\\n|^))\t*(\\[)(<)(])(?= )");
public final static Pattern SUBSCRIPT = Pattern.compile("(_\\{(?!~)(.+?)\\})");
public final static Pattern SUPERSCRIPT = Pattern.compile("(\\^\\{(?!~)(.+?)\\})");
public final static Pattern ZIMHEADER_CONTENT_TYPE_ONLY = Pattern.compile("^\\s*Content-Type:\\s*text/x-zim-wiki");
public final static Pattern ZIMHEADER = Pattern.compile(
"^Content-Type: text/x-zim-wiki(\r\n|\r|\n)" +
"Wiki-Format: zim \\d+\\.\\d+(\r\n|\r|\n)" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
*/
@SuppressWarnings("WeakerAccess")
public class WikitextTextConverter extends TextConverterBase {
private static final Pattern ZIMHEADER_CONTENT_TYPE_ONLY = Pattern.compile("^\\s*Content-Type:\\s*text/x-zim-wiki");

// Zim Source View plugin: {{{code: lang="..." ...\n...\n}}}
private static final Pattern CODE_BLOCK_SOURCE_VIEW = Pattern.compile(
"(?ms)^\\{\\{\\{code:([^\\r\\n]*)(?:\\r\\n?|\\n)(.*?)^\\}\\}\\}[ \\t]*$");
Expand Down Expand Up @@ -270,7 +272,7 @@ private String convertImage(final File file, final Context context, final String
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();
return ZIMHEADER_CONTENT_TYPE_ONLY.matcher(reader.readLine()).find();
} catch (Exception ignored) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
final File file = new File(basedir, fileName);

// Most of the logic we want is in the document class so we just reuse it
final Document document = new Document(file);
final Document document = new Document(file, fmt.format);

// These are done even if the file isn't created
final String titleFormat = formatEdit.getText().toString().trim();
Expand Down
16 changes: 12 additions & 4 deletions app/src/main/java/net/gsantner/markor/model/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ public class Document implements Serializable {
private int _lastLength = -1;

public Document(@NonNull final File f) {
path = GsFileUtils.getPath(f);
file = new File(path);
title = GsFileUtils.getFilenameWithoutExtension(file);
extension = GsFileUtils.getFilenameExtension(file);
this(f, FormatRegistry.FORMAT_UNKNOWN);

// Set initial format
for (final FormatRegistry.Format format : FormatRegistry.FORMATS) {
Expand All @@ -85,6 +82,17 @@ public Document(@NonNull final File f) {
}
}

/**
* Create a document with a known format, skipping automatic format detection.
*/
public Document(@NonNull final File f, @StringRes final int format) {
path = GsFileUtils.getPath(f);
file = new File(path);
title = GsFileUtils.getFilenameWithoutExtension(file);
extension = GsFileUtils.getFilenameExtension(file);
setFormat(format);
}

private void initModTimePref() {
// We do not do this in constructor as we want to init after deserialization too
if (_modTimePref == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public void zimHeaderAtBeginningOfTheFileShouldMatch() {
assertThat(matcher.group()).isEqualTo("Content-Type: text/x-zim-wiki\nWiki-Format: zim 0.4\nCreation-Date: 2019-03-31T14:48:06+02:00");
}

@Test
public void zimHeaderWithNegativeTimezoneOffsetShouldMatch() {
pattern = WikitextSyntaxHighlighter.ZIMHEADER;
Matcher matcher = pattern.matcher("Content-Type: text/x-zim-wiki\nWiki-Format: zim 0.4\nCreation-Date: 2019-03-31T14:48:06-07:00\nOther content...");
assertThat(matcher.find()).isTrue();
assertThat(matcher.group()).isEqualTo("Content-Type: text/x-zim-wiki\nWiki-Format: zim 0.4\nCreation-Date: 2019-03-31T14:48:06-07:00");
}

@Test
public void zimHeaderNotAtBeginningOfTheFileShouldNotMatch() {
pattern = WikitextSyntaxHighlighter.ZIMHEADER;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*#######################################################
*
* SPDX-FileCopyrightText: 2026 Harshad Vedartham
* SPDX-License-Identifier: Apache-2.0
*
#########################################################*/
package net.gsantner.markor.model;

import static org.assertj.core.api.Assertions.assertThat;

import net.gsantner.markor.format.FormatRegistry;

import org.junit.Test;

import java.io.File;

public class DocumentConstructionTests {

@Test
public void knownFormatConstructorSetsFormatForAmbiguousFilename() {
final Document document = new Document(new File("archive.txt"), FormatRegistry.FORMAT_TODOTXT);

assertThat(document.getFormat()).isEqualTo(FormatRegistry.FORMAT_TODOTXT);
}
}
Loading