4
4
import logging
5
5
import math
6
6
import os
7
+ import re
7
8
import sys
8
9
from urllib .parse import urljoin
9
10
@@ -295,6 +296,9 @@ def on_page_markdown(self, markdown, page, config, files):
295
296
#
296
297
# translate title
297
298
translated_page_title , _title_in_pofile = (None , False )
299
+ # translated custom description
300
+ page_meta_description = page .meta .get ('description' )
301
+ translated_page_desc , _desc_in_pofile = (None , False )
298
302
299
303
# translate site_name and site_description
300
304
translated_config_settings = {
@@ -304,15 +308,52 @@ def on_page_markdown(self, markdown, page, config, files):
304
308
key : False for key in self .config ['translate' ]
305
309
}
306
310
307
- for entry in po :
308
- if entry .msgid == page .title :
309
- # matching title found
310
- entry .obsolete = False
311
- translated_page_title = entry .msgstr
312
- _title_in_pofile = True
313
- _translated_entries_msgids .append (page .title )
314
- if entry .msgstr :
315
- _translated_entries_msgstrs .append (page .title )
311
+ if page_meta_description :
312
+ for entry in po :
313
+ if entry .msgid == page .title :
314
+ # matching title found
315
+ entry .obsolete = False
316
+ translated_page_title = entry .msgstr
317
+ _title_in_pofile = True
318
+ if entry .msgstr :
319
+ _translated_entries_msgstrs .append (
320
+ entry .msgstr ,
321
+ )
322
+
323
+ if entry .msgid == page_meta_description :
324
+ # matching description found
325
+ entry .obsolete = False
326
+ translated_page_desc = entry .msgstr
327
+ _desc_in_pofile = True
328
+ if entry .msgstr :
329
+ _translated_entries_msgstrs .append (
330
+ page_meta_description ,
331
+ )
332
+
333
+ # add description to PO file if not added
334
+ if not _desc_in_pofile :
335
+ po .insert (
336
+ 0 ,
337
+ polib .POEntry (
338
+ msgid = page_meta_description ,
339
+ msgstr = '' ,
340
+ ),
341
+ )
342
+
343
+ _translated_entries_msgids .append (
344
+ page_meta_description ,
345
+ )
346
+ else :
347
+ for entry in po :
348
+ if entry .msgid == page .title :
349
+ # matching title found
350
+ entry .obsolete = False
351
+ translated_page_title = entry .msgstr
352
+ _title_in_pofile = True
353
+ if entry .msgstr :
354
+ _translated_entries_msgstrs .append (
355
+ entry .msgstr ,
356
+ )
316
357
317
358
for entry in compendium_pofile :
318
359
for setting in translated_config_settings :
@@ -381,6 +422,19 @@ def on_page_markdown(self, markdown, page, config, files):
381
422
events = po2md_events ,
382
423
wrapwidth = math .inf , # ignore line wrapping
383
424
)
425
+ if page_meta_description :
426
+ po2md .translated_entries .append (
427
+ polib .POEntry (
428
+ msgid = page_meta_description ,
429
+ msgstr = '' ,
430
+ ),
431
+ )
432
+ po2md .translated_entries .append (
433
+ polib .POEntry (
434
+ msgid = page .title ,
435
+ msgstr = '' ,
436
+ ),
437
+ )
384
438
content = po2md .translate (markdown )
385
439
386
440
_disabled_msgids = [
@@ -395,6 +449,7 @@ def on_page_markdown(self, markdown, page, config, files):
395
449
# mock variables if the file is excluded from being translated
396
450
content = markdown
397
451
translated_page_title = None
452
+ translated_page_desc = None
398
453
_disabled_msgids = []
399
454
_translated_entries_msgstrs = []
400
455
_translated_entries_msgids = []
@@ -427,6 +482,8 @@ def on_page_markdown(self, markdown, page, config, files):
427
482
new_file ,
428
483
config ,
429
484
)
485
+ if translated_page_desc :
486
+ new_page .meta ['description' ] = translated_page_desc
430
487
431
488
# overwrite the edit uri for the translated page targetting
432
489
# the PO file located in the repository
@@ -459,6 +516,15 @@ def on_page_markdown(self, markdown, page, config, files):
459
516
self .translations .config_settings [language ] = (
460
517
translated_config_settings
461
518
)
519
+ if language not in self .translations .page_metas :
520
+ self .translations .page_metas [language ] = {}
521
+ if (
522
+ new_file .src_path
523
+ not in self .translations .page_metas [language ]
524
+ ):
525
+ self .translations .page_metas [
526
+ language
527
+ ][new_file .src_path ] = new_page .meta
462
528
463
529
# change file url
464
530
url = removesuffix (new_page .file .url , '.md' ) + '.html'
@@ -601,28 +667,43 @@ def on_post_page(self, output, page, config):
601
667
f'{ config ["site_name" ]} </title>' ,
602
668
f'{ tr_settings ["site_name" ]} </title>' ,
603
669
)
604
- if tr_settings .get ('site_description' ):
605
- # insert site_description into description meta tag
606
- # if the file is a translated index, only for
607
- # readthedocs and mkdocs themes
608
- if (
609
- config ['theme' ].name in {'mkdocs' , 'readthedocs' } and
610
- removepreffix (page .file .url , language ).count ('/' ) == 1
611
- ):
670
+
671
+ meta_description = self .translations .page_metas [
672
+ language
673
+ ][page .file .src_path ].get ('description' )
674
+
675
+ if (
676
+ meta_description or
677
+ tr_settings .get ('site_description' ) or
678
+ config .get ('site_description' )
679
+ ):
680
+ if meta_description :
681
+ tr_description = meta_description
682
+ elif tr_settings .get ('site_description' ):
683
+ tr_description = tr_settings ['site_description' ]
684
+ elif config .get ('site_description' ):
685
+ tr_description = config ['site_description' ]
686
+
687
+ if '<meta name="description"' not in output :
612
688
output = output .replace (
613
689
'/title>' ,
614
690
(
615
691
'/title><meta name="description"'
616
- f' content="{ tr_settings ["site_description" ]} "'
617
- ' />'
692
+ ' content="">'
618
693
),
619
694
)
620
- else :
621
- # mkdocs-material theme includes the description
622
- # in all pages
623
- output = output .replace (
624
- f'content="{ config ["site_description" ]} "' ,
625
- f'content="{ tr_settings ["site_description" ]} "' ,
695
+
696
+ if not (
697
+ config ['theme' ].name in {'mkdocs' , 'readthedocs' } and
698
+ removepreffix (page .file .url , language ).count ('/' ) > 1
699
+ ):
700
+ output = re .sub (
701
+ r'<meta name="description" content="[^"]*"' ,
702
+ (
703
+ '<meta name="description"'
704
+ f' content="{ tr_description } "'
705
+ ),
706
+ output ,
626
707
)
627
708
628
709
# write translated HTML file to 'site' directory
@@ -709,9 +790,8 @@ def on_post_build(self, config):
709
790
710
791
_msgids_appended_to_compendium = []
711
792
for translation in translations :
712
- po = polib .pofile (translation .po_filepath )
713
793
_entry_found = None
714
- for entry in po :
794
+ for entry in translation . po :
715
795
if entry .msgid == repeated_msgid :
716
796
if (
717
797
repeated_msgid not in
@@ -725,8 +805,8 @@ def on_post_build(self, config):
725
805
_entry_found = entry
726
806
break
727
807
if _entry_found :
728
- po .remove (_entry_found )
729
- po .save (translation .po_filepath )
808
+ translation . po .remove (_entry_found )
809
+ translation . po .save (translation .po_filepath )
730
810
731
811
for entry in compendium_pofile :
732
812
if entry .msgid not in repeated_msgids :
@@ -752,11 +832,10 @@ def on_post_build(self, config):
752
832
# po_filepath is None if the file has been excluded from
753
833
# translations using 'exclude' config setting
754
834
if translation .po_filepath is not None :
755
- po = polib .pofile (translation .po_filepath )
756
- for entry in po :
835
+ for entry in translation .po :
757
836
if entry .msgid not in translation .translated_msgids :
758
837
entry .obsolete = True
759
- po .save (translation .po_filepath )
838
+ translation . po .save (translation .po_filepath )
760
839
761
840
# reset mkdocs build instance
762
841
MkdocsBuild ._instance = None
0 commit comments