Skip to content

Commit

Permalink
Added new macro (MacroInlineFileLink) to link external files to docum…
Browse files Browse the repository at this point in the history
…entations output directory
  • Loading branch information
paveljanda committed Jan 28, 2018
1 parent 5312d45 commit 9560c67
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 2 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ublaboo/anabelle

## Api documentation generator (JSON-RPC / REST)

(No matter whether you're using REST or JSON-RPC)
(No matter whether you're using REST or JSON-RPC or some other api architecture/scheme)

### Example

Expand Down Expand Up @@ -76,6 +76,15 @@ Method section definition. This macro available only in `index.md` file.
@@ user.confirm-registration:methods/user.confirm-registration.md
```

#### `[File link](foo/bar/data.json)`

[File link](foo/bar/data.json) will create a link to file (`foo/bar/data.json`). The file will be copied to documentation output directory for safety reasons.

```md
[File link](foo/bar/data.json)
[Project root directory file link](../app/schema/user.json)
```


## How to use anabelle

Expand Down
2 changes: 2 additions & 0 deletions docs-src/about.md
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.
2 changes: 1 addition & 1 deletion docs/about.html
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 added docs/assets/favicon.ico
Binary file not shown.
55 changes: 55 additions & 0 deletions src/Markdown/Macros/MacroInlineFileLink.php
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
);
}
}
2 changes: 2 additions & 0 deletions src/Markdown/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Ublaboo\Anabelle\Markdown\Macros\MacroBlockVariableOutput;
use Ublaboo\Anabelle\Markdown\Macros\MacroCleanIndex;
use Ublaboo\Anabelle\Markdown\Macros\MacroInclude;
use Ublaboo\Anabelle\Markdown\Macros\MacroInlineFileLink;
use Ublaboo\Anabelle\Markdown\Macros\MacroInlineVariable;
use Ublaboo\Anabelle\Markdown\Macros\MacroInlineVariableOutput;
use Ublaboo\Anabelle\Markdown\Macros\MacroSection;
Expand Down Expand Up @@ -120,6 +121,7 @@ private function setupMacros(): void
$this->macros[] = new MacroInclude;
$this->macros[] = new MacroInlineVariable($this->docuScope);
$this->macros[] = new MacroInlineVariableOutput($this->docuScope);
$this->macros[] = new MacroInlineFileLink($this->docuScope);
$this->macros[] = new MacroBlockVariable($this->docuScope);
$this->macros[] = new MacroBlockVariableOutput($this->docuScope);

Expand Down

0 comments on commit 9560c67

Please sign in to comment.