doc: remove links to translation if building PDF
Closes IDF-1579
This commit is contained in:
parent
8a349d7102
commit
dc2c753a7d
1 changed files with 56 additions and 33 deletions
|
@ -7,6 +7,7 @@ import os
|
||||||
import subprocess
|
import subprocess
|
||||||
from docutils import nodes
|
from docutils import nodes
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
from sphinx.transforms.post_transforms import SphinxPostTransform
|
||||||
|
|
||||||
|
|
||||||
def get_github_rev():
|
def get_github_rev():
|
||||||
|
@ -54,27 +55,6 @@ def url_join(*url_parts):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def setup(app):
|
|
||||||
rev = get_github_rev()
|
|
||||||
submods = get_submodules()
|
|
||||||
|
|
||||||
# links to files or folders on the GitHub
|
|
||||||
app.add_role('idf', github_link('tree', rev, submods, '/', app.config))
|
|
||||||
app.add_role('idf_file', github_link('blob', rev, submods, '/', app.config))
|
|
||||||
app.add_role('idf_raw', github_link('raw', rev, submods, '/', app.config))
|
|
||||||
app.add_role('component', github_link('tree', rev, submods, '/components/', app.config))
|
|
||||||
app.add_role('component_file', github_link('blob', rev, submods, '/components/', app.config))
|
|
||||||
app.add_role('component_raw', github_link('raw', rev, submods, '/components/', app.config))
|
|
||||||
app.add_role('example', github_link('tree', rev, submods, '/examples/', app.config))
|
|
||||||
app.add_role('example_file', github_link('blob', rev, submods, '/examples/', app.config))
|
|
||||||
app.add_role('example_raw', github_link('raw', rev, submods, '/examples/', app.config))
|
|
||||||
|
|
||||||
# link to the current documentation file in specific language version
|
|
||||||
app.add_role('link_to_translation', link_to_translation(app.config))
|
|
||||||
|
|
||||||
return {'parallel_read_safe': True, 'parallel_write_safe': True, 'version': '0.4'}
|
|
||||||
|
|
||||||
|
|
||||||
def github_link(link_type, idf_rev, submods, root_path, app_config):
|
def github_link(link_type, idf_rev, submods, root_path, app_config):
|
||||||
def role(name, rawtext, text, lineno, inliner, options={}, content=[]):
|
def role(name, rawtext, text, lineno, inliner, options={}, content=[]):
|
||||||
msgs = []
|
msgs = []
|
||||||
|
@ -152,15 +132,58 @@ def github_link(link_type, idf_rev, submods, root_path, app_config):
|
||||||
return role
|
return role
|
||||||
|
|
||||||
|
|
||||||
def link_to_translation(config):
|
class translation_link(nodes.Element):
|
||||||
def role(name, rawtext, text, lineno, inliner, options={}, content=[]):
|
"""Node for "link_to_translation" role."""
|
||||||
(language, link_text) = text.split(':')
|
|
||||||
docname = inliner.document.settings.env.docname
|
|
||||||
doc_path = inliner.document.settings.env.doc2path(docname, None, None)
|
# Linking to translation is done at the "writing" stage to avoid issues with the info being cached between builders
|
||||||
return_path = '../' * doc_path.count('/') # path back to the root from 'docname'
|
def link_to_translation(name, rawtext, text, lineno, inliner, options={}, content=[]):
|
||||||
# then take off 3 more paths for language/release/targetname and build the new URL
|
node = translation_link()
|
||||||
url = "{}.html".format(os.path.join(return_path, '../../..', language, config.release,
|
node['expr'] = (rawtext, text, options)
|
||||||
config.idf_target, docname))
|
return [node], []
|
||||||
node = nodes.reference(rawtext, link_text, refuri=url, **options)
|
|
||||||
return [node], []
|
|
||||||
return role
|
class TranslationLinkNodeTransform(SphinxPostTransform):
|
||||||
|
# Transform needs to happen early to ensure the new reference node is also transformed
|
||||||
|
default_priority = 0
|
||||||
|
|
||||||
|
def run(self, **kwargs):
|
||||||
|
|
||||||
|
# Only output relative links if building HTML
|
||||||
|
for node in self.document.traverse(translation_link):
|
||||||
|
if 'html' in self.app.builder.name:
|
||||||
|
rawtext, text, options = node['expr']
|
||||||
|
(language, link_text) = text.split(':')
|
||||||
|
env = self.document.settings.env
|
||||||
|
docname = env.docname
|
||||||
|
doc_path = env.doc2path(docname, None, None)
|
||||||
|
return_path = '../' * doc_path.count('/') # path back to the root from 'docname'
|
||||||
|
# then take off 3 more paths for language/release/targetname and build the new URL
|
||||||
|
url = "{}.html".format(os.path.join(return_path, '../../..', language, env.config.release,
|
||||||
|
env.config.idf_target, docname))
|
||||||
|
node.replace_self(nodes.reference(rawtext, link_text, refuri=url, **options))
|
||||||
|
else:
|
||||||
|
node.replace_self([])
|
||||||
|
|
||||||
|
|
||||||
|
def setup(app):
|
||||||
|
rev = get_github_rev()
|
||||||
|
submods = get_submodules()
|
||||||
|
|
||||||
|
# links to files or folders on the GitHub
|
||||||
|
app.add_role('idf', github_link('tree', rev, submods, '/', app.config))
|
||||||
|
app.add_role('idf_file', github_link('blob', rev, submods, '/', app.config))
|
||||||
|
app.add_role('idf_raw', github_link('raw', rev, submods, '/', app.config))
|
||||||
|
app.add_role('component', github_link('tree', rev, submods, '/components/', app.config))
|
||||||
|
app.add_role('component_file', github_link('blob', rev, submods, '/components/', app.config))
|
||||||
|
app.add_role('component_raw', github_link('raw', rev, submods, '/components/', app.config))
|
||||||
|
app.add_role('example', github_link('tree', rev, submods, '/examples/', app.config))
|
||||||
|
app.add_role('example_file', github_link('blob', rev, submods, '/examples/', app.config))
|
||||||
|
app.add_role('example_raw', github_link('raw', rev, submods, '/examples/', app.config))
|
||||||
|
|
||||||
|
# link to the current documentation file in specific language version
|
||||||
|
app.add_role('link_to_translation', link_to_translation)
|
||||||
|
app.add_node(translation_link)
|
||||||
|
app.add_post_transform(TranslationLinkNodeTransform)
|
||||||
|
|
||||||
|
return {'parallel_read_safe': True, 'parallel_write_safe': True, 'version': '0.5'}
|
||||||
|
|
Loading…
Reference in a new issue