Add dependency checking for included files.
[xhtml-compiler.git] / XHTMLCompiler / DOMFilter / Acronymizer.php
blobc9361f8460680798fb2a51ccd656a4aad6e1dc55
1 <?php
3 /**
4 * Based on a list of known acronyms, populates of the title attribute
5 * of acronym elements in documents.
6 */
7 class XHTMLCompiler_DOMFilter_Acronymizer extends XHTMLCompiler_DOMFilter
10 protected $name = 'Acronymizer';
12 /**
13 * Associative array of recognized abbreviations/initialisms
14 * @note To be used when each letter should be spelled out, such
15 * as HTML.
17 protected $abbreviations = array(
18 // markup languages and related technologies
19 'SGML' => 'Standard Generalized Markup Language',
20 'HTML' => 'HyperText Markup Language',
21 'XHTML' => 'Extensible HyperText Markup Language',
22 'XML' => 'Extensible Markup Language',
23 'RSS' => 'Really Simple Syndication',
24 'DTD' => 'Document Type Definition',
25 'CSS' => 'Cascading Style Sheets',
26 // programming/apis
27 'PHP' => 'PHP: HyperText Preprocessor',
28 'CMS' => 'Content Management System',
29 'API' => 'Application Programming Interface',
30 // web-app security
31 'XSS' => 'Cross-Site Scripting',
32 // organizations/groups
33 'W3C' => 'World Wide Web Consortium',
34 'RFC' => 'Request for Comment',
35 'PECL' => 'PHP Extension Community Library',
36 // character encodings
37 'UTF-8' => '8-bit Unicode Transformation Format',
38 // other
39 'INI' => 'Initialization',
40 'CPU' => 'Central Processing Unit',
41 'LGPL' => 'Lesser GNU Public License',
42 'URI' => 'Uniform Resource Identifier',
45 /**
46 * Array of recognized acronyms.
47 * @note Acronyms can be spoken literally, if in doubt, make it
48 * an abbreviation.
49 * @todo Make a public API for this, allow multiple acronym sets
50 * and different precedences for them.
52 protected $acronyms = array(
53 // programming
54 'SAX' => 'Simple API for XML',
55 'DOM' => 'Document Object Module',
56 'PEAR' => 'PHP Extension and Application Repository',
57 'ASCII' => 'American Standard Code for Information Interchange',
58 'SHA-1' => 'Secure Hash Algorithm',
59 // paradigms
60 'WYSIWYG' => 'What You See Is What You Get',
61 'WYSIWYM' => 'What You See Is What You Mean',
64 public function process(DOMDocument $dom, $page, $manager) {
65 $nodes = $this->query("//html:acronym[not(@title)]");
66 foreach ($nodes as $node) $this->addAdvisoryTitle($node, $this->acronyms);
68 $nodes = $this->query("//html:abbr[not(@title)]");
69 foreach ($nodes as $node) $this->addAdvisoryTitle($node, $this->abbreviations);
71 // add self as dependency; when acronym lists change, so does the page
72 $manager->addDependency(__FILE__);
75 protected function addAdvisoryTitle($node, $lookup) {
76 $key = $node->textContent;
77 if (!isset($lookup[$key])) {
78 // not fatal, but good to let the document author know
79 trigger_error(htmlspecialchars($key) .
80 ' is not a recognized acronym/abbreviation (missing title attribute)');
81 return;
83 $node->setAttribute('title', $lookup[$key]);