-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes: #1178
- Loading branch information
Showing
9 changed files
with
307 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import cheerio from 'cheerio'; | ||
|
||
import { File } from '../../anki/zip'; | ||
import Deck from '../Deck'; | ||
import Note from '../Note'; | ||
import Settings from '../Settings'; | ||
import { PlainTextParser } from './PlainTextParser/PlainTextParser'; | ||
import { isBasicFlashcard, isClozeFlashcard } from './PlainTextParser/types'; | ||
|
||
class FallbackParser { | ||
constructor(private readonly files: File[]) {} | ||
|
||
htmlToTextWithNewlines(html: string) { | ||
const $ = cheerio.load(html); | ||
|
||
function processListItems(items: cheerio.Cheerio) { | ||
let result = ''; | ||
items.each((_, element) => { | ||
const itemText = $(element).text().trim(); | ||
result += `• ${itemText}\n`; | ||
}); | ||
return result; | ||
} | ||
|
||
const elem = $('ul, ol'); | ||
let items: string[] = []; | ||
elem.each((_, element) => { | ||
const listItems = $(element).find('li'); | ||
const listText = processListItems(listItems); | ||
items.push(listText); | ||
}); | ||
|
||
return items; | ||
} | ||
|
||
getTitleFromHTML(html: string) { | ||
const $ = cheerio.load(html); | ||
return $('title').text().trim(); | ||
} | ||
|
||
getStyleTagFromString(html: string) { | ||
const $ = cheerio.load(html); | ||
const styleTag = $('style'); | ||
|
||
if (styleTag.length === 0) { | ||
return ''; // No style tag found, return an empty string | ||
} | ||
|
||
return styleTag.text() ?? ''; | ||
} | ||
|
||
run(settings: Settings) { | ||
const decks = []; | ||
for (const file of this.files) { | ||
const contents = file.contents?.toString(); | ||
if (!contents) { | ||
continue; | ||
} | ||
const plainText = this.htmlToTextWithNewlines(contents); | ||
const plainTextParser = new PlainTextParser(); | ||
const found = plainTextParser.parse(plainText.join('\n')); | ||
|
||
const cards: Note[] = found.filter(Boolean).map((card, index) => { | ||
const note = new Note(card.front, ''); | ||
note.number = index; | ||
if (isClozeFlashcard(card)) { | ||
note.cloze = true; | ||
} else { | ||
note.back = card.back; | ||
} | ||
return note; | ||
}); | ||
|
||
decks.push( | ||
new Deck( | ||
this.getTitleFromHTML(contents), | ||
cards, | ||
'', // skip cover image | ||
'', // skip style | ||
Deck.GenerateId(), | ||
settings | ||
) | ||
); | ||
} | ||
return decks; | ||
} | ||
} | ||
|
||
export default FallbackParser; |
63 changes: 63 additions & 0 deletions
63
src/lib/parser/experimental/PlainTextParser/PlainTextParser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { | ||
BasicCard, | ||
ClozeCard, | ||
Flashcard, | ||
isPossiblyClozeFlashcard, | ||
} from './types'; | ||
|
||
export class PlainTextParser { | ||
getOneOrMoreAnswers(answers: string): string[] { | ||
const answerList = answers.split(', '); | ||
if (!answerList || answerList.length === 0) { | ||
return [answers]; | ||
} | ||
return answerList; | ||
} | ||
|
||
fillInTheBlanks(sentence: string, answers: string): ClozeCard { | ||
const answerList = this.getOneOrMoreAnswers(answers); | ||
let clozeSentence = sentence; | ||
|
||
for (let i = 0; i < answerList.length; i++) { | ||
clozeSentence = clozeSentence.replace( | ||
/_{1,}/, | ||
`{{c${i + 1}::${answerList[i]}}}` | ||
); | ||
} | ||
|
||
return { | ||
front: clozeSentence, | ||
isCloze: true, | ||
}; | ||
} | ||
|
||
getBasicFlashcard(flashcardText: string): BasicCard { | ||
const [front, back] = flashcardText.split(' - '); | ||
|
||
return { | ||
front: front, | ||
back: back, | ||
}; | ||
} | ||
|
||
parse(input: string): Flashcard[] { | ||
const flashcards = []; | ||
const bulletPoints = input.split(/\n\n|\n- /); | ||
|
||
for (const bulletPoint of bulletPoints) { | ||
const [question, answers] = bulletPoint.split(' - '); | ||
|
||
if (isPossiblyClozeFlashcard(question)) { | ||
const cards = this.fillInTheBlanks(question, answers); | ||
if (cards) { | ||
flashcards.push(cards); | ||
} | ||
continue; | ||
} | ||
|
||
flashcards.push(this.getBasicFlashcard(bulletPoint)); | ||
} | ||
|
||
return flashcards; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export interface ClozeCard { | ||
isCloze: boolean; | ||
front: string; | ||
extra?: string; | ||
} | ||
|
||
export interface BasicCard { | ||
front: string; | ||
back: string; | ||
tags?: string; | ||
} | ||
|
||
export type Flashcard = ClozeCard | BasicCard; | ||
|
||
export const isClozeFlashcard = ( | ||
flashcard: Flashcard | ||
): flashcard is ClozeCard => | ||
'isCloze' in flashcard && flashcard.isCloze === true; | ||
|
||
export const isBasicFlashcard = ( | ||
flashcard: Flashcard | ||
): flashcard is BasicCard => | ||
'back' in flashcard && flashcard.back !== undefined; | ||
|
||
export const isPossiblyClozeFlashcard = (question: string) => { | ||
return question.includes('_') && question.split('-'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.