Skip to content

Commit

Permalink
Merge branch 'release/0.1.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
lb803 committed Feb 24, 2021
2 parents 8d77059 + b81a83f commit 63d08fc
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 31 deletions.
4 changes: 2 additions & 2 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

class AccessAidePlugin(EditBookToolPlugin):
name = 'Access Aide'
version = (0, 1, 7)
version = (0, 1, 8)
author = 'Luca Baffa'
supported_platforms = ['windows', 'osx', 'linux']
description = 'Access Aide plugin for Calibre'
description = 'Enhance accessibility features in EPUB files.'
minimum_calibre_version = (1, 46, 0)

actual_plugin = 'calibre_plugins.access_aide.ui:AccessAidePlugin'
Expand Down
5 changes: 1 addition & 4 deletions assets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ applies to this HTML fragment:
</section>
```

## Advanced notes
Epub types and aria roles have a 1:1 relationship. However, these proprieties might apply to multiple HTML tags, hence the need to provide the 'tag' key with a list of 'value(s)'.

# `extra-tags.json` file
This files contains a list of additional HTML tags which accept any role value (on top of the ones already defined in `epubtype-aria-map.json`).

Exception has been made for `a` and `img` which are considered special cases as:
- `a` (accepted if without an href attribute)
- `img` (accepted only with alt text)

These requirements add a new layer of complexity and I decided to hold on on this for the moment. Leaving the tags out of the list is just a precaution not to introduce issues into the epub file.
These exceptions are hardcoded in the add_aria() method.
2 changes: 2 additions & 0 deletions assets/extra-tags.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[
"a",
"abbr",
"address",
"b",
Expand All @@ -14,6 +15,7 @@
"div",
"em",
"i",
"img",
"ins",
"kbd",
"mark",
Expand Down
Binary file modified docs/confirm_dialogue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 44 additions & 25 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

from calibre import force_unicode
from calibre.gui2 import error_dialog, info_dialog
from calibre.ebooks.oeb.polish.container import OEB_DOCS
from calibre.ebooks.oeb.base import OPF_MIME, OEB_DOCS

from calibre_plugins.access_aide.config import prefs

Expand Down Expand Up @@ -63,26 +63,18 @@ def main(self):
container = self.current_container

if not container:
message = 'No book open, you need to have a book open first.'
return error_dialog(self.gui, 'Access Aide',
'No book open, please open a book first.',
show=True)

return error_dialog(self.gui, 'Access Aide', message, show=True)

if container.book_type != 'epub':
message = 'Access Aide supports EPUB files only, {} given.' \
.format(container.book_type)
if container.book_type != 'epub' or \
container.opf_version_parsed.major not in [2, 3]:
message = 'Access Aide supports EPUB 2 and 3, {} {} given.' \
.format(container.book_type.upper(),
container.opf_version_parsed.major)

return error_dialog(self.gui, 'Access Aide', message, show=True)

# get book main language
try:
lang = container.opf_xpath('//dc:language/text()')[0]
except IndexError:
message = 'The OPF file does not report language info.'

return error_dialog(self.gui, 'Access Aide', message, show=True)

self.add_metadata(container)

blacklist = ['toc.xhtml']

# iterate over book files
Expand All @@ -91,10 +83,17 @@ def main(self):
if media_type in OEB_DOCS \
and name not in blacklist:

self.add_lang(container.parsed(name), lang)
self.add_lang(container.parsed(name),
self.get_lang(container))
self.add_aria(container.parsed(name))

container.dirty(name)
elif media_type in OPF_MIME:
self.add_metadata(container)

else:
continue

container.dirty(name)

info_dialog(self.gui, 'Access Aide', self.stats_report(), show=True)

Expand All @@ -105,6 +104,22 @@ def main(self):
# update the editor UI
self.boss.apply_container_update_to_gui()

def get_lang(self, container):
'''Retrieve book main language.
This method parses the OPF file, gets a list of the declared
languages and returns the first one (which we trust to be the
main language of the book).
'''

try:
lang = container.opf_xpath('//dc:language/text()')[0]
except IndexError:
message = 'The OPF file does not report language info.'

return error_dialog(self.gui, 'Access Aide', message, show=True)

return lang

def add_lang(self, root, lang):
'''Add language attributes to <html> tags.
Expand Down Expand Up @@ -154,6 +169,16 @@ def add_aria(self, root):

if map and (tag in map['tag'] or tag in extra_tags):

# EXCEPTIONS
# skip if <img> doesn't have alt text
if tag == 'img' and not node.get('alt'):
continue

# skip if <a> is not in map and has href value
if tag == 'a' and \
(tag not in map['tag'] and node.get('href')):
continue

self.write_attrib(node, 'role', map['aria'], self.aria_stat)

def write_attrib(self, node, attribute, value, stat):
Expand Down Expand Up @@ -241,9 +266,3 @@ def add_metadata(self, container):
container.insert_into_xml(metadata, element)

self.meta_stat.increase()

else:
# metadata currently available only for EPUB v2 and v3
return

container.dirty(container.opf_name)

0 comments on commit 63d08fc

Please sign in to comment.