1
1
import os
2
2
import webbrowser
3
3
from enum import Enum
4
+ from pathlib import Path
4
5
from typing import Any , Callable , Dict , Generator , List , Optional , Type , Union
5
6
6
7
import click
@@ -35,7 +36,7 @@ class Converter(BaseModel): # type: ignore
35
36
assets_dir : str = "{basename}_assets"
36
37
template : Optional [str ] = None
37
38
38
- def convert_to (self , dest : str ) -> None :
39
+ def convert_to (self , dest : Path ) -> None :
39
40
"""Converts self, i.e., a list of presentations, into a given format."""
40
41
raise NotImplementedError
41
42
@@ -45,7 +46,7 @@ def load_template(self) -> str:
45
46
An empty string is returned if no template is used."""
46
47
return ""
47
48
48
- def open (self , file : str ) -> bool :
49
+ def open (self , file : Path ) -> bool :
49
50
"""Opens a file, generated with converter, using appropriate application."""
50
51
raise NotImplementedError
51
52
@@ -285,12 +286,12 @@ class Config:
285
286
use_enum_values = True
286
287
extra = "forbid"
287
288
288
- def get_sections_iter (self ) -> Generator [str , None , None ]:
289
+ def get_sections_iter (self , assets_dir : Path ) -> Generator [str , None , None ]:
289
290
"""Generates a sequence of sections, one per slide, that will be included into the html template."""
290
291
for presentation_config in self .presentation_configs :
291
292
for slide_config in presentation_config .slides :
292
293
file = presentation_config .files [slide_config .start_animation ]
293
- file = os . path . join ( self . assets_dir , os . path . basename ( file ))
294
+ file = assets_dir / file . name
294
295
295
296
# TODO: document this
296
297
# Videos are muted because, otherwise, the first slide never plays correctly.
@@ -312,26 +313,27 @@ def load_template(self) -> str:
312
313
__name__ , "data/revealjs_template.html"
313
314
).decode ()
314
315
315
- def open (self , file : str ) -> bool :
316
- return webbrowser .open (file )
316
+ def open (self , file : Path ) -> bool :
317
+ return webbrowser .open (file . absolute (). as_uri () )
317
318
318
- def convert_to (self , dest : str ) -> None :
319
+ def convert_to (self , dest : Path ) -> None :
319
320
"""Converts this configuration into a RevealJS HTML presentation, saved to DEST."""
320
- dirname = os .path .dirname (dest )
321
- basename , ext = os .path .splitext (os .path .basename (dest ))
321
+ dirname = dest .parent
322
+ basename = dest .stem
323
+ ext = dest .suffix
322
324
323
- self . assets_dir = self . assets_dir . format (
324
- dirname = dirname , basename = basename , ext = ext
325
+ assets_dir = Path (
326
+ self . assets_dir . format ( dirname = dirname , basename = basename , ext = ext )
325
327
)
326
- full_assets_dir = os . path . join ( dirname , self . assets_dir )
328
+ full_assets_dir = dirname / assets_dir
327
329
328
330
os .makedirs (full_assets_dir , exist_ok = True )
329
331
330
332
for presentation_config in self .presentation_configs :
331
- presentation_config .concat_animations ().move_to (full_assets_dir )
333
+ presentation_config .concat_animations ().copy_to (full_assets_dir )
332
334
333
335
with open (dest , "w" ) as f :
334
- sections = "" .join (self .get_sections_iter ())
336
+ sections = "" .join (self .get_sections_iter (full_assets_dir ))
335
337
336
338
revealjs_template = self .load_template ()
337
339
content = revealjs_template .format (sections = sections , ** self .dict ())
@@ -396,7 +398,7 @@ def callback(ctx: Context, param: Parameter, value: bool) -> None:
396
398
@click .command ()
397
399
@click .argument ("scenes" , nargs = - 1 )
398
400
@folder_path_option
399
- @click .argument ("dest" )
401
+ @click .argument ("dest" , type = click . Path ( dir_okay = False , path_type = Path ) )
400
402
@click .option (
401
403
"--to" ,
402
404
type = click .Choice (["html" ], case_sensitive = False ),
@@ -423,21 +425,21 @@ def callback(ctx: Context, param: Parameter, value: bool) -> None:
423
425
"--use-template" ,
424
426
"template" ,
425
427
metavar = "FILE" ,
426
- type = click .Path (exists = True , dir_okay = False ),
428
+ type = click .Path (exists = True , dir_okay = False , path_type = Path ),
427
429
help = "Use the template given by FILE instead of default one. To echo the default template, use `--show-template`." ,
428
430
)
429
431
@show_template_option
430
432
@show_config_options
431
433
@verbosity_option
432
434
def convert (
433
435
scenes : List [str ],
434
- folder : str ,
435
- dest : str ,
436
+ folder : Path ,
437
+ dest : Path ,
436
438
to : str ,
437
439
open_result : bool ,
438
440
force : bool ,
439
441
config_options : Dict [str , str ],
440
- template : Optional [str ],
442
+ template : Optional [Path ],
441
443
) -> None :
442
444
"""
443
445
Convert SCENE(s) into a given format and writes the result in DEST.
0 commit comments