From f123acbd700ea8f2c40552d0746f8459ef006b25 Mon Sep 17 00:00:00 2001 From: Naruse Motoki Date: Fri, 13 Mar 2015 12:51:12 +0900 Subject: [PATCH] Initial commit --- README.rst | 15 +++++++++++++++ __init__.py | 1 + replacer.py | 23 +++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 README.rst create mode 100644 __init__.py create mode 100644 replacer.py diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..f60c815 --- /dev/null +++ b/README.rst @@ -0,0 +1,15 @@ +`Replacer `_ +====================================================== + +This is plugin of Pelican. You can replace a text of a generated HTML. You can write replacing rule in your pelicanconf.py. + +Example +======= + +This example is replacing from '' to '
'. + +:: + + REPLACES = ( + (u'
', u'
'), + ) diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..acc496b --- /dev/null +++ b/__init__.py @@ -0,0 +1 @@ +from .replacer import * diff --git a/replacer.py b/replacer.py new file mode 100644 index 0000000..876db57 --- /dev/null +++ b/replacer.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +import pelican + + +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: + 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')) + + +def register(): + pelican.signals.initialized.connect(init) + pelican.signals.content_written.connect(replace)