|
| 1 | +module Export.Anki exposing (download) |
| 2 | + |
| 3 | +import Data.AboutLink as AboutLink exposing (AboutLink) |
| 4 | +import Data.GlossaryItem exposing (GlossaryItem) |
| 5 | +import Data.GlossaryItems as GlossaryItems exposing (GlossaryItems) |
| 6 | +import Data.GlossaryTitle as GlossaryTitle exposing (GlossaryTitle) |
| 7 | +import File.Download as Download |
| 8 | +import Regex |
| 9 | + |
| 10 | + |
| 11 | +escape : String -> String |
| 12 | +escape string = |
| 13 | + string |
| 14 | + |> String.replace "\"" "\"\"" |
| 15 | + |> String.replace "\t" " " |
| 16 | + |
| 17 | + |
| 18 | +comment : String -> String |
| 19 | +comment = |
| 20 | + let |
| 21 | + linebreaks = |
| 22 | + "[\u{000D}\n]" |
| 23 | + |> Regex.fromString |
| 24 | + |> Maybe.withDefault Regex.never |
| 25 | + in |
| 26 | + Regex.replace linebreaks (always " ") |
| 27 | + >> (++) "# " |
| 28 | + |
| 29 | + |
| 30 | +crlf : String |
| 31 | +crlf = |
| 32 | + "\u{000D}\n" |
| 33 | + |
| 34 | + |
| 35 | +lines : List String -> String |
| 36 | +lines = |
| 37 | + String.join crlf |
| 38 | + |
| 39 | + |
| 40 | +paragraphs : List String -> String |
| 41 | +paragraphs = |
| 42 | + List.filter (not << String.isEmpty) |
| 43 | + >> String.join (crlf ++ crlf) |
| 44 | + |
| 45 | + |
| 46 | +itemToAnki : GlossaryItem -> String |
| 47 | +itemToAnki { terms, details } = |
| 48 | + let |
| 49 | + quote string = |
| 50 | + "\"" ++ string ++ "\"" |
| 51 | + |
| 52 | + front = |
| 53 | + terms |
| 54 | + |> List.map (.body >> escape) |
| 55 | + |> lines |
| 56 | + |> quote |
| 57 | + |
| 58 | + back = |
| 59 | + details |
| 60 | + |> List.map escape |
| 61 | + |> paragraphs |
| 62 | + |> quote |
| 63 | + in |
| 64 | + front ++ "\t" ++ back |
| 65 | + |
| 66 | + |
| 67 | +download : GlossaryTitle -> String -> List AboutLink -> GlossaryItems -> Cmd msg |
| 68 | +download glossaryTitle aboutParagraph aboutLinks glossaryItems = |
| 69 | + let |
| 70 | + filename = |
| 71 | + GlossaryTitle.toFilename "-Anki_deck.txt" glossaryTitle |
| 72 | + |
| 73 | + instructionsComment = |
| 74 | + comment "This file is meant to be imported into Anki (https://docs.ankiweb.net/importing.html#text-files)." |
| 75 | + |
| 76 | + titleComment = |
| 77 | + glossaryTitle |> GlossaryTitle.toString |> comment |
| 78 | + |
| 79 | + aboutLinksComment = |
| 80 | + aboutLinks |
| 81 | + |> List.map |
| 82 | + (\aboutLink -> |
| 83 | + "* " ++ AboutLink.body aboutLink ++ " - " ++ AboutLink.href aboutLink |
| 84 | + ) |
| 85 | + |> List.map comment |
| 86 | + |> lines |
| 87 | + |
| 88 | + itemsString = |
| 89 | + glossaryItems |
| 90 | + |> GlossaryItems.orderedAlphabetically |
| 91 | + |> List.map (Tuple.second >> itemToAnki) |
| 92 | + |> lines |
| 93 | + |
| 94 | + content = |
| 95 | + [ instructionsComment |
| 96 | + , comment "" |
| 97 | + , titleComment |
| 98 | + , comment "" |
| 99 | + , comment aboutParagraph |
| 100 | + , comment "" |
| 101 | + , aboutLinksComment |
| 102 | + , itemsString |
| 103 | + ] |
| 104 | + |> lines |
| 105 | + in |
| 106 | + Download.string filename "text/plain" content |
0 commit comments