[英]How to find title a la reStructuredText
是否有正則表達式模式匹配以下reStructuredText類文本中的標題? 困難在於等號的數量必須等於標題的長度。
Some basic text.
=========
One Title
=========
For titles the numbers of sign `=` must be equal to the length of the text title.
=============
Another title
=============
And so on...
搜索匹配項(?:^|\\n)(=+)\\r?\\n(?!=)([^\\n\\r]+)\\r?\\n(=+)(?:\\r?\\n|$)
。 如果找到匹配,請檢查第一,第二和第三組的長度是否相同。 如果是,則title是第二組的內容。
#!/usr/bin/env python3
"""
some text
=====
Title
=====
Subtitle
--------
Titles are underlined (or over- and underlined) with a printing
nonalphanumeric 7-bit ASCII character. Recommended choices are "``= -
` : ' " ~ ^ _ * + # < >``". The underline/overline must be at least
as long as the title text.
A lone top-level (sub)section is lifted up to be the document's (sub)title.
"""
from docutils.core import publish_doctree
def section_title(node):
"""Whether `node` is a section title.
Note: it DOES NOT include document title!
"""
try:
return node.parent.tagname == "section" and node.tagname == "title"
except AttributeError:
return None # not a section title
# get document tree
doctree = publish_doctree(__doc__)
titles = doctree.traverse(condition=section_title)
print("\n".join([t.astext() for t in titles]))
Title
Subtitle
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.