[#1] Compatible with Python3

When this plugin is running on Python3, it shouldn't call decode because the variable is str type, not bytes.
This commit is contained in:
Motoki Naruse 2017-04-08 23:27:53 +09:00 committed by GitHub
parent f123acbd70
commit 07925f938c

View File

@ -1,5 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import pelican import pelican
import sys
python3 = sys.version_info >= (3, 0, 0)
def init(pelican_object): def init(pelican_object):
@ -12,9 +15,15 @@ def replace(path, context):
with open(path, 'r') as f: with open(path, 'r') as f:
s = f.read() s = f.read()
for src, tgt in replaces: for src, tgt in replaces:
if python3:
s = s.replace(src, tgt)
else:
s = s.decode('utf-8').replace(src.decode('utf-8'), tgt.decode('utf-8')) s = s.decode('utf-8').replace(src.decode('utf-8'), tgt.decode('utf-8'))
with open(path, 'w') as f: with open(path, 'w') as f:
if python3:
f.write(s)
else:
f.write(s.encode('utf-8')) f.write(s.encode('utf-8'))