|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Author : Ken Youens-Clark <kyclark@gmail.com> |
| 4 | +Date : 2019-05-29 |
| 5 | +Purpose: Compile my book |
| 6 | +""" |
| 7 | + |
| 8 | +import argparse |
| 9 | +import os |
| 10 | +import sys |
| 11 | +from subprocess import getstatusoutput |
| 12 | +from dire import die |
| 13 | + |
| 14 | + |
| 15 | +# -------------------------------------------------- |
| 16 | +def get_args(): |
| 17 | + """Get command-line arguments""" |
| 18 | + |
| 19 | + parser = argparse.ArgumentParser( |
| 20 | + description='Compile my book', |
| 21 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 22 | + |
| 23 | + # parser.add_argument('positional', |
| 24 | + # metavar='str', |
| 25 | + # help='A positional argument') |
| 26 | + |
| 27 | + parser.add_argument('-i', |
| 28 | + '--dir', |
| 29 | + help='Input dir', |
| 30 | + metavar='str', |
| 31 | + type=str, |
| 32 | + default=os.getcwd()) |
| 33 | + |
| 34 | + parser.add_argument('-o', |
| 35 | + '--outdir', |
| 36 | + help='Output dir', |
| 37 | + metavar='str', |
| 38 | + type=str, |
| 39 | + default=os.getcwd()) |
| 40 | + |
| 41 | + parser.add_argument('-c', |
| 42 | + '--contents', |
| 43 | + help='Table of contents', |
| 44 | + metavar='str', |
| 45 | + type=str, |
| 46 | + default=None) |
| 47 | + |
| 48 | + # parser.add_argument('-i', |
| 49 | + # '--int', |
| 50 | + # help='A named integer argument', |
| 51 | + # metavar='int', |
| 52 | + # type=int, |
| 53 | + # default=0) |
| 54 | + |
| 55 | + # parser.add_argument('-f', |
| 56 | + # '--flag', |
| 57 | + # help='A boolean flag', |
| 58 | + # action='store_true') |
| 59 | + |
| 60 | + return parser.parse_args() |
| 61 | + |
| 62 | + |
| 63 | +# -------------------------------------------------- |
| 64 | +def main(): |
| 65 | + """Make a jazz noise here""" |
| 66 | + |
| 67 | + args = get_args() |
| 68 | + in_dir = args.dir |
| 69 | + out_dir = args.outdir |
| 70 | + contents = args.contents |
| 71 | + |
| 72 | + if not contents: |
| 73 | + cur_dir = os.path.dirname(sys.argv[0]) |
| 74 | + contents = os.path.join(cur_dir, 'contents.txt') |
| 75 | + |
| 76 | + if not os.path.isfile(contents): |
| 77 | + die('Bad --contents "{}"'.format(contents)) |
| 78 | + |
| 79 | + book_file = os.path.join(out_dir, 'book.md') |
| 80 | + with open(book_file, 'wt') as fh: |
| 81 | + for i, dir_name in enumerate(map(str.rstrip, open(contents)), 1): |
| 82 | + print('{:3}: {}'.format(i, dir_name)) |
| 83 | + readme = os.path.join(in_dir, dir_name, 'README.md') |
| 84 | + if os.path.isfile(readme): |
| 85 | + print('\tREADME') |
| 86 | + fh.write('# Chapter {}\n\n'.format(i)) |
| 87 | + fh.write(open(readme).read()) |
| 88 | + fh.write('\n\\pagebreak\n\n') |
| 89 | + |
| 90 | + solution = os.path.join(in_dir, dir_name, 'solution.py') |
| 91 | + if os.path.isfile(solution): |
| 92 | + print('\tSOLUTION') |
| 93 | + fh.write('# {} Solution\n\n'.format(dir_name)) |
| 94 | + fh.write('````\n') |
| 95 | + fh.write(open(solution).read()) |
| 96 | + fh.write('````\n') |
| 97 | + fh.write('\n\\pagebreak\n\n') |
| 98 | + |
| 99 | + cmd = 'pandoc {} --pdf-engine=xelatex -o book.pdf' |
| 100 | + rv, out = getstatusoutput(cmd.format(book_file)) |
| 101 | + |
| 102 | + if rv != 0: |
| 103 | + die('Error: {}'.format(out)) |
| 104 | + |
| 105 | +# -------------------------------------------------- |
| 106 | +if __name__ == '__main__': |
| 107 | + main() |
0 commit comments