mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
epub: Refactor around epub_writing_mode
This commit is contained in:
@@ -15,7 +15,7 @@ from datetime import datetime
|
|||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
from sphinx import package_dir
|
from sphinx import package_dir
|
||||||
from sphinx.config import string_classes
|
from sphinx.config import string_classes, ENUM
|
||||||
from sphinx.builders.epub import EpubBuilder
|
from sphinx.builders.epub import EpubBuilder
|
||||||
from sphinx.util import logging
|
from sphinx.util import logging
|
||||||
from sphinx.util.fileutil import copy_asset_file
|
from sphinx.util.fileutil import copy_asset_file
|
||||||
@@ -31,9 +31,21 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
NavPoint = namedtuple('NavPoint', ['text', 'refuri', 'children'])
|
NavPoint = namedtuple('NavPoint', ['text', 'refuri', 'children'])
|
||||||
|
|
||||||
DOCTYPE = u'''<!DOCTYPE html>'''
|
# writing modes
|
||||||
|
PAGE_PROGRESSION_DIRECTIONS = {
|
||||||
|
'horizontal': 'ltr',
|
||||||
|
'vertical': 'rtl',
|
||||||
|
}
|
||||||
|
IBOOK_SCROLL_AXIS = {
|
||||||
|
'horizontal': 'vertical',
|
||||||
|
'vertical': 'horizontal',
|
||||||
|
}
|
||||||
|
THEME_WRITING_MODES = {
|
||||||
|
'vertical': 'vertical-rl',
|
||||||
|
'horizontal': 'horizontal-tb',
|
||||||
|
}
|
||||||
|
|
||||||
# The epub3 publisher
|
DOCTYPE = u'''<!DOCTYPE html>'''
|
||||||
|
|
||||||
|
|
||||||
class Epub3Builder(EpubBuilder):
|
class Epub3Builder(EpubBuilder):
|
||||||
@@ -66,47 +78,23 @@ class Epub3Builder(EpubBuilder):
|
|||||||
"""Create a dictionary with all metadata for the content.opf
|
"""Create a dictionary with all metadata for the content.opf
|
||||||
file properly escaped.
|
file properly escaped.
|
||||||
"""
|
"""
|
||||||
|
writing_mode = self.config.epub_writing_mode
|
||||||
|
|
||||||
metadata = super(Epub3Builder, self).content_metadata()
|
metadata = super(Epub3Builder, self).content_metadata()
|
||||||
metadata['description'] = self.esc(self.config.epub_description)
|
metadata['description'] = self.esc(self.config.epub_description)
|
||||||
metadata['contributor'] = self.esc(self.config.epub_contributor)
|
metadata['contributor'] = self.esc(self.config.epub_contributor)
|
||||||
metadata['page_progression_direction'] = self._page_progression_direction()
|
metadata['page_progression_direction'] = PAGE_PROGRESSION_DIRECTIONS.get(writing_mode)
|
||||||
metadata['ibook_scroll_axis'] = self._ibook_scroll_axis()
|
metadata['ibook_scroll_axis'] = IBOOK_SCROLL_AXIS.get(writing_mode)
|
||||||
metadata['date'] = self.esc(datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"))
|
metadata['date'] = self.esc(datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"))
|
||||||
metadata['version'] = self.esc(self.config.version)
|
metadata['version'] = self.esc(self.config.version)
|
||||||
return metadata
|
return metadata
|
||||||
|
|
||||||
def _page_progression_direction(self):
|
|
||||||
# type: () -> unicode
|
|
||||||
if self.config.epub_writing_mode == 'horizontal':
|
|
||||||
page_progression_direction = 'ltr'
|
|
||||||
elif self.config.epub_writing_mode == 'vertical':
|
|
||||||
page_progression_direction = 'rtl'
|
|
||||||
else:
|
|
||||||
page_progression_direction = 'default'
|
|
||||||
return page_progression_direction
|
|
||||||
|
|
||||||
def _ibook_scroll_axis(self):
|
|
||||||
# type: () -> unicode
|
|
||||||
if self.config.epub_writing_mode == 'horizontal':
|
|
||||||
scroll_axis = 'vertical'
|
|
||||||
elif self.config.epub_writing_mode == 'vertical':
|
|
||||||
scroll_axis = 'horizontal'
|
|
||||||
else:
|
|
||||||
scroll_axis = 'default'
|
|
||||||
return scroll_axis
|
|
||||||
|
|
||||||
def _css_writing_mode(self):
|
|
||||||
# type: () -> unicode
|
|
||||||
if self.config.epub_writing_mode == 'vertical':
|
|
||||||
editing_mode = 'vertical-rl'
|
|
||||||
else:
|
|
||||||
editing_mode = 'horizontal-tb'
|
|
||||||
return editing_mode
|
|
||||||
|
|
||||||
def prepare_writing(self, docnames):
|
def prepare_writing(self, docnames):
|
||||||
# type: (Iterable[unicode]) -> None
|
# type: (Iterable[unicode]) -> None
|
||||||
super(Epub3Builder, self).prepare_writing(docnames)
|
super(Epub3Builder, self).prepare_writing(docnames)
|
||||||
self.globalcontext['theme_writing_mode'] = self._css_writing_mode()
|
|
||||||
|
writing_mode = self.config.epub_writing_mode
|
||||||
|
self.globalcontext['theme_writing_mode'] = THEME_WRITING_MODES.get(writing_mode)
|
||||||
|
|
||||||
def build_navlist(self, navnodes):
|
def build_navlist(self, navnodes):
|
||||||
# type: (List[nodes.Node]) -> List[NavPoint]
|
# type: (List[nodes.Node]) -> List[NavPoint]
|
||||||
@@ -193,7 +181,8 @@ def setup(app):
|
|||||||
|
|
||||||
app.add_config_value('epub_description', '', 'epub3', string_classes)
|
app.add_config_value('epub_description', '', 'epub3', string_classes)
|
||||||
app.add_config_value('epub_contributor', 'unknown', 'epub3', string_classes)
|
app.add_config_value('epub_contributor', 'unknown', 'epub3', string_classes)
|
||||||
app.add_config_value('epub_writing_mode', 'horizontal', 'epub3', string_classes)
|
app.add_config_value('epub_writing_mode', 'horizontal', 'epub3',
|
||||||
|
ENUM('horizontal', 'vertical'))
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'version': 'builtin',
|
'version': 'builtin',
|
||||||
|
|||||||
@@ -210,3 +210,38 @@ def test_nested_toc(app):
|
|||||||
grandchild = tocchildren[1].findall("./xhtml:ol/xhtml:li")
|
grandchild = tocchildren[1].findall("./xhtml:ol/xhtml:li")
|
||||||
assert len(grandchild) == 1
|
assert len(grandchild) == 1
|
||||||
assert navinfo(grandchild[0]) == ('foo.xhtml#foo-1-1', 'foo.1-1')
|
assert navinfo(grandchild[0]) == ('foo.xhtml#foo-1-1', 'foo.1-1')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.sphinx('epub', testroot='basic')
|
||||||
|
def test_epub_writing_mode(app):
|
||||||
|
# horizontal (default)
|
||||||
|
app.build()
|
||||||
|
|
||||||
|
# horizontal / page-progression-direction
|
||||||
|
opf = EPUBElementTree.fromstring((app.outdir / 'content.opf').text())
|
||||||
|
assert opf.find("./idpf:spine").get('page-progression-direction') == 'ltr'
|
||||||
|
|
||||||
|
# horizontal / ibooks:scroll-axis
|
||||||
|
metadata = opf.find("./idpf:metadata")
|
||||||
|
assert metadata.find("./idpf:meta[@property='ibooks:scroll-axis']").text == 'vertical'
|
||||||
|
|
||||||
|
# horizontal / writing-mode (CSS)
|
||||||
|
css = (app.outdir / '_static' / 'epub.css').text()
|
||||||
|
assert 'writing-mode: horizontal-tb;' in css
|
||||||
|
|
||||||
|
# vertical
|
||||||
|
app.config.epub_writing_mode = 'vertical'
|
||||||
|
(app.outdir / 'index.xhtml').unlink() # forcely rebuild
|
||||||
|
app.build()
|
||||||
|
|
||||||
|
# vertical / page-progression-direction
|
||||||
|
opf = EPUBElementTree.fromstring((app.outdir / 'content.opf').text())
|
||||||
|
assert opf.find("./idpf:spine").get('page-progression-direction') == 'rtl'
|
||||||
|
|
||||||
|
# vertical / ibooks:scroll-axis
|
||||||
|
metadata = opf.find("./idpf:metadata")
|
||||||
|
assert metadata.find("./idpf:meta[@property='ibooks:scroll-axis']").text == 'horizontal'
|
||||||
|
|
||||||
|
# vertical / writing-mode (CSS)
|
||||||
|
css = (app.outdir / '_static' / 'epub.css').text()
|
||||||
|
assert 'writing-mode: vertical-rl;' in css
|
||||||
|
|||||||
Reference in New Issue
Block a user