Skip to content

Commit

Permalink
fixed switching note types in conjunction with multiple src/dest fiel…
Browse files Browse the repository at this point in the history
…d names, increased compatibility to Fluent Forever note types, some more kanji->hanzi renaming
  • Loading branch information
Connum committed May 27, 2020
1 parent 03a6e1c commit 4944ee8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
2 changes: 1 addition & 1 deletion anki/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import kanji_colorizer
from . import hanzi_colorizer
6 changes: 3 additions & 3 deletions anki/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"value": 0.75,
"image-size": 327,
"group-mode": false,
"model":["chinese", "mandarin"],
"src-field":["Hanzi", "Character", "Word (in Kanji/Hanzi)"],
"dst-field":["Diagram", "Stroke Order", "Stroke Order Diagram 1"]}
"model":["chinese", "mandarin","hanzi"],
"src-field":["Hanzi", "Character", "Word (in Kanji/Hanzi)", "Word (in Hanzi)"],
"dst-field":["Diagram", "Stroke Order", "Stroke Order Diagram 1", "Stroke order diagram (if you'd like to test stroke order)", "Stroke Order Diagram 1 (And component parts/mnemonics for meaning)"]}
51 changes: 33 additions & 18 deletions anki/hanzi_colorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@
config += " --image-size "
config += str(addon_config["image-size"])

modelNameSubstring = ['chinese','mandarin']
srcField = ['Hanzi', 'Word (in Kanji/Hanzi)']
dstField = ['Diagram', 'Stroke Order Diagram 1']
modelNameSubstring = ['chinese','mandarin','hanzi']
srcField = ['Hanzi', 'Word (in Kanji/Hanzi)', 'Word (in Hanzi)']
dstField = ['Diagram', 'Stroke Order Diagram 1', 'Stroke order diagram (if you\'d like to test stroke order)', 'Stroke Order Diagram 1 (And component parts/mnemonics for meaning)']

# avoid errors due to invalid config
if 'model' in addon_config and type(addon_config['model']) is str:
modelNameSubstring = [addon_config['model'].lower()]
if 'model' in addon_config and isinstance(addon_config['model']), list):
if 'model' in addon_config and isinstance(addon_config['model'], list):
modelNameSubstring = addon_config['model']
if 'src-field' in addon_config and type(addon_config['src-field']) is str:
srcField = [addon_config['src-field']]
Expand All @@ -95,15 +95,14 @@ def modelIsCorrectType(model):
model_name = model['name'].lower()
fields = mw.col.models.fieldNames(model)

isValidModel = False
isValidModel = False
hasValidSrcField = False
hasValidDstField = False
if isinstance(modelNameSubstring, list):
for f in modelNameSubstring:
if f in model_name:
if f.lower() in model_name:
isValidModel = True
modelNameSubstring = f
break
else:
hasValidSrcField = modelNameSubstring in model_name
Expand All @@ -112,7 +111,6 @@ def modelIsCorrectType(model):
for f in srcField:
if f in fields:
hasValidSrcField = True
srcField = f
break
else:
hasValidSrcField = srcField in fields
Expand All @@ -121,19 +119,16 @@ def modelIsCorrectType(model):
for f in dstField:
if f in fields:
hasValidDstField = True
dstField = f
break
else:
hasValidDstField = dstField in fields

return ('chinese' in model_name or 'mandarin' in model_name) and
hasValidSrcField and
hasValidDstField)
return (isValidModel and hasValidSrcField and hasValidDstField)


def characters_to_colorize(s):
'''
Given a string, returns a lost of characters to colorize
Given a string, returns a list of characters to colorize
If the string consists of only a single character, returns a list
containing that character. If it is longer, returns a list of only the
Expand All @@ -151,15 +146,35 @@ def addKanji(note, flag=False, currentFieldIndex=None):
'''
if not modelIsCorrectType(note.model()):
return flag

fields = mw.col.models.fieldNames(note.model())

if isinstance(srcField, list):
for f in srcField:
if f in fields:
hasValidSrcField = True
useSrcField = f
break
else:
useSrcField = srcField

if isinstance(dstField, list):
for f in dstField:
if f in fields:
hasValidDstField = True
useDstField = f
break
else:
useDstField = dstField

if currentFieldIndex != None: # We've left a field
# But it isn't the relevant one
if note.model()['flds'][currentFieldIndex]['name'] != srcField:
if note.model()['flds'][currentFieldIndex]['name'] != useSrcField:
return flag

srcTxt = mw.col.media.strip(note[srcField])
srcTxt = mw.col.media.strip(note[useSrcField])

oldDst = note[dstField]
oldDst = note[useDstField]
dst=''

for character in characters_to_colorize(str(srcTxt)):
Expand All @@ -174,7 +189,7 @@ def addKanji(note, flag=False, currentFieldIndex=None):
dst += '<img src="{!s}">'.format(anki_fname)

if dst != oldDst and dst != '':
note[dstField] = dst
note[useDstField] = dst
note.flush()
return True

Expand Down
7 changes: 4 additions & 3 deletions kanji_colorize.py → hanzi_colorize.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

# kanjicolorizer.py is part of kanji-colorize which makes KanjiVG data
# kanji_colorize.py is part of hanzi-colorize which makes HanziVG data
# into colored stroke order diagrams
#
# Copyright 2012 Cayenne Boyer
# Copyright 2018 hanzi-colorize repository contributors
# based on kanji-colorize, Copyright 2012 Cayenne Boyer
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand All @@ -20,7 +21,7 @@
# License along with this program. If not, see
# <http://www.gnu.org/licenses/>.

from kanjicolorizer.colorizer import KanjiColorizer
from hanzicolorizer.colorizer import KanjiColorizer

if __name__ == "__main__":
kc = KanjiColorizer()
Expand Down
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[pytest]
addopts = --doctest-modules kanjicolorizer
addopts = --doctest-modules hanzicolorizer
norecursedirs = data

0 comments on commit 4944ee8

Please sign in to comment.