From 48a803f8f3efa761b686f956aec03583ddd605c4 Mon Sep 17 00:00:00 2001 From: ouczb Date: Thu, 17 Aug 2023 08:52:54 +0800 Subject: [PATCH] anki web & md --- addons21/fastmd/md/音标.md | 487 +++++++++++++++++++++ addons21/fastmd/src/anki.ipynb | 150 +++++++ addons21/fastmd/src/md/__init__.py | 3 + addons21/fastmd/src/md/anki_renderer.py | 173 ++++++++ addons21/fastmd/src/md/anki_tree.py | 45 ++ addons21/fastmd/src/service/__init__.py | 3 + addons21/fastmd/src/service/anki_client.py | 23 + addons21/fastmd/src/service/http.py | 35 ++ addons21/fastmd/src/音标.ipynb | 89 ++++ 9 files changed, 1008 insertions(+) create mode 100644 addons21/fastmd/md/音标.md create mode 100644 addons21/fastmd/src/anki.ipynb create mode 100644 addons21/fastmd/src/md/__init__.py create mode 100644 addons21/fastmd/src/md/anki_renderer.py create mode 100644 addons21/fastmd/src/md/anki_tree.py create mode 100644 addons21/fastmd/src/service/__init__.py create mode 100644 addons21/fastmd/src/service/anki_client.py create mode 100644 addons21/fastmd/src/service/http.py create mode 100644 addons21/fastmd/src/音标.ipynb diff --git a/addons21/fastmd/md/音标.md b/addons21/fastmd/md/音标.md new file mode 100644 index 0000000..aad50f7 --- /dev/null +++ b/addons21/fastmd/md/音标.md @@ -0,0 +1,487 @@ +# 单元音 +## [ɑ:] +### tags +- 长元音 +### imgs +- ![ɑ:](https://res.weread.qq.com/wrepub/epub_38381477_9) +- ![ɑ:](https://res.weread.qq.com/wrepub/epub_38381477_14) +### words +- car +- glass + +## [ʌ] +### tags +- 短元音 +### imgs +- ![ʌ](https://res.weread.qq.com/wrepub/epub_38381477_15) +- ![ʌ](https://res.weread.qq.com/wrepub/epub_38381477_20) +### words +- bus +- mother + +## [ɔ:] +### tags +- 长元音 +### imgs +- ![ɔ:](https://res.weread.qq.com/wrepub/epub_38381477_21) +- ![ɔ:](https://res.weread.qq.com/wrepub/epub_38381477_26) +### words +- ball +- horse + +## [ɒ] +### tags +- 短元音 +### imgs +- ![ɒ](https://res.weread.qq.com/wrepub/epub_38381477_27) +- ![ɒ](https://res.weread.qq.com/wrepub/epub_38381477_32) +### words +- dog +- wash + +## [ɜ:] +### tags +- 长元音 +### imgs +- ![ɜ:](https://res.weread.qq.com/wrepub/epub_38381477_33) +- ![ɜ:](https://res.weread.qq.com/wrepub/epub_38381477_38) +### words +- bird +- nurse + +## [ə] +### tags +- 短元音 +### imgs +- ![ə](https://res.weread.qq.com/wrepub/epub_38381477_39) +- ![ə](https://res.weread.qq.com/wrepub/epub_38381477_44) +### words +- tiger +- sofa +## [i:] +### tags +- 长元音 +### imgs +- ![i:](https://res.weread.qq.com/wrepub/epub_38381477_45) +- ![i:](https://res.weread.qq.com/wrepub/epub_38381477_50) +### words +- bee +- sea + +## [ɪ] +### tags +- 短元音 +### imgs +- ![ɪ](https://res.weread.qq.com/wrepub/epub_38381477_51) +- ![ɪ](https://res.weread.qq.com/wrepub/epub_38381477_56) +### words +- pig +- kid + +## [u:] +### tags +- 长元音 +### imgs +- ![u:](https://res.weread.qq.com/wrepub/epub_38381477_57) +- ![u:](https://res.weread.qq.com/wrepub/epub_38381477_62) +### words +- school +- shoe + +## [ʊ] +### tags +- 短元音 +### imgs +- ![ʊ](https://res.weread.qq.com/wrepub/epub_38381477_63) +- ![ʊ](https://res.weread.qq.com/wrepub/epub_38381477_68) +### words +- book +- foot + +## [æ] +### tags +- 短元音 +### imgs +- ![æ](https://res.weread.qq.com/wrepub/epub_38381477_69) +- ![æ](https://res.weread.qq.com/wrepub/epub_38381477_74) +### words +- cat +- bag + +## [e] +### tags +- 短元音 +### imgs +- ![e](https://res.weread.qq.com/wrepub/epub_38381477_75) +- ![e](https://res.weread.qq.com/wrepub/epub_38381477_80) +### words +- bed +- head + +# 双元音 +## [aɪ] +### tags +- [ɪ]结尾 +### imgs +- ![aɪ](https://res.weread.qq.com/wrepub/epub_38381477_95) +- ![aɪ](https://res.weread.qq.com/wrepub/epub_38381477_96) +- ![aɪ](https://res.weread.qq.com/wrepub/epub_38381477_101) +### words +- eye +- rice +## [eɪ] +### tags +- [ɪ]结尾 +### imgs +- ![eɪ](https://res.weread.qq.com/wrepub/epub_38381477_102) +- ![eɪ](https://res.weread.qq.com/wrepub/epub_38381477_103) +- ![eɪ](https://res.weread.qq.com/wrepub/epub_38381477_108) +### words +- cake +- rain +## [ɔɪ] +### tags +- [ɪ]结尾 +### imgs +- ![ɔɪ](https://res.weread.qq.com/wrepub/epub_38381477_109) +- ![ɔɪ](https://res.weread.qq.com/wrepub/epub_38381477_110) +- ![ɔɪ](https://res.weread.qq.com/wrepub/epub_38381477_115) +### words +- boy +- toy +## [ɪə] +### tags +- [ə]结尾 +### imgs +- ![ɪə](https://res.weread.qq.com/wrepub/epub_38381477_116) +- ![ɪə](https://res.weread.qq.com/wrepub/epub_38381477_117) +- ![ɪə](https://res.weread.qq.com/wrepub/epub_38381477_122) +### words +- ear +- hear +## [eə] +### tags +- [ə]结尾 +### imgs +- ![eə](https://res.weread.qq.com/wrepub/epub_38381477_123) +- ![eə](https://res.weread.qq.com/wrepub/epub_38381477_124) +- ![eə](https://res.weread.qq.com/wrepub/epub_38381477_129) +### words +- pear +- bear +## [ʊə] +### tags +- [ə]结尾 +### imgs +- ![ʊə](https://res.weread.qq.com/wrepub/epub_38381477_130) +- ![ʊə](https://res.weread.qq.com/wrepub/epub_38381477_131) +- ![ʊə](https://res.weread.qq.com/wrepub/epub_38381477_136) +### words +- tour +- poor +## [aʊ] +### tags +- [ʊ]结尾 +### imgs +- ![aʊ](https://res.weread.qq.com/wrepub/epub_38381477_137) +- ![aʊ](https://res.weread.qq.com/wrepub/epub_38381477_138) +- ![aʊ](https://res.weread.qq.com/wrepub/epub_38381477_143) +### words +- cow +- house +## [əʊ] +### tags +- [ʊ]结尾 +### imgs +- ![əʊ](https://res.weread.qq.com/wrepub/epub_38381477_144) +- ![əʊ](https://res.weread.qq.com/wrepub/epub_38381477_145) +- ![əʊ](https://res.weread.qq.com/wrepub/epub_38381477_150) +### words +- coat +- snow + +# 爆破音 +## [p] +### tags +- 清辅音 +### imgs +- ![p](https://res.weread.qq.com/wrepub/epub_38381477_160) +- ![p](https://res.weread.qq.com/wrepub/epub_38381477_165) +### words +- park +- sleep + +## [b] +### tags +- 浊辅音 +### imgs +- ![b](https://res.weread.qq.com/wrepub/epub_38381477_166) +- ![b](https://res.weread.qq.com/wrepub/epub_38381477_171) +### words +- baby +- rubber + +## [t] +### tags +- 清辅音 +### imgs +- ![t](https://res.weread.qq.com/wrepub/epub_38381477_172) +- ![t](https://res.weread.qq.com/wrepub/epub_38381477_177) +### words +- tea +- boat + +## [d] +### tags +- 浊辅音 +### imgs +- ![d](https://res.weread.qq.com/wrepub/epub_38381477_178) +- ![d](https://res.weread.qq.com/wrepub/epub_38381477_183) +### words +- door +- red + +## [k] +### tags +- 清辅音 +### imgs +- ![k](https://res.weread.qq.com/wrepub/epub_38381477_184) +- ![k](https://res.weread.qq.com/wrepub/epub_38381477_189) +### words +- cap +- milk + +## [g] +### tags +- 浊辅音 +### imgs +- ![g](https://res.weread.qq.com/wrepub/epub_38381477_190) +- ![g](https://res.weread.qq.com/wrepub/epub_38381477_195) +### words +- go +- egg + +# 摩擦音 +## [f] +### tags +- 清辅音 +### imgs +- ![f](https://res.weread.qq.com/wrepub/epub_38381477_203) +- ![f](https://res.weread.qq.com/wrepub/epub_38381477_208) +### words +- fish +- phone + +## [v] +### tags +- 浊辅音 +### imgs +- ![v](https://res.weread.qq.com/wrepub/epub_38381477_209) +- ![v](https://res.weread.qq.com/wrepub/epub_38381477_214) +### words +- river +- five + +## [s] +### tags +- 清辅音 +### imgs +- ![s](https://res.weread.qq.com/wrepub/epub_38381477_215) +- ![s](https://res.weread.qq.com/wrepub/epub_38381477_220) +### words +- sun +- ice + +## [z] +### tags +- 浊辅音 +### imgs +- ![z](https://res.weread.qq.com/wrepub/epub_38381477_221) +- ![z](https://res.weread.qq.com/wrepub/epub_38381477_226) +### words +- zoo +- nose + +## [ʃ] +### tags +- 清辅音 +### imgs +- ![ʃ](https://res.weread.qq.com/wrepub/epub_38381477_227) +- ![ʃ](https://res.weread.qq.com/wrepub/epub_38381477_232) +### words +- sheep +- shirt + +## [ʒ] +### tags +- 浊辅音 +### imgs +- ![ʒ](https://res.weread.qq.com/wrepub/epub_38381477_233) +- ![ʒ](https://res.weread.qq.com/wrepub/epub_38381477_238) +### words +- television +- measure + +## [θ] +### tags +- 清辅音 +### imgs +- ![θ](https://res.weread.qq.com/wrepub/epub_38381477_239) +- ![θ](https://res.weread.qq.com/wrepub/epub_38381477_244) +### words +- tooth +- three + +## [ð] +### tags +- 浊辅音 +### imgs +- ![ð](https://res.weread.qq.com/wrepub/epub_38381477_245) +- ![ð](https://res.weread.qq.com/wrepub/epub_38381477_250) +### words +- they +- father + +## [h] +### tags +- 清辅音 +### imgs +- ![h](https://res.weread.qq.com/wrepub/epub_38381477_251) +- ![h](https://res.weread.qq.com/wrepub/epub_38381477_256) +### words +- hair +- who + +## [r] +### tags +- 浊辅音 +### imgs +- ![r](https://res.weread.qq.com/wrepub/epub_38381477_257) +- ![r](https://res.weread.qq.com/wrepub/epub_38381477_262) +### words +- rose +- write + +# 破擦音 +## [tʃ] +### tags +- 清辅音 +### imgs +- ![tʃ](https://res.weread.qq.com/wrepub/epub_38381477_274) +- ![tʃ](https://res.weread.qq.com/wrepub/epub_38381477_279) +### words +- chair +- watch + +## [dʒ] +### tags +- 浊辅音 +### imgs +- ![dʒ](https://res.weread.qq.com/wrepub/epub_38381477_280) +- ![dʒ](https://res.weread.qq.com/wrepub/epub_38381477_285) +### words +- jeep +- cage + +## [tr] +### tags +- 清辅音 +### imgs +- ![tr](https://res.weread.qq.com/wrepub/epub_38381477_286) +- ![tr](https://res.weread.qq.com/wrepub/epub_38381477_291) +### words +- tree +- train + +## [dr] +### tags +- 浊辅音 +### imgs +- ![dr](https://res.weread.qq.com/wrepub/epub_38381477_292) +- ![dr](https://res.weread.qq.com/wrepub/epub_38381477_297) +### words +- dress +- draw + +## [ts] +### tags +- 清辅音 +### imgs +- ![ts](https://res.weread.qq.com/wrepub/epub_38381477_298) +- ![ts](https://res.weread.qq.com/wrepub/epub_38381477_303) +### words +- chair +- warch + +## [tz] +### tags +- 浊辅音 +### imgs +- ![tz](https://res.weread.qq.com/wrepub/epub_38381477_304) +- ![tz](https://res.weread.qq.com/wrepub/epub_38381477_309) +### words +- hands +- cards + +# 发鼻音 +## [m] +### tags +- 浊辅音 +### imgs +- ![m](https://res.weread.qq.com/wrepub/epub_38381477_317) +- ![m](https://res.weread.qq.com/wrepub/epub_38381477_322) +### words +- map +- summer + +## [n] +### tags +- 浊辅音 +### imgs +- ![n](https://res.weread.qq.com/wrepub/epub_38381477_323) +- ![n](https://res.weread.qq.com/wrepub/epub_38381477_328) +### words +- night +- pen + +## [ŋ] +### tags +- 浊辅音 +### imgs +- ![ŋ](https://res.weread.qq.com/wrepub/epub_38381477_329) +- ![ŋ](https://res.weread.qq.com/wrepub/epub_38381477_334) +### words +- bank +- sing + +# 舌侧音 +## [I] +### tags +- 浊辅音 +### imgs +- ![I](https://res.weread.qq.com/wrepub/epub_38381477_335) +- ![I](https://res.weread.qq.com/wrepub/epub_38381477_340) +### words +- leg +- table + +# 半元音 +## [w] +### tags +- 浊辅音 +### imgs +- ![w](https://res.weread.qq.com/wrepub/epub_38381477_342) +- ![w](https://res.weread.qq.com/wrepub/epub_38381477_347) +### words +- water +- white + +## [j] +### tags +- 浊辅音 +### imgs +- ![j](https://res.weread.qq.com/wrepub/epub_38381477_348) +- ![j](https://res.weread.qq.com/wrepub/epub_38381477_353) +### words +- yes +- yellow \ No newline at end of file diff --git a/addons21/fastmd/src/anki.ipynb b/addons21/fastmd/src/anki.ipynb new file mode 100644 index 0000000..382dcc2 --- /dev/null +++ b/addons21/fastmd/src/anki.ipynb @@ -0,0 +1,150 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "cf4f95a8", + "metadata": {}, + "outputs": [], + "source": [ + "from service import *" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "d77ecb1b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a filename was not supplied (nor was the CookieJar instance initialised with one) failed load cookie_file\n" + ] + }, + { + "data": { + "text/plain": [ + "{'result': ['Git', '句素', '示例牌组', '系统默认', '英语', '音标'], 'error': None}" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = Session()\n", + "anki = AnkiClient(s)\n", + "anki.make_version()\n", + "anki.deckNames()" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "id": "d13272f8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'result': ['Basic', '单词卡', '句素卡', '选择卡', '音标'], 'error': None}" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "anki.modelNames()" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "id": "b118fa48", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'result': ['word',\n", + " 'pronounce',\n", + " 'audio',\n", + " 'eg.',\n", + " 'Header',\n", + " 'sentences',\n", + " 'synonym',\n", + " 'part_of_speech',\n", + " 'meanEng',\n", + " 'meanEngLong',\n", + " 'picture'],\n", + " 'error': None}" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "anki.modelFieldNames(\"单词卡\")" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "id": "3a15fe88", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'result': 1692201363325, 'error': None}" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "note = {\n", + " \"word\":'[ʌ]'\n", + "}\n", + "param = {\n", + " \"deckName\": \"音标\",\n", + " \"modelName\": \"单词卡\",\n", + " \"options\": { \"allowDuplicate\": False },\n", + " \"fields\": note,\n", + " \"tags\": ['单元音', '短元音']\n", + "}\n", + "anki.addNote(param)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/addons21/fastmd/src/md/__init__.py b/addons21/fastmd/src/md/__init__.py new file mode 100644 index 0000000..7142943 --- /dev/null +++ b/addons21/fastmd/src/md/__init__.py @@ -0,0 +1,3 @@ +from .anki_tree import * +from .anki_renderer import * +__all__ = ['AnkiRenderer'] \ No newline at end of file diff --git a/addons21/fastmd/src/md/anki_renderer.py b/addons21/fastmd/src/md/anki_renderer.py new file mode 100644 index 0000000..f5db10e --- /dev/null +++ b/addons21/fastmd/src/md/anki_renderer.py @@ -0,0 +1,173 @@ +from typing import Dict, Any +from mistune import BaseRenderer, BlockState +from .anki_tree import AnkiData +# 自定义渲染器来提取标题 +class AnkiRenderer(BaseRenderer): + """A renderer to re-format Markdown text.""" + NAME = 'anki' + def __init__(self): + BaseRenderer.__init__(self) + self.anki = AnkiData() + def __call__(self, tokens, state: BlockState): + self.tokens = tokens + self.state = state + out = self.render_tokens(tokens, state) + # special handle for line breaks + out += '\n\n'.join(self.render_referrences(state)) + '\n' + return out + + def render_referrences(self, state: BlockState): + ref_links = state.env['ref_links'] + for key in ref_links: + attrs = ref_links[key] + text = '[' + attrs['label'] + ']: ' + attrs['url'] + title = attrs.get('title') + if title: + text += ' "' + title + '"' + yield text + + def render_children(self, token, state: BlockState): + children = token['children'] + return self.render_tokens(children, state) + + def text(self, token: Dict[str, Any], state: BlockState) -> str: + return token['raw'] + + def emphasis(self, token: Dict[str, Any], state: BlockState) -> str: + return '*' + self.render_children(token, state) + '*' + + def strong(self, token: Dict[str, Any], state: BlockState) -> str: + return '**' + self.render_children(token, state) + '**' + + def link(self, token: Dict[str, Any], state: BlockState) -> str: + label = token.get('label') + text = self.render_children(token, state) + out = '[' + text + ']' + if label: + return out + '[' + label + ']' + + attrs = token['attrs'] + url = attrs['url'] + title = attrs.get('title') + if text == url and not title: + return '<' + text + '>' + elif 'mailto:' + text == url and not title: + return '<' + text + '>' + + out += '(' + if '(' in url or ')' in url: + out += '<' + url + '>' + else: + out += url + if title: + out += ' "' + title + '"' + return out + ')' + + def image(self, token: Dict[str, Any], state: BlockState) -> str: + return '!' + self.link(token, state) + + def codespan(self, token: Dict[str, Any], state: BlockState) -> str: + return '`' + token['raw'] + '`' + + def linebreak(self, token: Dict[str, Any], state: BlockState) -> str: + return ' \n' + + def softbreak(self, token: Dict[str, Any], state: BlockState) -> str: + return '\n' + + def blank_line(self, token: Dict[str, Any], state: BlockState) -> str: + return '' + + def inline_html(self, token: Dict[str, Any], state: BlockState) -> str: + return token['raw'] + + def paragraph(self, token: Dict[str, Any], state: BlockState) -> str: + text = self.render_children(token, state) + return text + '\n\n' + + def heading(self, token: Dict[str, Any], state: BlockState) -> str: + level = token['attrs']['level'] + marker = '#' * level + text = self.render_children(token, state) + self.anki.enterScope(text , level) + return marker + ' ' + text + '\n\n' + + def thematic_break(self, token: Dict[str, Any], state: BlockState) -> str: + return '***\n\n' + + def block_text(self, token: Dict[str, Any], state: BlockState) -> str: + return self.render_children(token, state) + '\n' + + def block_code(self, token: Dict[str, Any], state: BlockState) -> str: + attrs = token.get('attrs', {}) + info = attrs.get('info', '') + code = token['raw'] + if code and code[-1] != '\n': + code += '\n' + + marker = token.get('marker') + if not marker: + marker = _get_fenced_marker(code) + return marker + info + '\n' + code + marker + '\n\n' + + def block_quote(self, token: Dict[str, Any], state: BlockState) -> str: + text = indent(self.render_children(token, state), '> ') + return text + '\n\n' + + def block_html(self, token: Dict[str, Any], state: BlockState) -> str: + return token['raw'] + '\n\n' + + def block_error(self, token: Dict[str, Any], state: BlockState) -> str: + return '' + def _render_list_item(self, renderer, parent, item, state): + leading = parent['leading'] + text = '' + for tok in item['children']: + if tok['type'] == 'list': + tok['parent'] = parent + elif tok['type'] == 'blank_line': + continue + text += renderer.render_token(tok, state) + + lines = text.splitlines() + text = (lines[0] if lines else '') + '\n' + self.anki.appendAttr(lines[0] if lines else '') + prefix = ' ' * len(leading) + for line in lines[1:]: + if line: + text += prefix + line + '\n' + else: + text += '\n' + return leading + text + def _render_ordered_list(self, renderer, token, state): + attrs = token['attrs'] + start = attrs.get('start', 1) + for item in token['children']: + leading = str(start) + token['bullet'] + ' ' + parent = { + 'leading': leading, + 'tight': token['tight'], + } + yield self._render_list_item(renderer, parent, item, state) + start += 1 + def _render_unordered_list(self, renderer, token, state): + parent = { + 'leading': token['bullet'] + ' ', + 'tight': token['tight'], + } + for item in token['children']: + yield self._render_list_item(renderer, parent, item, state) + def list(self, token: Dict[str, Any], state: BlockState) -> str: + attrs = token['attrs'] + if attrs['ordered']: + children = self._render_ordered_list(renderer, token, state) + else: + children = self._render_unordered_list(renderer, token, state) + + text = ''.join(children) + parent = token.get('parent') + if parent: + if parent['tight']: + return text + return text + '\n' + return text + '\n' \ No newline at end of file diff --git a/addons21/fastmd/src/md/anki_tree.py b/addons21/fastmd/src/md/anki_tree.py new file mode 100644 index 0000000..72164a2 --- /dev/null +++ b/addons21/fastmd/src/md/anki_tree.py @@ -0,0 +1,45 @@ +class AnkiTree: + def __init__(self, name, level, parent = None): + self.childList = [] + self.attrList = [] + self.name = name + self.level = level + self.parent = parent + pass + def push(self, name, level): + child = AnkiTree(name, level, self) + self.childList.append(child) + return child + def appendAttr(self, attr): + self.attrList.append(attr) +class AnkiData: + def __init__(self): + self.tree = AnkiTree("root", 0) + self.node = self.tree + pass + def parse(self, tree, parent): + tags = [] + tags.append(parent.name) + note = {"name":tree.name , "tags":tags} + for child in tree.childList: + if child.name not in note: + note[child.name] = [] + for attr in child.attrList: + note[child.name].append(attr) + return note + def __call__(self): + tree = self.tree + note_list = [] + for child in tree.childList: + for child2 in child.childList: + note = self.parse(child2, child) + note_list.append(note) + return note_list + def enterScope(self, name, level): + while self.node: + if self.node.level < level: + self.node = self.node.push(name, level) + return + self.node = self.node.parent + def appendAttr(self, attr): + self.node.appendAttr(attr) \ No newline at end of file diff --git a/addons21/fastmd/src/service/__init__.py b/addons21/fastmd/src/service/__init__.py new file mode 100644 index 0000000..acc89d9 --- /dev/null +++ b/addons21/fastmd/src/service/__init__.py @@ -0,0 +1,3 @@ +from .http import * +from .anki_client import * +__all__ = ['AnkiClient', 'Session', 'Http'] \ No newline at end of file diff --git a/addons21/fastmd/src/service/anki_client.py b/addons21/fastmd/src/service/anki_client.py new file mode 100644 index 0000000..f5dc53f --- /dev/null +++ b/addons21/fastmd/src/service/anki_client.py @@ -0,0 +1,23 @@ +from .http import * +class AnkiClient(Http): + def __init__(self, session): + Http.__init__(self, session) + self.url = 'http://127.0.0.1:8765' + self.version = None + def api(self, data): + if self.version: + data["version"] = self.version + data = json.dumps(data) + res = self._post(self.url, data) + return json.loads(res) + def make_version(self): + self.version = self.api({"action":"version"}) + return self.version + def deckNames(self): + return self.api({"action":"deckNames"}) + def modelNames(self): + return self.api({"action":"modelNames"}) + def modelFieldNames(self, modelName): + return self.api({"action":"modelFieldNames", "params" :{"modelName" : modelName}}) + def addNote(self, note): + return self.api({"action":"addNote", "params" : {"note": note}}) \ No newline at end of file diff --git a/addons21/fastmd/src/service/http.py b/addons21/fastmd/src/service/http.py new file mode 100644 index 0000000..0bc8379 --- /dev/null +++ b/addons21/fastmd/src/service/http.py @@ -0,0 +1,35 @@ +import json, requests +import http.cookiejar as cookielib +#from utils import singleton +class Session(requests.Session): + def __init__(self,headers={},cookie_file=None): + requests.Session.__init__(self) + self.cookies = cookielib.LWPCookieJar(filename=cookie_file) + try:self.cookies.load(ignore_discard=True) + except Exception as e:print(e,"failed load cookie_file") + self.headers=headers + self.auth = ('user', 'pass') + self._type='str' + def save(self): + self.cookies.save() +class Http(): + def __init__(self , session): + self.session = session + def _get(self, url, data = None): + r = self.session.get(url, params = data) + self._error(r.status_code,url) + return self._res_data(r,self.session._type) + + def _post(self, url, data): + r = self.session.post(url, data = data) + self._error(r.status_code, url) + return self._res_data(r, self.session._type) + + def _error(self, code, url): + if code==200:return + elif code==403:raise RuntimeError('403 - Forbidden '+url) + elif code==404:raise RuntimeError('404 - Not Found '+url) + + def _res_data(self, res, _type): + if _type=='json':return json.loads(res.content) + return res.content \ No newline at end of file diff --git a/addons21/fastmd/src/音标.ipynb b/addons21/fastmd/src/音标.ipynb new file mode 100644 index 0000000..5d88d29 --- /dev/null +++ b/addons21/fastmd/src/音标.ipynb @@ -0,0 +1,89 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "d9ed21dd", + "metadata": {}, + "outputs": [], + "source": [ + "import mistune\n", + "from md import *\n", + "with open(\"./md/音标.md\",\"r\", encoding='utf-8')as f:\n", + " data = f.read()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "c39beca4", + "metadata": {}, + "outputs": [], + "source": [ + "# 创建解析器并解析Markdown\n", + "renderer = AnkiRenderer()\n", + "md = mistune.create_markdown(renderer=renderer)\n", + "text = md(data)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "dc4eff8d", + "metadata": {}, + "outputs": [], + "source": [ + "note_list = renderer.anki()\n", + "import json\n", + "with open('data.json', 'w', encoding=\"utf8\") as f:\n", + " json.dump(note_list, f)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "d75276bc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'name': '[ʌ]',\n", + " 'tags': ['单元音', '短元音'],\n", + " 'imgs': ['![ʌ](https://res.weread.qq.com/wrepub/epub_38381477_15)',\n", + " '![ʌ](https://res.weread.qq.com/wrepub/epub_38381477_20)'],\n", + " 'words': ['bus', 'mother']}" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "note_list[1]" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}