3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
6 * Utility for finding and parsing documentation files
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * @category Documentation
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2011 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Utility for finding and parsing documentation files
40 * @category Documentation
42 * @author Evan Prodromou <evan@status.net>
43 * @copyright 2011 StatusNet, Inc.
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45 * @link http://status.net/
53 function __construct($filename)
55 $this->filename = $filename;
58 static function forTitle($title, array $paths, $language=null)
62 if (Event::handle('StartDocFileForTitle', array($title, &$paths, &$filename))) {
64 foreach ($paths as $path) {
66 $def = $path.'/'.$title;
68 if (!file_exists($def)) {
72 $lang = glob($path.'/'.$title.'.*');
74 if ($lang === false) {
78 if (!empty($lang) || !empty($def)) {
79 $filename = self::negotiateLanguage($lang, $def, $language);
84 Event::handle('EndDocFileForTitle', array($title, $paths, &$filename));
87 if (empty($filename)) {
90 return new DocFile($filename);
94 function toHTML(array $args=array())
96 if (empty($this->contents)) {
97 $this->contents = file_get_contents($this->filename);
100 return common_markup_to_html($this->contents, $args);
103 static function defaultPaths()
105 $paths = array(INSTALLDIR.'/local/doc-src/',
106 INSTALLDIR.'/doc-src/');
108 $site = GNUsocial::currentSite();
111 array_unshift($paths, INSTALLDIR.'/local/doc-src/'.$site.'/');
117 static function mailPaths()
119 $paths = array(INSTALLDIR.'/local/mail-src/',
120 INSTALLDIR.'/mail-src/');
122 $site = GNUsocial::currentSite();
125 array_unshift($paths, INSTALLDIR.'/local/mail-src/'.$site.'/');
131 private static function negotiateLanguage(array $filenames, $defaultFilename=null, $language = null)
133 // Default is current language
134 $langcode = common_language();
136 // Is a language set?
137 if (!empty($language)) {
139 if (common_valid_language($language)) {
140 // Use this as language (e.g. from form)
141 $langcode = strval($language);
145 foreach ($filenames as $filename) {
146 if (preg_match('/\.'.$langcode.'$/', $filename)) {
151 return $defaultFilename;