mod_line_edit

mod_line_edit is a general-purpose filter for text documents. It operates as a simple on-the-fly line editor, applying search-and-replace rules defined in a configuration or .htaccess file.

Unlike most of WebÞing's filter modules, it is not markup-aware, so it is not an optimal choice for processing HTML or XML, though it may nevertheless be used with caution (and may be far better than semi-markup-aware options such as mod_layout).

For non-markup document types such as plain text, and non-markup Web documents such as Javascript or Stylesheets, it is the best available option in the absence of a filter that parses any relevant document structures.

mod_line_edit is written for performance and reliability, and should scale without problems as document size grows. mod_line_edit is fully compatible with Apache 2.0 and 2.2, and all operating systems and MPMs.

Usage

LoadModule line_edit_module modules/mod_line_edit.so

The module implements a single output filter named line-editor. Insert it in the filter chain using the standard filter directives, e.g. to rewrite all text documents:

FilterProvider textedit line-editor resp=Content-Type $text/
FilterChain textedit

or, for backward compatibility with Apache 2.0:

AddOutputFilter	line-editor	.txt .css .js

or

SetOutputFilter	line-editor
SetEnv	LineEdit "text/plain;text/css;text/javascript"

Text Editing

The LERewriteRule directive defines search-and-replace rules. Both simple text and regular expression search and replace are supported.

Line Modes

mod_line_edit normally applies its edits line-by-line. This avoids the risk of missing a pattern to be matched because it is spread over more than one chunk of data when it reaches the parser, without having to resort to the performance and scalability limitations of loading an entire document into memory.

The LineEdit Environment Variable

If the LineEdit environment variable is set, it controls Content Types that will be filtered. This enables it to filter selectively on content-type in a proxy with Apache 2.0. Just set the variable to a list of content types, and mod_line_edit will leave other types untouched.

Example: to filter plain text and javascript, but leave other types alone:
SetEnv LineEdit "text/plain;text/javascript"

This also works with Apache 2.2, but is of course unnecessary there.

Directives

LELineEnd

LELineEnd UNIX|MAC|DOS|NONE|ANY|CUSTOM [char]

This tells the parser what characters in the text to interpret as line-endings:

  • UNIX - the line end is the traditional Unix \n.
  • MAC - the line end is the old MacOS \r. Note that modern MacOSX is Unix-based.
  • DOS - the line end is the MSDOS and Windows sequence \r\n.
  • ANY - Any of the above will be interpreted as a line break (with \r\n treated as one, not two, linebreaks). This is the default.
  • NONE - This will treat the entire document as a single line, enabling multi-line search-and-replace. Note that this will incur a substantial performance penalty for larger documents, as it requires an entire document to be loaded into memory and processed in a single operation.
  • CUSTOM - This enables you to partition the input by splitting on some character other than a conventional line end.

When you use LELineEnd Custom, you must specify a second single-character argument, which is the character to split the input on. For all other LELineEnd options, any second argument will be ignored.

LERewriteRule

LERewriteRule from-pattern to-pattern [flags]

This directive defines a search-and-replace edit rule that will be applied to the text.

  • The search string from-pattern may be a literal string or a regular expression.
  • The replacement string to-pattern may be a literal string, or may include backreferences $1-$9 in the case of a regular expression match.
  • The optional Flags argument may contain any combination of:
    • R - This rule is a regular expression search-and-replace.
    • i - Use case-insensitive matching on from-pattern.
    • m - Support multi-line regexp matching (in conjunction with the R flag and LELineEnd None).
    • V - Support environment variables in to-pattern. The string ${var} will be replaced with the value of the environment variable var.

Availability

mod_line_edit.c source code is available under the GNU General Public License (GPL). As with other opensource modules, we can consider alternative licenses by request.

Registered Users may request binaries for any available platform.

Version 0.9.2

Dec. 26th 2005: the first public release of mod_line_edit is version 0.9.2

Version 1.0

June 12th 2006: added capability to interpolate environment variables in rewrite rules. Bumping version to 1.0 because 0.9 has proved stable over six months in the wild.