/vendor/akelos/makelos/tasks/doc/website_files/app/helpers/docs_helper.php

https://github.com/onivan/akelos · PHP · 78 lines · 67 code · 11 blank · 0 comment · 4 complexity · 36f9ee3bb612d2b7e4852e4516a26dc1 MD5 · raw file

  1. <?php
  2. class DocsHelper extends AkBaseHelper
  3. {
  4. public $docs_path;
  5. public function get_doc_contents($doc_name){
  6. $doc_file = $this->_getdocPath($doc_name, Ak::lang());
  7. return file_exists($doc_file) ? file_get_contents($doc_file) : @file_get_contents($this->_getdocPath($doc_name, 'en'));
  8. }
  9. public function render_doc($doc_contents){
  10. return $this->_afterRender(AkTextHelper::textilize($this->_beforeRender($doc_contents)));
  11. }
  12. public function link_to_guide($guide_name, $slug = '', $html_options = array()){
  13. return $this->_controller->ak_url_helper->link_to($this->t($guide_name), array('controller'=>'docs', 'action'=>'guide', 'id' => $this->t($slug), 'format' =>'html'), $html_options);
  14. }
  15. private function _getdocPath($doc_name, $language){
  16. $doc_name = AkInflector::underscore($language != 'en' ? Ak::untranslate($doc_name, $language) : $doc_name);
  17. return AkConfig::getDir('docs').DS.(empty($this->docs_path) ? '' : trim($this->docs_path, DS).DS).$language.DS.$doc_name.'.textile';
  18. }
  19. private function _afterRender($html){
  20. $html = $this->_highlightNotes($html);
  21. return $html;
  22. }
  23. private function _beforeRender($textile){
  24. $textile = $this->_replacePlusPlus($textile);
  25. $textile = $this->_setCodeBlocks($textile);
  26. return $textile;
  27. }
  28. private function _highlightNotes($html){
  29. if(preg_match_all('/(IMPORTANT|CAUTION|WARNING|NOTE|INFO|TIP)(?:\.|\:)(.*)/', $html, $matches)){
  30. foreach ($matches[1] as $k => $class){
  31. $css_class = strtolower($class);
  32. $css_class = in_array($css_class, array('caution', 'important')) ? 'warning' : $css_class;
  33. $css_class = in_array($css_class, array('tip')) ? 'info' : $css_class;
  34. $html = str_replace($matches[0][$k], "<div class='$css_class'><p>".strip_tags($matches[2][$k]).'</p></div>', $html);
  35. }
  36. }
  37. return $html;
  38. }
  39. private function _replacePlusPlus($textile){
  40. if(preg_match_all('/\+(.+)\+/', $textile, $matches)){
  41. foreach ($matches[1] as $k => $tt){
  42. $textile = str_replace($matches[0][$k], "<notextile><tt>$tt</tt></notextile>", $textile);
  43. }
  44. }
  45. $textile = str_replace('<plus>', '+', $textile);
  46. return $textile;
  47. }
  48. private function _setCodeBlocks($textile){
  49. if(preg_match_all('/<(yaml|shell|php|tpl|html|sql|plain)>(.*?)<\/\\1>/ms', $textile, $matches)){
  50. foreach ($matches[1] as $k => $class){
  51. $css_class = strtolower($class);
  52. $css_class = in_array($css_class, array('shell')) ? 'html' : $css_class;
  53. $escaped = AkTextHelper::html_escape($matches[2][$k]);
  54. $textile = str_replace($matches[0][$k], $this->_tabText("<notextile><div class='code_container'><code class='$css_class'>$escaped</code></div></notextile>"), $textile);
  55. }
  56. }
  57. return $textile;
  58. }
  59. private function _tabText($text){
  60. $lines = explode("\n", $text."\n");
  61. $result = array();
  62. foreach ($lines as $line){
  63. $result[] = str_repeat(' ', 4).$line;
  64. }
  65. return trim(join("\n", $result));
  66. }
  67. }