Exemple #1
0
def main(args, configs):
    if args.source:
        if not file_exists(args.source):
            log.warn('File ' + args.source + ' does not exist.')
            exit(1)
        elif not os.path.isfile(args.source):
            log.warn(args.source + ' is not a valid file')
            exit(1)
        result = link.link(args.source)
        if args.markdown:
            markdown_obj = Markdown(result)
            for k, v in markdown_obj.variables.items():
                configs['VARIABLES'][k] = v
            result = markdown_obj.text
        result = link.substitutions(result,
                                    configs.get('SUBSTITUTIONS', []),
                                    args)
        result, cache = link.retrieve_blocks(result)
        if 'TOC_BUILDER' in configs:
            configs['VARIABLES']['table-of-contents'] = link.scrape_headers(result, configs['TOC_BUILDER'])
            for import_stmt in (
                    'from datetime import datetime',
                    ):
                exec(import_stmt, configs)
        for k, v in cache.items():
            configs['VARIABLES'][k] = v

    result = compile(args.template, configs)
    if not args.destination:
        log.log(result)
        return
    file_write(args.destination, result)
    log.info('Result can be found at ' + args.destination)
Exemple #2
0
def main(args, configs):
    if args.source:
        if not file_exists(args.source):
            log.warn('File ' + args.source + ' does not exist.')
            exit(1)
        elif not os.path.isfile(args.source):
            log.warn(args.source + ' is not a valid file')
            exit(1)
        result = link.link(args.source)
        if args.markdown:
            markdown_obj = Markdown(result)
            for k, v in markdown_obj.variables.items():
                configs['VARIABLES'][k] = v
            result = markdown_obj.text
        result = link.substitutions(result, configs.get('SUBSTITUTIONS', []),
                                    args)
        result, cache = link.retrieve_blocks(result)
        if 'TOC_BUILDER' in configs:
            configs['VARIABLES']['table-of-contents'] = link.scrape_headers(
                result, configs['TOC_BUILDER'])
            for import_stmt in ('from datetime import datetime', ):
                exec(import_stmt, configs)
        for k, v in cache.items():
            configs['VARIABLES'][k] = v

    result = compile(args.template, configs)
    if not args.destination:
        log.log(result)
        return
    file_write(args.destination, result)
    log.info('Result can be found at ' + args.destination)
Exemple #3
0
def main(args=None):
    if not args:
        parser = argparse.ArgumentParser()
        cmd_options(parser)
        args = parser.parse_args()
    if not args.source:
        text = ''
        log.log('--- BEGIN MARKDOWN (type Ctrl-D to finish) ---')
        while True:
            try:
                text += input() + '\n'
            except EOFError:
                log.log('--- END MARKDOWN ---')
                break
            except KeyboardInterrupt:
                log.warn('Aborting script')
                exit(1)
    else:
        if not os.path.exists(args.source):
            log.warn('File ' + args.source + ' does not exist.')
            exit(1)
        elif not os.path.isfile(args.source):
            log.warn(args.source + ' is not a valid file')
            exit(1)
        with open(args.source, 'r') as f:
            text = f.read()
    result = convert(text)
    if args.destination:
        with open(args.destination, 'w') as f:
            f.write(result)
        log.info('Result can be found in ' + args.destination)
    else:
        log.log(result)
Exemple #4
0
def main(args=None):
    if not args:
        parser = argparse.ArgumentParser()
        cmd_options(parser)
        args = parser.parse_args()
    if not args.source:
        text = ''
        log.log('--- BEGIN MARKDOWN (type Ctrl-D to finish) ---')
        while True:
            try:
                text += input() + '\n'
            except EOFError:
                log.log('--- END MARKDOWN ---')
                break
            except KeyboardInterrupt:
                log.warn('Aborting script')
                exit(1)
    else:
        if not os.path.exists(args.source):
            log.warn('File ' + args.source + ' does not exist.')
            exit(1)
        elif not os.path.isfile(args.source):
            log.warn(args.source + ' is not a valid file')
            exit(1)
        with open(args.source, 'r') as f:
            text = f.read()
    result = convert(text)
    if args.destination:
        with open(args.destination, 'w') as f:
            f.write(result)
        log.info('Result can be found in ' + args.destination)
    else:
        log.log(result)
