Commit 66477091 by Carsten Brandt

generate PDF file chapters from README.md structure

parent d52299ba
......@@ -59,6 +59,19 @@ Currently there is only the `bootstrap` template available.
You may also add the `yii\apidoc\commands\RenderController` to your console application class map and
run it inside of your applications console app.
Creating a PDF from the guide
-----------------------------
You need `pdflatex` and `make` for this.
```
vendor/bin/apidoc guide source/docs ./output --template=pdf
cd ./output
make pdf
```
If all runs without errors the PDF will be `guide.pdf` in the `output` dir.
Creating your own templates
---------------------------
......
<?php
/**
* Created by PhpStorm.
* User: cebe
* Date: 26.05.14
* Time: 18:15
*/
namespace yii\apidoc\helpers;
use cebe\markdown\Markdown;
class IndexFileAnalyzer extends Markdown
{
private $_chapter = 0;
private $_chapters = [];
public function analyze($text)
{
$this->parse($text);
return $this->_chapters;
}
protected function renderHeadline($block)
{
$this->_chapters[++$this->_chapter] = [
'headline' => $block['content'],
'content' => [],
];
return parent::renderHeadline($block);
}
protected function renderList($block)
{
foreach ($block['items'] as $item => $itemLines) {
if (preg_match('~\[([^\]]+)\]\(([^\)]+)\)(.*)~', implode("\n", $itemLines), $matches)) {
$this->_chapters[$this->_chapter]['content'][] = [
'headline' => $matches[1],
'file' => $matches[2],
'teaser' => $matches[3],
];
}
}
return parent::renderList($block);
}
}
\ No newline at end of file
......@@ -10,6 +10,7 @@ namespace yii\apidoc\templates\pdf;
use cebe\markdown\latex\GithubMarkdown;
use Yii;
use yii\apidoc\helpers\ApiIndexer;
use yii\apidoc\helpers\IndexFileAnalyzer;
use yii\helpers\Console;
use yii\helpers\FileHelper;
......@@ -43,15 +44,17 @@ class GuideRenderer extends \yii\apidoc\templates\html\GuideRenderer
}
$done = 0;
$fileData = [];
// $headlines = [];
$chapters = [];
foreach ($files as $file) {
if (basename($file) == 'README.md') {
$indexAnalyzer = new IndexFileAnalyzer();
$chapters = $indexAnalyzer->analyze(file_get_contents($file));
continue; // to not add index file to nav
}
if (basename($file) == 'tutorial-i18n.md') {
continue; // TODO avoid i18n tut because of non displayable characters right now. need to fix it.
}
$fileData[$file] = file_get_contents($file);
$fileData[basename($file)] = file_get_contents($file);
// if (preg_match("/^(.*)\n=+/", $fileData[$file], $matches)) {
// $headlines[$file] = $matches[1];
// } else {
......@@ -61,22 +64,19 @@ class GuideRenderer extends \yii\apidoc\templates\html\GuideRenderer
$md = new GithubMarkdown();
$output = '';
foreach ($fileData as $file => $content) {
$output .= $md->parse($content) . "\n\n"; // TODO generate links to yiiframework.com by default
// $output = $this->fixMarkdownLinks($output);
// if ($this->layout !== false) {
// $params = [
//// 'headlines' => $headlines,
// 'currentFile' => $file,
// 'content' => $output,
// ];
// $output = $this->getView()->renderFile($this->layout, $params, $this);
// }
// $fileName = $this->generateGuideFileName($file);
// file_put_contents($targetDir . '/' . $fileName, $output);
foreach ($chapters as $chapter) {
$output .= '\chapter{' . $chapter['headline'] . "}\n";
foreach($chapter['content'] as $content) {
if (isset($fileData[$content['file']])) {
$output .= $md->parse($fileData[$content['file']]) . "\n\n";
} else {
$output .= '\newpage\textbf{Error: not existing file: '.$content['file'].'}\newpage'."\n";
}
if ($this->controller !== null) {
Console::updateProgress(++$done, $fileCount);
if ($this->controller !== null) {
Console::updateProgress(++$done, $fileCount);
}
}
}
file_put_contents($targetDir . '/guide.tex', $output);
......
\documentclass[a4paper, 10pt]{article}
\documentclass[a4paper, 10pt]{book}
% english and utf8
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment