Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for atomic clozures #797

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
2 changes: 1 addition & 1 deletion src/Deck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class Deck {
let result: number = 0;
for (let i = 0; i < cards.length; i++) {
const card = cards[i];
if (Object.is(question, cards[i].question)) result++;
if (Object.is(question, card.question)) result++;
}
return result;
}
Expand Down
1 change: 1 addition & 0 deletions src/NoteQuestionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class NoteQuestionParser {
const settings: SRSettings = this.settings;
const result: [CardType, string, number][] = parse(
this.noteText,
settings.multilineCloze,
settings.singleLineCardSeparator,
settings.singleLineReversedCardSeparator,
settings.multilineCardSeparator,
Expand Down
1 change: 1 addition & 0 deletions src/lang/locale/ar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export default {
CARD_MODAL_WIDTH_PERCENT: "نسبة عرض البطاقة",
RANDOMIZE_CARD_ORDER: "ترتيب بطاقة عشوائي أثناء المراجعة؟",
DISABLE_CLOZE_CARDS: "؟cloze تعطيل بطاقات",
MULTILINE_CLOZE: "Multiline clozes?",
CONVERT_HIGHLIGHTS_TO_CLOZES: "Convert ==hightlights== to clozes?",
CONVERT_BOLD_TEXT_TO_CLOZES: "Convert **bolded text** to clozes?",
CONVERT_CURLY_BRACKETS_TO_CLOZES: "Convert {{curly brackets}} to clozes?",
Expand Down
1 change: 1 addition & 0 deletions src/lang/locale/cz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export default {
CARD_MODAL_WIDTH_PERCENT: "Šířka kartiček v procentech",
RANDOMIZE_CARD_ORDER: "Náhodně změnit pořadí kartiček během revize?",
DISABLE_CLOZE_CARDS: "Vypnout cloze kartičky?",
MULTILINE_CLOZE: "Multiline clozes?",
CONVERT_HIGHLIGHTS_TO_CLOZES: "Převést ==zvýraznění== na clozes?",
CONVERT_BOLD_TEXT_TO_CLOZES: "Převést **tučný text** na clozes?",
CONVERT_CURLY_BRACKETS_TO_CLOZES: "Převést {{složené závorky}} na clozes?",
Expand Down
1 change: 1 addition & 0 deletions src/lang/locale/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export default {
CARD_MODAL_WIDTH_PERCENT: "Breite einer Lernkarte in Prozent",
RANDOMIZE_CARD_ORDER: "Während der Wiederhoung die Reihenfolge zufällig mischen?",
DISABLE_CLOZE_CARDS: "Lückentextkarten (cloze deletions) deaktivieren?",
MULTILINE_CLOZE: "Multiline clozes?",
CONVERT_HIGHLIGHTS_TO_CLOZES: "==Hervorgehobenen== Text in Lückentextkarten umwandeln?",
CONVERT_BOLD_TEXT_TO_CLOZES: "**Fettgedruckten** Text in Lückentextkarten umwandeln?",
CONVERT_CURLY_BRACKETS_TO_CLOZES:
Expand Down
1 change: 1 addition & 0 deletions src/lang/locale/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export default {
CARD_MODAL_WIDTH_PERCENT: "Flashcard Width Percentage",
RANDOMIZE_CARD_ORDER: "Randomize card order during review?",
DISABLE_CLOZE_CARDS: "Disable cloze cards?",
MULTILINE_CLOZE: "Multiline clozes?",
CONVERT_HIGHLIGHTS_TO_CLOZES: "Convert ==hightlights== to clozes?",
CONVERT_BOLD_TEXT_TO_CLOZES: "Convert **bolded text** to clozes?",
CONVERT_CURLY_BRACKETS_TO_CLOZES: "Convert {{curly brackets}} to clozes?",
Expand Down
1 change: 1 addition & 0 deletions src/lang/locale/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export default {
CARD_MODAL_WIDTH_PERCENT: "Porcentaje del ancho de las tarjetas de memoria",
RANDOMIZE_CARD_ORDER: "¿Aleatorizar el orden de las tarjetas para revisión?",
DISABLE_CLOZE_CARDS: "¿Deshabilitar deletreo de huecos en las tarjetas?",
MULTILINE_CLOZE: "Multiline clozes?",
CONVERT_HIGHLIGHTS_TO_CLOZES: "¿Convertir ==resaltados== a deletreo de huecos?",
CONVERT_BOLD_TEXT_TO_CLOZES: "¿Convertir **texto en negrita** a deletreo de huecos?",
CONVERT_CURLY_BRACKETS_TO_CLOZES: "¿Convertir {{llaves rizadas}} a deletreo de huecos?",
Expand Down
1 change: 1 addition & 0 deletions src/lang/locale/ja.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export default {
CARD_MODAL_WIDTH_PERCENT: "フラッシュカードの横サイズのパーセンテージ",
RANDOMIZE_CARD_ORDER: "レビュー中のカードの順番をランダムにしますか?",
DISABLE_CLOZE_CARDS: "穴埋めカードを無効化しますか?",
MULTILINE_CLOZE: "Multiline clozes?",
CONVERT_HIGHLIGHTS_TO_CLOZES: "==ハイライト==を穴埋めとして使用しますか?",
CONVERT_BOLD_TEXT_TO_CLOZES: "**ボールド体**を穴埋めとして使用しますか?",
CONVERT_CURLY_BRACKETS_TO_CLOZES: "{{中括弧}}を穴埋めとして使用しますか?",
Expand Down
1 change: 1 addition & 0 deletions src/lang/locale/ko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export default {
CARD_MODAL_WIDTH_PERCENT: "플래시카드 너비 비율",
RANDOMIZE_CARD_ORDER: "리뷰중인 카드의 순서를 랜덤으로 두시겠습니까?",
DISABLE_CLOZE_CARDS: "빈 칸 채우기 카드를 비활성화하시겠습니까?",
MULTILINE_CLOZE: "Multiline clozes?",
CONVERT_HIGHLIGHTS_TO_CLOZES: "==hightlights== 를 빈 칸 채우기로 전환하시겠습니까?",
CONVERT_BOLD_TEXT_TO_CLOZES: "**bolded text** 를 빈 칸 채우기로 전환하시겠습니까?",
CONVERT_CURLY_BRACKETS_TO_CLOZES: "{{curly brackets}} 를 빈 칸 채우기로 전환하시겠습니까?",
Expand Down
1 change: 1 addition & 0 deletions src/lang/locale/pt-br.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export default {
CARD_MODAL_WIDTH_PERCENT: "Porcentagem de Largura do Flashcard",
RANDOMIZE_CARD_ORDER: "Aleatorizar a ordem das cartas durante a revisão?",
DISABLE_CLOZE_CARDS: "Desabilitar cartas que usam omissão de palavras?",
MULTILINE_CLOZE: "Multiline clozes?",
CONVERT_HIGHLIGHTS_TO_CLOZES: "Converter ==marca-texto== em omissões?",
CONVERT_BOLD_TEXT_TO_CLOZES: "Converter **texto em negrito** em omissões?",
CONVERT_CURLY_BRACKETS_TO_CLOZES: "Converter {{chaves}} em omissões?",
Expand Down
1 change: 1 addition & 0 deletions src/lang/locale/ru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export default {
CARD_MODAL_WIDTH_PERCENT: "Ширина карточки в процентах",
RANDOMIZE_CARD_ORDER: "Случайный порядок карточек во время повторения?",
DISABLE_CLOZE_CARDS: "Выключить карты с пропусками (пример: [...])?",
MULTILINE_CLOZE: "Multiline clozes?",
CONVERT_HIGHLIGHTS_TO_CLOZES: "Конвертировать ==выделенный текст== в пропуски (пример: [...])?",
CONVERT_BOLD_TEXT_TO_CLOZES: "Конвертировать **жирный текст** в пропуски (пример: [...])?",
CONVERT_CURLY_BRACKETS_TO_CLOZES:
Expand Down
1 change: 1 addition & 0 deletions src/lang/locale/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export default {
CARD_MODAL_WIDTH_PERCENT: "卡片宽度百分比",
RANDOMIZE_CARD_ORDER: "复习时随机显示卡片?",
DISABLE_CLOZE_CARDS: "不进行完形填空?",
MULTILINE_CLOZE: "Multiline clozes?",
CONVERT_HIGHLIGHTS_TO_CLOZES: "将 ==高亮== 转换为完形填空?",
CONVERT_BOLD_TEXT_TO_CLOZES: "将 **粗体** 转换为完形填空?",
CONVERT_CURLY_BRACKETS_TO_CLOZES: "将 {{大括号}} 转换为完形填空?",
Expand Down
1 change: 1 addition & 0 deletions src/lang/locale/zh-tw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export default {
CARD_MODAL_WIDTH_PERCENT: "卡片寬度百分比",
RANDOMIZE_CARD_ORDER: "復習時隨機顯示卡片?",
DISABLE_CLOZE_CARDS: "停用填空克漏字卡片?",
MULTILINE_CLOZE: "Multiline clozes?",
CONVERT_HIGHLIGHTS_TO_CLOZES: "將 ==高亮== 轉換為填空克漏字?",
CONVERT_BOLD_TEXT_TO_CLOZES: "將 **粗體** 轉換為填空克漏字?",
CONVERT_CURLY_BRACKETS_TO_CLOZES: "將 {{大括號}} 轉換為填空克漏字?",
Expand Down
14 changes: 14 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CardType } from "./Question";
* Returns flashcards found in `text`
*
* @param text - The text to extract flashcards from
* @param multilineCloze - Whether to extract clozes spanning multiple lines
* @param singlelineCardSeparator - Separator for inline basic cards
* @param singlelineReversedCardSeparator - Separator for inline reversed cards
* @param multilineCardSeparator - Separator for multiline basic cards
Expand All @@ -12,6 +13,7 @@ import { CardType } from "./Question";
*/
export function parse(
text: string,
multilineCloze: boolean,
singlelineCardSeparator: string,
singlelineReversedCardSeparator: string,
multilineCardSeparator: string,
Expand Down Expand Up @@ -71,6 +73,18 @@ export function parse(
) {
cardType = CardType.Cloze;
lineNo = i;

if (!multilineCloze) {
let clozeText = currentLine;
if (i + 1 < lines.length && lines[i + 1].startsWith("<!--SR:")) {
clozeText += "\n" + lines[i + 1];
i++;
}
cards.push([cardType, clozeText, lineNo]);
cardType = null;
cardText = "";
continue;
}
} else if (currentLine.trim() === multilineCardSeparator) {
cardType = CardType.MultiLineBasic;
lineNo = i;
Expand Down
9 changes: 9 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface SRSettings {
flashcardHeightPercentage: number;
flashcardWidthPercentage: number;
randomizeCardOrder: boolean;
multilineCloze: boolean;
convertHighlightsToClozes: boolean;
convertBoldTextToClozes: boolean;
convertCurlyBracketsToClozes: boolean;
Expand Down Expand Up @@ -56,6 +57,7 @@ export const DEFAULT_SETTINGS: SRSettings = {
flashcardHeightPercentage: Platform.isMobile ? 100 : 80,
flashcardWidthPercentage: Platform.isMobile ? 100 : 40,
randomizeCardOrder: true,
multilineCloze: true,
convertHighlightsToClozes: true,
convertBoldTextToClozes: false,
convertCurlyBracketsToClozes: false,
Expand Down Expand Up @@ -251,6 +253,13 @@ export class SRSettingTab extends PluginSettingTab {
}),
);

new Setting(containerEl).setName(t("MULTILINE_CLOZE")).addToggle((toggle) =>
toggle.setValue(this.plugin.data.settings.multilineCloze).onChange(async (value) => {
this.plugin.data.settings.multilineCloze = value;
await this.plugin.savePluginData();
}),
);

new Setting(containerEl).setName(t("CONVERT_HIGHLIGHTS_TO_CLOZES")).addToggle((toggle) =>
toggle
.setValue(this.plugin.data.settings.convertHighlightsToClozes)
Expand Down
15 changes: 8 additions & 7 deletions tests/unit/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { parse } from "src/parser";
import { CardType } from "src/Question";

const defaultArgs: [string, string, string, string, boolean, boolean, boolean] = [
const defaultArgs: [boolean, string, string, string, string, boolean, boolean, boolean] = [
false,
"::",
":::",
"?",
Expand Down Expand Up @@ -120,9 +121,9 @@ test("Test parsing of cloze cards", () => {
expect(parse("lorem ipsum ==p\ndolor won==", ...defaultArgs)).toEqual([]);
expect(parse("lorem ipsum ==dolor won=", ...defaultArgs)).toEqual([]);
// ==highlights== turned off
expect(parse("cloze ==deletion== test", "::", ":::", "?", "??", false, true, false)).toEqual(
[],
);
expect(
parse("cloze ==deletion== test", false, "::", ":::", "?", "??", false, true, false),
).toEqual([]);

// **bolded**
expect(parse("cloze **deletion** test", ...defaultArgs)).toEqual([
Expand Down Expand Up @@ -151,9 +152,9 @@ test("Test parsing of cloze cards", () => {
expect(parse("lorem ipsum **p\ndolor won**", ...defaultArgs)).toEqual([]);
expect(parse("lorem ipsum **dolor won*", ...defaultArgs)).toEqual([]);
// **bolded** turned off
expect(parse("cloze **deletion** test", "::", ":::", "?", "??", true, false, false)).toEqual(
[],
);
expect(
parse("cloze **deletion** test", false, "::", ":::", "?", "??", true, false, false),
).toEqual([]);

// both
expect(parse("cloze **deletion** test ==another deletion==!", ...defaultArgs)).toEqual([
Expand Down