Skip to content

Commit

Permalink
Merge pull request #8 from Machy8/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Machy8 committed Mar 30, 2016
2 parents 0644a32 + ad76b25 commit f9bc9b1
Show file tree
Hide file tree
Showing 14 changed files with 290 additions and 223 deletions.
166 changes: 87 additions & 79 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

namespace Machy8\Macdom;

class Compiler {
class Compiler
{

/**
* The skip are tag
Expand Down Expand Up @@ -63,7 +64,8 @@ class Compiler {
* @param int $spacesPerIndent
* @param bool $compressCode
*/
public function __construct($Elements, $Macros, $Replicator, $indentMethod, $spacesPerIndent, $compressCode) {
public function __construct($Elements, $Macros, $Replicator, $indentMethod, $spacesPerIndent, $compressCode)
{
$this->indentMethod = $indentMethod ?: 3;
$this->spacesPerIndent = $spacesPerIndent ?: 4;
$this->lnBreak = $compressCode ? '' : "\n";
Expand All @@ -76,7 +78,8 @@ public function __construct($Elements, $Macros, $Replicator, $indentMethod, $spa
* @param string $content
* @return string
*/
public function compile($content) {
public function compile($content)
{
$lns = preg_split('/\n/', $content);

foreach ($lns as $key => $ln) {
Expand Down Expand Up @@ -119,32 +122,22 @@ public function compile($content) {
return $this->codeStorage;
}

/**
* @param string $txt
* @return string
*/
private function getElement($txt) {
$element = explode(' ', trim($txt));
return $element[0];
}

/**
* HOW LEVELS WORKS
*
* method 1 = spaces
* - is better to set the number of the constant SPACES_PER_INDENT on the number
* you have in your editor setted as "spaces per indent"
* method 2 = tabulators
* method 3 = combined
* - tabulators are always twice bigger
* - Example:
* - spaces per indent = 4 => tab size = 8
* - spaces per indent = 8 => tab size = 16
* - etc...
* - tabulators are always twice bigger
* - example:
* - spaces per indent = 4 => tab size = 8
* - spaces per indent = 8 => tab size = 16
* - etc...
* @param string $ln
* @return int
*/
private function getLnLvl($ln) {
private function getLnLvl($ln)
{
$method = $this->indentMethod;
preg_match('/^\s+/', $ln, $matches);
$whites = implode('', $matches);
Expand All @@ -165,15 +158,80 @@ private function getLnLvl($ln) {
* @param string $ln
* @return string
*/
private function getLnTxt($ln) {
private function getLnTxt($ln)
{
return ltrim($ln);
}

/**
* @param string $txt
* @return array
* @param string $txt
* @return string
*/
private function getElement($txt)
{
$element = explode(' ', trim($txt));
return $element[0];
}

/**
* @param string $element
* @return bool
*/
private function getLnAttributes($txt) {
private function detectNoCompileArea($element)
{
$tagDetected = FALSE;
$areaClosed = $this->inNoCompileArea ? FALSE : NULL;

// For skip tag
$closeTag = '/' . self::AREA_TAG;
if ($element === self::AREA_TAG) {
$tagDetected = TRUE;
$this->inNoCompileArea = TRUE;
} elseif ($element === $closeTag) {
$tagDetected = TRUE;
$this->inNoCompileArea = FALSE;
}

// For style tag
$tag = 'style';
$open = '<' . $tag;
$close = '</' . $tag . '>';
if ($element === $open . '>' || $element === $open) {
$this->inNoCompileArea = TRUE;
} elseif ($element === $close) {
$this->inNoCompileArea = FALSE;
}

// For script tag
$tag = 'script';
$open = '<' . $tag;
$close = '</' . $tag . '>';
if ($element === $open . '>' || $element === $open) {
$this->inNoCompileArea = TRUE;
} elseif ($element === $close) {
$this->inNoCompileArea = FALSE;
}

// For php
$open = '<?';
$close = '?>';
if ($element === $open . 'php' || $element === $open) {
$this->inNoCompileArea = TRUE;
} elseif ($element === $close) {
$this->inNoCompileArea = FALSE;
}

// Set and return
$this->noCompileAreaClosed = $areaClosed;
return $tagDetected;
}

/**
* @param string $txt
* @return array
*/
private function getLnAttributes($txt)
{

// Store the text from the first tag to the end of the line
$re = '/\<.*$/';
Expand Down Expand Up @@ -281,7 +339,8 @@ private function getLnAttributes($txt) {
* @param int $lvl
* @param array $attributes
*/
private function addOpenTag($element, $lvl, $attributes) {
private function addOpenTag($element, $lvl, $attributes)
{
$elementSettings = $this->Elements->findElement($element, TRUE);
$openTag = '<' . $element;
if ($elementSettings['qkAttributes'] && $attributes['qkAttributes']) {
Expand All @@ -298,7 +357,7 @@ private function addOpenTag($element, $lvl, $attributes) {
}
} elseif (!in_array($withoutKey, $usedKeys)) {
$newAttr = $elementSettings['qkAttributes'][$withoutKey] . '="' . $attribute['value'] . '"';
$withoutKey ++;
$withoutKey++;
}
if ($newAttr)
$openTag .= ' ' . $newAttr;
Expand Down Expand Up @@ -337,7 +396,8 @@ private function addOpenTag($element, $lvl, $attributes) {
}

/** @param int $lvl */
private function addCloseTags($lvl) {
private function addCloseTags($lvl)
{
$length = count($this->closeTags);
$lastTag = $length;
if ($length > 0) {
Expand All @@ -352,56 +412,4 @@ private function addCloseTags($lvl) {
array_splice($this->closeTags, $lastTag);
}
}

/**
* @param string $element
* @return bool
*/
private function detectNoCompileArea($element) {
$tagDetected = FALSE;
$areaClosed = $this->inNoCompileArea ? FALSE : NULL;

// For skip tag
$closeTag = '/' . self::AREA_TAG;
if ($element === self::AREA_TAG) {
$tagDetected = TRUE;
$this->inNoCompileArea = TRUE;
} elseif ($element === $closeTag) {
$tagDetected = TRUE;
$this->inNoCompileArea = FALSE;
}

// For style tag
$tag = 'style';
$open = '<' . $tag;
$close = '</' . $tag . '>';
if ($element === $open . '>' || $element === $open) {
$this->inNoCompileArea = TRUE;
} elseif ($element === $close) {
$this->inNoCompileArea = FALSE;
}

// For script tag
$tag = 'script';
$open = '<' . $tag;
$close = '</' . $tag . '>';
if ($element === $open . '>' || $element === $open) {
$this->inNoCompileArea = TRUE;
} elseif ($element === $close) {
$this->inNoCompileArea = FALSE;
}

// For php
$open = '<?';
$close = '?>';
if ($element === $open . 'php' || $element === $open) {
$this->inNoCompileArea = TRUE;
} elseif ($element === $close) {
$this->inNoCompileArea = FALSE;
}

// Set and return
$this->noCompileAreaClosed = $areaClosed;
return $tagDetected;
}
}
3 changes: 2 additions & 1 deletion src/Elements/BooleanAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

namespace Machy8\Macdom\Elements;

class BooleanAttributes {
class BooleanAttributes
{

/** @var array */
protected $booleanAttributes = [
Expand Down
20 changes: 12 additions & 8 deletions src/Elements/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

namespace Machy8\Macdom\Elements;

use Machy8\Macdom\Elements\ElementsSettings;

class Elements extends ElementsSettings {
class Elements extends ElementsSettings
{

/**
* @param string $attribute
* @return bool
*/
public function isBoolean($attribute) {
public function isBoolean($attribute)
{
return in_array($attribute, $this->booleanAttributes);
}

Expand All @@ -29,7 +29,8 @@ public function isBoolean($attribute) {
* @param string $returnSettings
* @return bool|array
*/
public function findElement($el, $returnSettings) {
public function findElement($el, $returnSettings)
{
$return = FALSE;
if (in_array($el, $this->elements))
$return = $returnSettings ? $this->getElementSettings($el) : TRUE;
Expand All @@ -40,7 +41,8 @@ public function findElement($el, $returnSettings) {
* @param string $el
* @return array
*/
private function getElementSettings($el) {
private function getElementSettings($el)
{
$qkAttributes = NULL;
$paired = TRUE;
$settings = $this->elementsSettings;
Expand All @@ -60,7 +62,8 @@ private function getElementSettings($el) {
}

/** @param array $elements */
public function addElements($elements) {
public function addElements($elements)
{
if ($elements) {
foreach ($elements as $element => $settings) {
$settingsExists = TRUE;
Expand All @@ -84,7 +87,8 @@ public function addElements($elements) {
}

/** @param array $attributes */
public function addBooleanAttributes($attributes) {
public function addBooleanAttributes($attributes)
{
if ($attributes && is_array($attributes)) {
if (count($attributes)) {
$merged = array_merge($this->booleanAttributes, $attributes);
Expand Down
5 changes: 2 additions & 3 deletions src/Elements/ElementsList.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@

namespace Machy8\Macdom\Elements;

use Machy8\Macdom\Elements\BooleanAttributes;

class ElementsList extends BooleanAttributes {
class ElementsList extends BooleanAttributes
{

/** @var array */
protected $elements = [
Expand Down
5 changes: 2 additions & 3 deletions src/Elements/ElementsSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@

namespace Machy8\Macdom\Elements;

use Machy8\Macdom\Elements\ElementsList;

class ElementsSettings extends ElementsList {
class ElementsSettings extends ElementsList
{

/** @var array */
protected $elementsSettings = [
Expand Down
14 changes: 8 additions & 6 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@

use Machy8\Macdom\Replicator\Replicator;

class Loader extends Setup {
class Loader extends Setup
{

/**
* @param string $file
* @return string $compiled
* Loader constructor
*/
public function __construct() {
public function __construct()
{
parent::__construct();
}

/**
* @param sting $content
* @param string $content
* @return string
*/
public function compileContent($content) {
public function compileContent($content)
{
$compiler = new Compiler($this->elements, $this->macros, new Replicator, $this->indentMethod, $this->spacesCount, $this->compressCode);
$compiled = $compiler->compile($content);
return $compiled;
Expand Down
Loading

0 comments on commit f9bc9b1

Please sign in to comment.