Debian Codegraph @

click_man/man.py

raw
1 """
2 click-man - Generate man pages for click application
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5 This module provides functionality to
6 write a man page from some given information
7 about a CLI application.
8
9 :copyright: (c) 2016 by Timo Furrer.
10 :license: MIT, see LICENSE for more details.
11 """
12
13 import os
14 import time
15
16
17 class ManPage(object):
18 """
19 Represent a man page
20
21 :param str command: the name of the command
22 """
23
24 TITLE_KEYWORD = '.TH'
25 SECTION_HEADING_KEYWORD = '.SH'
26 PARAGRAPH_KEYWORD = '.PP'
27 BOLD_KEYWORD = '.B'
28 INDENT_KEYWORDD = '.TP'
29
30 def __init__(self, command):
31 #: Holds the command of the man page
32 self.command = command
33
34 #: Holds the short help of the man page
35 self.short_help = ''
36
37 #: Holds the synopsis of the man page
38 self.synopsis = ''
39
40 #: Holds the description of the man page
41 self.description = ''
42
43 #: Holds the version of the man page
44 self.version = '1.0.0'
45
46 #: Holds a list of tuple options of the man page
47 # the first item in the tuple are the option switches
48 # and the second one is the option's description
49 self.options = []
50
51 #: Holds the commands of the man page
52 self.commands = []
53
54 #: Holds the date of the man page creation time.
55 self.date = time.strftime(
56 '%Y-%m-%d',
57 time.gmtime(int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))),
58 )
59
60 def replace_blank_lines(self, s):
61 """Find any blank lines and replace them with .PP"""
62
63 lines = map(
64 lambda x: self.PARAGRAPH_KEYWORD if x == '' else x, s.split('\n')
65 )
66 return '\n'.join(lines)
67
68 def __str__(self):
69 """
70 Generate and return the string representation
71 of this man page.
72 """
73 lines = []
74
75 # write title and footer
76 lines.append(
77 '{0} "{1}" "1" "{2}" "{3}" "{4} Manual"'.format(
78 self.TITLE_KEYWORD,
79 self.command.upper(),
80 self.date,
81 self.version,
82 self.command,
83 )
84 )
85
86 # write name section
87 lines.append('{0} NAME'.format(self.SECTION_HEADING_KEYWORD))
88 lines.append(
89 r'{0} \- {1}'.format(
90 self.command.replace(' ', r'\-'), self.short_help
91 )
92 )
93
94 # write synopsis
95 lines.append('{0} SYNOPSIS'.format(self.SECTION_HEADING_KEYWORD))
96 lines.append('{0} {1}'.format(self.BOLD_KEYWORD, self.command))
97 lines.append(self.synopsis.replace('-', r'\-'))
98
99 # write the description
100 if self.description:
101 lines.append(
102 '{0} DESCRIPTION'.format(self.SECTION_HEADING_KEYWORD)
103 )
104 lines.append(self.replace_blank_lines(self.description))
105
106 # write the options
107 if self.options:
108 lines.append('{0} OPTIONS'.format(self.SECTION_HEADING_KEYWORD))
109 for option, description in self.options:
110 lines.append(self.INDENT_KEYWORDD)
111 option_unpacked = option.replace('-', r'\-').split()
112 lines.append(
113 r'\fB{0}\fP{1}'.format(
114 option_unpacked[0],
115 (' ' + ' '.join(option_unpacked[1:]))
116 if len(option_unpacked) > 1
117 else '',
118 )
119 )
120 lines.append(self.replace_blank_lines(description))
121
122 # write commands
123 if self.commands:
124 lines.append('{0} COMMANDS'.format(self.SECTION_HEADING_KEYWORD))
125 for name, description in self.commands:
126 lines.append(self.PARAGRAPH_KEYWORD)
127 lines.append(r'\fB{0}\fP'.format(name))
128 lines.append(' ' + self.replace_blank_lines(description))
129 lines.append(
130 r' See \fB{0}-{1}(1)\fP for full documentation on the \fB{1}\fP command.'.format(
131 self.command, name
132 )
133 )
134
135 man_page = '\n'.join(lines)
136
137 if not man_page.endswith('\n'):
138 # Ensure man page ends with newline.
139 man_page += '\n'
140
141 return man_page