Exemple #5
0
def get_template(filename, template_dirs):
    """Return the contents of `filename` as a string.

    PARAMETERS:
    filename -- string: name of template file relative to a 'template'
                directory

    BEHAVIOR:
    `filename` should be of the format:

        [<app>:]<filepath>

    The filepath is expected to be a relative path to a template file
    (usually an html template). If <app> is provided, `get_template`
    will look only in that app's template directory. Otherwise,
    `get_template` will look through the list TEMPLATE_DIRS in order,
    and search in a directory called 'templates' in each of them for
    `filename`.

    By default, the repo home directory is searched first,
    before any app directories.

    If no such `filename` is found, the program exits with status 1.
    """
    if ':' in filename:
        app, filename = filename.split(':')
        dirs = [path for path in template_dirs if app in path]
    else:
        dirs = template_dirs
    for path in dirs:
        template = os.path.join(path, 'templates', filename)
        if file_exists(template):
            return file_read(template)
    log.warn('The template "' + filename \
            + '" could not be found in these directories:')
    for path in dirs:
        log.log(os.path.join(path, 'templates'))
    exit(1)
Exemple #6
0
def get_template(filename, template_dirs):
    """Return the contents of `filename` as a string.

    PARAMETERS:
    filename -- string: name of template file relative to a 'template'
                directory

    BEHAVIOR:
    `filename` should be of the format:

        [<app>:]<filepath>

    The filepath is expected to be a relative path to a template file
    (usually an html template). If <app> is provided, `get_template`
    will look only in that app's template directory. Otherwise,
    `get_template` will look through the list TEMPLATE_DIRS in order,
    and search in a directory called 'templates' in each of them for
    `filename`.

    By default, the repo home directory is searched first,
    before any app directories.

    If no such `filename` is found, the program exits with status 1.
    """
    if ':' in filename:
        app, filename = filename.split(':')
        dirs = [path for path in template_dirs if app in path]
    else:
        dirs = template_dirs
    for path in dirs:
        template = os.path.join(path, 'templates', filename)
        if file_exists(template):
            return file_read(template)
    log.warn('The template "' + filename \
            + '" could not be found in these directories:')
    for path in dirs:
        log.log(os.path.join(path, 'templates'))
    exit(1)
Exemple #7
0
def main(args, configs):
    if not os.path.exists(args.source):
        log.warn('File ' + args.source + ' does not exist.')
        exit(1)
    elif not os.path.isfile(args.source):
        log.warn(args.source + ' is not a valid file')
        exit(1)
    result = link(args.source)
    if args.markdown:
        result = convert(result)
    result = substitutions(result,
                           configs.get('SUBSTITUTIONS', []),
                           args)
    result, cache = retrieve_blocks(result)
    if args.destination:
        file_write(args.destination, result)
        log.info('Result can be found in ' + args.destination)
    else:
        log.log('--- BEGIN RESULT ---', args.quiet)
        log.log(result)
        log.log('--- END RESULT ---', args.quiet)
Exemple #8
0
    parser.add_argument('-q', '--quiet', action='store_true',
                        help="Suppresses extraneous output")
    parser.add_argument('-c', '--conditions', action='append',
                        help="Specify conditions for substitutions")

def main(args, configs):
    if not os.path.exists(args.source):
        log.warn('File ' + args.source + ' does not exist.')
        exit(1)
    elif not os.path.isfile(args.source):
        log.warn(args.source + ' is not a valid file')
        exit(1)
    result = link(args.source)
    if args.markdown:
        result = convert(result)
    result = substitutions(result,
                           configs.get('SUBSTITUTIONS', []),
                           args)
    result, cache = retrieve_blocks(result)
    if args.destination:
        file_write(args.destination, result)
        log.info('Result can be found in ' + args.destination)
    else:
        log.log('--- BEGIN RESULT ---', args.quiet)
        log.log(result)
        log.log('--- END RESULT ---', args.quiet)

if __name__ == '__main__':
    log.log('Usage: python3 __main__.py link ...')