cayennes/kanji-colorize — pull request #5 by ospalh (2012-12-09 14:05:14+00:00)
Title: Use ElementTree, add a mode that uses colors from a css sfile.

Hi.

A while ago i did fork your project and then i made a number of
changes that weren't really compatible with yours.

Now i have come back to your way for a number of these points.
You had made the points that

* My old approach didn't work on AnkiWeb/AnkiMobile/AnkiDroid
* For my approach, the colors were hard-coded and there should be an
easy way for people to specify their own colors
* There wasn't really much of a point to put all of the data into one
file.

also
* You added argarse argument parsing and i added Optionparser

For those four point i have now come over to your point of view.
(But i will personally keep on using my "Kanji stroke color" add-on
that still doesn't work on AnkiWeb and needs a patched AnkiDroid.)

I did made some other changes, though:

* I switched over from treating the svgs as text to treating them more
abstractly as an element tree using xml.etree.ElementTree. This
is admittedly a bit slower, but it should be harder to produce
invalid output. (This somehow also swallows the copyright notice,
which i add back by hand.)
* The ElementTree approach makes it easy to add xml namespaces to
everything, which makes some of the changes below work.
* I have removed the size argument, instead the size is set to "100%"
in the svgs. Instead the size is now set by the add-on, in the `` tag.
* Instead of using `` tags, i changed the add-on to use ``
with width and height set. This is where the size option in the anki
add-on goes to. And this only works with the namespace set (i
think). My new color setting scheme doesn't work with img, but the
colors in the files work with embed. Setting the size here also
makes it easier to change it later.
* I've also changed it so that lower case letters get names like
a_.svg. So diagrams for "a" and "A" should both be available. Also,
non-alphanumeric characters keep their code file names. (There isn't
a diagram for "/" but making that work was the idea.)
* I've also changed the way the data is loaded when you do a
bulk convert, only loading the ElementTrees one at a time. I thought
that would give you a performance advantage, but it looks as if it
doesn't. At least not too much.
* I have also added a (somewhat crude) argument parsing from a dict,
mostly for aesthetic reasons in the add-on code.
* The big change i was interested in: a way to use colors from a
list. What i call `--mode css`. This now uses a style sheet that is
copied to the media folder, `_kanji_style.css`. That loading of the
style sheet works only with the embed, not with img tags, too. Like
this the colors can be changed after the svgs have been
added. (Admittedly not that easily without an external css editor
with some sort of color picker.) There is also a
JavaScript/ecmascript file loaded, but by default this does
nothing. The idea was to add a colored shadow by kanji group, but
the processing somehow doesn't work on Macs (not sure about
Windows). See `_kanji_script.js` and `_kanji_script_shadow.js`
* Some other clean up/changes:
* Looking at the Anki code, there is no need to call
`mw.col.media.addFile()` when you do the copying yourself
* A few things where `flake8`, a PEP8 checker, or my emacs
complained.
* The code now uses the standard exceptions IOError, TypeError,
ValueError
* These changes brake the doctests. I don't really know how those
work. Sorry.