@@ -187,6 +187,7 @@ def chatter(*args):
187
187
188
188
# ------------------------------------------------
189
189
# These properties are available in the env object
190
+ # in macros
190
191
# ------------------------------------------------
191
192
192
193
@property
@@ -600,6 +601,20 @@ def _load_modules(self):
600
601
# ----------------------------------
601
602
# output elements
602
603
# ----------------------------------
604
+ @property
605
+ def env (self ) -> Environment :
606
+ """
607
+ The templating environment for Jinja2.
608
+
609
+ It is a core component of the macros engine.
610
+ It is defined in `on_config`.
611
+
612
+ NOTE: Do NOT confuse with the env argument in a module.
613
+ """
614
+ try :
615
+ return self ._env
616
+ except AttributeError :
617
+ raise AttributeError ("Jinja2 environment is not defined yet!" )
603
618
604
619
def render (self , markdown : str , force_rendering :bool = False ) -> str :
605
620
"""
@@ -700,6 +715,24 @@ def render(self, markdown: str, force_rendering:bool=False) -> str:
700
715
701
716
else :
702
717
return error_message
718
+
719
+
720
+ def has_j2 (self , s :str ) -> bool :
721
+ """
722
+ Defines whether a string might contain j2 code.
723
+
724
+ The criterion is: does it contain any start strings,
725
+ such as, e.g., `{{`?
726
+
727
+ It takes into account the j2_..._start_string
728
+ parameters of the config file.
729
+ """
730
+ env = self .env
731
+ CANDIDATES = [env .variable_start_string ,
732
+ env .block_start_string ,
733
+ env .comment_start_string ]
734
+ return any (item in s for item in CANDIDATES )
735
+
703
736
704
737
705
738
# ----------------------------------
@@ -814,7 +847,7 @@ def on_config(self, config):
814
847
env_config [variable_name ] = value
815
848
816
849
# finally build the environment:
817
- self .env = Environment (** env_config )
850
+ self ._env = Environment (** env_config )
818
851
819
852
# -------------------
820
853
# Process macros
@@ -836,7 +869,11 @@ def on_config(self, config):
836
869
self .env .filters .update (self .filters )
837
870
838
871
debug ("End of environment config" )
839
-
872
+
873
+
874
+
875
+
876
+
840
877
def on_pre_build (self , * , config ):
841
878
"""
842
879
Provide information on the variables.
@@ -932,7 +969,7 @@ def on_page_markdown(self, markdown, page:Page,
932
969
# There is a bizarre issue #215 where setting the title
933
970
# prevents interpretation of icons with pymdownx.emoji
934
971
debug ("Page title:" ,page .title )
935
- if "{" in page .title :
972
+ if self . has_j2 ( page .title ) :
936
973
page .title = self .render (markdown = page .title ,
937
974
force_rendering = force_rendering )
938
975
debug ("Page title after macro rendering:" ,page .title )
0 commit comments