-
-
Notifications
You must be signed in to change notification settings - Fork 104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding include tag/shortcode to include markdown files #194
Comments
Hi there. Ready to get this working?Skip all of this riffraff and scroll down to get to the PHP code. Your original MD files will remain intactYour original *.md files will NOT be overwritten with the included file. Instead, Couscous includes these files into memory, so the update I made edits the contents before it goes into Memory, parsing these Folder path for includesYour includes should be in the folder relative to your current *.md file. Optionally if you provide a Include syntax
Example Markdown using IncludesThe extension for my includes is .mdd so the files are not parsed by Couscous and turned into HTML. These file extension can be anything you want as long as they are not ending in .md
Nested includesSince this code performs a "while" statement on content while it's being replaced, it should automatically handle nested includes (such as an included file containing an include to another file). Replace file contents with PHP belowsrc/Module/Markdown/Step/LoadMarkdownFiles.php<?php
namespace Couscous\Module\Markdown\Step;
use Couscous\Model\Project;
use Couscous\Module\Markdown\Model\MarkdownFile;
use Couscous\Step;
use Symfony\Component\Finder\SplFileInfo;
/**
* Loads Markdown files in memory.
*
* @author Matthieu Napoli <[email protected]>
*/
class LoadMarkdownFiles implements Step
{
public function __invoke(Project $project)
{
$files = $project->sourceFiles();
$files->name('*.md');
foreach ($files as $file) {
/** @var SplFileInfo $file */
$content = $this->importIncludes(file_get_contents($file->getPathname()), $file->getPath());
$project->addFile(new MarkdownFile($file->getRelativePathname(), $content));
}
$project->watchlist->watchFiles($files);
}
private function importIncludes($content, $base_path)
{
$open = '[include(';
$close = ')]';
if ( ! strstr($content, $open) || ! strstr($content, $close) )
{
return $content;
}
while ( strstr($content, $open) && strstr($content, $close) )
{
$statement = substr($content, strpos($content, $open));
$statement = trim(substr($statement, 0, strpos($statement, $close) + strlen($close)));
$file_include = substr($statement, strpos($statement, '(') + 1);
$file_include = substr($file_include, 0, strpos($file_include, $close));
$file_include = $this->unwrap($file_include, ["'", '"']);
if ( substr($file_include, 0, 1) != '/' )
{
$file_include = $base_path . '/' . $file_include;
}
$file_contents = "# File Not Found: {$file_include}";
if ( is_file($file_include) )
{
$file_contents = file_get_contents($file_include);
}
$content = str_replace($statement, $file_contents, $content);
}
return $content;
}
private function unwrap($str, $encapsulated = [])
{
$str = trim($str);
$encapsulated = ! is_array($encapsulated) ? [$encapsulated] : $encapsulated;
foreach ( $encapsulated as $unwrap )
{
if ( substr($str, 0, 1) == $unwrap )
{
$str = substr($str, 1);
$str = $this->unwrap($str, $encapsulated);
}
if ( substr($str, -1) == $unwrap )
{
$str = substr($str, 0, -1);
$str = $this->unwrap($str, $encapsulated);
}
}
return $str;
}
} Recompile Couscous from SourceRun this command in a Linux command prompt:
You will run it from a directory that looks like this: How to get source installedGo to the main page on this repo - perform a I hope some of this helps. If you can get the PHP file updated, recompile the |
Hello! im using couscous to generate docs and it will be great to use something like this
{% include ../parts/api-rules.md %}
so i want to change content of partial and cous cous will update all html with this partialThe text was updated successfully, but these errors were encountered: