@@ -89,36 +89,75 @@ def render_file(input_path, output_path, env, page_context={}):
89
89
ConversionResult = namedtuple ("ConversionResult" , ["converted" , "skipped" ])
90
90
91
91
92
+ import os
93
+ import shutil
94
+
92
95
def render_markdown (input_dir , output_dir , env , page_context = {}) -> ConversionResult :
93
96
converted = 0
94
97
skipped = 0
98
+
95
99
for root , _ , files in os .walk (input_dir ):
96
100
for filename in files :
97
- # Only Markdown files will have their extension changed, everything
98
- # else is copied.
99
101
input_path = os .path .join (root , filename )
100
- should_render = False
101
- if filename .endswith (".md" ):
102
- output_filename = replace_extension (filename , "md" , "html" )
102
+ # Determine the relative path (directory structure under input_dir).
103
+ relative_path = os .path .relpath (os .path .dirname (input_path ), input_dir )
104
+
105
+ # Decide how to handle the file.
106
+ if filename == "index.md" :
107
+ # Special case index.md to turn into index.html in the exact same location
108
+ output_path = os .path .join (
109
+ output_dir ,
110
+ relative_path ,
111
+ "index.html"
112
+ )
113
+ should_render = True
114
+ elif filename .endswith (".md" ):
115
+ # Instead of simply replacing .md with .html, place it in:
116
+ #
117
+ # output_dir / relative_path / [filename-without-.md] / index.html
118
+ #
119
+ folder_name = os .path .splitext (filename )[0 ] # remove .md
120
+ output_path = os .path .join (
121
+ output_dir ,
122
+ relative_path ,
123
+ folder_name ,
124
+ "index.html"
125
+ )
103
126
should_render = True
127
+
104
128
elif filename .endswith (".jinja2" ):
129
+ # No change for jinja2 behavior:
130
+ output_filename = filename [:- 7 ] # remove '.jinja2'
131
+ output_path = os .path .join (
132
+ output_dir ,
133
+ relative_path ,
134
+ output_filename
135
+ )
105
136
should_render = True
106
- output_filename = filename [:- 7 ]
107
- else :
108
- output_filename = filename
109
- relative_path = os .path .relpath (os .path .dirname (input_path ), input_dir )
110
- output_path = os .path .normpath (
111
- os .path .join (output_dir , relative_path , output_filename )
112
- )
113
137
114
- # Only Markdown files are rendered.
138
+ else :
139
+ # All other files are copied as-is (unchanged behavior):
140
+ output_path = os .path .join (
141
+ output_dir ,
142
+ relative_path ,
143
+ filename
144
+ )
145
+ should_render = False
146
+
147
+ # Normalize the path (important on Windows, but generally good practice).
148
+ output_path = os .path .normpath (output_path )
149
+
150
+ # Render if needed; otherwise just copy the file.
115
151
if should_render :
152
+ # Ensure the parent directory (or in the case of .md, the subfolder) exists.
153
+ os .makedirs (os .path .dirname (output_path ), exist_ok = True )
116
154
render_file (input_path , output_path , env , page_context = page_context )
117
155
converted += 1
118
156
else :
119
157
os .makedirs (os .path .dirname (output_path ), exist_ok = True )
120
158
shutil .copy (input_path , output_path )
121
159
skipped += 1
160
+
122
161
return ConversionResult (converted , skipped )
123
162
124
163
0 commit comments