From: Dmitry Shachnev <mitya57@debian.org>
Date: Sat, 21 Jan 2023 19:25:03 +0400
Subject: manpage writer: Do not output empty "manual" in ``.TH``

Origin: upstream, commits:
 - https://sourceforge.net/p/docutils/code/9199/
 - https://sourceforge.net/p/docutils/code/9203/
 - https://sourceforge.net/p/docutils/code/9205/
---
 docutils/writers/manpage.py       | 13 ++++++-----
 test/test_writers/test_manpage.py | 46 +++++++++++++++++++++++++++++++--------
 2 files changed, 45 insertions(+), 14 deletions(-)

diff --git a/docutils/writers/manpage.py b/docutils/writers/manpage.py
index 7127502..9096b9c 100644
--- a/docutils/writers/manpage.py
+++ b/docutils/writers/manpage.py
@@ -375,11 +375,14 @@ class Translator(nodes.NodeVisitor):
         self._list_char.pop()
 
     def header(self):
-        tmpl = (".TH \"%(title_upper)s\" %(manual_section)s"
-                " \"%(date)s\" \"%(version)s\" \"%(manual_group)s\"\n"
-                ".SH NAME\n"
-                "%(title)s \\- %(subtitle)s\n")
-        return tmpl % self._docinfo
+        th = (".TH \"%(title_upper)s\" %(manual_section)s"
+              " \"%(date)s\" \"%(version)s\"") % self._docinfo
+        if self._docinfo["manual_group"]:
+            th += " \"%(manual_group)s\"" % self._docinfo
+        th += "\n"
+        sh_tmpl = (".SH NAME\n"
+                   "%(title)s \\- %(subtitle)s\n")
+        return th + sh_tmpl % self._docinfo
 
     def append_header(self):
         """append header with .TH and .SH NAME"""
diff --git a/test/test_writers/test_manpage.py b/test/test_writers/test_manpage.py
index 37eacaf..a61ceda 100644
--- a/test/test_writers/test_manpage.py
+++ b/test/test_writers/test_manpage.py
@@ -55,7 +55,7 @@ totest['blank'] = [
         ["",
         r""".\" Man page generated from reStructuredText.
 .
-""" + indend_macros + r""".TH ""  "" "" ""
+""" + indend_macros + r""".TH ""  "" ""
 .SH NAME
  \- 
 .\" Generated by docutils manpage writer.
@@ -70,7 +70,7 @@ totest['blank'] = [
 """,
         r""".\" Man page generated from reStructuredText.
 .
-""" + indend_macros + r""".TH "HELLO, WORLD."  "" "" ""
+""" + indend_macros + r""".TH "HELLO, WORLD."  "" ""
 .SH NAME
 Hello, world. \- 
 .sp
@@ -225,7 +225,7 @@ totest['table'] = [
 '''\
 .\\" Man page generated from reStructuredText.
 .
-''' + indend_macros + '''.TH ""  "" "" ""
+''' + indend_macros + '''.TH ""  "" ""
 .SH NAME
  \\- \n\
 .INDENT 0.0
@@ -282,7 +282,7 @@ $
         """\
 .\\" Man page generated from reStructuredText.
 .
-""" + indend_macros + """.TH ""  "" "" ""
+""" + indend_macros + """.TH ""  "" ""
 .SH NAME
  \\- \n\
 optin group with dot as group item
@@ -338,7 +338,7 @@ Section
 '''\
 .\\" Man page generated from reStructuredText.
 .
-''' + indend_macros + '''.TH "DEFINITION LIST TEST"  "" "" ""
+''' + indend_macros + '''.TH "DEFINITION LIST TEST"  "" ""
 .SH NAME
 Definition List Test \\- \n\
 ''' + '''.SS Abstract
@@ -366,7 +366,7 @@ totest['cmdlineoptions'] = [
 """, 
         r""".\" Man page generated from reStructuredText.
 .
-""" + indend_macros + r""".TH ""  "" "" ""
+""" + indend_macros + r""".TH ""  "" ""
 .SH NAME
  \- 
 .INDENT 0.0
@@ -395,7 +395,7 @@ totest['citation'] = [
 """,
         r""".\" Man page generated from reStructuredText.
 .
-""" + indend_macros + r""".TH ""  "" "" ""
+""" + indend_macros + r""".TH ""  "" ""
 .SH NAME
  \- 
 .IP [docutils] 5
@@ -414,7 +414,7 @@ totest['rubric'] = [
 """,
         r""".\" Man page generated from reStructuredText.
 .
-""" + indend_macros + r""".TH ""  "" "" ""
+""" + indend_macros + r""".TH ""  "" ""
 .SH NAME
  \- 
 some rubric
@@ -437,7 +437,7 @@ They are "escaped" anywhere.
 """,
         r""".\" Man page generated from reStructuredText.
 .
-""" + indend_macros + r""".TH ""  "" "" ""
+""" + indend_macros + r""".TH ""  "" ""
 .SH NAME
  \- 
 .INDENT 0.0
@@ -452,6 +452,34 @@ They are \(dqescaped\(dq anywhere.
 """],
     ]
 
+totest['man_header'] = [
+        ["""
+============
+ page title
+============
+
+in short
+--------
+
+:Manual section: 3
+:Manual group: the books
+:Version: 0.0
+:Date: 3/Nov/2022
+
+Test title, docinfo to man page header.
+""",
+        r""".\" Man page generated from reStructuredText.
+.
+""" + indend_macros + r""".TH "PAGE TITLE" 3 "3/Nov/2022" "0.0" "the books"
+.SH NAME
+page title \- in short
+.sp
+Test title, docinfo to man page header.
+.\" Generated by docutils manpage writer.
+.
+"""],
+    ]
+
 
 if __name__ == '__main__':
     import unittest
