Skip to content

Commit

Permalink
Updated rules
Browse files Browse the repository at this point in the history
  • Loading branch information
alberti42 committed Aug 16, 2024
1 parent b9098ee commit 1940e7b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 85 deletions.
82 changes: 39 additions & 43 deletions src/generateParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,22 @@ html_comment
return createParsedQuestionInfo(CardType.Ignore,"",0,0);
}
tag
= $("#" + name:([a-zA-Z/\\-_] { return 1; } / [0-9]{ return 0;})+ &{
// check if it is a valid Obsidian tag
return name.includes(1);
})
tag_line
= tag:tag _* nl:newline { return tag + nl; }
inline_card
= e:inline newline? { return e; }
inline
= left:(!inline_mark [^\\n\\r])+ inline_mark right:not_newline (newline annotation)? {
return createParsedQuestionInfo(CardType.SingleLineBasic,text(),location().start.line-1,location().end.line-1);
}
return createParsedQuestionInfo(CardType.SingleLineBasic,text(),location().start.line-1,location().end.line-1);
}
inline_rev_card
= e:inline_rev newline? { return e; }
Expand All @@ -69,14 +78,14 @@ multiline_card
= c:multiline separator_line {
return c;
}
multiline
= arg1:multiline_before question_mark arg2:multiline_after {
return createParsedQuestionInfo(CardType.MultiLineBasic,(arg1+"${settings.multilineCardSeparator}"+"\\n"+arg2.trim()),location().start.line-1,location().end.line-2);
= tag:tag_line? arg1:multiline_before multiline_mark arg2:multiline_after {
return createParsedQuestionInfo(CardType.MultiLineBasic,((tag ?? "") + arg1+"${settings.multilineCardSeparator}"+"\\n"+arg2.trim()),location().start.line-1,location().end.line-2);
}
multiline_before
= $(!question_mark nonempty_text_line)+
= $(!multiline_mark nonempty_text_line)+
multiline_after
= $(!separator_line (tilde_code / backprime_code / text_line))+
Expand All @@ -92,33 +101,26 @@ backprime_code
backprime_marker
= "\`\`\`" "\`"*
multiline_rev_card
= d:multiline_rev separator_line {
return d;
}
= @multiline_rev separator_line
multiline_rev
= arg1:multiline_rev_before double_question_mark arg2:multiline_rev_after {
return createParsedQuestionInfo(CardType.MultiLineReversed,(arg1+"${settings.multilineReversedCardSeparator}"+"\\n"+arg2.trim()),location().start.line-1,location().end.line-2);
= tag:tag_line? arg1:multiline_rev_before multiline_rev_mark arg2:multiline_rev_after {
return createParsedQuestionInfo(CardType.MultiLineReversed,((tag ?? "") + arg1+"${settings.multilineReversedCardSeparator}"+"\\n"+arg2.trim()),location().start.line-1,location().end.line-2);
}
multiline_rev_before
= e:(!double_question_mark nonempty_text_line)+ {
return text();
}
= $(!multiline_rev_mark nonempty_text_line)+
multiline_rev_after
= $(!separator_line text_line)+
close_card
= t:$close {
return createParsedQuestionInfo(CardType.Cloze,t.trim(),location().start.line-1,location().end.line-1);
= $(multiline_before_close? f:close_line e:(multiline_after_close)? e1:(newline annotation)?) {
return createParsedQuestionInfo(CardType.Cloze,text().trim(),location().start.line-1,location().end.line-1);
}
close
= $(multiline_before_close? f:close_line e:(multiline_after_close)? e1:(newline annotation)?)
close_line
= ((!close_text [^\\n\\r])* close_text) text_line_nonterminated?
Expand Down Expand Up @@ -158,11 +160,11 @@ inline_mark
inline_rev_mark
= "${settings.singleLineReversedCardSeparator}"
question_mark
= "${settings.multilineCardSeparator}" _ newline
multiline_mark
= "${settings.multilineCardSeparator}" _* newline
double_question_mark
= "${settings.multilineReversedCardSeparator}" _ newline
multiline_rev_mark
= "${settings.multilineReversedCardSeparator}" _* newline
end_card_mark
= "${settings.multilineCardEndMarker}"
Expand All @@ -171,56 +173,50 @@ separator_line
= end_card_mark newline
text_line_nonterminated
= t:$[^\\n\\r]+ {
return t;
}
= $[^\\n\\r]+
nonempty_text_line
= t:$[^\\n\\r]+ newline {
return t;
}
= @$[^\\n\\r]+ newline
text_line
= t:$[^\\n\\r]* newline {
return t;
}
= @$[^\\n\\r]* newline
text_line1
= newline @$[^\\n\\r]*
loose_line
= (([^\\n\\r]* newline) / [^\\n\\r]+) {
= $(([^\\n\\r]* newline) / [^\\n\\r]+) {
return createParsedQuestionInfo(CardType.Ignore,"",0,0);
}
annotation
= "<!--SR:" (!"-->" .)+ "-->"
= $("<!--SR:" (!"-->" .)+ "-->")
not_newline
= [^\\n\\r]*
= $[^\\n\\r]*
newline
= [\\n\\r]
= $[\\n\\r]
empty_line
= $(_ [\\n\\r])
= $(_* [\\n\\r])
empty_lines
= emptylines:empty_line+
= $empty_line+
nonemptyspace
= [^ \\f\\t\\v\\u0020\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000\\ufeff]
emptyspace
= _
= _*
_ = ([ \\f\\t\\v\\u0020\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000\\ufeff])*
_ = ([ \\f\\t\\v\\u0020\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000\\ufeff])
`;

if (settings.showDebugMessages) {
// console.log(grammar);
console.log(grammar);
}

// const t0 = Date.now();
const parser = generate(grammar);
// const t1 = Date.now();
Expand Down
2 changes: 0 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ export function parseEx(
parser = defaultParser;
}

// console.log(this.)

// Use this function when you call the parse method
try {
cards = parser.parse(text + "\n\n\n", {
Expand Down
44 changes: 4 additions & 40 deletions tests/unit/FlashcardReviewSequencer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ Q1::A1

test("Answer includes MathJax within $$", async () => {
let fileText: string = `#flashcards
What is Newman's equation for gravitational force
What is Newton's equation for gravitational force
?
$$\\huge F_g=\\frac {G m_1 m_2}{d^2}$$`;

Expand All @@ -684,7 +684,7 @@ $$\\huge F_g=\\frac {G m_1 m_2}{d^2}$$`;
fileText,
);
await c.setSequencerDeckTreeFromOriginalText();
expect(c.reviewSequencer.currentCard.front).toContain("What is Newman's equation");
expect(c.reviewSequencer.currentCard.front).toContain("What is Newton's equation");

// Reviewing the card doesn't change the question, only adds the schedule info
await c.reviewSequencer.processReview(ReviewResponse.Easy);
Expand Down Expand Up @@ -762,39 +762,6 @@ describe("updateCurrentQuestionText", () => {
describe("Checking update to file", () => {
describe("Single line card type; Settings - schedule on following line", () => {

test("Question starts line after tag (single space after tag before newline); New card", async () => {
let originalQuestionStr: string = `#flashcards${space}
Q2
?
A2`;

let fileText: string = `
${originalQuestionStr}
#flashcards Q1::A1
#flashcards Q3::A3`;

let updatedQuestionText: string = `Multiline question
Question starting immediately after tag
?
A2 (answer now includes more detail)
extra answer line 2`;

let expectedUpdatedStr: string = `#flashcards
${updatedQuestionText}`;

await checkUpdateCurrentQuestionText(
fileText,
updatedQuestionText,
originalQuestionStr,
expectedUpdatedStr,
DEFAULT_SETTINGS,
);
});

return;

test("Question has schedule on following line before/after update", async () => {
let text: string = `
#flashcards Q1::A1
Expand Down Expand Up @@ -839,7 +806,7 @@ ${updatedQuestionText}`;
);
});
});
return;

describe("Single line card type; Settings - schedule on same line", () => {
let settings: SRSettings = { ...DEFAULT_SETTINGS };
settings.cardCommentOnSameLine = true;
Expand Down Expand Up @@ -1060,6 +1027,7 @@ ${updatedQuestionText}`;
});
});


describe("getDeckStats", () => {
describe("Single level deck with some new and due cards", () => {
test("Initial stats", async () => {
Expand Down Expand Up @@ -1203,10 +1171,6 @@ async function checkUpdateCurrentQuestionText(
// originalText should remain the same except for the specific substring change from originalStr => updatedStr
if (!c.originalText.includes(originalStr)) throw `Text not found: ${originalStr}`;
const expectedFileText: string = c.originalText.replace(originalStr, updatedStr);
console.log("<<<"+await c.file.read()+">>>");
console.log("Original string:<<<"+originalStr+">>>");
console.log("Updated string:<<<"+updatedStr+">>>");
console.log("<<<"+expectedFileText+">>>");
expect(await c.file.read()).toEqual(expectedFileText);
return c;
}

0 comments on commit 1940e7b

Please sign in to comment.