替代内部的链接,在其余/狮身人面像
-
22-07-2019 - |
题
我使用的狮身人面像文件a web服务,将部署在不同的服务器。该文件是完整的URL的例子为户击,他们应该就工作。我的问题是,主、港口和部署根会有所不同,文件将需要重新生成的每一个部署。
我试图确定替代这样的:
|base_url|/path
.. |base_url| replace:: http://localhost:8080
但是,所产生HTML不是我想要的(不包括"/path"在所产生的链接):
<a href="http://localhost:8080">http://localhost:8080</a>/path
没有任何人知道如何解决这个吗?
解决方案
新在斯芬克斯V1.0
sphinx.ext.extlinks - 标记缩短外部链接
http://sphinx.pocoo.org/ext/extlinks.html
在扩展增加一个新的配置值:
<强> extlinks 强>
此配置值必须是外部站点的字典,映射唯一的短别名到基座URL和一个前缀。例如,以产生用于上述问题的别名,可以添加
extlinks = {'issue':
('http://bitbucket.org/birkenfeld/sphinx/issue/%s', 'issue ')}
现在,您可以使用别名作为一个新的角色,例如:issue:`123`
。这然后插入一个链接到 http://bitbucket.org/birkenfeld/sphinx/issue/123一>。正如可以看到,在给定的角色目标是取代在%s
的地方的基URL。
在链接标题取决于第二项在元组中,前缀:
如果前缀是None,链路标题是完整的URL。
如果前缀是空字符串,链接标题是在角色内容给出的部分URL(在这种情况下,123)。
如果前缀是一个非空字符串,链接标题是部分URL,由前缀前缀 - 在上面的例子中,链接标题是问题123。
您还可以使用通常的“明确的标题是”由生成链接的其他角色,即:issue:`this issue <123>`
支持的语法。在此情况下,前缀是不相关的。
其他提示
好吧,这里是我是如何做到的。首先,apilinks.py
(斯芬克斯扩展名):
from docutils import nodes, utils
def setup(app):
def api_link_role(role, rawtext, text, lineno, inliner, options={},
content=[]):
ref = app.config.apilinks_base + text
node = nodes.reference(rawtext, utils.unescape(ref), refuri=ref,
**options)
return [node], []
app.add_config_value('apilinks_base', 'http://localhost/', False)
app.add_role('apilink', api_link_role)
现在,在conf.py
中,添加'apilinks'
到扩展列表,并为'apilinks_base'
(否则将默认为'http://localhost/'
)为适当的值。我的文件看起来像这样:
extensions = ['sphinx.ext.autodoc', 'apilinks']
# lots of other stuff
apilinks_base = 'http://host:88/base/'
用法:
:apilink:`path`
输出:
<a href="http://host:88/base/path">http://host:88/base/path</a>
我其中我需要也替代网址在图像目标的类似的问题。
作为图像extlinks
属性的值使用时:target:
不扩大。
最后,我写了重写以给定前缀开头的URL,在我的情况,http://mybase/
定制的狮身人面像的转变。下面是一个conf.py相关代码:
from sphinx.transforms import SphinxTransform
class ReplaceMyBase(SphinxTransform):
default_priority = 750
prefix = 'http://mybase/'
def apply(self):
from docutils.nodes import reference, Text
baseref = lambda o: (
isinstance(o, reference) and
o.get('refuri', '').startswith(self.prefix))
basetext = lambda o: (
isinstance(o, Text) and o.startswith(self.prefix))
base = self.config.mybase.rstrip('/') + '/'
for node in self.document.traverse(baseref):
target = node['refuri'].replace(self.prefix, base, 1)
node.replace_attr('refuri', target)
for t in node.traverse(basetext):
t1 = Text(t.replace(self.prefix, base, 1), t.rawsource)
t.parent.replace(t, t1)
return
# end of class
def setup(app):
app.add_config_value('mybase', 'https://en.wikipedia.org/wiki', 'env')
app.add_transform(ReplaceMyBase)
return
这扩展了以下RST源指向英文维基百科。
当conf.py套mybase="https://es.wikipedia.org/wiki"
链接将指向西班牙语维基。
* inline link http://mybase/Helianthus
* `link with text <http://mybase/Helianthus>`_
* `link with separate definition`_
* image link |flowerimage|
.. _link with separate definition: http://mybase/Helianthus
.. |flowerimage| image:: https://upload.wikimedia.org/wikipedia/commons/f/f1/Tournesol.png
:target: http://mybase/Helianthus