內容簡介
文字過濾套件(以下簡稱TFS)是一個 WordPress 外掛程式,
可以加入一些新的文字過濾功能。從某種意義上來說,TFS 的核心
功能可以被視為一個"元過濾器",因為它們實際上提供了一個框架,
讓你輕鬆地構建新的過濾器。此外,它們還提供了一種簡單的方式,
可以按照每篇文章的不同需求,對文章內容和評論進行過濾。
嗯? 你能用通俗易懂的語言講一遍嗎?
好的,我們來舉個例子。在很多網站上,常見的一個功能就是對縮寫字的自動處理。
你經常會看到像"XHTML"這樣的縮寫以不同的樣式顯示,當你將鼠標指向它時,
會彈出一個提示工具,顯示它的定義(例如"擴展超文本標記語言")。
你不想每次發佈內容都要輸入這些標記,因此就有了多個可以處理這種自動文字替換功能的外掛。
TFS 自帶"TFS Acronymit"插件來執行此功能。它基於 Matt Mullenweg 的原始"Acronymit"功能,
但是有些改進。在 Matt 的原始功能中,你必須將縮寫列表按最長到最短排序,
並且它可能會對"PHP"("PHP Hypertext Processor")或 GNU("GNU's Not Unix")這樣的循環縮寫感到困惑。
而 TFS Acronymit 則沒有這些限制。
對於那些認為技術縮寫展開是"teh sux0r"(即"無聊")的人,TFS 還提供了多種更有趣的過濾器。
目前的設置包括"chef"、"fudd"、"jive"、"kraut"、"pirate"和上述提到的"acronymit"。
需要警告的是,"jive"和"kraut"過濾器並不是你所謂的"政治正確"。
默認情況下,如果啟用了"TFS Pirate"過濾器,它會自動應用到 Talk Like a Pirate Day(9 月 19 日)的所有內容中。
如果你不想自動啟用此過濾器,請在插件源碼的頂部設置"$talk_like_a_pirate"變數的值為"false"。
只要啟用了"TFS Acronymit"過濾器,它就會自動應用到所有文章的內容上。你不需要設置特殊的文章自定義字段即可使用它。
如果要修改定義的縮寫,請參見插件開始的列表,按照你看到的格式進行修改即可。
你可以在不啟用任何其他插件的情況下使用 TFS 核心功能。你可以使用內建的 PHP 函數接受一個字符串作為參數,並返回一個字符串。
例如,你可以設置一個文章自定義的"content_filter",其值為"strrev",那麼文章的內容就會倒過來顯示;或者使用"strtoupper"的值將內容轉換為全大寫文字。
每個文章自定義字段中只能指定一個功能。但是,你可以通過多次使用相同的鍵來鏈接多個函數。
例如,如果你想讓文章的所有評論顯示為大寫"Elmer Fudd",那麼可以使用"strtoupper"過濾器多次調用該鍵。
外掛標籤
開發者團隊
原文外掛簡介
The Text Filter Suite (“TFS”, hereafter) is a WordPress plugin which
adds some new text filtering functions. In a sense, the core TFS
functions could be considered a “meta filter”, because they actually
provide a framework that let you construct new filters fairly easily.
They also provide an easy way to apply filters to post content and
comments on a per-post basis.
Huh? Can you repeat that in English?
Okay, let’s try a real-world example. A common feature on many web
sites is the automatic handling of acronyms. You’ll often see an
acronym such as “XHTML” displayed in an alternate style, and when you
hover your mouse pointer over it, you get a tool-tip which displays the
definition (“eXtended HyperText Markup Language”, in this case). You
don’t want to have to type in the markup for this every time you post
something, and automation is what computers are for, right? So, there
are several plugins available to handle this sort of automated text
subsitution.
TFS comes with the “TFS Acronymit” plugin to perform this function.
It’s based on Matt Mullenweg’s original “Acronymit” function, but with
a couple of improvements. In Matt’s original function, you had to keep
the acronym list sorted, longest-to-shortest, and it could get confused
by recursive acronyms like ‘PHP’ (“PHP Hyptertext Processor”) or GNU
(“GNU’s Not Unix”). TFS Acronymit does not have those restrictions.
For those who think that expansion of technical acronyms is “teh sux0r”
(i.e. “boring”), TFS comes with a variety of more entertaining filters.
The current set includes “chef”, “fudd”, “jive”, “kraut”, “pirate”, and
of course, the aforementioned “acronymit”. As a word of caution, the
“jive” and “kraut” filters are not what you would call “politically
correct”.
By default, if the “TFS Pirate” filter is active, it will automatically
apply itself to all content on Talk Like a Pirate Day (September 19).
If you do not want this filter to automatically activate, set the value
of the “$talk_like_a_pirate” variable at the top of the plugin source
to “false”.
The “TFS Acronymit” filter is automatically applied to all posts
whenever it is active. You do not need to set special post custom
fields in order to use it. Just activate the plugin, and you’re ready.
To modify which acronyms are defined, see the list at the beginning of
the plugin, and modify it as you like, following the format you see
there.
It is possible to use the TFS core without activating any additional
plugins. You can do this with any built-in PHP function accepts a
single string as a parameter and returns a string. For example, you
could set a post custom “content_filter” with the value “strrev”, and
the contents of the post would be displayed backwards, or with a value
of “strtoupper” to convert the content to all uppercase text.
You can only specify a single function in each post custom field.
However, you can chain multiple functions together by using the key
more than once. For instance, if you wanted all comments for a post to
display in uppercase Elmer Fudd text, you would set two post custom
fields:
comment_filter = strtoupper
comment_filter = fudd
HOWEVER, note that using PHP built-in functions in this way will bypass
the power of the filter_cdata_content() function, which means that it
can and will mangle your HTML tags, possibly rendering them useless.
For example, applying the strrev function to the string “
<
p>” will
transform it into “>p<“, which will confuse your browser in new and
wonderful ways.
NOTE: These filters can be very CPU intensive. For one thing, they make
extensive use of regular expressions, which can be expensive on their own.
And for another, they break your content into many small chunks, in order to
separate the filterable text from the HTML code, and the filters run
separately on each text chunk found. This probably won’t be a problem in
most cases. But if you have long posts being filtered, and you get a lot of
traffic, it could start to add up. A caching plugin (e.g., WP Super Cache,
or W3 Total Cache) would probably help in that case.
The Future
I will one day release a version 2.0 of this plugin which will be completely
refactored. You can probably expect to see:
* Consolidate the code so that it is not a collection of separately-enabled
mini-plugins.
* PHP5 OOP architecture to encapsulate everything.
* An actual admin interface to select which filters are enabled, which
bits of content you will allow to be filtered (post titles, post content, comments,
blog title, widget titles, etc), whether to auto-activate the Pirate
filter on Talk Like a Pirate Day, etc.
Eventually, there may also be a way to edit the string substitutions so that
you can tailor it to your tastes.
Credits
I created TFS on my own, but I borrowed ideas from several sources. Here are
some links you might also want to check out:
PhotoMatt's original Acronymit code:
http://photomatt.net/scripts/acronymit
Simon Willison and I traded some ideas when I started my original hack
for Talk Like a Pirate Day, in 2003:
http://simon.incutio.com/archive/2003/09/19/aaar
I borrowed, modified, and mangled a ton of stuff from Adam Kalsey's
"MovableJive" plugin for Movable Type. See tfs-jive, tfs-chef, tfs-fudd,
and tfs-kraut.
http://kalsey.com/2003/02/movablejive/
If all you want to do is stuff like the acronym definitions (or similar
"turn this shorthand into a tag" substitions), then Michel Valdrighi's
"Tag, You're It" plugin is really a better solution:
http://zengun.org/weblog/archives/2004/05/tag-you-re-it
The original inspiration that led to TFS was my desire to apply a
"pirate" filter on my blog for "Talk Like a Pirate Day", which is on
September 19 of each year:
http://talklikeapirate.com/
