@@ -312,75 +312,6 @@ def getTemplateHtml(self, env: dict[str, Any]) -> str:
312
312
)
313
313
314
314
315
- class Generate :
316
- # pylint: disable=too-few-public-methods
317
- # This class is just here to organize a bunch of related functions together.
318
- # This class should never be instantiated. Generate.generate(...) should
319
- # be called to write the output of a processed Content object.
320
- @staticmethod
321
- def writeMdContents (md : Md ) -> None :
322
- if os .path .exists ("index.html" ):
323
- raise AltezaException (
324
- f"An index.html already exists, and conflicts with { md } , at { os .getcwd ()} ."
325
- )
326
- with open ("index.html" , "w" , encoding = "utf-8" ) as pageHtml :
327
- pageHtml .write (md .getPyPageOutput ())
328
-
329
- @staticmethod
330
- def writeMd (md : Md ) -> None :
331
- if not md .isIndex ():
332
- os .mkdir (md .realName )
333
- with enterDir (md .realName ):
334
- Generate .writeMdContents (md )
335
- else :
336
- Generate .writeMdContents (md )
337
-
338
- @staticmethod
339
- def writeNonMd (nonMd : NonMd ) -> None :
340
- fileName = nonMd .rectifiedFileName
341
- if os .path .exists (fileName ):
342
- raise AltezaException (
343
- f"File { fileName } already exists, and conflicts with { nonMd } ."
344
- )
345
- with open (fileName , "w" , encoding = "utf-8" ) as nonMdPageFile :
346
- nonMdPageFile .write (nonMd .getPyPageOutput ())
347
-
348
- @staticmethod
349
- def writePyPageNode (pyPageNode : PyPageNode ) -> None :
350
- if isinstance (pyPageNode , Md ):
351
- Generate .writeMd (pyPageNode )
352
-
353
- elif isinstance (pyPageNode , NonMd ):
354
- Generate .writeNonMd (pyPageNode )
355
-
356
- else :
357
- raise AltezaException (f"{ pyPageNode } pyPage attribute is invalid." )
358
-
359
- @staticmethod
360
- def linkStaticAsset (fileNode : FileNode , shouldCopy : bool ) -> None :
361
- if shouldCopy :
362
- shutil .copyfile (fileNode .absoluteFilePath , fileNode .fileName )
363
- else :
364
- os .symlink (fileNode .absoluteFilePath , fileNode .fileName )
365
-
366
- @staticmethod
367
- def generate (args : Args , content : Content ) -> None :
368
- def walk (curDir : DirNode ) -> None :
369
- for subDir in filter (lambda node : node .shouldPublish , curDir .subDirs ):
370
- os .mkdir (subDir .dirName )
371
- with enterDir (subDir .dirName ):
372
- walk (subDir )
373
-
374
- for fileNode in filter (lambda node : node .shouldPublish , curDir .files ):
375
- if isinstance (fileNode , PyPageNode ):
376
- Generate .writePyPageNode (fileNode )
377
- else :
378
- Generate .linkStaticAsset (fileNode , args .copy_assets )
379
-
380
- with enterDir (args .output ):
381
- walk (content .rootDir )
382
-
383
-
384
315
@contextlib .contextmanager
385
316
def enterDir (newDir : str ) -> Generator [None , None , None ]:
386
317
# https://stackoverflow.com/a/13847807/908430
@@ -393,6 +324,74 @@ def enterDir(newDir: str) -> Generator[None, None, None]:
393
324
394
325
395
326
class Engine :
327
+ class Generate :
328
+ # These classes are just here to organize a bunch of related functions together.
329
+ # This class should never be instantiated. Generate.generate(...) is the entry
330
+ # point to be called to write the output of a processed Content object.
331
+ # Similarly, Engine.run(args) is used to invoke Alteza overall.
332
+ @staticmethod
333
+ def writeMdContents (md : Md ) -> None :
334
+ if os .path .exists ("index.html" ):
335
+ raise AltezaException (
336
+ f"An index.html already exists, and conflicts with { md } , at { os .getcwd ()} ."
337
+ )
338
+ with open ("index.html" , "w" , encoding = "utf-8" ) as pageHtml :
339
+ pageHtml .write (md .getPyPageOutput ())
340
+
341
+ @staticmethod
342
+ def writeMd (md : Md ) -> None :
343
+ if not md .isIndex ():
344
+ os .mkdir (md .realName )
345
+ with enterDir (md .realName ):
346
+ Engine .Generate .writeMdContents (md )
347
+ else :
348
+ Engine .Generate .writeMdContents (md )
349
+
350
+ @staticmethod
351
+ def writeNonMd (nonMd : NonMd ) -> None :
352
+ fileName = nonMd .rectifiedFileName
353
+ if os .path .exists (fileName ):
354
+ raise AltezaException (
355
+ f"File { fileName } already exists, and conflicts with { nonMd } ."
356
+ )
357
+ with open (fileName , "w" , encoding = "utf-8" ) as nonMdPageFile :
358
+ nonMdPageFile .write (nonMd .getPyPageOutput ())
359
+
360
+ @staticmethod
361
+ def writePyPageNode (pyPageNode : PyPageNode ) -> None :
362
+ if isinstance (pyPageNode , Md ):
363
+ Engine .Generate .writeMd (pyPageNode )
364
+
365
+ elif isinstance (pyPageNode , NonMd ):
366
+ Engine .Generate .writeNonMd (pyPageNode )
367
+
368
+ else :
369
+ raise AltezaException (f"{ pyPageNode } pyPage attribute is invalid." )
370
+
371
+ @staticmethod
372
+ def linkStaticAsset (fileNode : FileNode , shouldCopy : bool ) -> None :
373
+ if shouldCopy :
374
+ shutil .copyfile (fileNode .absoluteFilePath , fileNode .fileName )
375
+ else :
376
+ os .symlink (fileNode .absoluteFilePath , fileNode .fileName )
377
+
378
+ @staticmethod
379
+ def generate (args : Args , content : Content ) -> None :
380
+ def walk (curDir : DirNode ) -> None :
381
+ for subDir in filter (lambda node : node .shouldPublish , curDir .subDirs ):
382
+ os .mkdir (subDir .dirName )
383
+ with enterDir (subDir .dirName ):
384
+ walk (subDir )
385
+
386
+ for fileNode in filter (lambda node : node .shouldPublish , curDir .files ):
387
+ if isinstance (fileNode , PyPageNode ):
388
+ Engine .Generate .writePyPageNode (fileNode )
389
+ else :
390
+ Engine .Generate .linkStaticAsset (fileNode , args .copy_assets )
391
+
392
+ with enterDir (args .output ):
393
+ walk (content .rootDir )
394
+
396
395
@staticmethod
397
396
def checkContentDir (contentDir : str ) -> None :
398
397
if not os .path .isdir (contentDir ):
@@ -445,7 +444,7 @@ def run(args: Args) -> None:
445
444
446
445
content = Engine .process (args )
447
446
print ("Generating..." )
448
- Generate .generate (args , content )
447
+ Engine . Generate .generate (args , content )
449
448
450
449
elapsedMilliseconds = (time .time_ns () - startTimeNs ) / 10 ** 6
451
450
# pylint: disable=consider-using-f-string
0 commit comments