Skip to content

Commit

Permalink
Merge pull request #1 from stefan2904/patch-orgzly-revived-95
Browse files Browse the repository at this point in the history
Prevent adding of undesired newline between drawers in header
  • Loading branch information
amberin authored Jan 3, 2024
2 parents 014fbfe + a7ddf53 commit a6ac3a3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/main/java/com/orgzly/org/parser/OrgParserWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,13 @@ public String whiteSpacedHead(OrgHead head, int level, boolean isIndented) {
if (head.hasContent()) {
/*
* Separate content from header with an empty line,
* unless it starts with following strings.
* unless it starts with following strings or a custom drawer.
* Until LOGBOOK and CLOCK support is added.
*/
String content = head.getContent().trim();
if (!content.startsWith(":LOGBOOK:") && !content.startsWith("CLOCK: ") && !isLogNoteHeading(content)) {
String firstLine = content.split("\n")[0].trim();
if (!content.startsWith(":LOGBOOK:") && !content.startsWith("CLOCK: ")
&& !isLogNoteHeading(content) && !lineStartswithDrawer(firstLine)) {
if (settings.separateHeaderAndContentWithNewLine) {
s.append("\n");
}
Expand Down Expand Up @@ -260,6 +262,11 @@ private boolean isLogNoteHeading(String content) {
return false;
}

private boolean lineStartswithDrawer(String line) {
// example of a drawer: :BACKLINKS:
return line.startsWith(":") && line.endsWith(":");
}

/** Append (level + 1) spaces. */
private void appendIndent(StringBuilder s, int level, boolean isIndented) {
if (isIndented) {
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/com/orgzly/org/parser/OrgParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,39 @@ public void testNoteBodyNote() {
Assert.assertEquals("* Title 1\n\n\n\nContent\n\n* Title 2\n", out);
}

@Test
public void testSeparateHeaderDrawerContent1() throws Exception {
// test that we don't prepend content with a newline if content starts with a drawer
// fixes https://github.com/orgzly-revived/orgzly-android-revived/issues/95
OrgParserSettings settings = new OrgParserSettings();
settings.separateHeaderAndContentWithNewLine = true;
parserWriter = new OrgParserWriter(settings);

OrgHead head1 = new OrgHead("Title 1");
head1.setContent(":SOMEDRAWER:\nfoo\n:END:\n\nContent");
OrgNodeInList nodeInList1 = new OrgNodeInList(1, 1, head1);

String out = parserWriter.whiteSpacedFilePreface("") + parserWriter.whiteSpacedHead(nodeInList1, false);
Assert.assertEquals("* Title 1\n:SOMEDRAWER:\nfoo\n:END:\n\nContent\n\n", out);
// although `separateHeaderAndContentWithNewLine` is true, we don't expect extra new line at the end of the drawer
// since custom drawers are part of the content, which we currently don't modify during export
}

@Test
public void testSeparateHeaderDrawerContent0() throws Exception {
// test that we don't prepend content with a newline if content starts with a drawer
OrgParserSettings settings = new OrgParserSettings();
settings.separateHeaderAndContentWithNewLine = false;
parserWriter = new OrgParserWriter(settings);

OrgHead head1 = new OrgHead("Title 1");
head1.setContent(":SOMEDRAWER:\nfoo\n:END:\nContent");
OrgNodeInList nodeInList1 = new OrgNodeInList(1, 1, head1);

String out = parserWriter.whiteSpacedFilePreface("") + parserWriter.whiteSpacedHead(nodeInList1, false);
Assert.assertEquals("* Title 1\n:SOMEDRAWER:\nfoo\n:END:\nContent\n\n", out);
}

@Test
public void testNotSeparateHeaderContent() throws Exception {
OrgParserSettings settings = new OrgParserSettings();
Expand Down

0 comments on commit a6ac3a3

Please sign in to comment.