kunpeng9 / GTD2020-05-31

2020-05-31创建【将github的项目链接等放入滴答清单进行管理或者印象笔记等,实践证明都不可行,不好用,完全被搁置了】
The Unlicense
26 stars 5 forks source link

Pseudonium/Obsidian_to_Anki: Script to add flashcards from text/markdown files to Anki #55

Open kunpeng9 opened 3 years ago

kunpeng9 commented 3 years ago

Script to add flashcards from a text or markdown file to Anki. Run from the command line. Built with Obsidian markdown syntax in mind. Supports user-defined custom syntax for flashcards.
See the Trello for planned features.

脚本将文本或降价文件中的抽认卡添加到 Anki。从命令行运行。使用黑曜石标记语法构建。支持用户定义的自定义语法的抽认卡。参见 Trello 的计划功能。

Features 功能

Current features:

当前特点:

Note that all custom syntax is off by default, and must be programmed into the script via the config file - see Custom syntax for instructions.

注意,默认情况下所有自定义语法都是关闭的,必须通过配置文件编程到脚本中——请参阅自定义语法以获得说明。

Who is this for? 这是给谁的?

It might be useful to show the motivation for me personally writing the script in the first place.
My workflow is essentially to have notes be flashcards - one of my notes (before I discovered Obsidian). However, it got tedious to keep copy-pasting cards into Anki, so I got the idea to write a script to do it automatically.

我的工作流程基本上就是让笔记成为抽认卡——我的笔记之一 (在我发现黑曜石之前)。然而,把卡片复制粘贴到 Anki 中变得很乏味,所以我想写一个脚本来自动完成它。

However, you don't need to have your notes be files of flashcards to use this script! You just need to be fine with visibly embedding flashcards in your notes, and keeping them there for future reference/editing. The script will ignore anything it doesn't think is a flashcard, so you're free to add context/information not needed for Anki to your notes.

然而,你不需要有你的笔记是文件的闪存卡使用这个脚本!你只需要在你的笔记中明显地嵌入抽认卡,并保留它们以备将来参考 / 编辑之用。脚本会忽略任何它认为不是闪卡的内容,因此您可以自由地在笔记中添加 Anki 不需要的上下文 / 信息。

Setup 设置

  1. Install the latest version of 安装最新的Python 巨蟒.
  2. Start up 启动Anki 女名女子名, and navigate to your desired profile. ,并导航到您想要的配置文件
  3. Ensure that you've installed 确保你已经安装了AnkiConnect.
  4. If you are a new user, download 如果你是一个新用户,请下载obstoanki_setup.py from the 从releases page 释放页, and place it in the folder you want the script installed (for example your notes folder). ,并将其放置在您希望安装的脚本文件夹中 (例如您的备注文件夹)
  5. Run 快跑obstoanki_setup.py, for example by double-clicking it in a file explorer. This will download the latest version of the script and required dependencies automatically. Existing users should be able to run their existing ,例如在档案总管中双击它。这将自动下载脚本的最新版本和所需的依赖项。现有用户应该能够运行他们现有的obstoanki_setup.py to get the latest version of the script. 才能拿到剧本的最新版本
  6. Check the Permissions tab below to ensure the script is able to run. 选中下面的 Permissions 选项卡,以确保脚本能够运行
  7. Run 快跑obsidian_to_anki.py, for example by double-clicking it in a file explorer. This will generate a config file, ,例如在档案总管中按两下。这会产生一个配置文件,obsidian_to_anki_config.ini.

See Troubleshooting if you have problems.

如果有问题,请查看故障排除。

Permissions 权限

The script needs to be able to:

脚本需要能够:

Usage 用法

Apart from editing the config file, all operations of the script require Anki to be running.
New in v2.7.0 - if you supply Anki Path and Anki Profile, the script will automatically attempt to open Anki if it isn't already running.

除了编辑配置文件,脚本的所有操作都需要运行 Anki。新的 v2.7.0 - 如果你提供 Anki Path 和 Anki 配置文件,该脚本将自动尝试打开 Anki,如果它还没有运行。

The GUI of the script looks like this:

脚本的图形用户界面是这样的:

Hopefully the options and path are self-explanatory. The 'recurse' option can be used on the top-level notes directory - it'll pick up all the notes in subfolders automatically. Note that you can run the script over the same file twice perfectly fine - it won't add duplicate cards.

