Multiple extensions that extends a translator ?

9 views
Skip to first unread message

Yves Chevallier

unread,
Aug 7, 2020, 8:12:08 AM8/7/20
to sphinx-users
I am writing an extension that uses `listings` instead of `SphinxVerbatim`. This extension extends the current translator:

def setup(app):
 app
.set_translator('latex', MyLaTeXTranslator)

 
return {
 
'version': '0.1',
 
'parallel_read_safe': True,
 
'parallel_write_safe': True,
 
}

Then I want to write another extension that adds a colored frame around the code

from sphinx.writers.latex import LaTeXTranslator

class Translator(LaTeXTranslator):
 
def visit_literal_block(self, node):
 
return ''.join([
 
'foo',
 
super().visit_literal_block(node),
 
'bar'
 
])

def setup(app):
 app
.set_translator('latex', Translator)

 
return {
 
'version': '0.1',
 
'parallel_read_safe': True,
 
'parallel_write_safe': True,
 
}

Unfortunately this lead to an error: 

sphinx.errors.ExtensionError: Translator for 'latex' already exists


Extension error:
Translator for 'latex' already exists

What is the proper way to do this?

Komiya Takeshi

unread,
Aug 7, 2020, 8:18:54 AM8/7/20
to sphinx...@googlegroups.com
Hi,

Unfortunately, we can't use two translators at the same time. Only one
translator can be registered to the specific builder. So it would be
better to integrate these translators to a single one.

As a workaround, you can override the method of the default
LaTeXTranslator via assigning method::

from sphinx.writers.latex import LaTeXTranslator

original_visit_literal_block = LaTeXTranslator.visit_literal_block

def my_visit_literal_block(self, node):
...

LaTeXTranslator.visit_literal_block = my_visit_literal_block

Thanks,
Takeshi KOMIYA

2020年8月7日(金) 21:12 Yves Chevallier <cana...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups "sphinx-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/81fb2c56-2711-4262-89a1-02a2552d1ce5o%40googlegroups.com.

Daniel Scott

unread,
Nov 25, 2020, 10:36:23 PM11/25/20
to sphinx...@googlegroups.com
--
Reply all
Reply to author
Forward
0 new messages