mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 02:34:06 -04:00
Viewer: Don't map epub:type to ARIA role
Apparently the EPUB committee has decided not to phase out epub:type after all, so keep it around. This way it can be used in namespaced CSS stylesheets.
This commit is contained in:
parent
7369171e33
commit
27d3a28a1d
@ -8,7 +8,7 @@ import json
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
from collections import OrderedDict, defaultdict
|
from collections import defaultdict
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from itertools import count
|
from itertools import count
|
||||||
@ -142,37 +142,11 @@ def transform_declaration(decl):
|
|||||||
return changed
|
return changed
|
||||||
|
|
||||||
|
|
||||||
def replace_epub_type_selector(m):
|
|
||||||
which = m.group(2)
|
|
||||||
roleval = EPUB_TYPE_MAP.get(which)
|
|
||||||
if roleval is None:
|
|
||||||
return m.group()
|
|
||||||
return 'role{}"{}"'.format(m.group(1), roleval)
|
|
||||||
|
|
||||||
|
|
||||||
def epub_type_pat():
|
|
||||||
ans = getattr(epub_type_pat, 'ans', None)
|
|
||||||
if ans is None:
|
|
||||||
ans = epub_type_pat.ans = re.compile(r'epub\|type([$*~]?=)"(\S+)"')
|
|
||||||
return ans
|
|
||||||
|
|
||||||
|
|
||||||
def transform_selector(rule):
|
|
||||||
selector = rule.selectorText
|
|
||||||
if 'epub|type' in selector:
|
|
||||||
ns, num = epub_type_pat().subn(replace_epub_type_selector, selector)
|
|
||||||
if num > 0 and ns != selector:
|
|
||||||
rule.selectorText = ns
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def transform_sheet(sheet):
|
def transform_sheet(sheet):
|
||||||
changed = False
|
changed = False
|
||||||
for rule in sheet.cssRules.rulesOfType(CSSRule.STYLE_RULE):
|
for rule in sheet.cssRules.rulesOfType(CSSRule.STYLE_RULE):
|
||||||
if transform_declaration(rule.style):
|
if transform_declaration(rule.style):
|
||||||
changed = True
|
changed = True
|
||||||
if transform_selector(rule):
|
|
||||||
changed = True
|
|
||||||
return changed
|
return changed
|
||||||
|
|
||||||
|
|
||||||
@ -543,41 +517,6 @@ def split_name(name):
|
|||||||
|
|
||||||
boolean_attributes = frozenset('allowfullscreen,async,autofocus,autoplay,checked,compact,controls,declare,default,defaultchecked,defaultmuted,defaultselected,defer,disabled,enabled,formnovalidate,hidden,indeterminate,inert,ismap,itemscope,loop,multiple,muted,nohref,noresize,noshade,novalidate,nowrap,open,pauseonexit,readonly,required,reversed,scoped,seamless,selected,sortable,truespeed,typemustmatch,visible'.split(',')) # noqa
|
boolean_attributes = frozenset('allowfullscreen,async,autofocus,autoplay,checked,compact,controls,declare,default,defaultchecked,defaultmuted,defaultselected,defer,disabled,enabled,formnovalidate,hidden,indeterminate,inert,ismap,itemscope,loop,multiple,muted,nohref,noresize,noshade,novalidate,nowrap,open,pauseonexit,readonly,required,reversed,scoped,seamless,selected,sortable,truespeed,typemustmatch,visible'.split(',')) # noqa
|
||||||
|
|
||||||
# see https://idpf.github.io/epub-guides/epub-aria-authoring/
|
|
||||||
EPUB_TYPE_MAP = {k:'doc-' + k for k in (
|
|
||||||
'abstract acknowledgements afterword appendix biblioentry bibliography biblioref chapter colophon conclusion cover credit'
|
|
||||||
' credits dedication epigraph epilogue errata footnote footnotes forward glossary glossref index introduction link noteref notice'
|
|
||||||
' pagebreak pagelist part preface prologue pullquote qna locator subtitle title toc').split(' ')}
|
|
||||||
for k in 'figure term definition directory list list-item table row cell'.split(' '):
|
|
||||||
EPUB_TYPE_MAP[k] = k
|
|
||||||
|
|
||||||
EPUB_TYPE_MAP['help'] = 'doc-tip'
|
|
||||||
EPUB_TYPE_MAP['page-list'] = 'doc-pagelist'
|
|
||||||
|
|
||||||
|
|
||||||
def map_epub_type(epub_type, attribs, elem):
|
|
||||||
val = EPUB_TYPE_MAP.get(epub_type.lower())
|
|
||||||
if val:
|
|
||||||
role = None
|
|
||||||
in_attribs = None
|
|
||||||
for i, x in enumerate(attribs):
|
|
||||||
if x[0] == 'role':
|
|
||||||
role = x[1]
|
|
||||||
in_attribs = i
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
role = elem.get('role')
|
|
||||||
roles = OrderedDict([(k, True) for k in role.split()]) if role else OrderedDict()
|
|
||||||
if val not in roles:
|
|
||||||
roles[val] = True
|
|
||||||
role = ' '.join(roles)
|
|
||||||
if in_attribs is None:
|
|
||||||
attribs.append(['role', role])
|
|
||||||
else:
|
|
||||||
attribs[in_attribs] = ['role', role]
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
known_tags = ('img', 'script', 'link', 'image', 'style')
|
known_tags = ('img', 'script', 'link', 'image', 'style')
|
||||||
discarded_tags = ('meta', 'base')
|
discarded_tags = ('meta', 'base')
|
||||||
@ -611,9 +550,6 @@ def serialize_elem(elem, nsmap):
|
|||||||
if val and val.lower() in (al, ''):
|
if val and val.lower() in (al, ''):
|
||||||
attribs.append([al, al])
|
attribs.append([al, al])
|
||||||
continue
|
continue
|
||||||
if attr_ns == EPUB_NS and al == 'type':
|
|
||||||
map_epub_type(val, attribs, elem)
|
|
||||||
continue
|
|
||||||
attrib = [aname, val]
|
attrib = [aname, val]
|
||||||
if attr_ns:
|
if attr_ns:
|
||||||
attr_ns = nsmap[attr_ns]
|
attr_ns = nsmap[attr_ns]
|
||||||
|
@ -164,7 +164,7 @@ def finalize_resources(book, root_name, resource_data):
|
|||||||
|
|
||||||
js_types = {k: True for k in 'text/javascript text/ecmascript application/javascript application/ecmascript'.split(' ')}
|
js_types = {k: True for k in 'text/javascript text/ecmascript application/javascript application/ecmascript'.split(' ')}
|
||||||
resource_tag_names = {'script':'src', 'link':'href', 'img':'src', 'image':'xlink:href'}
|
resource_tag_names = {'script':'src', 'link':'href', 'img':'src', 'image':'xlink:href'}
|
||||||
ns_rmap = {'http://www.w3.org/2000/svg':'svg', 'http://www.w3.org/1999/xlink':'xlink', 'http://www.w3.org/1998/Math/MathML':'math', 'http://www.w3.org/XML/1998/namespace': 'xml'}
|
ns_rmap = {'http://www.w3.org/2000/svg':'svg', 'http://www.w3.org/1999/xlink':'xlink', 'http://www.w3.org/1998/Math/MathML':'math', 'http://www.w3.org/XML/1998/namespace': 'xml', 'http://www.idpf.org/2007/ops': 'epub'}
|
||||||
ns_count = 0
|
ns_count = 0
|
||||||
hide_tooltips = False
|
hide_tooltips = False
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user