希望选项和路径是不言自明的。“递归” 选项可以在顶级的笔记目录中使用——它会自动拾取子文件夹中的所有笔记。请注意,您可以在同一个文件上运行脚本两次完美无缺 - 它不会添加重复卡。

Command line usage 命令行用法

If you set 'GUI' in the config file to False, the script is then run from the command line:

如果你在配置文件中将‘ GUI’设置为 False,脚本就会从命令行运行:

New users 新用户

If you are a new user, these steps are recommended:

如果你是一个新用户,推荐以下步骤:

  1. Check 将军Custom syntax 自定义语法 to see if there is a template that works for you. 看看有没有适合你的模板
  2. Then, check the information on the following topics: 然后,检查以下主题的信息:
  1. You should be good to go simply running the script with the 'Regex' option checked. 您应该可以简单地运行脚本,并选择 “Regex” 选项

The sections below describe the default syntax of the script (with the 'Regex' option not checked).

下面的部分描述了脚本的默认语法 (未选中“ Regex” 选项)。

Config 配置

DEFAULT section 缺省部分

Allows you to change the default deck and tag of the script.
New in v2.2.2 - allows you to enable/disable the 'CurlyCloze' option, which is explained in Cloze formatting . New in v2.4.0 - allows you to enable/disable the GUI of the script - see Command line usage.
New in v2.5.0 - allows you to enable/disable IDs being embedded in HTML comments. The script can read IDs whether or not they are in a HTML comment.
New in v2.5.0 - allows you to have regex mode on by default.
New in v2.7.0 - Anki Path and Anki Profile. If you supply both the absolute path to the Anki executable, and your profile on Anki, the script will attempt to open Anki when run if it's not already running. Useful for automation - see Technical

允许您更改脚本的默认甲板和标记。2.2.2 - 允许您启用 / 禁用 “CurlyCloze” 选项,这在完形填空格式中有所解释。V2.4.0 - 允许您启用 / 禁用脚本的 GUI - 请参阅命令行使用。V2.5.0 - 允许您启用 / 禁用嵌入在 HTML 注释中的 id。不管 id 是否在 HTML 注释中,脚本都可以读取它们。新的 v2.5.0 - 允许你在默认情况下使用正则表达式。V2.7.0 中的新版本 - Anki Path 和 Anki Profile。如果你同时提供了 Anki 可执行文件的绝对路径和 Anki 上的配置文件,脚本将尝试在运行时打开 Anki,如果它还没有运行。有用的自动化 - 参见技术

Cloze note types 完形填空注释类型

New in v2.8.0 - allows you to indicate whether or not a note type should be interpreted as a 'Cloze' type. You must set this to 'True' if you wish to use the 'CurlyCloze' option with this note type - see Cloze formatting.

V2.8.0 中的 New 允许您指出一个注释类型是否应该被解释为 “完形填空” 类型。如果您希望使用 “ CurlyCloze” 选项并使用此注释类型,则必须将其设置为“ True”——请参阅完形填空格式。

Obsidian 黑曜石

This section is new in v2.9.0.
Vault name: The name of your obsidian vault that you're adding flashcards from.
Add file link: Whether you want to append a link to the associated obsidian file on the first field of the card.

本节是 v2.9.0 中的新版本。储藏室名称: 你正在从中添加抽认卡的黑曜石储藏室的名称。添加文件链接: 是否要在卡的第一个字段上附加相关的黑曜石文件的链接。

Syntax 语法

Note that START, END, TARGET DECK, FILE TAGS and DELETE all require an exact match on the line - you cannot have spaces afterwards. As of v1.2, the Config file now allows you to change the syntax of the script:

注意,START、 END、 TARGET DECK、 FILE TAGS 和 DELETE 都需要行上的精确匹配——之后不能有空格。从 1.2 版本开始,Config 文件允许你修改脚本语法:

Field substitutions 场替换

The substitutions for field prefixes. For example, under the section ['Basic'], you'll see something like this:

字段前缀的替换。例如,在[‘ Basic’]部分,你会看到这样的代码:

Front = Front: Back = Back:

If you edit and save this to say

如果你编辑并保存这个

Front = Front: Back = A:

Then you now format your notes like this:

然后你现在把你的笔记格式化成这样:

START Basic This is a test. A: Test successful! END

As an inline note:

作为一个内嵌注释:

STARTI [Basic] This is a test. A: Test successful! ENDI

