Skip to content

Commit

Permalink
Merge pull request #230 from ckoepp/filter/replace
Browse files Browse the repository at this point in the history
Added Replace Filter
  • Loading branch information
posativ committed Dec 8, 2014
2 parents aa6b591 + e3aef4f commit d956855
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
27 changes: 27 additions & 0 deletions acrylamid/filters/replace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- encoding: utf-8 -*-
#
# Copyright 2014 Christian Koepp <[email protected]>. All rights reserved.
# License: BSD Style, 2 clauses -- see LICENSE.

from acrylamid import log
from acrylamid.filters import Filter

class Replace(Filter):
match = ['replace']
version = 1
priority = 0.0

def init(self, conf, env, *args):
try:
self._db = conf.replace_rules
except AttributeError:
log.warn('No configuration named REPLACE_RULES found. Replace filter has nothing to do.')
self._db = dict()

def transform(self, content, entry, *args):
if len(self._db) == 0:
return content

for k,v in self._db.items():
content = content.replace(k, v)
return content
34 changes: 31 additions & 3 deletions docs/filters/post.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,34 @@ INTRO_IGNORE : a list of tags

see ``SUMMARIZE_IGNORE``

.. _filters-post-replace:

replace
-------

Replaces user-defined strings with assigned values. This can be used to
include arbitrary information or to replace common symbols like ``:-)``
into proper HTML code matching with the theme.

In order to know what to replace, an additional :doc:`conf.py` parameter is
required for configuration of the filter.

REPLACE_RULES : a dict (may be empty)

The keys of the dictionary are describing the strings to replace with
their corresponding value. A missing or empty dict raises a warning
within the logs and disables the filter even when it is included in
the filter chain.

An example of how a typical rule-set for this filter may look like:

::

REPLACE_RULES = {
':-)' : '<i class="fa fa-smile-o"></i>',
'$last_compilation_date$' : datetime.now().strftime("%Y-%m-%d"),
}

.. _filters-post-hyphenate:

hyphenate
Expand Down Expand Up @@ -139,7 +167,7 @@ All filters are applied in the order as they are written down.

.. code-block:: python
TYPOGRAPHY_MODE = "2" # in your conf.oy
TYPOGRAPHY_MODE = "2" # in your conf.py
`Smarty Pants`_ has modes that let you customize the modification. See `their
options`_ for reference. Acrylamid adds a custom mode ``"a"`` that behaves like
Expand All @@ -164,8 +192,8 @@ acronyms
This filter is a direct port of `Pyblosxom's acrynoms plugin
<http://pyblosxom.bluesock.org/1.5/plugins/acronyms.html>`_, that marks acronyms
and abbreviations in your text based on either a built-in acronyms list or a
user-specified. To use a custom list just add the FILE to your conf.py like
this:
user-specified. To use a custom list just add the FILE to your :doc:`conf.py`
like this:

::

Expand Down

0 comments on commit d956855

Please sign in to comment.