manpage: render references in writer not in man/mandoc/...
[docutils.git] / docutils / test / test_writers / test_manpage.py
blob5bf581b6af07877cee54e3dbc7dbe47f170fbc67
1 #! /usr/bin/env python3
3 # Author: engelbert gruber <grubert@users.sourceforge.net>
4 # Copyright: This module has been placed in the public domain.
6 """
7 Tests for manpage writer.
8 """
10 from pathlib import Path
11 import sys
12 import unittest
14 if __name__ == '__main__':
15 # prepend the "docutils root" to the Python library path
16 # so we import the local `docutils` package.
17 sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
19 from docutils.core import publish_string
22 class WriterPublishTestCase(unittest.TestCase):
24 maxDiff = None
26 def test_publish(self):
27 writer_name = 'manpage'
28 for name, cases in totest.items():
29 for casenum, (case_input, case_expected) in enumerate(cases):
30 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
31 output = publish_string(
32 source=case_input,
33 writer_name=writer_name,
34 settings_overrides={
35 '_disable_config': True,
36 'strict_visitor': True,
37 }).decode()
38 self.assertEqual(case_expected, output)
41 indend_macros = r""".
42 .nr rst2man-indent-level 0
44 .de1 rstReportMargin
45 \\$1 \\n[an-margin]
46 level \\n[rst2man-indent-level]
47 level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
49 \\n[rst2man-indent0]
50 \\n[rst2man-indent1]
51 \\n[rst2man-indent2]
53 .de1 INDENT
54 .\" .rstReportMargin pre:
55 . RS \\$1
56 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin]
57 . nr rst2man-indent-level +1
58 .\" .rstReportMargin post:
60 .de UNINDENT
61 . RE
62 .\" indent \\n[an-margin]
63 .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]]
64 .nr rst2man-indent-level -1
65 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
66 .in \\n[rst2man-indent\\n[rst2man-indent-level]]u
68 """
70 totest = {}
72 totest['blank'] = [
73 ["",
74 r""".\" Man page generated from reStructuredText.
76 """ + indend_macros + """.TH "" "" ""
77 .SH NAME
78 \\- \n\
79 .\\" Generated by docutils manpage writer.
81 """],
82 [r"""Hello, world.
83 =============
85 .. WARNING::
86 This broke docutils-sphinx.
88 """,
89 r""".\" Man page generated from reStructuredText.
91 """ + indend_macros + """.TH "HELLO, WORLD." "" ""
92 .SH NAME
93 Hello, world. \\- \n\
94 .sp
95 \\fBWARNING:\\fP
96 .INDENT 0.0
97 .INDENT 3.5
98 This broke docutils\\-sphinx.
99 .UNINDENT
100 .UNINDENT
101 .\\" Generated by docutils manpage writer.
103 """],
106 totest['simple'] = [
107 ["""\
108 ========
109 simple
110 ========
112 ---------------
113 The way to go
114 ---------------
116 :Author: someone@somewhere.net
117 :Date: 2009-08-05
118 :Copyright: public domain
119 :Version: 0.1
120 :Manual section: 1
121 :Manual group: text processing
122 :Arbitrary field: some text
124 SYNOPSIS
125 ========
129 K.I.S.S keep it simple.
131 DESCRIPTION
132 ===========
134 General rule of life.
136 OPTIONS
137 =======
139 --config=<file> Read configuration settings from <file>, if it exists.
140 --version, -V Show this program's version number and exit.
141 --help, -h Show this help message and exit.
143 OtHeR SECTION
144 =============
146 link to http://docutils.sourceforge.io
148 With mixed case.
150 .. Attention::
152 Admonition with title
154 * bullet list
155 * bull and list
157 .. admonition:: homegrown
159 something important
161 . period at line start.
163 and . in a line and at line start
164 .in a paragraph
165 """,
166 r""".\" Man page generated from reStructuredText.
168 """ + indend_macros + r""".TH "SIMPLE" 1 "2009-08-05" "0.1" "text processing"
169 .SH NAME
170 simple \- The way to go
171 .SH SYNOPSIS
172 .INDENT 0.0
173 .INDENT 3.5
176 K.I.S.S keep it simple.
178 .UNINDENT
179 .UNINDENT
180 .SH DESCRIPTION
182 General rule of life.
183 .SH OPTIONS
184 .INDENT 0.0
186 .BI \-\-config\fB= <file>
187 Read configuration settings from <file>, if it exists.
189 .B \-\-version\fP,\fB \-V
190 Show this program\(aqs version number and exit.
192 .B \-\-help\fP,\fB \-h
193 Show this help message and exit.
194 .UNINDENT
195 .SH OTHER SECTION
197 link to <http://docutils.sourceforge.io>
199 With mixed case.
201 \fBATTENTION!:\fP
202 .INDENT 0.0
203 .INDENT 3.5
204 Admonition with title
205 .INDENT 0.0
206 .IP \(bu 2
207 bullet list
208 .IP \(bu 2
209 bull and list
210 .UNINDENT
211 .UNINDENT
212 .UNINDENT
213 .INDENT 0.0
214 .INDENT 3.5
215 .IP "homegrown"
217 something important
218 .UNINDENT
219 .UNINDENT
221 \&. period at line start.
223 and . in a line and at line start
224 \&.in a paragraph
225 .SH AUTHOR
226 someone@somewhere.net
228 Arbitrary field: some text
229 .SH COPYRIGHT
230 public domain
231 .\" Generated by docutils manpage writer.
233 """],
236 totest['table'] = [
237 ["""\
238 ====== =====
239 head and
240 ====== =====
242 abc so
243 ====== =====
244 """,
245 '''\
246 \'\\" t
247 .\\" Man page generated from reStructuredText.
249 ''' + indend_macros + '''.TH "" "" ""
250 .SH NAME
251 \\- \n\
252 .INDENT 0.0
253 .INDENT 3.5
255 box center;
256 l|l.
258 head
259 T}\tT{
265 T}\tT{
271 T}\tT{
275 .UNINDENT
276 .UNINDENT
277 .\\" Generated by docutils manpage writer.
279 ''']
282 totest['optiongroup'] = [
283 ["""
284 optin group with dot as group item
287 bla bla bla
290 bla bla bla
293 bla bla bla
296 bla bla bla
299 bla bla bla
300 """,
301 """\
302 .\\" Man page generated from reStructuredText.
304 """ + indend_macros + """.TH "" "" ""
305 .SH NAME
306 \\- \n\
307 optin group with dot as group item
308 .INDENT 0.0
310 .B $
311 bla bla bla
312 .UNINDENT
313 .INDENT 0.0
315 .B #
316 bla bla bla
317 .UNINDENT
318 .INDENT 0.0
320 .B \\&.
321 bla bla bla
322 .UNINDENT
323 .INDENT 0.0
325 .B [
326 bla bla bla
327 .UNINDENT
328 .INDENT 0.0
330 .B ]
331 bla bla bla
332 .UNINDENT
333 .\\" Generated by docutils manpage writer.
335 """],
338 totest['definitionlist'] = [
339 ["""
340 ====================
341 Definition List Test
342 ====================
344 :Abstract: Docinfo is required.
346 Section
347 =======
349 :term1:
351 Description of Term 1 Description of Term 1 Description of Term 1
352 Description of Term 1 Description of Term 1
354 Description of Term 1 Description of Term 1 Description of Term 1
355 Description of Term 1 Description of Term 1
357 """,
358 '''\
359 .\\" Man page generated from reStructuredText.
361 ''' + indend_macros + '''.TH "DEFINITION LIST TEST" "" ""
362 .SH NAME
363 Definition List Test \\- \n\
364 ''' + '''.SS Abstract
366 Docinfo is required.
367 .SH SECTION
368 .INDENT 0.0
370 .B term1
371 Description of Term 1 Description of Term 1 Description of Term 1
372 Description of Term 1 Description of Term 1
374 Description of Term 1 Description of Term 1 Description of Term 1
375 Description of Term 1 Description of Term 1
376 .UNINDENT
377 .\\" Generated by docutils manpage writer.
379 '''],
382 totest['cmdlineoptions'] = [
383 ["""optional arguments:
384 -h, --help show this help
385 --output FILE, -o FILE output filename
386 -i DEVICE, --input DEVICE input device
387 """,
388 r""".\" Man page generated from reStructuredText.
390 """ + indend_macros + """.TH "" "" ""
391 .SH NAME
392 \\- \n\
393 .INDENT 0.0
395 .B optional arguments:
396 .INDENT 7.0
398 .B \\-h\\fP,\\fB \\-\\-help
399 show this help
401 .BI \\-\\-output \\ FILE\\fR,\\fB \\ \\-o \\ FILE
402 output filename
404 .BI \\-i \\ DEVICE\\fR,\\fB \\ \\-\\-input \\ DEVICE
405 input device
406 .UNINDENT
407 .UNINDENT
408 .\\" Generated by docutils manpage writer.
410 """],
413 totest['citation'] = [
414 [""".. [docutils] blah blah blah
415 .. [empty_citation]
416 """,
417 r""".\" Man page generated from reStructuredText.
419 """ + indend_macros + """.TH "" "" ""
420 .SH NAME
421 \\- \n\
422 .IP [docutils] 5
423 blah blah blah
424 .IP [empty_citation] 5
425 .\\" Generated by docutils manpage writer.
427 """],
430 totest['rubric'] = [
431 [""".. rubric:: some rubric
433 - followed by
434 - a list
435 """,
436 r""".\" Man page generated from reStructuredText.
438 """ + indend_macros + """.TH "" "" ""
439 .SH NAME
440 \\- \n\
441 some rubric
442 .INDENT 0.0
443 .IP \\(bu 2
444 followed by
445 .IP \\(bu 2
446 a list
447 .UNINDENT
448 .\\" Generated by docutils manpage writer.
450 """],
453 totest['double_quote'] = [
454 ["""in "defintion list"
455 double quotes must be escaped on macro invocations.
457 They are "escaped" anywhere.
458 """,
459 r""".\" Man page generated from reStructuredText.
461 """ + indend_macros + """.TH "" "" ""
462 .SH NAME
463 \\- \n\
464 .INDENT 0.0
466 .B in \\(dqdefintion list\\(dq
467 double quotes must be escaped on macro invocations.
468 .UNINDENT
470 They are \\(dqescaped\\(dq anywhere.
471 .\\" Generated by docutils manpage writer.
473 """],
476 totest['man_header'] = [
477 ["""
478 ============
479 page title
480 ============
482 in short
483 --------
485 :Manual section: 3
486 :Manual group: the books
487 :Version: 0.0
488 :Date: 3/Nov/2022
490 Test title, docinfo to man page header.
491 """,
492 r""".\" Man page generated from reStructuredText.
494 """ + indend_macros + r""".TH "PAGE TITLE" 3 "3/Nov/2022" "0.0" "the books"
495 .SH NAME
496 page title \- in short
498 Test title, docinfo to man page header.
499 .\" Generated by docutils manpage writer.
501 """],
505 if __name__ == '__main__':
506 unittest.main()