Merge pull request #6206 from tk0miya/refactor_glossary2

Refactoring glossary directive
This commit is contained in:
Takeshi KOMIYA
2019-03-30 00:54:23 +09:00
committed by GitHub
2 changed files with 146 additions and 11 deletions

View File

@@ -319,23 +319,23 @@ class Glossary(SphinxDirective):
# first term of definition
if in_definition:
if not was_empty:
messages.append(self.state.reporter.system_message(
2, 'glossary term must be preceded by empty line',
messages.append(self.state.reporter.warning(
_('glossary term must be preceded by empty line'),
source=source, line=lineno))
entries.append(([(line, source, lineno)], StringList()))
in_definition = False
# second term and following
else:
if was_empty:
messages.append(self.state.reporter.system_message(
2, 'glossary terms must not be separated by empty '
'lines', source=source, line=lineno))
messages.append(self.state.reporter.warning(
_('glossary terms must not be separated by empty lines'),
source=source, line=lineno))
if entries:
entries[-1][0].append((line, source, lineno))
else:
messages.append(self.state.reporter.system_message(
2, 'glossary seems to be misformatted, check '
'indentation', source=source, line=lineno))
messages.append(self.state.reporter.warning(
_('glossary seems to be misformatted, check indentation'),
source=source, line=lineno))
else:
if not in_definition:
# first line of definition, determines indentation
@@ -344,9 +344,9 @@ class Glossary(SphinxDirective):
if entries:
entries[-1][1].append(line[indent_len:], source, lineno)
else:
messages.append(self.state.reporter.system_message(
2, 'glossary seems to be misformatted, check '
'indentation', source=source, line=lineno))
messages.append(self.state.reporter.warning(
_('glossary seems to be misformatted, check indentation'),
source=source, line=lineno))
was_empty = False
# now, parse all the entries into a big definition list

View File

@@ -11,8 +11,12 @@
from unittest import mock
from docutils import nodes
from docutils.nodes import definition, definition_list, definition_list_item, term
from sphinx.addnodes import glossary, index
from sphinx.domains.std import StandardDomain
from sphinx.testing import restructuredtext
from sphinx.testing.util import assert_node
def test_process_doc_handle_figure_caption():
@@ -80,3 +84,134 @@ def test_get_full_qualified_name():
kwargs = {'std:program': 'ls'}
node = nodes.reference(reftype='option', reftarget='-l', **kwargs)
assert domain.get_full_qualified_name(node) == 'ls.-l'
def test_glossary(app):
text = (".. glossary::\n"
"\n"
" term1\n"
" term2\n"
" description\n"
"\n"
" term3 : classifier\n"
" description\n"
" description\n"
"\n"
" term4 : class1 : class2\n"
" description\n")
# doctree
doctree = restructuredtext.parse(app, text)
assert_node(doctree, (
[glossary, definition_list, ([definition_list_item, ([term, ("term1",
index)],
[term, ("term2",
index)],
definition)],
[definition_list_item, ([term, ("term3",
index)],
definition)],
[definition_list_item, ([term, ("term4",
index)],
definition)])],
))
assert_node(doctree[0][0][0][0][1],
entries=[("single", "term1", "term-term1", "main", None)])
assert_node(doctree[0][0][0][1][1],
entries=[("single", "term2", "term-term2", "main", None)])
assert_node(doctree[0][0][0][2],
[definition, nodes.paragraph, "description"])
assert_node(doctree[0][0][1][0][1],
entries=[("single", "term3", "term-term3", "main", "classifier")])
assert_node(doctree[0][0][1][1],
[definition, nodes.paragraph, ("description\n"
"description")])
assert_node(doctree[0][0][2][0][1],
entries=[("single", "term4", "term-term4", "main", "class1")])
assert_node(doctree[0][0][2][1],
[nodes.definition, nodes.paragraph, "description"])
# index
objects = list(app.env.get_domain("std").get_objects())
assert ("term1", "term1", "term", "index", "term-term1", -1) in objects
assert ("term2", "term2", "term", "index", "term-term2", -1) in objects
assert ("term3", "term3", "term", "index", "term-term3", -1) in objects
assert ("term4", "term4", "term", "index", "term-term4", -1) in objects
def test_glossary_warning(app, status, warning):
# empty line between terms
text = (".. glossary::\n"
"\n"
" term1\n"
"\n"
" term2\n")
restructuredtext.parse(app, text, "case1")
assert ("case1.rst:4: WARNING: glossary terms must not be separated by empty lines"
in warning.getvalue())
# glossary starts with indented item
text = (".. glossary::\n"
"\n"
" description\n"
" term\n")
restructuredtext.parse(app, text, "case2")
assert ("case2.rst:3: WARNING: glossary term must be preceded by empty line"
in warning.getvalue())
# empty line between terms
text = (".. glossary::\n"
"\n"
" term1\n"
" description\n"
" term2\n")
restructuredtext.parse(app, text, "case3")
assert ("case3.rst:4: WARNING: glossary term must be preceded by empty line"
in warning.getvalue())
def test_glossary_comment(app):
text = (".. glossary::\n"
"\n"
" term1\n"
" description\n"
" .. term2\n"
" description\n"
" .. description\n")
doctree = restructuredtext.parse(app, text)
assert_node(doctree, (
[glossary, definition_list, definition_list_item, ([term, ("term1",
index)],
definition)],
))
assert_node(doctree[0][0][0][1],
[nodes.definition, nodes.paragraph, ("description\n"
"description\n"
".. description")])
def test_glossary_sorted(app):
text = (".. glossary::\n"
" :sorted:\n"
"\n"
" term3\n"
" description\n"
"\n"
" term2\n"
" term1\n"
" description\n")
doctree = restructuredtext.parse(app, text)
assert_node(doctree, (
[glossary, definition_list, ([definition_list_item, ([term, ("term2",
index)],
[term, ("term1",
index)],
definition)],
[definition_list_item, ([term, ("term3",
index)],
definition)])],
))
assert_node(doctree[0][0][0][2],
[nodes.definition, nodes.paragraph, "description"])
assert_node(doctree[0][0][1][1],
[nodes.definition, nodes.paragraph, "description"])