Avoid code duplication between xetex and latex2e writer (solves [ 3512728 ]).
[docutils.git] / docutils / writers / xetex / __init__.py
blob82d918949dc046ad235897cb3fdcbad3b5735b3f
1 #!/usr/bin/env python
2 # -*- coding: utf8 -*-
4 # :Author: Günter Milde <milde@users.sourceforge.net>
5 # :Revision: $Revision$
6 # :Date: $Date$
7 # :Copyright: © 2010 Günter Milde.
8 # :License: Released under the terms of the `2-Clause BSD license`_, in short:
9 #
10 # Copying and distribution of this file, with or without modification,
11 # are permitted in any medium without royalty provided the copyright
12 # notice and this notice are preserved.
13 # This file is offered as-is, without any warranty.
15 # .. _2-Clause BSD license: http://www.spdx.org/licenses/BSD-2-Clause
17 """
18 XeLaTeX document tree Writer.
20 A variant of Docutils' standard 'latex2e' writer producing output
21 suited for processing with XeLaTeX (http://tug.org/xetex/).
22 """
24 __docformat__ = 'reStructuredText'
26 import os
27 import os.path
28 import re
30 import docutils
31 from docutils import frontend, nodes, utils, writers, languages
32 from docutils.writers import latex2e
34 class Writer(latex2e.Writer):
35 """A writer for Unicode-based LaTeX variants (XeTeX, LuaTeX)"""
37 supported = ('xetex','xelatex','luatex')
38 """Formats this writer supports."""
40 default_template = 'xelatex.tex'
41 default_preamble = '\n'.join([
42 r'% Linux Libertine (free, wide coverage, not only for Linux)',
43 r'\setmainfont{Linux Libertine O}',
44 r'\setsansfont{Linux Biolinum O}',
45 r'\setmonofont[HyphenChar=None]{DejaVu Sans Mono}',
48 config_section = 'xetex writer'
49 config_section_dependencies = ('writers', 'latex2e writer')
51 settings_spec = frontend.filter_settings_spec(
52 latex2e.Writer.settings_spec,
53 'font_encoding',
54 template=('Template file. Default: "%s".' % default_template,
55 ['--template'], {'default': default_template, 'metavar': '<file>'}),
56 latex_preamble=('Customization by LaTeX code in the preamble. '
57 'Default: select PDF standard fonts (Times, Helvetica, Courier).',
58 ['--latex-preamble'],
59 {'default': default_preamble}),
62 def __init__(self):
63 latex2e.Writer.__init__(self)
64 self.settings_defaults.update({'fontencoding': ''}) # use default (EU1 or EU2)
65 self.translator_class = XeLaTeXTranslator
68 class Babel(latex2e.Babel):
69 """Language specifics for XeTeX.
71 Use `polyglossia` instead of `babel` and adapt settings.
72 """
73 language_codes = latex2e.Babel.language_codes.copy()
74 # Additionally supported or differently named languages:
75 language_codes.update({
76 # code Polyglossia-name comment
77 'cop': 'coptic',
78 'de': 'german', # new spelling (de_1996)
79 'de_1901': 'ogerman', # old spelling
80 'dv': 'divehi', # Maldivian
81 'dsb': 'lsorbian',
82 'el_polyton': 'polygreek',
83 'fa': 'farsi',
84 'grc': 'ancientgreek',
85 'hsb': 'usorbian',
86 'sh-cyrl': 'serbian', # Serbo-Croatian, Cyrillic script
87 'sh-latn': 'croatian', # Serbo-Croatian, Latin script
88 'sq': 'albanian',
89 'sr': 'serbian', # Cyrillic script (sr-cyrl)
90 'th': 'thai',
91 'vi': 'vietnamese',
92 # zh-latn: ??? # Chinese Pinyin
94 # Languages without Polyglossia support:
95 for key in ('af', # 'afrikaans',
96 'de_at', # 'naustrian',
97 'de_at_1901', # 'austrian',
98 'fr_ca', # 'canadien',
99 'grc_ibycus', # 'ibycus', (Greek Ibycus encoding)
100 'sr-latn', # 'serbian script=latin'
102 del(language_codes[key])
104 def __init__(self, language_code, reporter):
105 self.language_code = language_code
106 self.reporter = reporter
107 self.language = self.language_name(language_code)
108 self.otherlanguages = {}
109 self.warn_msg = 'Language "%s" not supported by XeTeX (polyglossia).'
110 self.quote_index = 0
111 self.quotes = ('"', '"')
112 # language dependent configuration:
113 # double quotes are "active" in some languages (e.g. German).
114 self.literal_double_quote = u'"' # TODO: use \textquotedbl
116 def __call__(self):
117 setup = [r'\usepackage{polyglossia}',
118 r'\setdefaultlanguage{%s}' % self.language]
119 if self.otherlanguages:
120 setup.append(r'\setotherlanguages{%s}' %
121 ','.join(self.otherlanguages.keys()))
122 return '\n'.join(setup)
125 class XeLaTeXTranslator(latex2e.LaTeXTranslator):
127 def __init__(self, document):
128 self.is_xetex = True # typeset with XeTeX or LuaTeX engine
129 latex2e.LaTeXTranslator.__init__(self, document, Babel)
130 if self.latex_encoding == 'utf8':
131 self.requirements.pop('_inputenc', None)
132 else:
133 self.requirements['_inputenc'] = (r'\XeTeXinputencoding %s '
134 % self.latex_encoding)