From adfda3e7df05b2e64be36316a5841f68b6af88a9 Mon Sep 17 00:00:00 2001 From: milde Date: Sat, 4 Nov 2017 21:14:37 +0000 Subject: [PATCH] Ignore backslash-escaped separators when extracting authors from a paragraph. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@8198 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/HISTORY.txt | 2 ++ docutils/RELEASE-NOTES.txt | 4 +++ docutils/docutils/transforms/frontmatter.py | 24 +++++++++++---- docutils/test/test_transforms/test_docinfo.py | 43 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 6 deletions(-) diff --git a/docutils/HISTORY.txt b/docutils/HISTORY.txt index c342295bf..fa5b3020c 100644 --- a/docutils/HISTORY.txt +++ b/docutils/HISTORY.txt @@ -47,6 +47,8 @@ Changes Since 0.14 * docutils/transforms/frontmatter.py: - Add field name as class argument to generic docinfo fields unconditionally. + - Ignore backslash-escaped separators when extracting authors from a + paragraph. * docutils/transforms/references.py: diff --git a/docutils/RELEASE-NOTES.txt b/docutils/RELEASE-NOTES.txt index 3c1b3f6c3..476c9afd5 100644 --- a/docutils/RELEASE-NOTES.txt +++ b/docutils/RELEASE-NOTES.txt @@ -58,6 +58,10 @@ Release 0.15b.dev - Fixed a bug with the "trim" options of the "unicode" directive. + - Allow escaping of author-separators in `bibliographic fields`__. + + __ docs/ref/rst/restructuredtext.html#bibliographic-fields + Release 0.14 (2017-08-03) ========================= diff --git a/docutils/docutils/transforms/frontmatter.py b/docutils/docutils/transforms/frontmatter.py index 147ebe65b..041632274 100644 --- a/docutils/docutils/transforms/frontmatter.py +++ b/docutils/docutils/transforms/frontmatter.py @@ -424,6 +424,7 @@ class DocInfo(Transform): base_node=field) raise TransformError title = nodes.title(name, labels[canonical]) + title[0].rawsource = labels[canonical] topics[canonical] = biblioclass( '', title, classes=[canonical], *field[1].children) else: @@ -503,20 +504,30 @@ class DocInfo(Transform): raise def authors_from_one_paragraph(self, field): - text = field[1][0].astext().strip() + """Return list of Text nodes for ";"- or ","-separated authornames.""" + # @@ keep original formatting? (e.g. ``:authors: A. Test, *et-al*``) + rawnames = (node.rawsource or node.astext + for node in field[1].traverse(nodes.Text)) + text = ''.join(rawnames) if not text: raise TransformError for authorsep in self.language.author_separators: - authornames = text.split(authorsep) + # don't split at escaped `authorsep`: + pattern = r'(?<=\\\\)%s|(? 1: break - authornames = [author.strip() for author in authornames] - authors = [[nodes.Text(author)] for author in authornames if author] + authornames = ((utils.unescape_rawsource(rawname).strip(), + rawname.strip()) for rawname in authornames) + authors = [[nodes.Text(author, rawname)] + for (author, rawname) in authornames if author] return authors def authors_from_bullet_list(self, field): authors = [] for item in field[1][0]: + if isinstance(item, nodes.comment): + continue if len(item) != 1 or not isinstance(item[0], nodes.paragraph): raise TransformError authors.append(item[0].children) @@ -526,7 +537,8 @@ class DocInfo(Transform): def authors_from_paragraphs(self, field): for item in field[1]: - if not isinstance(item, nodes.paragraph): + if not isinstance(item, (nodes.paragraph, nodes.comment)): raise TransformError - authors = [item.children for item in field[1]] + authors = [item.children for item in field[1] + if not isinstance(item, nodes.comment)] return authors diff --git a/docutils/test/test_transforms/test_docinfo.py b/docutils/test/test_transforms/test_docinfo.py index d29633517..2a03d364e 100755 --- a/docutils/test/test_transforms/test_docinfo.py +++ b/docutils/test/test_transforms/test_docinfo.py @@ -230,6 +230,49 @@ totest['bibliographic_field_lists'] = ((DocInfo,), [ One, Only """], +[r""":Authors: Me\, Myself; **I** +:Authors: Pac\;Man\\; Ms. Pac\Man; Pac\ Man, Jr. +:Authors: + Here + + The\re + + *Every\ where* +:Authors: - First\\ + - Se\ cond + - Thir\d +""", +"""\ + + + + + Me, Myself + + I + + + Pac;Man\\ + + Ms. PacMan + + PacMan, Jr. + + + Here + + There + + + Everywhere + + + First\\ + + Second + + Third +"""], ["""\ :Authors: -- 2.11.4.GIT