Note Type Substitutions 注释类型替换

These are under the section ['Note Substitutions']. Similar to the above, you'll see something like this:

这些都在[注释替换]一节中。类似于上面的内容,你会看到这样的东西:

... Basic = Basic Basic (and reversed card) = Basic (and reversed card) ...

If you edit and save this to say

如果你编辑并保存这个

... Basic = B Basic (and reversed card) = Basic (and reversed card) ...

Then you now format your notes like this:

然后你现在把你的笔记格式化成这样:

START B This is a test. Back: Test successful! END

As an inline note:

作为一个内嵌注释:

STARTI [B] This is a test. Back: Test successful! ENDI

Added Media 新增媒体

This section is reserved for the script to keep track of what media files it has added. You can clear this by running the script with the -m flag.

此部分为脚本保留,用于跟踪添加的媒体文件。您可以通过运行带有 - m 标志的脚本来清除这个问题。

Deck formatting 甲板格式

Anywhere within the file, format the deck that you want the notes to go into as follows:

在文件中的任何地方,设置你想要记录的文件夹的格式如下:

{Target Deck Line} {Deck name}

For example, with the default settings:

例如,使用默认设置:

TARGET DECK Mathematics

You may place more than one target deck in the same file, but only the first instance will be read and used.

您可以在同一个文件中放置多个目标卡片组,但是只能读取和使用第一个实例。

Note formatting 注释格式

In the markdown file, you must format your 'block' notes as follows (see Inline notes for notes on a single line):

在 markdown 文件中,您必须将 “block” 注释格式设置为如下(参见 Inline notes 以获得单行注释) :

START {Note Type} {Note Fields} Tags: END

Markdown formatting 减价格式

Standard markdown formatting is supported. You can check test.md as an example. Card produced:

支持标准的标记格式。您可以检查 test.md 作为一个例子。卡片生成:

Math formatting 数学格式

Supports both inline mode and displayed mode:

支持内联模式和显示模式:

Inline $x = 5$

Displayed $$z = 10$$

Image formatting 图像格式化

Embedded images are supported if they are embedded using the standard markdown syntax: ![alt-text](path_to_image)

如果嵌入图像使用标准 markdown 语法: ! [ alt-text ](path to image) ,则支持嵌入图像

v2.3 - Web-hosted images are now supported! Do ![alt-text](image_url). You'll want to do'copy image address' on the image, and use that for the image url.

V2.3 - 现在支持 web 托管图像!去吧![ alt-text ](image _ url). 你需要在图片上复制图片地址,并用它作为图片的网址。

Audio formatting 音频格式

Embedded audio is supported if the following criteria are met:

如果符合以下条件,则支持嵌入式音频:

  1. The audio file is stored locally 音频文件存储在本地
  2. It is embedded using the syntax 它是使用语法嵌入的[sound:{path_to_file}]. So, for example, if the filename was 。因此,例如,如果文件名是record.wav and it was in a 而且是在Media folder, you'd write 文件夹,你会写[sound:Media/record.wav]

Tag formatting 标记格式化

For reference, the note formatting style is:

作为参考,注释格式样式如下:

START {Note Type} {Note Fields} Tags: END

Note that the Tags: line is optional - if you don't want tags, you may leave out the line.

注意 Tags: 行是可选的——如果你不想要标记,你可以省略这一行。

Tags should be formatted as such:

标签应该这样格式化:

Tags: Tag1 Tag2 Tag3

So, a space between the colon and the first tag, and a space between tags.

因此,冒号和第一个标记之间有空格,标记之间有空格。

Hence, this syntax would not work:

因此,这种语法是行不通的:

Tags:Tag1 Tag2 Tag3

File tag formatting 文件标记格式化

v1.1.1 now allows you to specify 'file tags' for a file - these tags will be added to every card in the file.

V1.1.1 现在允许您为文件指定 “文件标记”——这些标记将添加到文件中的每个卡。

To do this: Anywhere within the file, format the file tags as follows:

要做到这一点: 在文件的任何地方,格式化文件标记如下:

{File Tags Line} {Tag list}

For example, with the default settings:

例如,使用默认设置:

FILE TAGS Maths School Physics

Like with tag-line formatting, you need a space between tags - however, do not include the "Tags:" prefix.

与标记行格式一样,标记之间需要一定的空格,但是不要包含 “Tags:” 前缀。

