예제 #1
0
파일: compile.py 프로젝트: guoguo12/templar
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)
예제 #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)
예제 #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)
예제 #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)
예제 #5
0
파일: compile.py 프로젝트: guoguo12/templar
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)
예제 #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)
예제 #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)
예제 #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 ...')