From 8756d5e09b2b9bfa40adb270b85d68a0a38b1313 Mon Sep 17 00:00:00 2001 From: Christian Koepp Date: Sun, 7 Dec 2014 14:02:04 +0100 Subject: [PATCH 1/2] added replace filter --- acrylamid/filters/replace.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 acrylamid/filters/replace.py diff --git a/acrylamid/filters/replace.py b/acrylamid/filters/replace.py new file mode 100644 index 00000000..2c10ebb8 --- /dev/null +++ b/acrylamid/filters/replace.py @@ -0,0 +1,27 @@ +# -*- encoding: utf-8 -*- +# +# Copyright 2014 Christian Koepp . 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 \ No newline at end of file From e3aef4f821b453ac9df6f753aa09405f756511d3 Mon Sep 17 00:00:00 2001 From: Christian Koepp Date: Mon, 8 Dec 2014 15:37:33 +0100 Subject: [PATCH 2/2] added replace docs + fixed typo in docs + \n in filter --- acrylamid/filters/replace.py | 2 +- docs/filters/post.rst | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/acrylamid/filters/replace.py b/acrylamid/filters/replace.py index 2c10ebb8..6d2e3353 100644 --- a/acrylamid/filters/replace.py +++ b/acrylamid/filters/replace.py @@ -24,4 +24,4 @@ def transform(self, content, entry, *args): for k,v in self._db.items(): content = content.replace(k, v) - return content \ No newline at end of file + return content diff --git a/docs/filters/post.rst b/docs/filters/post.rst index 01adbb4c..c5d034e2 100644 --- a/docs/filters/post.rst +++ b/docs/filters/post.rst @@ -93,6 +93,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 = { + ':-)' : '', + '$last_compilation_date$' : datetime.now().strftime("%Y-%m-%d"), + } + .. _filters-post-hyphenate: hyphenate @@ -138,7 +166,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 @@ -163,8 +191,8 @@ acronyms This filter is a direct port of `Pyblosxom's acrynoms plugin `_, 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: ::