Consistently use "utf-8" (not "utf8") in magic comment giving source encoding.
[docutils.git] / docutils / writers / xetex / __init__.py
blobbfa0783b032ebedf0727cef2cda71e89436f493a
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
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,Scale=MatchLowercase]{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 # normalize (downcase) keys
95 language_codes = dict([(k.lower(), v) for (k,v) in language_codes.items()])
97 # Languages without Polyglossia support:
98 for key in ('af', # 'afrikaans',
99 'de-AT', # 'naustrian',
100 'de-AT-1901', # 'austrian',
101 'fr-CA', # 'canadien',
102 'grc-ibycus', # 'ibycus', (Greek Ibycus encoding)
103 'sr-Latn', # 'serbian script=latin'
105 del(language_codes[key.lower()])
107 def __init__(self, language_code, reporter):
108 self.language_code = language_code
109 self.reporter = reporter
110 self.language = self.language_name(language_code)
111 self.otherlanguages = {}
112 self.warn_msg = 'Language "%s" not supported by XeTeX (polyglossia).'
113 self.quote_index = 0
114 self.quotes = ('"', '"')
115 # language dependent configuration:
116 # double quotes are "active" in some languages (e.g. German).
117 self.literal_double_quote = u'"' # TODO: use \textquotedbl
119 def __call__(self):
120 setup = [r'\usepackage{polyglossia}',
121 r'\setdefaultlanguage{%s}' % self.language]
122 if self.otherlanguages:
123 setup.append(r'\setotherlanguages{%s}' %
124 ','.join(sorted(self.otherlanguages.keys())))
125 return '\n'.join(setup)
128 class XeLaTeXTranslator(latex2e.LaTeXTranslator):
130 def __init__(self, document):
131 self.is_xetex = True # typeset with XeTeX or LuaTeX engine
132 latex2e.LaTeXTranslator.__init__(self, document, Babel)
133 if self.latex_encoding == 'utf8':
134 self.requirements.pop('_inputenc', None)
135 else:
136 self.requirements['_inputenc'] = (r'\XeTeXinputencoding %s '
137 % self.latex_encoding)