From 07925f938c6f810b831fd6d0d6a84c3925685ecd Mon Sep 17 00:00:00 2001 From: Motoki Naruse Date: Sat, 8 Apr 2017 23:27:53 +0900 Subject: [PATCH] [#1] Compatible with Python3 When this plugin is running on Python3, it shouldn't call decode because the variable is str type, not bytes. --- replacer.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/replacer.py b/replacer.py index 876db57..2793703 100644 --- a/replacer.py +++ b/replacer.py @@ -1,5 +1,8 @@ # -*- coding: utf-8 -*- import pelican +import sys + +python3 = sys.version_info >= (3, 0, 0) def init(pelican_object): @@ -12,10 +15,16 @@ def replace(path, context): with open(path, 'r') as f: s = f.read() for src, tgt in replaces: - s = s.decode('utf-8').replace(src.decode('utf-8'), tgt.decode('utf-8')) + if python3: + s = s.replace(src, tgt) + else: + s = s.decode('utf-8').replace(src.decode('utf-8'), tgt.decode('utf-8')) with open(path, 'w') as f: - f.write(s.encode('utf-8')) + if python3: + f.write(s) + else: + f.write(s.encode('utf-8')) def register():