Skip to content

Commit

Permalink
Generate .html files by default. When using http auth, generate .php …
Browse files Browse the repository at this point in the history
…files. closes #6
  • Loading branch information
paveljanda committed Nov 21, 2017
1 parent c1381d5 commit 5357c6a
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 8 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ vendor/bin/anabelle docuDir docuOutputDir -o
vendor/bin/anabelle docuDir docuOutputDir --overwriteOutputDir
```

#### Add http auth to generated php files
#### Add http auth to generated files

**Beware! Anabelle generates by default .html files. If you are using http auth, it generates .php files due to the need of validating http auth headers.**

```bash
vendor/bin/anabelle docuDir docuOutputDir -u user -p pass
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions demoOutput/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html> <html> <head> <title>Awesome cookbook JSON-RPC API doc</title> <style>html,body{margin:0;padding:0;height:100%;width:100%}h1,h2,h3,h4,h5,h6,p{margin:0}body{background-color:#2980b9;background-color:#34495e;font-size:12px;font-family:Arial,Helvetica,sans-serif;color:#2c3e50;overflow:auto;box-sizing:border-box;width:100vw;max-width:100vw;height:100vh;max-height:100vh;display:grid;grid-template-columns:auto 300px 900px auto;grid-template-rows:80px auto 80px;grid-template-areas:"header header header header" "empty-left nav main empty-right" "footer footer footer footer";grid-column-gap:2em}header{grid-area:header}nav{grid-area:nav;align-self:start}.empty-left{grid-area:empty-left}.empty-right{grid-area:empty-right}main{grid-area:main}footer{grid-area:footer}button.section-method,a.section-site{font-size:12px;display:block;width:100%;border:0;margin:0;line-height:30px;vertical-align:middle;padding:0 1em;background-color:#fff;color:#505050;box-sizing:border-box;text-align:left;border-radius:0;cursor:pointer}button.section-method:hover,a.section-site:hover,button.section-method:active,a.section-site:active,button.section-method:focus,a.section-site:focus,button.section-method:target,a.section-site:target{background-color:#eee;cursor:pointer;outline:none}.section-site:first-of-type{font-weight:700}.section-method:first-of-type,.section-site:first-of-type{border-top-right-radius:2px;border-top-left-radius:2px}.section-method:last-of-type,.section-site:last-of-type{border-bottom-right-radius:2px;border-bottom-left-radius:2px}.section-method:first-of-type{margin-top:1em}iframe{width:100%;height:0;border:0;padding:0;margin:0}</style> <link rel="shortcut icon" type="image/x-icon" href="favicon.ico"/> </head> <body> <header>Awesome cookbook JSON-RPC API doc</header> <div class="empty-left"></div> <nav> <p><a data-section-href="home.html" class="section-site" data-target="home">Home</a> <a data-section-href="about.html" class="section-site" data-target="about-project">About project</a> <button data-section-href="methods/user.login.html" class="section-method" data-target="user-login">user.login</button> <button data-section-href="methods/user.logout.html" class="section-method" data-target="user-logout">user.logout</button></p></nav> <div class="empty-right"></div> <main> <iframe src="" scrolling="no"></iframe> </main> <footer></footer> <script type="text/javascript">document.addEventListener("DOMContentLoaded",function(event){var buttons=document.querySelectorAll('[data-section-href]');for(var i=0;i<buttons.length;i++){buttons[i].addEventListener("click",function(event){event.stopPropagation();event.preventDefault();window.location.hash=this.getAttribute("data-target")})} if(buttons.length){if(window.location.hash){onHashChangeRouter()}else{var sectionLink=buttons[0].getAttribute("data-section-href");document.getElementsByTagName('iframe')[0].setAttribute("src",sectionLink)}}});document.getElementsByTagName("iframe")[0].addEventListener("load",function(){this.style.height=this.contentWindow.document.body.scrollHeight+'px'});document.getElementsByTagName("iframe")[0].addEventListener("load",function(){var jsonAreas=this.contentWindow.document.getElementsByClassName('language-json');for(var i=0;i<jsonAreas.length;i++){var content=jsonAreas[i].innerHTML;content=content.replace(/"([^"]+)":/g,"<span class=language-json-key>&quot;$1&quot;:</span>");content=content.replace(/"([^"]+)"([^:])/g,"<span class=language-json-string>&quot;$1&quot;</span>$2");content=content.replace(/([ ,:{\n])(\d+)/g,"$1<span class=language-json-number>$2</span>");jsonAreas[i].innerHTML=content}});var onHashChangeRouter=function(){if(window.location.hash){var hash=window.location.hash.replace(/#/,'');var sectionLink=document.querySelectorAll('[data-target="'+hash+'"]');if(sectionLink.length){var sectionSrc=sectionLink[0].getAttribute("data-section-href");document.getElementsByTagName('iframe')[0].setAttribute("src",sectionSrc)}}};window.onhashchange=function(){onHashChangeRouter()}</script> </body> </html>
1 change: 0 additions & 1 deletion demoOutput/index.php

This file was deleted.

File renamed without changes.
File renamed without changes.
9 changes: 8 additions & 1 deletion src/Generator/DocuGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ final class DocuGenerator
*/
private $outputDirectory;

/**
* @var AuthCredentials
*/
private $authCredentials;

/**
* @var Logger
*/
Expand Down Expand Up @@ -56,9 +61,11 @@ public function __construct(
*/
public function run(): void
{
$fileType = $this->authCredentials->getUser() ? 'php' : 'html';

$this->parser->parseFile(
$this->inputDirectory . '/index.md',
$this->outputDirectory . '/index.php'
$this->outputDirectory . "/index.{$fileType}"
);
}
}
17 changes: 13 additions & 4 deletions src/Markdown/Macros/MacroSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ final class MacroSection implements IMacro
*/
private $parser;

/**
* @var AuthCredentials
*/
private $authCredentials;


public function __construct(
Logger $logger,
AuthCredentials $authCredentials,
DocuScope $docuScope
) {
$this->authCredentials = $authCredentials;

$this->parser = new Parser(false, $authCredentials, $logger, $docuScope);
}

Expand All @@ -37,6 +44,8 @@ public function runMacro(
string & $content // Intentionally &
): void
{
$fileType = $this->authCredentials->getUser() ? 'php' : 'html';

/**
* Find "@@" sections and parse their child .md file
* == normal section with json-rpc methods
Expand All @@ -46,13 +55,13 @@ public function runMacro(
*/
$content = preg_replace_callback(
'/^@@? (.+[^:]):(.+\.md)/m',
function(array $input) use ($inputDirectory, $outputDirectory): string {
function(array $input) use ($inputDirectory, $outputDirectory, $fileType): string {
$inputFile = $inputDirectory . '/' . dirname($input[2]) . '/' . basename($input[2]);

/**
* Output file is .php
* Output file is of type .(php|html)
*/
$outputFile = preg_replace('/md$/', 'php', $inputFile);
$outputFile = preg_replace('/md$/', $fileType, $inputFile);

/**
* Substitute input dir for output dir
Expand All @@ -61,7 +70,7 @@ function(array $input) use ($inputDirectory, $outputDirectory): string {

$this->parser->parseFile($inputFile, $outputFile);

return preg_replace('/md$/', 'php', $input[0]);
return preg_replace('/md$/', $fileType, $input[0]);
},
$content
);
Expand Down
2 changes: 1 addition & 1 deletion src/Parsedown/CustomParsedown.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function __construct()
*/
protected function inlineSection($excerpt): ?array
{
if (preg_match('/^(@@?) ?(.+[^:]):(.+\.php)/', $excerpt['text'], $matches)) {
if (preg_match('/^(@@?) ?(.+[^:]):(.+\.(php|html))/', $excerpt['text'], $matches)) {
$class = strlen($matches[1]) == 1 ? 'section-site' : 'section-method';
$element = strlen($matches[1]) == 1 ? 'a' : 'button';

Expand Down

0 comments on commit 5357c6a

Please sign in to comment.