8
0
Fork 0

Code Style and documentation

Dieser Commit ist enthalten in:
Stéphane Goetz 2015-07-17 23:38:06 +02:00
Ursprung e7afd9aa28
Commit 4f8d817365
22 geänderte Dateien mit 415 neuen und 177 gelöschten Zeilen

Datei anzeigen

@ -2,11 +2,18 @@
use ArrayObject;
class Config extends ArrayObject {
public function merge($newValues, $override = true) {
class Config extends ArrayObject
{
/**
* Merge an array into the object
*
* @param array $newValues
* @param bool $override
*/
public function merge($newValues, $override = true)
{
foreach ($newValues as $key => $value) {
if (array_key_exists($key, $this) && $override == false) {
if ($override === false && array_key_exists($key, $this)) {
continue;
}
@ -14,7 +21,13 @@ class Config extends ArrayObject {
}
}
public function conservativeMerge($newValues) {
/**
* Merge an array into the object, ignore already added keys.
*
* @param $newValues
*/
public function conservativeMerge($newValues)
{
$this->merge($newValues, false);
}
}

Datei anzeigen

@ -19,9 +19,10 @@ class Daux
protected $processor;
/**
* @var Tree\Entry
* @var Tree\Directory
*/
public $tree;
/**
* @var Config
*/
@ -84,6 +85,14 @@ class Daux
$this->options->merge($default_config);
}
/**
* Load the configuration files, first, "config.json"
* in the documentation and then the file specified
* when running the configuration
*
* @param string $override_file
* @throws Exception
*/
private function loadConfigOverrides($override_file)
{
// Read documentation overrides

Datei anzeigen

@ -4,6 +4,11 @@ use Todaymade\Daux\Tree\Directory;
class DauxHelper
{
/**
* @param Config $params
* @param string $current_url
* @return array
*/
public static function getTheme($params, $current_url)
{
$theme_folder = $params['local_base'] . DS . 'resources' . DS . 'themes' . DS . $params['theme-name'];
@ -39,21 +44,19 @@ class DauxHelper
$theme['templates'] = strtr($theme['templates'], $substitutions);
$theme['favicon'] = utf8_encode(strtr($theme['favicon'], $substitutions));
foreach ($theme['css'] as $key => $css) {
$theme['css'][$key] = utf8_encode(strtr($css, $substitutions));
foreach (['css', 'js', 'fonts'] as $element) {
foreach ($theme[$element] as $key => $value) {
$theme[$element][$key] = utf8_encode(strtr($value, $substitutions));
}
foreach ($theme['fonts'] as $key => $font) {
$theme['fonts'][$key] = utf8_encode(strtr($font, $substitutions));
}
foreach ($theme['js'] as $key => $js) {
$theme['js'][$key] = utf8_encode(strtr($js, $substitutions));
}
return $theme;
}
/**
* @param string $path
* @return string
*/
public static function getCleanPath($path)
{
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
@ -72,24 +75,31 @@ class DauxHelper
return implode(DIRECTORY_SEPARATOR, $absolutes);
}
/**
* Get pathinfo for a file
*
* @param string $path
* @return array
*/
public static function pathinfo($path)
{
preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', $path, $m);
if (isset($m[1])) {
$ret['dir']=$m[1];
$ret = [];
foreach (['dir' => 1, 'basename' => 2, 'filename' => 3, 'extension' => 5] as $key => $group) {
if (isset($m[$group])) {
$ret[$key] = $m[$group];
}
if (isset($m[2])) {
$ret['basename']=$m[2];
}
if (isset($m[5])) {
$ret['extension']=$m[5];
}
if (isset($m[3])) {
$ret['filename']=$m[3];
}
return $ret;
}
/**
* Locate a file in the tree. Returns the file if found or false
*
* @param Directory $tree
* @param string $request
* @return Tree\Entry|false
*/
public static function getFile($tree, $request)
{
$request = explode('/', $request);
@ -149,10 +159,13 @@ class DauxHelper
$separator = '_';
// Convert all dashes into underscores
$title = preg_replace('![' . preg_quote("-") . ']+!u', $separator, $title);
// Remove all characters that are not the separator, letters, numbers, or whitespace.
$title = preg_replace('![^' . preg_quote($separator) . '\pL\pN\s]+!u', '', $title);
// Replace all separator characters and whitespace by a single separator
$title = preg_replace('![' . preg_quote($separator) . '\s]+!u', $separator, $title);
return trim($title, $separator);
}
@ -167,7 +180,9 @@ class DauxHelper
{
static $charsArray;
if (isset($charsArray)) return $charsArray;
if (isset($charsArray)) {
return $charsArray;
}
return $charsArray = array(
'a' => array(

Datei anzeigen

@ -22,12 +22,12 @@ class LinkRenderer extends \League\CommonMark\Inline\Renderer\LinkRenderer
}
/**
* @param $url
* @param string $url
* @return Entry
* @throws Exception
*/
protected function resolveInternalFile($url) {
protected function resolveInternalFile($url)
{
$file = DauxHelper::getFile($this->daux['tree'], $url);
if ($file) {
return $file;

Datei anzeigen

@ -3,11 +3,9 @@
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\ParseException;
use GuzzleHttp\Exception\TransferException;
class Api
{
protected $base_url;
protected $user;
protected $pass;
@ -75,60 +73,23 @@ class Api
}
/**
* /rest/api/content/{id}/child/{type}
* Get a list of pages
*
* @param $rootPage
* @return mixed
* @param integer $rootPage
* @param bool $recursive
* @return array
*/
public function getList($rootPage)
{
//We do a limit of 15 as it appears that confluence has
//a bug when retrieving more than 20 entries with "body.storage"
$url = "content/$rootPage/child/page?expand=version,body.storage&limit=15";
$pages = [];
do {
try {
$list = $this->getClient()->get($url)->json();
} catch (BadResponseException $e) {
throw $this->handleError($e);
}
foreach ($list['results'] as $result) {
$pages[$result['title']] = [
"id" => $result['id'],
"title" => $result['title'],
"version" => $result['version']['number'],
"content" => $result['body']['storage']['value'],
];
}
if (array_key_exists('next', $list['_links'])) {
$url = $list['_links']['next'];
}
} while (array_key_exists('next', $list['_links']));
return $pages;
}
/**
* /rest/api/content/{id}/child/{type}
*
* @param $rootPage
* @return mixed
*/
public function getHierarchy($rootPage)
public function getList($rootPage, $recursive = false)
{
$increment = 15;
//We do a limit of 15 as it appears that confluence has
//a bug when retrieving more than 20 entries with "body.storage"
// We set a limit of 15 as it appears that
// Confluence fails silently when retrieving
// more than 20 entries with "body.storage"
$base_url = $url = "content/$rootPage/child/page?expand=version,body.storage&limit=$increment";
$start = 0;
$children = [];
$pages = [];
do {
try {
@ -138,24 +99,35 @@ class Api
}
foreach ($hierarchy['results'] as $result) {
$children[$result['title']] = [
$pages[$result['title']] = [
"id" => $result['id'],
"title" => $result['title'],
"version" => $result['version']['number'],
"content" => $result['body']['storage']['value'],
"children" => $this->getHierarchy($result['id'])
];
if ($recursive) {
$pages[$result['title']]['children'] = $this->getList($result['id'], true);
}
}
//We don't use _links->next as after ~30 elements it doesn't show any new elements
// We don't use _links->next as after ~30 elements
// it doesn't show any new elements. This seems
// to be a bug in Confluence
$start += $increment;
$url = "$base_url&start=$start";
} while (!empty($hierarchy['results']));
return $children;
return $pages;
}
/**
* @param integer $parent_id
* @param string $title
* @param string $content
* @return integer
*/
public function createPage($parent_id, $title, $content)
{
$body = [
@ -175,6 +147,13 @@ class Api
return $response['id'];
}
/**
* @param integer $parent_id
* @param integer $page_id
* @param integer $newVersion
* @param string $title
* @param string $content
*/
public function updatePage($parent_id, $page_id, $newVersion, $title, $content)
{
$body = [
@ -193,6 +172,12 @@ class Api
}
}
/**
* Delete a page
*
* @param integer $page_id
* @return mixed
*/
public function deletePage($page_id)
{
try {
@ -202,6 +187,10 @@ class Api
}
}
/**
* @param integer $id
* @param array $attachment
*/
public function uploadAttachment($id, $attachment)
{
//get if attachment is uploaded
@ -211,7 +200,7 @@ class Api
throw $this->handleError($e);
}
$url = "content/$id/child/attachment" . (count($result['results'])? "/{$result['results'][0]['id']}/data" : "");
$url = "content/$id/child/attachment" . count($result['results']) ? "/{$result['results'][0]['id']}/data" : "";
try {
$this->getClient()->post(

Datei anzeigen

@ -45,7 +45,8 @@ class Publisher
$this->client->setSpace($confluence['space_id']);
}
public function run($title, $closure) {
public function run($title, $closure)
{
try {
return $this->runAction($title, $this->output, $this->width, $closure);
} catch (BadResponseException $e) {
@ -69,7 +70,7 @@ class Publisher
"Getting already published pages...",
function() use (&$published) {
if ($published != null) {
$published['children'] = $this->client->getHierarchy($published['id']);
$published['children'] = $this->client->getList($published['id'], true);
}
}
);

Datei anzeigen

@ -32,6 +32,17 @@ class Generator
$this->generateRecursive($daux->tree, $destination, $params, $output, $width);
}
/**
* Recursively generate the documentation
*
* @param \Todaymade\Daux\Tree\Entry $tree
* @param string $output_dir
* @param \Todaymade\Daux\Config $params
* @param OutputInterface $output
* @param integer $width
* @param string $base_url
* @throws \Exception
*/
private function generateRecursive($tree, $output_dir, $params, $output, $width, $base_url = '')
{
$params['base_url'] = $params['base_page'] = $base_url;
@ -46,7 +57,7 @@ class Generator
foreach ($tree->value as $key => $node) {
if ($node instanceof Directory) {
$new_output_dir = $output_dir . DS . $key;
@mkdir($new_output_dir);
mkdir($new_output_dir);
$this->generateRecursive($node, $new_output_dir, $params, $output, $width, '../' . $base_url);
} elseif ($node instanceof Content) {
$this->runAction(

Datei anzeigen

@ -7,6 +7,10 @@ class Template
{
protected $engine;
/**
* @param string $base
* @param string $theme
*/
public function __construct($base, $theme)
{
// Create new Plates instance
@ -19,6 +23,11 @@ class Template
$this->registerFunctions();
}
/**
* @param string $name
* @param array $data
* @return string
*/
public function render($name, array $data = array())
{
$this->engine->addData([
@ -123,6 +132,10 @@ class Template
return $nav;
}
/**
* @param string $separator
* @return string
*/
private function getSeparator($separator)
{
switch ($separator) {

Datei anzeigen

@ -4,7 +4,6 @@ use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Todaymade\Daux\Daux;
use Todaymade\Daux\Format\HTML\Generator as HTMLGenerator;
use Todaymade\Daux\Format\Confluence\Generator as ConfluenceGenerator;

Datei anzeigen

@ -2,15 +2,26 @@
class Helper
{
/**
* Copy all files from $path to $local_base
*
* @param string $path
* @param string $local_base
*/
public static function copyAssets($path, $local_base)
{
@mkdir($path);
mkdir($path);
static::rmdir($path);
@mkdir($path . DS . 'resources');
mkdir($path . DS . 'resources');
static::copyRecursive($local_base . DS . 'resources', $path . DS . 'resources');
}
/**
* Remove a directory recursively
*
* @param string $dir
*/
private static function rmdir($dir)
{
$it = new \RecursiveDirectoryIterator($dir);
@ -27,16 +38,22 @@ class Helper
}
}
private static function copyRecursive($src, $dst)
/**
* Copy files recursively
*
* @param string $source
* @param string $destination
*/
private static function copyRecursive($source, $destination)
{
$dir = opendir($src);
@mkdir($dst);
$dir = opendir($source);
mkdir($destination);
while (false !== ($file = readdir($dir))) {
if (( $file != '.' ) && ( $file != '..' )) {
if (is_dir($src . '/' . $file)) {
static::copyRecursive($src . '/' . $file, $dst . '/' . $file);
if ($file != '.' && $file != '..') {
if (is_dir($source . '/' . $file)) {
static::copyRecursive($source . '/' . $file, $destination . '/' . $file);
} else {
copy($src . '/' . $file, $dst . '/' . $file);
copy($source . '/' . $file, $destination . '/' . $file);
}
}
}

Datei anzeigen

@ -21,6 +21,11 @@ class Processor
*/
protected $width;
/**
* @param Daux $daux
* @param OutputInterface $output
* @param integer $width
*/
public function __construct(Daux $daux, OutputInterface $output, $width)
{
$this->daux = $daux;
@ -28,10 +33,24 @@ class Processor
$this->width = $width;
}
/**
* With this connection point, you can transform
* the tree as you want, move pages, modify
* pages and even add new ones.
*
* @param Directory $root
*/
public function manipulateTree(Directory $root)
{
}
/**
* This connection point provides
* a way to extend the Markdown
* parser and renderer.
*
* @param Environment $environment
*/
public function extendCommonMarkEnvironment(Environment $environment)
{
}

Datei anzeigen

@ -9,19 +9,32 @@ class ErrorPage extends SimplePage
const MISSING_PAGE_ERROR_TYPE = 'MISSING_PAGE_ERROR';
const FATAL_ERROR_TYPE = 'FATAL_ERROR';
/**
* @var \Todaymade\Daux\Config
*/
private $params;
/**
* @param string $title
* @param string $content
* @param \Todaymade\Daux\Config $params
*/
public function __construct($title, $content, $params)
{
parent::__construct($title, $content);
$this->params = $params;
}
/**
* @return string
*/
protected function generatePage()
{
$params = $this->params;
$page['title'] = $this->title;
$page['content'] = $this->content;
$page = [
'title' => $this->title,
'content' => $this->content,
];
$template = new Template($params['templates'], $params['theme']['templates']);
return $template->render('error', ['page' => $page, 'params' => $params]);

Datei anzeigen

@ -6,7 +6,6 @@ use Todaymade\Daux\Exception;
use Todaymade\Daux\Format\HTML\MarkdownPage;
use Todaymade\Daux\Format\HTML\RawPage;
use Todaymade\Daux\Format\HTML\SimplePage;
use Todaymade\Daux\Tree\Directory;
use Todaymade\Daux\Tree\Raw;
class Server
@ -16,6 +15,11 @@ class Server
private $host;
private $base_url;
/**
* Serve the documentation
*
* @throws Exception
*/
public static function serve()
{
$daux = new Daux(Daux::LIVE_MODE);
@ -63,6 +67,9 @@ class Server
}
}
/**
* @return \Todaymade\Daux\Config
*/
public function getParams()
{
$params = $this->daux->getParams();
@ -83,6 +90,14 @@ class Server
return $params;
}
/**
* Handle an incoming request
*
* @param array $query
* @return \Todaymade\Daux\Tree\Entry
* @throws Exception
* @throws NotFoundException
*/
public function handle($query = [])
{
$this->params = $this->getParams();
@ -108,6 +123,14 @@ class Server
}
}
/**
* @param string $request
* @param string $content
* @return SimplePage
*
* @throws Exception
* @throws NotFoundException
*/
private function saveFile($request, $content)
{
$file = $this->getFile($request);
@ -123,6 +146,11 @@ class Server
return new SimplePage('Success', 'Successfully Edited');
}
/**
* @param string $request
* @return \Todaymade\Daux\Tree\Entry
* @throws NotFoundException
*/
private function getPage($request)
{
$file = DauxHelper::getFile($this->daux->tree, $request);

Datei anzeigen

@ -5,6 +5,15 @@ use Todaymade\Daux\DauxHelper;
class Builder
{
/**
* Build the initial tree
*
* @param string $dir
* @param array $ignore
* @param \Todaymade\Daux\Config $params
* @param array $parents
* @return Directory|void
*/
public static function build($dir, $ignore, $params, $parents = null)
{
if (!$dh = opendir($dir)) {
@ -62,7 +71,13 @@ class Builder
return $node;
}
public static function getOrCreateDir($parent, $title) {
/**
* @param Entry $parent
* @param String $title
* @return Directory
*/
public static function getOrCreateDir($parent, $title)
{
$slug = DauxHelper::slug($title);
if (array_key_exists($slug, $parent->value)) {
@ -77,7 +92,13 @@ class Builder
return $dir;
}
public static function getOrCreatePage($parents, $title) {
/**
* @param array $parents
* @param string $title
* @return Content
*/
public static function getOrCreatePage($parents, $title)
{
$slug = DauxHelper::slug($title);
$uri = $slug . ".html";

Datei anzeigen

@ -4,6 +4,9 @@ use Todaymade\Daux\DauxHelper;
class Content extends Entry
{
/**
* @var string
*/
protected $content;
public function __construct($path = '', $parents = array())
@ -13,6 +16,9 @@ class Content extends Entry
$this->value = $this->uri;
}
/**
* @return string
*/
public function getContent()
{
if (!$this->content) {
@ -22,11 +28,18 @@ class Content extends Entry
return $this->content;
}
/**
* @param string $content
*/
public function setContent($content)
{
$this->content = $content;
}
/**
* @param string $file
* @return string
*/
protected function getFilename($file)
{
return DauxHelper::pathinfo($file)['filename'];

Datei anzeigen

@ -4,53 +4,90 @@ use Todaymade\Daux\DauxHelper;
abstract class Entry
{
/** @var string */
protected $title;
/** @var string */
protected $name;
/** @var Content */
protected $index_page;
/** @var Content */
protected $first_page;
/** @var string */
protected $uri;
/** @var string */
protected $local_path;
/** @var integer */
protected $last_modified;
/** @var array */
protected $parents;
/**
* @param string $path
* @param array $parents
*/
public function __construct($path = '', $parents = array())
{
$this->setPath($path);
$this->setParents($parents);
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return string
*/
public function getUri()
{
return $this->uri;
}
/**
* @param string $uri
*/
public function setUri($uri)
{
$this->uri = $uri;
}
/**
* @return Content
*/
public function getIndexPage()
{
return $this->index_page;
}
/**
* @param Content $index_page
*/
public function setIndexPage($index_page)
{
$this->index_page = $index_page;
}
/**
* @return Entry
* @return Content
*/
public function getFirstPage()
{
@ -80,36 +117,57 @@ abstract class Entry
return false;
}
/**
* @param Content $first_page
*/
public function setFirstPage($first_page)
{
$this->first_page = $first_page;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* @return array
*/
public function getParents()
{
return $this->parents;
}
/**
* @param array $parents
*/
public function setParents($parents)
{
$this->parents = $parents;
}
/**
* @return string
*/
public function getPath()
{
return $this->local_path;
}
/**
* @param string $path
*/
public function setPath($path)
{
if (!isset($path) || $path == '' || !file_exists($path)) {
@ -123,6 +181,10 @@ abstract class Entry
$this->index_page = false;
}
/**
* @param string $content
* @return bool
*/
public function write($content)
{
if (!is_writable($this->local_path)) {
@ -133,6 +195,9 @@ abstract class Entry
return true;
}
/**
* @return string
*/
public function getUrl()
{
$url = '';
@ -143,12 +208,20 @@ abstract class Entry
return $url;
}
/**
* @param string $file
* @return string
*/
protected function getFilename($file)
{
$parts = explode('/', $file);
return end($parts);
}
/**
* @param string $filename
* @return string
*/
protected function getTitleInternal($filename)
{
$filename = explode('_', $filename);
@ -164,6 +237,10 @@ abstract class Entry
return $filename;
}
/**
* @param string $filename
* @return string
*/
protected function getUrlInternal($filename)
{
$filename = explode('_', $filename);