} $extraPreNewline = false; $out[] = $lines[$i]; } return implode("\n", $out); } protected function getLines($s) { // Ensure that we only have a single \n at the end of each line. $s = str_replace("\r\n", "\n", $s); $s = str_replace("\r", "\n", $s); return explode("\n", $s); } } $inputDirectory = "../master/"; $cleanup = new MarkdownCleanup(); $path = realpath($inputDirectory); $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); foreach ($objects as $name => $object) { $filename = $object->getFilename(); $ext = pathinfo($filename, PATHINFO_EXTENSION); if ($filename == "." || $filename == ".." || $ext != 'md') { continue; } $inputDir = $object->getPath(); if (is_dir($object->getPathname())) { continue; } $newContent = $cleanup->process("{$inputDir}/{$filename}"); file_put_contents("{$inputDir}/{$filename}", $newContent); }
function convert($s) { $lines = $this->getLines($s); $output = ""; $lineMode = "text"; $this->listItemType = ""; $this->lineNumber = 0; foreach ($lines as $line) { $this->lineNumber++; $prevLineMode = $lineMode; // Determine if the line mode is changing $tl = trim($line); if ($lineMode != "code" && preg_match('/^\\<code(|\\s([a-zA-Z0-9])*)\\>$/U', $tl)) { $line = "~~~"; if ($tl != "<code>") { $line .= " {" . substr($tl, 6, -1) . "}"; } $lineMode = "code"; } else { if ($lineMode == "code" && $tl == "</code>") { $line = "~~~"; $lineMode = "text"; } else { if ($lineMode == "text" && strlen($tl) > 0 && ($tl[0] == "^" || $tl[0] == "|")) { // first char is a ^ so its the start of a table. In table mode we // just accumulate table rows in $table, and render when // we switch out of table mode so we can do column widths right. $lineMode = "table"; $table = array(); } else { if ($lineMode == "table" && ($tl == "" || $tl[0] != "^" && $tl[0] != "|")) { $lineMode = "text"; } } } } if ($prevLineMode == "table" && $lineMode != "table") { $output .= $this->renderTable($table); } // perform mode-specific translations switch ($lineMode) { case "text": $line = $this->convertInlineMarkup($line); $line = $this->convertListItems($line); break; case "code": break; case "table": // Grab this line, break it up and add it to $table after // performing inline transforms on each cell. $parts = explode($tl[0], $this->convertInlineMarkup($line)); for ($i = 0; $i < count($parts); $i++) { $parts[$i] = trim($parts[$i]); } $table[] = $parts; break; } if ($lineMode != "table") { $output .= $line . "\n"; } } $cleanup = new MarkdownCleanup(); $output = $cleanup->process($output); return $output; }