openFile(); } catch (RuntimeException $e) { throw new Exception($e->getMessage()); } if ($fileObject->flock(LOCK_SH) === false) { throw new Exception('Couldn\'t get the lock'); } while (!$fileObject->eof()) { $line = $fileObject->fgets(); if ($line && $line[0] === '#' && preg_match('/^#+/', $line, $match) === 1 ) { // Atx-style $level = strlen($match[0]); $heading = trim(strip_tags(substr($line, $level))); $fragment = urlencode($heading); $toc[] = array( 'heading' => $heading, 'level' => $level, 'fragment' => $fragment ); $line = '' . "\n" . $line; } elseif ( $line && ($line[0] === '=' || $line[0] === '-') && preg_match('/^[=-]+\s*$/', $line, $match) === 1 ) { // Setext if ($match[0][0] === '=') { // H1 $level = 1; } else { // H2 $level = 2; } $heading = trim(strip_tags(end($cat))); $fragment = urlencode($heading); $toc[] = array( 'heading' => $heading, 'level' => $level, 'fragment' => $fragment ); $line = '' . "\n" . $line; } $cat[] = $line; } $fileObject->flock(LOCK_UN); } $html = Parsedown::instance()->parse(implode('', $cat)); return array($html, $toc); } }