|
9 | 9 |
|
10 | 10 |
|
11 | 11 | class Command(BaseCommand):
|
12 |
| - help = "Renders all patterns." |
| 12 | + help = "Renders all django-pattern-library patterns to HTML files, in a directory structure." |
| 13 | + |
| 14 | + def add_arguments(self, parser): |
| 15 | + super().add_arguments(parser) |
| 16 | + parser.add_argument( |
| 17 | + '--output', |
| 18 | + '-o', |
| 19 | + action='store', |
| 20 | + dest='output_dir', |
| 21 | + default='dpl-rendered-patterns', |
| 22 | + help='Directory where to render your patterns', |
| 23 | + type=str, |
| 24 | + ) |
| 25 | + parser.add_argument( |
| 26 | + '--dry-run', |
| 27 | + action='store_true', |
| 28 | + help="Render the patterns without writing them to disk.", |
| 29 | + ) |
13 | 30 |
|
14 | 31 | def handle(self, **options):
|
| 32 | + self.verbosity = options['verbosity'] |
| 33 | + self.dry_run = options['dry_run'] |
| 34 | + self.output_dir = options['output_dir'] |
| 35 | + |
15 | 36 | templates = get_pattern_templates()
|
| 37 | + |
16 | 38 | factory = RequestFactory()
|
17 | 39 | request = factory.get('/')
|
18 |
| - parent_dir = Path.cwd().joinpath('dpl-static') |
| 40 | + |
| 41 | + if self.verbosity >= 2: |
| 42 | + self.stdout.write(f'Target directory: {self.output_dir}') |
| 43 | + |
| 44 | + # Resolve the output dir according to the directory the command is run from. |
| 45 | + parent_dir = Path.cwd().joinpath(self.output_dir) |
19 | 46 | parent_dir.mkdir(exist_ok=True)
|
| 47 | + |
20 | 48 | self.render_group(request, parent_dir, templates)
|
21 | 49 |
|
22 | 50 | def render_group(self, request, parent_dir: Path, pattern_templates):
|
23 | 51 | for template in pattern_templates['templates_stored']:
|
24 |
| - print(template.origin.template_name) |
25 |
| - print('-------------------') |
| 52 | + if self.verbosity >= 2: |
| 53 | + self.stdout.write(f'Pattern: {template.pattern_name}') |
| 54 | + if self.verbosity >= 1: |
| 55 | + self.stdout.write(template.origin.template_name) |
| 56 | + |
26 | 57 | render_path = parent_dir.joinpath(template.pattern_name)
|
27 |
| - render_path.write_text(render_pattern(request, template.origin.template_name)) |
| 58 | + rendered_pattern = render_pattern(request, template.origin.template_name) |
| 59 | + render_path.write_text(rendered_pattern) |
28 | 60 |
|
29 | 61 | if not pattern_templates['template_groups']:
|
30 | 62 | return
|
31 | 63 |
|
32 | 64 | for pattern_type_group, pattern_templates in pattern_templates['template_groups'].items():
|
33 |
| - print(pattern_type_group) |
| 65 | + if self.verbosity >= 2: |
| 66 | + self.stdout.write(f'Group: {pattern_type_group}') |
34 | 67 | group_parent = parent_dir.joinpath(pattern_type_group)
|
35 | 68 | group_parent.mkdir(exist_ok=True)
|
36 | 69 | self.render_group(request, group_parent, pattern_templates)
|
0 commit comments