Skip to content

Commit

Permalink
get book language by parsing OPF file
Browse files Browse the repository at this point in the history
  • Loading branch information
lb803 committed Jun 20, 2020
1 parent 8465986 commit f51c586
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ This project is a port of the [Access-Aide](https://github.com/kevinhendricks/Ac
The _Access Aide_ plugin for Calibre is at a very early developmental stage.

# Features
- Add 'en-GB' language declaration to `<html>` tags.
- Add language declaration to `<html>` tags.

# Coming features
- Retrieve book language from book metadata;
- Add in appropriate epub:type semantic tags;
- Map epub:type attributes to their appropriate aria role attribute;
- Add accessibility declarations to book metadata.

# Licence
# License
Source code by Luca Baffa, released under the GPL 3 licence.

The plugin icon (`./icon/icon.png`) comes from the `adwaita-icon-theme` pack ([gitlab page](https://gitlab.gnome.org/GNOME/adwaita-icon-theme) of the project), released as CC SA 3.0 by the GNOME Project.
The plugin icon (`./icon/icon.png`) comes from the `adwaita-icon-theme` pack ([gitlab page](https://gitlab.gnome.org/GNOME/adwaita-icon-theme) of the project), released as LGPL v3 by the GNOME Project.
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class DemoPlugin(EditBookToolPlugin):
name = 'Access Aide'
version = (0, 0, 1)
version = (0, 1, 0)
author = 'Luca Baffa'
supported_platforms = ['windows', 'osx', 'linux']
description = 'Access Aide plugin for Calibre'
Expand Down
20 changes: 18 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def main(self):
return error_dialog(self.gui, 'No book open',
'Need to have a book open first', show=True)

# TODO Parse book metadata and find book language
lang = 'en-GB'
# get the book main language
lang = self.get_lang(container)

# iterate over book files
for name, media_type in container.mime_map.items():
Expand All @@ -48,6 +48,22 @@ def main(self):

container.dirty(name)

def get_lang(self, container):
'''
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)
'''

languages = container.opf_xpath('//dc:language/text()')

if not languages:
return error_dialog(self.gui, 'No language declaration for book',
'The OPF file does not report language info.',
show=True)

return languages[0]

def add_lang(self, root, lang):
'''
This method finds the <html> tag of the given 'root' element
Expand Down

0 comments on commit f51c586

Please sign in to comment.