@@ -204,68 +204,6 @@ def get_url_setting(self, key):
204
204
key = key if self .in_default_lang else 'lang_%s' % key
205
205
return self ._expand_settings (key )
206
206
207
- def _link_replacer (self , siteurl , m ):
208
- what = m .group ('what' )
209
- value = urlparse (m .group ('value' ))
210
- path = value .path
211
- origin = m .group ('path' )
212
-
213
- # XXX Put this in a different location.
214
- if what in {'filename' , 'attach' }:
215
- if path .startswith ('/' ):
216
- path = path [1 :]
217
- else :
218
- # relative to the source path of this content
219
- path = self .get_relative_source_path (
220
- os .path .join (self .relative_dir , path )
221
- )
222
-
223
- if path not in self ._context ['filenames' ]:
224
- unquoted_path = path .replace ('%20' , ' ' )
225
-
226
- if unquoted_path in self ._context ['filenames' ]:
227
- path = unquoted_path
228
-
229
- linked_content = self ._context ['filenames' ].get (path )
230
- if linked_content :
231
- if what == 'attach' :
232
- if isinstance (linked_content , Static ):
233
- linked_content .attach_to (self )
234
- else :
235
- logger .warning (
236
- "%s used {attach} link syntax on a "
237
- "non-static file. Use {filename} instead." ,
238
- self .get_relative_source_path ())
239
- origin = '/' .join ((siteurl , linked_content .url ))
240
- origin = origin .replace ('\\ ' , '/' ) # for Windows paths.
241
- else :
242
- logger .warning (
243
- "Unable to find '%s', skipping url replacement." ,
244
- value .geturl (), extra = {
245
- 'limit_msg' : ("Other resources were not found "
246
- "and their urls not replaced" )})
247
- elif what == 'category' :
248
- origin = '/' .join ((siteurl , Category (path , self .settings ).url ))
249
- elif what == 'tag' :
250
- origin = '/' .join ((siteurl , Tag (path , self .settings ).url ))
251
- elif what == 'index' :
252
- origin = '/' .join ((siteurl , self .settings ['INDEX_SAVE_AS' ]))
253
- elif what == 'author' :
254
- origin = '/' .join ((siteurl , Author (path , self .settings ).url ))
255
- else :
256
- logger .warning (
257
- "Replacement Indicator '%s' not recognized, "
258
- "skipping replacement" ,
259
- what )
260
-
261
- # keep all other parts, such as query, fragment, etc.
262
- parts = list (value )
263
- parts [2 ] = origin
264
- origin = urlunparse (parts )
265
-
266
- return '' .join ((m .group ('markup' ), m .group ('quote' ), origin ,
267
- m .group ('quote' )))
268
-
269
207
def _update_content (self , content , siteurl ):
270
208
"""Update the content attribute.
271
209
@@ -289,26 +227,69 @@ def _update_content(self, content, siteurl):
289
227
\2""" .format (instrasite_link_regex )
290
228
hrefs = re .compile (regex , re .X )
291
229
292
- return hrefs .sub (lambda m : self ._link_replacer (siteurl , m ), content )
230
+ def replacer (m ):
231
+ what = m .group ('what' )
232
+ value = urlparse (m .group ('value' ))
233
+ path = value .path
234
+ origin = m .group ('path' )
293
235
294
- def __getattribute__ (self , name ):
295
- try :
296
- # Avoid infinite recursion
297
- settings = object .__getattribute__ (self , 'settings' )
298
- link_fields = settings ['INTERNAL_LINK_FIELDS' ]
299
- if name in link_fields :
300
- link_regex = r"""^
301
- (?P<markup>)(?P<quote>)
302
- (?P<path>{0}(?P<value>.*))
303
- $""" .format (settings ['INTRASITE_LINK_REGEX' ])
304
- links = re .compile (link_regex , re .X )
305
- return links .sub (
306
- lambda m : self ._link_replacer (self .get_siteurl (), m ),
307
- object .__getattribute__ (self , name ))
236
+ # XXX Put this in a different location.
237
+ if what in {'filename' , 'attach' }:
238
+ if path .startswith ('/' ):
239
+ path = path [1 :]
240
+ else :
241
+ # relative to the source path of this content
242
+ path = self .get_relative_source_path (
243
+ os .path .join (self .relative_dir , path )
244
+ )
245
+
246
+ if path not in self ._context ['filenames' ]:
247
+ unquoted_path = path .replace ('%20' , ' ' )
248
+
249
+ if unquoted_path in self ._context ['filenames' ]:
250
+ path = unquoted_path
251
+
252
+ linked_content = self ._context ['filenames' ].get (path )
253
+ if linked_content :
254
+ if what == 'attach' :
255
+ if isinstance (linked_content , Static ):
256
+ linked_content .attach_to (self )
257
+ else :
258
+ logger .warning (
259
+ "%s used {attach} link syntax on a "
260
+ "non-static file. Use {filename} instead." ,
261
+ self .get_relative_source_path ())
262
+ origin = '/' .join ((siteurl , linked_content .url ))
263
+ origin = origin .replace ('\\ ' , '/' ) # for Windows paths.
264
+ else :
265
+ logger .warning (
266
+ "Unable to find '%s', skipping url replacement." ,
267
+ value .geturl (), extra = {
268
+ 'limit_msg' : ("Other resources were not found "
269
+ "and their urls not replaced" )})
270
+ elif what == 'category' :
271
+ origin = '/' .join ((siteurl , Category (path , self .settings ).url ))
272
+ elif what == 'tag' :
273
+ origin = '/' .join ((siteurl , Tag (path , self .settings ).url ))
274
+ elif what == 'index' :
275
+ origin = '/' .join ((siteurl , self .settings ['INDEX_SAVE_AS' ]))
276
+ elif what == 'author' :
277
+ origin = '/' .join ((siteurl , Author (path , self .settings ).url ))
308
278
else :
309
- return object .__getattribute__ (self , name )
310
- except AttributeError :
311
- return object .__getattribute__ (self , name )
279
+ logger .warning (
280
+ "Replacement Indicator '%s' not recognized, "
281
+ "skipping replacement" ,
282
+ what )
283
+
284
+ # keep all other parts, such as query, fragment, etc.
285
+ parts = list (value )
286
+ parts [2 ] = origin
287
+ origin = urlunparse (parts )
288
+
289
+ return '' .join ((m .group ('markup' ), m .group ('quote' ), origin ,
290
+ m .group ('quote' )))
291
+
292
+ return hrefs .sub (replacer , content )
312
293
313
294
def get_siteurl (self ):
314
295
return self ._context .get ('localsiteurl' , '' )
0 commit comments