-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new macro (MacroInlineFileLink) to link external files to docum…
…entations output directory
- Loading branch information
1 parent
5312d45
commit 9560c67
Showing
6 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
## About | ||
|
||
Blah blah blah about | ||
|
||
[This](../assets/favicon.ico) is a link to a file (`assets/favicon.ico`) that will be copied do documentations directory. It can be used for example for linking project-related json files, config YAML files and other important data. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<h2>About</h2> <p>Blah blah blah about</p> | ||
<h2>About</h2> <p>Blah blah blah about</p> <p><a href="assets/favicon.ico" target="_blank">This</a> is a link to a file (<code>assets/favicon.ico</code>) that will be copied do documentations directory. It can be used for example for linking project-related json files, config YAML files and other important data.</p> |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ublaboo\Anabelle\Markdown\Macros; | ||
|
||
use Nette\Utils\Html; | ||
use Ublaboo\Anabelle\Generator\Exception\DocuGeneratorException; | ||
|
||
final class MacroInlineFileLink implements IMacro | ||
{ | ||
|
||
const LINK_PATTERN = '/\[((?:[^][]++|(?R))*+)\]\(([^\)]*)\)/um'; | ||
|
||
|
||
/** | ||
* @throws DocuGeneratorException | ||
*/ | ||
public function runMacro( | ||
string $inputDirectory, | ||
string $outputDirectory, | ||
string & $content // Intentionally & | ||
): void | ||
{ | ||
/** | ||
* Substitute "#include" macros with actual files | ||
*/ | ||
$content = preg_replace_callback( | ||
self::LINK_PATTERN, | ||
function(array $input) use ($inputDirectory, $outputDirectory): string { | ||
$text = $input[1]; | ||
$path = trim($input[2], '/'); | ||
|
||
if (file_exists($inputDirectory . '/' . $path)) { | ||
$targetPath = str_replace(['../', './'], '', $outputDirectory . '/' . $path); | ||
|
||
if (!file_exists(dirname($targetPath))) { | ||
mkdir(dirname($targetPath), 0755, true); | ||
} | ||
|
||
copy($inputDirectory . '/' . $path, $targetPath); | ||
|
||
$targetPath = preg_replace('~^[^/]+/~', '', $targetPath); | ||
|
||
return (string) Html::el('a')->href($targetPath) | ||
->setText($text) | ||
->target('_blank'); | ||
} | ||
|
||
return "[$text]($path)"; | ||
}, | ||
$content | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters