Exemplo n.º 1
0
    /**
     * Prepare the parsed markdown and/or user messages
     */
    protected function _prepareParsedMarkdown()
    {
        $curent_file_with_root = $this->getTree()->getSearch()->getCurrentFile(false);
        $curent_file_without_root = $this->getTree()->getSearch()->getCurrentFile(true);
        // Prepare the parsedown content
        if (empty($curent_file_without_root)) {
            if ($this->_tree->getSearch()->isCurrentFileValid()) {
                $this->_user_messages['info'][] = array('title' => 'No file selected', 'message' => 'Browse the file tree on the left and click a file.');
            } else {
                $this->_user_messages['danger'][] = array('title' => 'Invalid request', 'message' => 'The file you requested is not accessable by this application.');
            }
        } elseif (file_exists($curent_file_with_root) && is_dir($curent_file_with_root)) {
            $this->_user_messages['warning'][] = array('title' => 'Directory selected', 'message' => 'Cannot display the content of directories.
							  Browse the file tree on the left and click a file.');
        } elseif (file_exists($curent_file_with_root) && is_readable($curent_file_with_root)) {
            try {
                // Parsedown execution
                $parser = new \ParsedownExtra();
                $file_encoder = new FileEncoder($curent_file_with_root);
                $markdown = $parser->text(utf8_decode($file_encoder->getFileContents()));
                if (!empty($markdown)) {
                    $dom_implementation = new \DOMImplementation();
                    $doc_type = $dom_implementation->createDocumentType('html', '', '');
                    $dom = $dom_implementation->createDocument('', 'html', $doc_type);
                    libxml_use_internal_errors(true);
                    $dom->loadHTML($markdown);
                    $errors = libxml_get_errors();
                    if (!empty($errors)) {
                        $messages = array();
                        /** @var \LibXMLError $error */
                        foreach ($errors as $error) {
                            $messages[] = $error->message;
                        }
                        $this->_user_messages['warning'][] = array('title' => 'This markdown file contains erroneous code', 'message' => join(', ', $messages));
                    }
                    $this->_parsed_markdown = $dom->documentElement;
                    $this->modifyInternalLinks();
                } else {
                    $this->_user_messages['warning'][] = array('title' => ":-( You're not done yet!", 'message' => 'This file has no content at all.');
                }
            } catch (\Exception $e) {
                $this->_parsed_markdown = null;
                $this->_user_messages['danger'][] = array('title' => "Oops! An error occured while parsing markdown file", 'message' => $curent_file_without_root . ': ' . $e->getMessage());
            }
        } else {
            $this->_user_messages['danger'][] = array('title' => '404', 'message' => 'The file you requested does not exist or is not readable.');
        }
    }
Exemplo n.º 2
0
 /**
  * Return the page output (HTML / Raw)
  *
  * @param array $headers Output headers
  *
  * @return string|\DOMDocument
  */
 public function getOutput(array &$headers = array())
 {
     $this->_prepareOptions();
     $this->_prepareSearch();
     $this->_prepareTree();
     // Raw output?
     switch ($this->_options->get(Opt::OUTPUT_TYPE)) {
         case Opt::OUTPUT_TYPE_RAW:
             $headers['Content-type'] = 'text/plain; charset=UTF-8';
             $current_file = $this->_search->getCurrentFile(false);
             if ($this->_search->isCurrentFileValid() && is_file($current_file)) {
                 $file_encoder = new FileEncoder($current_file);
                 $output = $file_encoder->getFileContents();
             } else {
                 $output = 'Your current selection is not valid.';
             }
             break;
         case Opt::OUTPUT_TYPE_DOM:
             $headers['Content-type'] = 'text/html; charset=UTF-8';
             $this->_tree->buildTree();
             $this->_preparePage();
             $output = $this->_page->getDOMDocument();
             break;
         default:
             $output = 'No valid output type set.';
     }
     return $output;
 }