@@ -33,284 +33,284 @@ |
||
33 | 33 | class Docs extends \Myth\Controllers\ThemedController |
34 | 34 | { |
35 | 35 | |
36 | - protected $ignoreFiles = array('_404.md'); |
|
36 | + protected $ignoreFiles = array('_404.md'); |
|
37 | 37 | |
38 | - protected $tocFile; |
|
38 | + protected $tocFile; |
|
39 | 39 | |
40 | - protected $doc_folders = []; |
|
40 | + protected $doc_folders = []; |
|
41 | 41 | |
42 | - protected $current_group = null; |
|
42 | + protected $current_group = null; |
|
43 | 43 | |
44 | - protected $current_path = null; |
|
44 | + protected $current_path = null; |
|
45 | 45 | |
46 | - private $showAppDocs; |
|
46 | + private $showAppDocs; |
|
47 | 47 | |
48 | - private $showDevDocs; |
|
48 | + private $showDevDocs; |
|
49 | 49 | |
50 | - protected $theme = 'docs'; |
|
50 | + protected $theme = 'docs'; |
|
51 | 51 | |
52 | - protected $auto_escape = false; |
|
52 | + protected $auto_escape = false; |
|
53 | 53 | |
54 | - //-------------------------------------------------------------------------- |
|
54 | + //-------------------------------------------------------------------------- |
|
55 | 55 | |
56 | - /** |
|
57 | - * Constructor |
|
58 | - * |
|
59 | - * @return \Docs |
|
60 | - */ |
|
61 | - public function __construct() |
|
62 | - { |
|
63 | - parent::__construct(); |
|
56 | + /** |
|
57 | + * Constructor |
|
58 | + * |
|
59 | + * @return \Docs |
|
60 | + */ |
|
61 | + public function __construct() |
|
62 | + { |
|
63 | + parent::__construct(); |
|
64 | 64 | |
65 | - $this->load->config('docs'); |
|
66 | - $this->lang->load('docs'); |
|
65 | + $this->load->config('docs'); |
|
66 | + $this->lang->load('docs'); |
|
67 | 67 | |
68 | - // Save our folders |
|
69 | - $this->doc_folders = config_item('docs.folders'); |
|
68 | + // Save our folders |
|
69 | + $this->doc_folders = config_item('docs.folders'); |
|
70 | 70 | |
71 | - list($this->current_group, $this->current_path) = $this->determineFromURL(); |
|
71 | + list($this->current_group, $this->current_path) = $this->determineFromURL(); |
|
72 | 72 | |
73 | - $this->determineVisibleGroups($this->current_group, $this->current_path); |
|
73 | + $this->determineVisibleGroups($this->current_group, $this->current_path); |
|
74 | 74 | |
75 | - $this->load->helper('form'); |
|
76 | - $this->load->helper('language'); |
|
75 | + $this->load->helper('form'); |
|
76 | + $this->load->helper('language'); |
|
77 | 77 | |
78 | - $this->docbuilder = new \Myth\Docs\Builder( array('apppath' => APPPATH) ); |
|
78 | + $this->docbuilder = new \Myth\Docs\Builder( array('apppath' => APPPATH) ); |
|
79 | 79 | |
80 | - $formatter = function ($str) { |
|
81 | - $converter = new \League\CommonMark\CommonMarkConverter(); |
|
82 | - return $converter->convertToHtml($str); |
|
83 | - }; |
|
84 | - $this->docbuilder->registerFormatter($formatter, true); |
|
85 | - } |
|
80 | + $formatter = function ($str) { |
|
81 | + $converter = new \League\CommonMark\CommonMarkConverter(); |
|
82 | + return $converter->convertToHtml($str); |
|
83 | + }; |
|
84 | + $this->docbuilder->registerFormatter($formatter, true); |
|
85 | + } |
|
86 | 86 | |
87 | - //-------------------------------------------------------------------- |
|
87 | + //-------------------------------------------------------------------- |
|
88 | 88 | |
89 | - /** |
|
90 | - * Display the list of documents available and the current document |
|
91 | - * |
|
92 | - * @return void |
|
93 | - */ |
|
94 | - public function index() |
|
95 | - { |
|
96 | - $data = array(); |
|
89 | + /** |
|
90 | + * Display the list of documents available and the current document |
|
91 | + * |
|
92 | + * @return void |
|
93 | + */ |
|
94 | + public function index() |
|
95 | + { |
|
96 | + $data = array(); |
|
97 | 97 | |
98 | - // Make sure the builder knows where to look |
|
99 | - foreach ($this->doc_folders as $alias => $folder) { |
|
100 | - $this->docbuilder->addDocFolder($alias, $folder); |
|
101 | - } |
|
102 | - |
|
103 | - try { |
|
104 | - $content = $this->docbuilder->readPage($this->current_path, $this->current_group); |
|
105 | - $content = $this->docbuilder->postProcess($content, site_url(), current_url()); |
|
106 | - |
|
107 | - $data['sidebar'] = $this->buildSidebar($content); |
|
108 | - $data['toc'] = $this->buildTOC(); |
|
109 | - $data['content'] = $content; |
|
110 | - $data['page_title'] = $this->docbuilder->pageTitle(); |
|
111 | - } catch (Exception $e) { |
|
112 | - $this->setMessage($e->getMessage(), 'warning'); |
|
113 | - } |
|
114 | - |
|
115 | - $this->render($data, config_item('docs.cache_time')); |
|
116 | - } |
|
117 | - |
|
118 | - //-------------------------------------------------------------------- |
|
119 | - |
|
120 | - /** |
|
121 | - * Display search results and handles the search itself. |
|
122 | - * |
|
123 | - * @return void |
|
124 | - */ |
|
125 | - public function search() |
|
126 | - { |
|
127 | - $this->benchmark->mark('search_start'); |
|
128 | - $this->docsearch = new \Myth\Docs\Search(); |
|
129 | - $this->load->helper('markdown_extended'); |
|
130 | - $this->docsearch->registerFormatter('MarkdownExtended', true); |
|
131 | - |
|
132 | - $data = array(); |
|
133 | - |
|
134 | - $terms = $this->input->post('search_terms'); |
|
135 | - |
|
136 | - if ($terms) { |
|
137 | - $search_folders = $this->doc_folders; |
|
138 | - |
|
139 | - $data['results'] = $this->docsearch->search($terms, $search_folders); |
|
140 | - } |
|
141 | - |
|
142 | - $this->benchmark->mark('search_end'); |
|
143 | - |
|
144 | - $data['search_time'] = $this->benchmark->elapsed_time('search_start', 'search_end'); |
|
145 | - $data['search_terms'] = $terms; |
|
146 | - $data['page_title'] = 'Search Results'; |
|
147 | - |
|
148 | - $this->render($data); |
|
149 | - } |
|
150 | - |
|
151 | - //-------------------------------------------------------------------------- |
|
152 | - // Private Methods |
|
153 | - //-------------------------------------------------------------------------- |
|
154 | - |
|
155 | - /** |
|
156 | - * Determines which groups are allowed to be viewed by the current system |
|
157 | - * and the user/environment. |
|
158 | - * |
|
159 | - * todo Allow docs groups to be shown/hidden per-environment, using assoc-array for permitted environments |
|
160 | - */ |
|
161 | - private function determineVisibleGroups($current_group, $current_path) |
|
162 | - { |
|
163 | - // Is displaying docs permitted for this environment? |
|
164 | - if (config_item('docs.permitted_environments') |
|
165 | - && !in_array(ENVIRONMENT, config_item('docs.permitted_environments')) |
|
166 | - ) { |
|
167 | - $this->setMessage(lang('docs_env_disabled'), 'danger'); |
|
168 | - redirect(); |
|
169 | - } |
|
170 | - |
|
171 | - $this->showAppDocs = config_item('docs.show_app_docs'); |
|
172 | - $this->showDevDocs = config_item('docs.show_dev_docs'); |
|
173 | - $this->tocFile = config_item('docs.toc_file') ?: '_toc.ini'; |
|
174 | - |
|
175 | - // Make sure we can still get to the search method. |
|
176 | - if ($current_group == 'search') { |
|
177 | - $this->current_group = false; |
|
178 | - } // Are we allowed to show developer docs in this environment? |
|
179 | - elseif ($current_group == 'developer' |
|
180 | - && !$this->showDevDocs |
|
181 | - && ENVIRONMENT != 'development' |
|
182 | - ) { |
|
183 | - if ($this->showAppDocs) { |
|
184 | - $this->setMessage(lang('docs_not_allowed_dev'), 'warning'); |
|
185 | - |
|
186 | - redirect('docs/application'); |
|
187 | - } |
|
188 | - |
|
189 | - show_error(lang('docs_not_allowed')); |
|
190 | - } |
|
191 | - } |
|
192 | - |
|
193 | - //-------------------------------------------------------------------- |
|
194 | - |
|
195 | - /** |
|
196 | - * Determines the current doc group and file path from the current URL. |
|
197 | - * |
|
198 | - * Returns an array with the group and file path in the 0 and 1 positions, respectively. |
|
199 | - * |
|
200 | - * @return array |
|
201 | - */ |
|
202 | - private function determineFromURL() |
|
203 | - { |
|
204 | - $return = [ |
|
205 | - '', // Group |
|
206 | - '', // File Path |
|
207 | - ]; |
|
208 | - |
|
209 | - $segments = $this->uri->segment_array(); |
|
210 | - |
|
211 | - // Remove the 'docs' from the array |
|
212 | - // for now, we assume this is the first one |
|
213 | - // since that is how Bonfire is setup to show docs |
|
214 | - // @todo Make it so the path can be modified and this still works. |
|
215 | - array_shift($segments); |
|
216 | - |
|
217 | - // If nothing left, then assign the default group and redirect to |
|
218 | - // a page we can do something with... |
|
219 | - if (!count($segments)) { |
|
220 | - redirect('docs/' . config_item('docs.default_group')); |
|
221 | - } |
|
222 | - |
|
223 | - // Do we have a group specified? Bonfire Docs requires that a group |
|
224 | - // be part of the URI so it should be the first element on the array. |
|
225 | - $return[0] = array_shift($segments); |
|
226 | - |
|
227 | - // If there's any more left, then join them together and they'll |
|
228 | - // form the path to the file. This will allow for subfolders with the |
|
229 | - // docs folder itself. |
|
230 | - $return[1] = count($segments) ? implode('/', $segments) : 'index'; |
|
231 | - |
|
232 | - return $return; |
|
233 | - } |
|
234 | - |
|
235 | - //-------------------------------------------------------------------- |
|
236 | - |
|
237 | - /** |
|
238 | - * Builds a TOC for the sidebar out of files found in the following folders: |
|
239 | - * - application/docs |
|
240 | - * - bonfire/docs |
|
241 | - * - {module}/docs |
|
242 | - * |
|
243 | - * @param $content The HTML generated for the page content. |
|
244 | - * @return string The HTML for the sidebar. |
|
245 | - */ |
|
246 | - private function buildSidebar(&$content) |
|
247 | - { |
|
248 | - $data = []; |
|
249 | - |
|
250 | - // Set the remaining data for the view |
|
251 | - $data['docsDir'] = 'docs/' . $this->current_group . '/'; |
|
252 | - $data['docsExt'] = config_item('docs.extension'); |
|
253 | - |
|
254 | - $data['docMap'] = $this->docbuilder->buildDocumentMap($content); |
|
255 | - |
|
256 | - return $this->docbuilder->postProcess( |
|
257 | - $this->load->view('docs/_document_map', $data, true), |
|
258 | - site_url(), |
|
259 | - current_url() |
|
260 | - ); |
|
261 | - } |
|
262 | - |
|
263 | - //-------------------------------------------------------------------- |
|
264 | - |
|
265 | - /** |
|
266 | - * Builds out the nested lists of items that are needed |
|
267 | - */ |
|
268 | - private function buildTOC() |
|
269 | - { |
|
270 | - $folder = $this->doc_folders[$this->current_group] . '/'; |
|
271 | - |
|
272 | - $map = $this->docbuilder->buildTOC($folder); |
|
273 | - |
|
274 | - return $this->docbuilder->postProcess( |
|
275 | - $this->load->view('docs/_toc', ['map' => $map], true), |
|
276 | - site_url(), |
|
277 | - current_url() |
|
278 | - ); |
|
279 | - } |
|
280 | - |
|
281 | - //-------------------------------------------------------------------- |
|
282 | - |
|
283 | - /** |
|
284 | - * Checks all modules to see if they include docs and prepares their doc |
|
285 | - * information for use in the sidebar. |
|
286 | - * |
|
287 | - * @return array |
|
288 | - */ |
|
289 | - private function get_module_docs() |
|
290 | - { |
|
291 | - $docs_modules = array(); |
|
292 | - foreach (\Bonfire\Modules::list_modules() as $module) { |
|
293 | - $ignored_folders = array(); |
|
294 | - $path = \Bonfire\Modules::path($module) . $this->docsDir; |
|
295 | - |
|
296 | - // If these are developer docs, add the folder to the path. |
|
297 | - if ($this->current_group == $this->docsTypeBf) { |
|
298 | - $path .= '/' . $this->docsTypeBf; |
|
299 | - } // For Application docs, ignore the 'developers' folder. |
|
300 | - else { |
|
301 | - $ignored_folders[] = $this->docsTypeBf; |
|
302 | - } |
|
303 | - |
|
304 | - if (is_dir($path)) { |
|
305 | - $files = $this->get_folder_files($path, $module, $ignored_folders); |
|
306 | - if (is_array($files) && count($files)) { |
|
307 | - $docs_modules[$module] = $files; |
|
308 | - } |
|
309 | - } |
|
310 | - } |
|
311 | - |
|
312 | - return $docs_modules; |
|
313 | - } |
|
314 | - //-------------------------------------------------------------------- |
|
98 | + // Make sure the builder knows where to look |
|
99 | + foreach ($this->doc_folders as $alias => $folder) { |
|
100 | + $this->docbuilder->addDocFolder($alias, $folder); |
|
101 | + } |
|
102 | + |
|
103 | + try { |
|
104 | + $content = $this->docbuilder->readPage($this->current_path, $this->current_group); |
|
105 | + $content = $this->docbuilder->postProcess($content, site_url(), current_url()); |
|
106 | + |
|
107 | + $data['sidebar'] = $this->buildSidebar($content); |
|
108 | + $data['toc'] = $this->buildTOC(); |
|
109 | + $data['content'] = $content; |
|
110 | + $data['page_title'] = $this->docbuilder->pageTitle(); |
|
111 | + } catch (Exception $e) { |
|
112 | + $this->setMessage($e->getMessage(), 'warning'); |
|
113 | + } |
|
114 | + |
|
115 | + $this->render($data, config_item('docs.cache_time')); |
|
116 | + } |
|
117 | + |
|
118 | + //-------------------------------------------------------------------- |
|
119 | + |
|
120 | + /** |
|
121 | + * Display search results and handles the search itself. |
|
122 | + * |
|
123 | + * @return void |
|
124 | + */ |
|
125 | + public function search() |
|
126 | + { |
|
127 | + $this->benchmark->mark('search_start'); |
|
128 | + $this->docsearch = new \Myth\Docs\Search(); |
|
129 | + $this->load->helper('markdown_extended'); |
|
130 | + $this->docsearch->registerFormatter('MarkdownExtended', true); |
|
131 | + |
|
132 | + $data = array(); |
|
133 | + |
|
134 | + $terms = $this->input->post('search_terms'); |
|
135 | + |
|
136 | + if ($terms) { |
|
137 | + $search_folders = $this->doc_folders; |
|
138 | + |
|
139 | + $data['results'] = $this->docsearch->search($terms, $search_folders); |
|
140 | + } |
|
141 | + |
|
142 | + $this->benchmark->mark('search_end'); |
|
143 | + |
|
144 | + $data['search_time'] = $this->benchmark->elapsed_time('search_start', 'search_end'); |
|
145 | + $data['search_terms'] = $terms; |
|
146 | + $data['page_title'] = 'Search Results'; |
|
147 | + |
|
148 | + $this->render($data); |
|
149 | + } |
|
150 | + |
|
151 | + //-------------------------------------------------------------------------- |
|
152 | + // Private Methods |
|
153 | + //-------------------------------------------------------------------------- |
|
154 | + |
|
155 | + /** |
|
156 | + * Determines which groups are allowed to be viewed by the current system |
|
157 | + * and the user/environment. |
|
158 | + * |
|
159 | + * todo Allow docs groups to be shown/hidden per-environment, using assoc-array for permitted environments |
|
160 | + */ |
|
161 | + private function determineVisibleGroups($current_group, $current_path) |
|
162 | + { |
|
163 | + // Is displaying docs permitted for this environment? |
|
164 | + if (config_item('docs.permitted_environments') |
|
165 | + && !in_array(ENVIRONMENT, config_item('docs.permitted_environments')) |
|
166 | + ) { |
|
167 | + $this->setMessage(lang('docs_env_disabled'), 'danger'); |
|
168 | + redirect(); |
|
169 | + } |
|
170 | + |
|
171 | + $this->showAppDocs = config_item('docs.show_app_docs'); |
|
172 | + $this->showDevDocs = config_item('docs.show_dev_docs'); |
|
173 | + $this->tocFile = config_item('docs.toc_file') ?: '_toc.ini'; |
|
174 | + |
|
175 | + // Make sure we can still get to the search method. |
|
176 | + if ($current_group == 'search') { |
|
177 | + $this->current_group = false; |
|
178 | + } // Are we allowed to show developer docs in this environment? |
|
179 | + elseif ($current_group == 'developer' |
|
180 | + && !$this->showDevDocs |
|
181 | + && ENVIRONMENT != 'development' |
|
182 | + ) { |
|
183 | + if ($this->showAppDocs) { |
|
184 | + $this->setMessage(lang('docs_not_allowed_dev'), 'warning'); |
|
185 | + |
|
186 | + redirect('docs/application'); |
|
187 | + } |
|
188 | + |
|
189 | + show_error(lang('docs_not_allowed')); |
|
190 | + } |
|
191 | + } |
|
192 | + |
|
193 | + //-------------------------------------------------------------------- |
|
194 | + |
|
195 | + /** |
|
196 | + * Determines the current doc group and file path from the current URL. |
|
197 | + * |
|
198 | + * Returns an array with the group and file path in the 0 and 1 positions, respectively. |
|
199 | + * |
|
200 | + * @return array |
|
201 | + */ |
|
202 | + private function determineFromURL() |
|
203 | + { |
|
204 | + $return = [ |
|
205 | + '', // Group |
|
206 | + '', // File Path |
|
207 | + ]; |
|
208 | + |
|
209 | + $segments = $this->uri->segment_array(); |
|
210 | + |
|
211 | + // Remove the 'docs' from the array |
|
212 | + // for now, we assume this is the first one |
|
213 | + // since that is how Bonfire is setup to show docs |
|
214 | + // @todo Make it so the path can be modified and this still works. |
|
215 | + array_shift($segments); |
|
216 | + |
|
217 | + // If nothing left, then assign the default group and redirect to |
|
218 | + // a page we can do something with... |
|
219 | + if (!count($segments)) { |
|
220 | + redirect('docs/' . config_item('docs.default_group')); |
|
221 | + } |
|
222 | + |
|
223 | + // Do we have a group specified? Bonfire Docs requires that a group |
|
224 | + // be part of the URI so it should be the first element on the array. |
|
225 | + $return[0] = array_shift($segments); |
|
226 | + |
|
227 | + // If there's any more left, then join them together and they'll |
|
228 | + // form the path to the file. This will allow for subfolders with the |
|
229 | + // docs folder itself. |
|
230 | + $return[1] = count($segments) ? implode('/', $segments) : 'index'; |
|
231 | + |
|
232 | + return $return; |
|
233 | + } |
|
234 | + |
|
235 | + //-------------------------------------------------------------------- |
|
236 | + |
|
237 | + /** |
|
238 | + * Builds a TOC for the sidebar out of files found in the following folders: |
|
239 | + * - application/docs |
|
240 | + * - bonfire/docs |
|
241 | + * - {module}/docs |
|
242 | + * |
|
243 | + * @param $content The HTML generated for the page content. |
|
244 | + * @return string The HTML for the sidebar. |
|
245 | + */ |
|
246 | + private function buildSidebar(&$content) |
|
247 | + { |
|
248 | + $data = []; |
|
249 | + |
|
250 | + // Set the remaining data for the view |
|
251 | + $data['docsDir'] = 'docs/' . $this->current_group . '/'; |
|
252 | + $data['docsExt'] = config_item('docs.extension'); |
|
253 | + |
|
254 | + $data['docMap'] = $this->docbuilder->buildDocumentMap($content); |
|
255 | + |
|
256 | + return $this->docbuilder->postProcess( |
|
257 | + $this->load->view('docs/_document_map', $data, true), |
|
258 | + site_url(), |
|
259 | + current_url() |
|
260 | + ); |
|
261 | + } |
|
262 | + |
|
263 | + //-------------------------------------------------------------------- |
|
264 | + |
|
265 | + /** |
|
266 | + * Builds out the nested lists of items that are needed |
|
267 | + */ |
|
268 | + private function buildTOC() |
|
269 | + { |
|
270 | + $folder = $this->doc_folders[$this->current_group] . '/'; |
|
271 | + |
|
272 | + $map = $this->docbuilder->buildTOC($folder); |
|
273 | + |
|
274 | + return $this->docbuilder->postProcess( |
|
275 | + $this->load->view('docs/_toc', ['map' => $map], true), |
|
276 | + site_url(), |
|
277 | + current_url() |
|
278 | + ); |
|
279 | + } |
|
280 | + |
|
281 | + //-------------------------------------------------------------------- |
|
282 | + |
|
283 | + /** |
|
284 | + * Checks all modules to see if they include docs and prepares their doc |
|
285 | + * information for use in the sidebar. |
|
286 | + * |
|
287 | + * @return array |
|
288 | + */ |
|
289 | + private function get_module_docs() |
|
290 | + { |
|
291 | + $docs_modules = array(); |
|
292 | + foreach (\Bonfire\Modules::list_modules() as $module) { |
|
293 | + $ignored_folders = array(); |
|
294 | + $path = \Bonfire\Modules::path($module) . $this->docsDir; |
|
295 | + |
|
296 | + // If these are developer docs, add the folder to the path. |
|
297 | + if ($this->current_group == $this->docsTypeBf) { |
|
298 | + $path .= '/' . $this->docsTypeBf; |
|
299 | + } // For Application docs, ignore the 'developers' folder. |
|
300 | + else { |
|
301 | + $ignored_folders[] = $this->docsTypeBf; |
|
302 | + } |
|
303 | + |
|
304 | + if (is_dir($path)) { |
|
305 | + $files = $this->get_folder_files($path, $module, $ignored_folders); |
|
306 | + if (is_array($files) && count($files)) { |
|
307 | + $docs_modules[$module] = $files; |
|
308 | + } |
|
309 | + } |
|
310 | + } |
|
311 | + |
|
312 | + return $docs_modules; |
|
313 | + } |
|
314 | + //-------------------------------------------------------------------- |
|
315 | 315 | |
316 | 316 | } |