转义索引指令中的特殊字符 [英] Escaping special characters in index directives

查看:24
本文介绍了转义索引指令中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Sphinx为Prolog系统实现生成文档。PROLOG语言包括分别由复合术语(',')/2(;)/2表示的合取和析取控制结构。

但由于逗号和分号的存在,以下索引指令不会生成正确的条目:

.. index:: (',')/2

.. index:: (;)/2

到目前为止,我一直无法找到字符转义的解决方案。我在Prolog!/0控制构造中也遇到了同样的问题,但我通过编写以下代码找到了解决方法:

.. index:: !!/0

试图使用反斜杠,但无济于事。在我遗漏的指令中是否支持对特殊字符进行转义?是否有其他解决方案可以使用(',')/2(;)/2!/0索引项?

推荐答案

<;(;)/2>;

(;)/2取决于IndexEntry类使用的Split_into函数。

  • 我猜:value.split(';', n - 1)->;value.split('; ', n - 1)
  • 我在#8904上发表了评论。
  • 重写此代码似乎很容易。

Sphinx/util/init.py(Sphinx 4.2.0)

365
366 def split_into(n: int, type: str, value: str) -> List[str]:
367     """Split an index entry into a given number of parts at semicolons."""
368     parts = [x.strip() for x in value.split(';', n - 1)]
369     if sum(1 for part in parts if part) < n:
370         raise ValueError('invalid %s index entry %r' % (type, value))
371     return parts
372

<;!!/0>;

!!/0取决于索引指令/角色使用的PROCESS_INDEX_ENTRY函数和其他函数。

  • 解决方法是使用!!!/0

sphinx/util/nodes.py(Sphinx 4.2.0)

363
364 def process_index_entry(entry: str, targetid: str
365                         ) -> List[Tuple[str, str, str, str, Optional[str]]]:
366     from sphinx.domains.python import pairindextypes
367
368     indexentries: List[Tuple[str, str, str, str, Optional[str]]] = []
369     entry = entry.strip()
370     oentry = entry
371     main = ''
372     if entry.startswith('!'):
373         main = 'main'
374         entry = entry[1:].lstrip()
375     for type in pairindextypes:
376         if entry.startswith(type + ':'):
377             value = entry[len(type) + 1:].strip()
378             value = pairindextypes[type] + '; ' + value
379             indexentries.append(('pair', value, targetid, main, None))
380             break
381     else:

Sphinx/Domones/index.py(Sphinx 4.2.0)

 62
 63 class IndexDirective(SphinxDirective):
 64     """
 65     Directive to add entries to the index.
 66     """
...
 90         for entry in arguments:
 91             indexnode['entries'].extend(process_index_entry(entry, targetnode['ids'][0]))
 92         return [indexnode, targetnode]
...
 94
 95 class IndexRole(ReferenceRole):
 96     def run(self) -> Tuple[List[Node], List[system_message]]:
...
102         else:
103             # otherwise we just create a single entry
104             if self.target.startswith('!'):
105                 title = self.title[1:]
106                 entries = [('single', self.target[1:], target_id, 'main', None)]
107             else:

其他

理想但困难的解决方案是开发sphinx/domains/prolog.py

这篇关于转义索引指令中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