Field formatting 字段格式化

Apart from the first field, each field must have a prefix to indicate to the program when to move on to the next field. For example:

除了第一个字段之外,每个字段都必须有一个前缀来指示程序何时移动到下一个字段。例如:

START Basic This is a test. Back: Test successful! END

Note that you must start new fields on a new line for non-inline notes.
When the script successfully adds a note, it will append an ID to the Note Data. This allows you to update existing notes by running the script again.

请注意,必须在非内联注释的新行上启动新字段。当脚本成功地添加注释时,它将向注释数据追加一个 ID。这允许您通过再次运行脚本来更新现有的注释。

Example output:

输出示例:

START Basic This is a test. Back: Test successful!

END

Deleting notes 删除笔记

The script can delete notes that it has added automatically. To do this:

该脚本可以删除自动添加的注释:

  1. Find the formatted note in your file: 在你的文件中找到格式化的注释:

START {Note Type} {Note Data}

END

  1. Change this to read: 把这个改成:

START

END

  1. If you run the script on the file, it will interpret this as "delete the note with ID {identifier}". For convenience, it will also delete the unnecessary 如果在文件上运行脚本,它将解释为 “ delete the note with ID { identifier }”。为了方便起见,它还会删除不必要的内容START END block from the file. 从文件中删除

Note that if you manually delete a note in Anki, you must remove the ID line from the text file. Otherwise, the script will throw an error.

请注意,如果您手动删除 Anki 中的注释,则必须从文本文件中删除 ID 行。否则,脚本将抛出一个错误。

See Deleting inline notes for how to do this with inline notes.

有关如何使用内联注释完成此操作,请参阅删除内联注释。

Inline note formatting 内嵌注释格式

v1.2 feature v1.2 of the script introduces inline notes - notes which are entirely on a single line. They are formatted as such:

该脚本的 v1.2 特性 v1.2 引入了完全在单行上的内嵌 notes-notes。它们的格式如下:

STARTI [{Note Type}] {Note Data} ENDI

For example

例如

STARTI [Basic] This is a test. Back: Test successful! ENDI

Unlike regular 'block' notes, you can put inline notes anywhere on a line - for example, you could have a bulletpointed list of inline notes.
Also, unlike regular 'block' notes, the script identifies the note type through the string in square brackets. Hence, note types with [ or ] in the name are not supported for inline notes.

与普通的 “块” 注释不同,你可以把内联注释放在行上的任何地方——例如,你可以有一个内联注释的公告指针列表。此外,与普通的 “块” 音符不同,脚本通过方括号中的字符串标识音符类型。因此,内联注释不支持名称中带有[或]的注释类型。

Deleting inline notes 删除行内注释

The instructions are quite similar to deleting normal notes:

这些说明与删除普通注释非常相似:

  1. Find the formatted note in your file: 在你的文件中找到格式化的注释:

STARTI [{Note Type}] {Note Data} ENDI

  1. Change this to read: 把这个改成:

STARTI ENDI

  1. If you run the script on the file, it will interpret this as "delete the note with ID {identifier}". For convenience, it will also delete the unnecessary 如果在文件上运行脚本,它将解释为 “ delete the note with ID { identifier }”。为了方便起见,它还会删除不必要的内容STARTI ENDI block from the file. 从文件中删除

Note that if you manually delete a note in Anki, you must remove the ID line from the text file. Otherwise, the script will throw an error.

请注意,如果您手动删除 Anki 中的注释,则必须从文本文件中删除 ID 行。否则,脚本将抛出一个错误。

Cloze formatting 完形格式

New in v2.8.0
In any note, you can do clozes using Anki's standard syntax:
This is a {{c1::cloze note}}
However, by enabling the 'CurlyCloze' option (see Config), you have access to many easier options:

在 v2.8.0 中的新功能在任何情况下,你都可以使用 Anki 的标准语法来完成完形填空: 这是 {c1: : cloze note}} 然而,通过启用‘ CurlyCloze’选项(参见配置) ,你可以使用许多更简单的选项:

  1. This is a {cloze} note with {two clozes} -> This is a {{c1::cloze}} note with {{c2::two clozes}}
  2. This is a {2:cloze} note with {1:id syntax} -> This is a {{c2::cloze}} note with {{c1::id syntax}}
  3. This is a {2|cloze} {3|note} with {1|alternate id syntax} -> This is a {{c2::cloze}} {{c3::note}} with {{c1::alternate id syntax}}
  4. This is a {c1:cloze} note with {c2:another} type of {c3:id syntax} -> This is a {{c1::cloze}} note with {{c2::another}} type of {{c3::id syntax}}
  5. This is a {c1|cloze} note with {c2|yet another} type of {c3|id syntax} -> This is a {{c1::cloze}} note with {{c2::yet another}} type of {{c3::id syntax}}

