Initial commit

This commit is contained in:
Naruse Motoki 2015-03-13 12:51:12 +09:00
parent 5e1ca7f2f0
commit f123acbd70
3 changed files with 39 additions and 0 deletions

15
README.rst Normal file
View File

@ -0,0 +1,15 @@
`Replacer <https://github.com/narusemotoki/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 '<table border="1" class="docutils">' to '<table class="table table-striped table-bordered table-hover">'.
::
REPLACES = (
(u'<table border="1" class="docutils">', u'<table class="table table-striped table-bordered table-hover">'),
)

1
__init__.py Normal file
View File

@ -0,0 +1 @@
from .replacer import *

23
replacer.py Normal file
View File

@ -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)