mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
LaTeX: Allow to override papersize and pointsize from LaTeX themes
This commit is contained in:
parent
3fba1d57ec
commit
1ca0b7ab62
1
CHANGES
1
CHANGES
@ -14,6 +14,7 @@ Features added
|
||||
--------------
|
||||
|
||||
* LaTeX: Make the ``toplevel_sectioning`` setting optional in LaTeX theme
|
||||
* LaTeX: Allow to override papersize and pointsize from LaTeX themes
|
||||
|
||||
Bugs fixed
|
||||
----------
|
||||
|
@ -314,6 +314,8 @@ class LaTeXBuilder(Builder):
|
||||
self.context['title'] = title
|
||||
self.context['author'] = author
|
||||
self.context['docclass'] = theme.docclass
|
||||
self.context['papersize'] = theme.papersize
|
||||
self.context['pointsize'] = theme.pointsize
|
||||
self.context['wrapperclass'] = theme.wrapperclass
|
||||
|
||||
def assemble_doctree(self, indexfile: str, toctree_only: bool, appendices: List[str]) -> nodes.document: # NOQA
|
||||
|
@ -69,8 +69,8 @@ LUALATEX_DEFAULT_FONTPKG = XELATEX_DEFAULT_FONTPKG
|
||||
|
||||
DEFAULT_SETTINGS = {
|
||||
'latex_engine': 'pdflatex',
|
||||
'papersize': 'letterpaper',
|
||||
'pointsize': '10pt',
|
||||
'papersize': '',
|
||||
'pointsize': '',
|
||||
'pxunit': '.75bp',
|
||||
'classoptions': '',
|
||||
'extraclassoptions': '',
|
||||
|
@ -24,12 +24,23 @@ logger = logging.getLogger(__name__)
|
||||
class Theme:
|
||||
"""A set of LaTeX configurations."""
|
||||
|
||||
LATEX_ELEMENTS_KEYS = ['papersize', 'pointsize']
|
||||
|
||||
def __init__(self, name: str) -> None:
|
||||
self.name = name
|
||||
self.docclass = name
|
||||
self.wrapperclass = name
|
||||
self.papersize = 'letterpaper'
|
||||
self.pointsize = '10pt'
|
||||
self.toplevel_sectioning = 'chapter'
|
||||
|
||||
def update(self, config: Config) -> None:
|
||||
"""Override theme settings by user's configuration."""
|
||||
for key in self.LATEX_ELEMENTS_KEYS:
|
||||
if config.latex_elements.get(key):
|
||||
value = config.latex_elements[key]
|
||||
setattr(self, key, value)
|
||||
|
||||
|
||||
class BuiltInTheme(Theme):
|
||||
"""A built-in LaTeX theme."""
|
||||
@ -59,7 +70,7 @@ class UserTheme(Theme):
|
||||
"""A user defined LaTeX theme."""
|
||||
|
||||
REQUIRED_CONFIG_KEYS = ['docclass', 'wrapperclass']
|
||||
OPTIONAL_CONFIG_KEYS = ['toplevel_sectioning']
|
||||
OPTIONAL_CONFIG_KEYS = ['papersize', 'pointsize', 'toplevel_sectioning']
|
||||
|
||||
def __init__(self, name: str, filename: str) -> None:
|
||||
super().__init__(name)
|
||||
@ -89,6 +100,7 @@ class ThemeFactory:
|
||||
def __init__(self, app: Sphinx) -> None:
|
||||
self.themes = {} # type: Dict[str, Theme]
|
||||
self.theme_paths = [path.join(app.srcdir, p) for p in app.config.latex_theme_path]
|
||||
self.config = app.config
|
||||
self.load_builtin_themes(app.config)
|
||||
|
||||
def load_builtin_themes(self, config: Config) -> None:
|
||||
@ -99,13 +111,14 @@ class ThemeFactory:
|
||||
def get(self, name: str) -> Theme:
|
||||
"""Get a theme for given *name*."""
|
||||
if name in self.themes:
|
||||
return self.themes[name]
|
||||
theme = self.themes[name]
|
||||
else:
|
||||
theme = self.find_user_theme(name)
|
||||
if theme:
|
||||
return theme
|
||||
else:
|
||||
return Theme(name)
|
||||
if not theme:
|
||||
theme = Theme(name)
|
||||
|
||||
theme.update(self.config)
|
||||
return theme
|
||||
|
||||
def find_user_theme(self, name: str) -> Theme:
|
||||
"""Find a theme named as *name* from latex_theme_path."""
|
||||
|
@ -1,4 +1,6 @@
|
||||
[theme]
|
||||
docclass = book
|
||||
wrapperclass = sphinxbook
|
||||
papersize = a4paper
|
||||
pointsize = 12pt
|
||||
toplevel_sectioning = chapter
|
||||
|
@ -221,7 +221,18 @@ def test_latex_theme(app, status, warning):
|
||||
result = (app.outdir / 'python.tex').read_text(encoding='utf8')
|
||||
print(result)
|
||||
assert r'\def\sphinxdocclass{book}' in result
|
||||
assert r'\documentclass[letterpaper,10pt,english]{sphinxbook}' in result
|
||||
assert r'\documentclass[a4paper,12pt,english]{sphinxbook}' in result
|
||||
|
||||
|
||||
@pytest.mark.sphinx('latex', testroot='latex-theme',
|
||||
confoverrides={'latex_elements': {'papersize': 'b5paper',
|
||||
'pointsize': '9pt'}})
|
||||
def test_latex_theme_papersize(app, status, warning):
|
||||
app.builder.build_all()
|
||||
result = (app.outdir / 'python.tex').read_text(encoding='utf8')
|
||||
print(result)
|
||||
assert r'\def\sphinxdocclass{book}' in result
|
||||
assert r'\documentclass[b5paper,9pt,english]{sphinxbook}' in result
|
||||
|
||||
|
||||
@pytest.mark.sphinx('latex', testroot='basic', confoverrides={'language': 'zh'})
|
||||
|
Loading…
Reference in New Issue
Block a user