4 * Based on a list of known acronyms, populates of the title attribute
5 * of acronym and abbr elements in documents.
6 * @note Title is a bit of legacy baggage.
8 class XHTMLCompiler_DOMFilter_Acronymizer
extends XHTMLCompiler_DOMFilter
11 protected $name = 'Acronymizer';
14 * Associative array of recognized abbreviations/initialisms
15 * @note To be used when each letter should be spelled out, such
18 protected $abbreviations = array(
19 // markup languages and related technologies
20 'SGML' => 'Standard Generalized Markup Language',
21 'HTML' => 'HyperText Markup Language',
22 'XHTML' => 'Extensible HyperText Markup Language',
23 'XML' => 'Extensible Markup Language',
24 'RSS' => 'Really Simple Syndication',
25 'DTD' => 'Document Type Definition',
26 'CSS' => 'Cascading Style Sheets',
28 'PHP' => 'PHP: HyperText Preprocessor',
29 'CMS' => 'Content Management System',
30 'API' => 'Application Programming Interface',
31 'SVN' => 'Subversion',
32 'XSLT' => 'Extensible Stylesheet Language Transformations',
33 'SQL' => 'Structured Query Language',
35 'XSS' => 'Cross-Site Scripting',
36 // organizations/groups
37 'W3C' => 'World Wide Web Consortium',
38 'RFC' => 'Request for Comment',
39 'PECL' => 'PHP Extension Community Library',
40 // character encodings
41 'UTF-8' => '8-bit Unicode Transformation Format',
43 'INI' => 'Initialization',
44 'CPU' => 'Central Processing Unit',
45 'LGPL' => 'Lesser GNU Public License',
46 'FTP' => 'File Transfer Protocol',
47 'URI' => 'Uniform Resource Identifier',
51 * Array of recognized acronyms.
52 * @note Acronyms can be spoken literally, if in doubt, make it
54 * @todo Make a public API for this, allow multiple acronym sets
55 * and different precedences for them.
57 protected $acronyms = array(
59 'SAX' => 'Simple API for XML',
60 'DOM' => 'Document Object Module',
61 'PEAR' => 'PHP Extension and Application Repository',
62 'ASCII' => 'American Standard Code for Information Interchange',
63 'SHA-1' => 'Secure Hash Algorithm',
65 'WYSIWYG' => 'What You See Is What You Get',
66 'WYSIWYM' => 'What You See Is What You Mean',
69 public function process(DOMDocument
$dom, $page, $manager) {
70 $nodes = $this->query("//html:acronym[not(@title)]");
71 foreach ($nodes as $node) $this->addAdvisoryTitle($node, $this->acronyms
);
73 $nodes = $this->query("//html:abbr[not(@title)]");
74 foreach ($nodes as $node) $this->addAdvisoryTitle($node, $this->abbreviations
);
76 // add self as dependency; when acronym lists change, so does the page
77 $manager->addDependency(__FILE__
);
80 protected function addAdvisoryTitle($node, $lookup) {
81 $key = $node->textContent
;
82 if (!isset($lookup[$key])) {
83 // not fatal, but good to let the document author know
84 trigger_error(htmlspecialchars($key) .
85 ' is not a recognized acronym/abbreviation (missing title attribute)');
88 $node->setAttribute('title', $lookup[$key]);