Skip to content

Commit

Permalink
HTMLBook Prototype (#1032)
Browse files Browse the repository at this point in the history
  • Loading branch information
dac514 authored and Ned Zimmerman committed Jan 7, 2018
1 parent 0e91f59 commit ebffe2b
Show file tree
Hide file tree
Showing 11 changed files with 1,704 additions and 9 deletions.
7 changes: 7 additions & 0 deletions inc/htmlbook/block/class-paragraph.php
Expand Up @@ -22,4 +22,11 @@ class Paragraph extends Element {
*/
protected $tag = 'p';

/**
* @var array
*/
protected $dataTypes = [
'subtitle',
'author',
];
}
51 changes: 47 additions & 4 deletions inc/htmlbook/class-element.php
Expand Up @@ -109,6 +109,11 @@ class Element {
'wbr',
];

/**
* @var \Pressbooks\HtmLawed
*/
protected $tidy;

/**
* @var string
*/
Expand Down Expand Up @@ -148,6 +153,13 @@ public function __construct() {
}
}

/**
* @param bool $tidy
*/
public function setTidy( bool $tidy, $obj = null ) {
$this->tidy = $tidy;
}

/**
* @return string
*/
Expand All @@ -169,6 +181,13 @@ public function getDataType() {
return $this->dataType;
}

/**
* @return array
*/
public function getSupportedDataTypes() {
return $this->dataTypes;
}

/**
* @param string $data_type
*
Expand Down Expand Up @@ -199,7 +218,17 @@ public function setAttributes( array $attributes ) {
* @param mixed $attribute
*/
public function appendAttribute( $attribute ) {
$this->attributes[] = $attribute;
if ( is_array( $attribute ) ) {
foreach ( $attribute as $k => $v ) {
if ( isset( $this->attributes[ $k ] ) ) {
$this->attributes[ $k ] .= " $v";
} else {
$this->attributes[ $k ] = $v;
}
}
} else {
$this->attributes[] = $attribute;
}
}

/**
Expand All @@ -210,11 +239,14 @@ public function getContent() {
}

/**
* @param array $content
* @param mixed $content
*
* @throws \LogicException
*/
public function setContent( array $content ) {
public function setContent( $content ) {
if ( ! is_array( $content ) ) {
$content = [ $content ];
}
foreach ( $content as $v ) {
if ( $this === $v ) {
throw new \LogicException( 'Recursion problem: cannot set self as content to self' );
Expand Down Expand Up @@ -361,9 +393,20 @@ public function __toString() {
} else {
$html .= '>';
}

$inner_html = '';
foreach ( $this->content as $content ) {
$html .= (string) $content;
$inner_html .= (string) $content;
}
if ( $this->tidy ) {
$inner_html = \Pressbooks\HtmLawed::filter(
$inner_html, [
'tidy' => 5,
]
);
}
$html .= $inner_html;

$html .= "</{$this->tag}>";

return $html;
Expand Down
18 changes: 16 additions & 2 deletions inc/htmlbook/class-validator.php
Expand Up @@ -11,6 +11,11 @@ class Validator {
*/
protected $schemaPath;

/**
* @var array
*/
protected $errors = [];

public function __construct() {
if ( ! defined( 'PB_XMLLINT_COMMAND' ) ) {
define( 'PB_XMLLINT_COMMAND', '/usr/bin/xmllint' );
Expand All @@ -31,6 +36,13 @@ public function setSchemaPath( string $schema_path ) {
$this->schemaPath = $schema_path;
}

/**
* @return array
*/
public function getErrors() {
return $this->errors;
}

/**
* Validate an HTMLBook file
*
Expand All @@ -43,15 +55,17 @@ public function validate( string $path ) {
$command = PB_XMLLINT_COMMAND . ' --noout --schema ' . escapeshellcmd( $this->getSchemaPath() ) . ' ' . escapeshellcmd( $path ) . ' 2>&1';

// Execute command
$this->errors = [];
$output = [];
$return_var = 0;
exec( $command, $output, $return_var );

if ( isset( $output[0] ) && str_ends_with( $output[0], ' validates' ) ) {
return true;
} else {
$this->errors = $output;
return false;
}

return false;
}

}
4 changes: 3 additions & 1 deletion inc/modules/export/class-export.php
Expand Up @@ -400,7 +400,6 @@ protected function transformXML( $content, $path_to_xsl ) {
* @param string $section_author (deprecated)
*
* @return string $html blob
* @throws \Exception
*/
protected function doCopyrightLicense( $metadata, $title = '', $id = 0, $section_author = '' ) {

Expand Down Expand Up @@ -600,6 +599,9 @@ static function formSubmit() {
if ( isset( $x['odt'] ) ) {
$modules[] = '\Pressbooks\Modules\Export\Odt\Odt';
}
if ( isset( $x['htmlbook'] ) ) {
$modules[] = '\Pressbooks\Modules\Export\HTMLBook\HTMLBook';
}

// --------------------------------------------------------------------------------------------------------
// Other People's Plugins
Expand Down

0 comments on commit ebffe2b

Please sign in to comment.