You can also mix and match styles! Note that clozes without an id will always be assigned an id starting from 1, increasing for each new cloze: This is a {cloze} note with {multiple} non-id clozes, as well as {2:some clozes} with {c1|other styles} -> This is a {{c1::cloze}} note with {{c2::multiple}} non-id clozes, as well as {{c2::some clozes}} with {{c1::other styles}}

你也可以混搭风格!请注意,没有 id 的 clozes 将始终被分配一个 id,从 1 开始,每个新的 cloze 都会增加一个 id: 这是一个 {cloze} 注释,带有 { multiple } non-id clozes,以及{ c1 | other styles }-> 这是一个{ c1: : cloze } 注释,带有{ c2: : multiple } non-id clozes,以及{ c1: other styles }}{{ c2: : some clozes }

Default 违约

By default, the script:

默认情况下,脚本:

Troubleshooting 故障排除

If the script itself is not able to run, try running python3 {PATH_TO_SCRIPT}.

如果脚本本身不能运行,试着运行 python3{ PATH to script }。

If you are unable to get pip to run, see this user guide.

如果您无法使 pip 运行,请参阅此用户指南。

Some people have reported issues with installing the Gooey dependency. From v2.5.0 onwards, the script can run without Gooey, falling back to a command-line interface.

一些人报告了安装 Gooey 依赖关系的问题。从 2.5.0 版本开始,脚本可以在没有 Gooey 的情况下运行,退回到 google 命令行界面。

If you are getting a KeyError, you may have typed one of the substitutions wrong - double check the config file and what you actually wrote. Examples:

如果你得到一个 KeyError,你可能输入了错误的一个替换 - 仔细检查配置文件和你实际写了什么。例子:

The script seems to have unexpected behaviour when reading from a file for the first time, while the file is open in another program (though this doesn't always happen!).
The script was written in Python 3.8.5, and it uses os module features from Python 3.6+ This issue confirms that the script does not run on Python 2.

第一次读取文件时,脚本似乎有意外的行为,而文件在另一个程序中是打开的 (尽管这种情况并不总是发生!). 这个脚本是用 Python 3.8.5 编写的,它使用了 Python 3.6 + 中的操作系统模块特性。

Technical 技术

The script doesn't need to be in the same folder as your notes - you can put it in a Scripts folder if you have the means to run it remotely. Just ensure that the config file ends up in the same folder as the script.

脚本不需要与笔记放在同一个文件夹中——如果您有办法远程运行它,可以将其放在 Scripts 文件夹中。只要确保配置文件与脚本位于同一个文件夹中即可。

You may also want to prepend the following shebang to the start of the file:

你也可以在文件的开头添加如下的 shebang:

#!/usr/bin/env python

# !/usr/bin/env python

For more information, see this pull request.

有关更多信息,请参见此拉请求。

New in v2.7.0 - the script can now automatically open Anki if Anki Path and Anki Profile are supplied. This would allow you to schedule the script to run at a certain time without needing to worry about whether Anki was open at that time.

新的 v2.7.0 - 该脚本现在可以自动打开 Anki,如果 Anki 路径和 Anki 配置文件提供。这将允许您安排脚本在特定的时间运行,而不必担心 Anki 在那个时间是否打开。

Note that the script will never close Anki by itself, so you may find Anki open when you return to your computer!

请注意,脚本永远不会关闭 Anki 本身,所以你可能会发现 Anki 打开当你回到你的电脑!

Scheduling 日程安排

On Windows, check out Task Scheduler.

在 Windows 上,查看任务计划程序。

On macOS/Linux, check out Corey Schafer's excellent tutorial on cron.

在 macOS/Linux 上,查看 Corey Schafer 关于 cron 的优秀教程。

As an example, you could schedule the script to run recursively over your top-level notes folder, at midnight every day.

例如,您可以安排脚本在每天午夜递归地在顶级备忘录文件夹上运行。 https://github.com/Pseudonium/Obsidian_to_Anki