2015-03-13 03:51:12 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import pelican
|
2017-04-08 14:27:53 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
python3 = sys.version_info >= (3, 0, 0)
|
2015-03-13 03:51:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def init(pelican_object):
|
|
|
|
# I have no good idea. Pass settings to replace function.
|
|
|
|
global replaces
|
|
|
|
replaces = pelican_object.settings.get('REPLACES', ())
|
|
|
|
|
|
|
|
|
|
|
|
def replace(path, context):
|
|
|
|
with open(path, 'r') as f:
|
|
|
|
s = f.read()
|
|
|
|
for src, tgt in replaces:
|
2017-04-08 14:27:53 +00:00
|
|
|
if python3:
|
|
|
|
s = s.replace(src, tgt)
|
|
|
|
else:
|
|
|
|
s = s.decode('utf-8').replace(src.decode('utf-8'), tgt.decode('utf-8'))
|
2015-03-13 03:51:12 +00:00
|
|
|
|
|
|
|
with open(path, 'w') as f:
|
2017-04-08 14:27:53 +00:00
|
|
|
if python3:
|
|
|
|
f.write(s)
|
|
|
|
else:
|
|
|
|
f.write(s.encode('utf-8'))
|
2015-03-13 03:51:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def register():
|
|
|
|
pelican.signals.initialized.connect(init)
|
|
|
|
pelican.signals.content_written.connect(replace)
|