def test_extends(): from tempfile import gettempdir from textwrap import dedent from pathlib import Path from ninjadog import render parent_string = dedent(""" h1 Title block content """) child_string = dedent(""" extends parent block content h2 Subtitle """) parent_path = Path(gettempdir(), 'parent.pug') child_path = Path(gettempdir(), 'child.pug') with parent_path.open('w+') as parent, child_path.open('w+') as child: parent.write(parent_string) parent.seek(0) child.write(child_string) child.seek(0) assert render(file=child_path) == '<h1>Title</h1><h2>Subtitle</h2>' assert render(file=str(child_path)) == '<h1>Title</h1><h2>Subtitle</h2>'
def test_render_no_string_argument(): from tempfile import NamedTemporaryFile import ninjadog string = 'h1 hello' with NamedTemporaryFile('w+') as tempfile: tempfile.write(string) tempfile.seek(0) assert ninjadog.render(file=tempfile.name) == ninjadog.render(string) == '<h1>hello</h1>'
def test_conditional(): from textwrap import dedent import ninjadog string = dedent(""" if name == 'sam' h1 hello #{ name } """) assert ninjadog.render(string, context={'name': 'sam'}) == '<h1>hello sam</h1>' string = dedent(""" if person.name == 'sam' h1 hello #{ person.name } """) assert ninjadog.render(string, context={'person': {'name': 'sam'}}) == '<h1>hello sam</h1>'
def get_watch_render(id): print(id) result = get_container(id) result['target'] = platform_name[result['target']] print(result) return ninjadog.render(file='template/watchinfo.pug', context={'server': result})
def test_with_pug_with_jinja2(): from textwrap import dedent from ninjadog import render string = dedent(""" if person.name == "Bob" h1 Hello Bob else h1 My name is #{ person.name } p The persons's uppercase name is {{ person.get('name').upper() }} p The person's name is #{ person.name } if animal h1 This should not output else p animal value is false """).strip() context = {'person': {'name': 'Bob'}, 'animal': None} expected_output = dedent(""" <h1>Hello Bob</h1> <p>The persons's uppercase name is BOB</p> <p>The person's name is Bob</p> <p>animal value is false</p> """).strip() actual_output = render(string, context=context, pretty=True, with_jinja=True).strip() assert expected_output == actual_output
def render_directory(source: Path, destination: Path = None, **kwargs): """ Render a directory of pug templates. Args: source: the source directory destination: the destination directory [default: source] kwargs: arguments that will be passed to the ninjadog renderer Returns: None """ destination = destination or source destination.mkdir(exist_ok=True) pug_templates = filter(lambda p: p.suffix == '.pug', source.iterdir()) for template in pug_templates: rendered_template = render(file=template, **kwargs) new_template = Path(destination, template.stem).with_suffix('.html') new_template.write_text(rendered_template) print('Rendered', new_template.absolute())
def main(argv: T.Optional[T.Iterable] = None): """Render pug template to stdout.""" args = docopt(__doc__, argv=argv, version='0.5.2') if args['--file'] and args['<file>']: raise ValueError("Cannot combine --file and <file> arguments") string = sys.stdin.read() if args['-'] else args['<string>'] file = args['--file'] or args['<file>'] pretty = args['--pretty'] context = args['--context'] with_jinja = args['--with-jinja'] verbose = args['--verbose'] dry_run = args['--dry-run'] source = args['<source>'] destination = args['<destination>'] if dry_run or verbose: print(args, file=sys.stderr, end='\n\n') if dry_run: print('dry run, no changes made', file=sys.stderr) return if source: render_directory(Path(source), Path(destination), pretty=pretty, context=context, with_jinja=with_jinja) return output = render(string=string, file=file, pretty=pretty, context=context, with_jinja=with_jinja) return output
def watches_new_archive_render(): return ninjadog.render(file='template/newserverfromlocal.pug')
def unauthorized_handler(): return ninjadog.render(file='template/unauthorized.pug')
def login_render(): return ninjadog.render(file='template/login.pug')
def test_context(): import ninjadog context = {'name': 'Derp'} assert ninjadog.render('h1 hello #{ name }', context=context) == '<h1>hello Derp</h1>' assert ninjadog.render("h1= name", context=context) == '<h1>Derp</h1>'
def test_jinja2_variable(): from ninjadog import render assert render('h1 {{ title }}', context={'title': 'hello world'}, with_jinja=True) == '<h1>hello world</h1>'
def test_pug_variable(): from ninjadog import render assert render('h1= title', context={'title': 'hello world'}) == '<h1>hello world</h1>'
def test_hello_world(): from ninjadog import render assert render('h1 hello world') == '<h1>hello world</h1>'
def home(): return ninjadog.render(file='template/home.pug')
def watches_new_render(): return ninjadog.render(file='template/newserver.pug')
def watches_render(): servers = get_servers() print(servers) return ninjadog.render(file='template/watches.pug', context={'servers': servers})