Skip to content

Commit

Permalink
Begin to fix output option
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Dec 6, 2016
1 parent 61ee2fe commit f1e5488
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 162 deletions.
18 changes: 16 additions & 2 deletions src/shellman/cli.py
Expand Up @@ -24,6 +24,7 @@
"""

import argparse
import os
import sys

from .doc import Doc
Expand Down Expand Up @@ -65,13 +66,26 @@ def main(argv=None):
'-i', '--whitelist', '--ignore', action='store', dest='whitelist',
help='whitelisted tags: "customtag:1+,customtag2"')
parser.add_argument(
'-o', '--output', action='store', dest='output', nargs=1,
'-o', '--output', action='store', dest='output',
default=sys.stdout,
help='file to write to (stdout by default)')
parser.add_argument(
'-w', '--warn', action='store_true', dest='warn',
help='actually display the warnings (false)')
parser.add_argument('FILE', help='path to the file to read')

def valid_file(value):
if not value:
raise argparse.ArgumentTypeError("'' is not a valid file path")
elif not os.path.exists(value):
raise argparse.ArgumentTypeError("%s is not a valid file path" %
value)
elif os.path.isdir(value):
raise argparse.ArgumentTypeError("%s is a directory, "
"not a regular file" % value)
return value

parser.add_argument('FILE', type=valid_file,
help='path to the file to read')

args = parser.parse_args(argv)

Expand Down
1 change: 0 additions & 1 deletion src/shellman/formatter/base.py
Expand Up @@ -93,7 +93,6 @@ def write(self):
self.write_init()
for section in self.SECTIONS_ORDER:
self.render[section](section)
# self.output.close()

def write_init(self):
"""Write some header on stdout (useful for man pages)."""
Expand Down
118 changes: 59 additions & 59 deletions src/shellman/formatter/man.py
Expand Up @@ -54,40 +54,40 @@ def esc(self, string):
return string

def write_init(self):
print('.if n.ad l')
print('.nh')
print('.TH %s 1 "%s" "Shellman %s" "User Commands"' % (
self.out('.if n.ad l')
self.out('.nh')
self.out('.TH %s 1 "%s" "Shellman %s" "User Commands"' % (
self.doc['_file'], self.esc(self.doc['date']) or '', __version__))

def render_single_many(self, title, value):
if value:
print('.SH "%s"' % title)
print('%s' % self.esc(''.join(value)))
self.out('.SH "%s"' % title)
self.out('%s' % self.esc(''.join(value)))

def render_multi_many(self, title, value):
if value:
print('.SH "%s"' % title)
self.out('.SH "%s"' % title)
for v in value:
if len(v) == 1:
s = v[0].split(' ')
h, b = s[0], ' '.join(s[1:]).rstrip('\n')
else:
h, b = v[0].rstrip('\n'), ''.join(v[1:]).rstrip('\n')
print('.IP "\\fB%s\\fR" 4' % h)
print(self.esc(b))
self.out('.IP "\\fB%s\\fR" 4' % h)
self.out(self.esc(b))

def render_multi_many_no_head(self, title, value):
if value:
print('.SH "%s"' % title)
self.out('.SH "%s"' % title)
for v in value:
print('%s' % self.esc(''.join(v)))
self.out('%s' % self.esc(''.join(v)))

def render_authors(self, title):
if self.doc['author']:
print('.SH "%s"' % title)
self.out('.SH "%s"' % title)
for author in self.doc['author']:
print('.br')
print(author)
self.out('.br')
self.out(author)

def render_bugs(self, title):
self.render_multi_many_no_head(title, self.doc['bug'])
Expand Down Expand Up @@ -120,80 +120,80 @@ def render_files(self, title):
self.render_multi_many(title, self.doc['file'])

def render_function_fn(self, fn):
print('.IP "\\fB%s\\fR" 4' % self.esc(fn['fn']))
self.out('.IP "\\fB%s\\fR" 4' % self.esc(fn['fn']))

def render_function_brief(self, fn):
if fn['brief']:
print('%s' % self.esc(fn['brief']))
print('')
self.out('%s' % self.esc(fn['brief']))
self.out('')

def render_function_desc(self, fn):
if fn['desc']:
print('%s' % self.esc(''.join(fn['desc'])))
self.out('%s' % self.esc(''.join(fn['desc'])))

def render_function_param(self, fn):
if fn['param']:
print('.ul')
print('Parameters:')
self.out('.ul')
self.out('Parameters:')
for param in fn['param']:
if len(param) == 1:
s = param[0].split(' ')
param, desc = s[0], s[1:]
print(' \\fB%-12s\\fR %s' % (
self.out(' \\fB%-12s\\fR %s' % (
param, self.esc(' '.join(desc)).rstrip('\n')))
else:
param, desc = param[0], param[1:]
print(' \\fB%s\\fR' % self.esc(param).rstrip('\n'))
print(' %s' % self.esc(''.join(desc)))
print('')
self.out(' \\fB%s\\fR' % self.esc(param).rstrip('\n'))
self.out(' %s' % self.esc(''.join(desc)))
self.out('')

def render_function_pre(self, fn):
if fn['pre']:
print('.ul')
print('Preconditions:')
self.out('.ul')
self.out('Preconditions:')
for pre in fn['pre']:
print(' %s' % self.esc(''.join(pre)))
print('')
self.out(' %s' % self.esc(''.join(pre)))
self.out('')

def render_function_return(self, fn):
if fn['return']:
print('.ul')
print('Return code:')
self.out('.ul')
self.out('Return code:')
for ret in fn['return']:
print(' %s' % self.esc(''.join(ret)))
print('')
self.out(' %s' % self.esc(''.join(ret)))
self.out('')

def render_function_seealso(self, fn):
if fn['seealso']:
print('.ul')
print('See also:')
self.out('.ul')
self.out('See also:')
for seealso in fn['seealso']:
print(' %s' % self.esc(''.join(seealso)))
print('')
self.out(' %s' % self.esc(''.join(seealso)))
self.out('')

def render_function_stderr(self, fn):
if fn['stderr']:
print('.ul')
print('Standard error:')
self.out('.ul')
self.out('Standard error:')
for stderr in fn['stderr']:
print(' %s' % self.esc(''.join(stderr)))
print('')
self.out(' %s' % self.esc(''.join(stderr)))
self.out('')

def render_function_stdin(self, fn):
if fn['stdin']:
print('.ul')
print('Standard input:')
self.out('.ul')
self.out('Standard input:')
for stdin in fn['stdin']:
print(' %s' % self.esc(''.join(stdin)))
print('')
self.out(' %s' % self.esc(''.join(stdin)))
self.out('')

def render_function_stdout(self, fn):
if fn['stdout']:
print('.ul')
print('Standard output:')
self.out('.ul')
self.out('Standard output:')
for stdout in fn['stdout']:
print(' %s' % self.esc(''.join(stdout)))
print('')
self.out(' %s' % self.esc(''.join(stdout)))
self.out('')

def render_function(self, fn):
for order in FUNCTION_ORDER:
Expand All @@ -203,11 +203,11 @@ def render_functions(self, title):
if not self.doc['_fn']:
return

print('.SH "%s"' % title)
self.out('.SH "%s"' % title)
# summary
for fn in self.doc['_fn']:
print('%s' % self.esc(fn['fn']))
print('.br')
self.out('%s' % self.esc(fn['fn']))
self.out('.br')

# all
for fn in self.doc['_fn']:
Expand All @@ -221,21 +221,21 @@ def render_license(self, title):

def render_name(self, title):
if self.doc['brief']:
print('.SH "%s"' % title)
print('%s \- %s' % (self.doc['_file'],
self.esc(self.doc['brief'])))
self.out('.SH "%s"' % title)
self.out('%s \- %s' % (self.doc['_file'],
self.esc(self.doc['brief'])))

def render_notes(self, title):
self.render_multi_many_no_head(title, self.doc['note'])

def render_options(self, title):
if not self.doc['option']:
return
print('.SH "%s"' % title)
self.out('.SH "%s"' % title)
for option in self.doc['option']:
print('.IP "\\fB%s\\fR" 4' % option[0]
.rstrip('\n')
.replace(',', '\\fR,\\fB'))
self.out('.IP "\\fB%s\\fR" 4' % option[0]
.rstrip('\n')
.replace(',', '\\fR,\\fB'))
sys.stdout.write(''.join(option[1:]))

def render_see_also(self, title):
Expand All @@ -253,14 +253,14 @@ def render_stdout(self, title):
def render_usage(self, title):
if not self.doc['usage']:
return
print('.SH "%s"' % title)
self.out('.SH "%s"' % title)
rep_reg_opt = re.compile(r'(--?[a-z0-9-]+=?)')
rep_reg_arg = re.compile(r'([A-Z]+)')
for usage in self.doc['usage']:
syn = ''.join(usage)
syn = rep_reg_arg.sub(r'\\fI\1\\fR', syn) # order is important!
syn = rep_reg_opt.sub(r'\\fB\1\\fR', syn)
print('.br')
self.out('.br')
sys.stdout.write('\\fB%s\\fR %s' % (
self.doc['_file'], self.esc(syn)))

Expand Down

0 comments on commit f1e5488

Please sign in to comment.